News for hackers. Stuff that matters.
Re:Too scared to write a line of code by tikhonj on Friday May 24, 2013 @06:51PM (#5765646)
I don't know--my impression is that most people go to far in the other direction. They write code with no thought at all for the future, and then shortly have nightmarish debugging sessions in spaghetti code that's impossible to extend or reuse. All in the name of a rather extreme "worse is better" philosophy.

I've certainly experienced this with some of the people I worked with recently. Anything you win in the short term gets lost several weeks--if not days--later when everything is a horrible mess. Don't even think about coming back to that code months later!

I've found that putting in a little bit of care for the future pays off even in the short term. Perhaps not on the scale of hours or days, but definitely weeks. Which is still pretty immediate.

However, how you do this is also very important. The handy rule I've been using is simple: simplicity. Improve your code by making it do less not more. If you can make code more maitainable or general by simplifying, do it. If it would require adding new concepts or mental overhead, reconsider. Try to reuse existing abstractions as much as possible.

This usually--but not always--means making your code more functional. Do you really need to write this operation in place? Do you really need to tie everything together with a bunch of implicit state changes? Probably not! This is not to say that you should never use state: just be sure to only use it when it fits well and makes sense. And be explicit about it.

The functional style (at least in languages like Haskell) also lends itself very well to reusing simple and general abstractions. The key idea here is that these abstractions are not about doing more: they're about doing less. More general code simply leaves you less room to make mistakes. If you're working with lists of numbers, there are all sorts of arithmetic mistakes you can make; if you manage to write the same code against lists of anything, the only mistakes possible will be in list handling.

Haskell just makes this more practical by providing abstractions with a great "power-to-weight ratio": ones that manage to be simple, general and yet still expressive. Code with functors or monoids or traversables is easier to write than more concrete code and yet still flexible enough for many useful tasks. As a bonus, it's also more reusable. For free.

So the key idea is reduction: all these abstractions work by stripping concrete types of power. A functor can do much less than a list. A monoid can do much less than a number. This lets you write nicer code without becoming an "architecture astronaut" because instead of adding structure, you're really taking it away.

I've found that programming like this really helps in maintaining and extending the code later on. But it doesn't slow me down when I'm writing either--I actually save time because I spend less getting time getting the code correct in the first place.

These days, I've started being able to make large changes or add complicated features and have them work on the first try. Not every time, but surprisingly often. Certainly much more often than before! In large part, I think this is because of writing the code with an eye towards simplicity.

So the important insight: take the simple route, not the easy route.

Re:Too scared to write a line of code by gfosco on Friday May 24, 2013 @09:28PM (#5766043)
How long have you been programming, and how many jobs have you had writing code in an actual business? Your profile says you're a student, so I'm curious how you can feel justified in writing such a thesis.

Personally, after 16 years in the industry, I do stress about my code as described in the article... Not about making it work, because that will happen regardless, but about how. It affects my ability to code, because "make it work" is far drowned out by "make it work the way your peers will accept it." Many times, that "acceptable" approach is something I find far less intuitive, and far less maintainable... Also, that approach has changed so many times over the years and with each different team, it's hard to keep track. I can build far more in a weekend, on my own terms, than I can build in a week by someone else's. I am not complaining... I actually like to learn those terms, and I enjoy it as long as people are constructive about it rather than condescending. It's trying to pre-determine those terms that is the block, for me.

Re:Too scared to write a line of code by btian on Friday May 24, 2013 @10:11PM (#5766103)
His CV on his website http://jelv.is/resume.pdf.

Seems he's fairly experienced. Anyway such things are highly subjective, and YMMV

Re:Too scared to write a line of code by nnq on Saturday May 25, 2013 @12:33AM (#5766351)
wow, SO experience is now something one puts on top of the Achievements section! (I don't say it in a bad way, I'm just surprised things have evolved to such a point)
Re:Too scared to write a line of code by Zakharov on Friday May 24, 2013 @08:01PM (#5765858)
If you write code too slowly, go faster. If you write code too quickly, go slower.

You can tell if you're too slow when you've spent two weeks thinking about the program and have neither working code nor a detailed design, or if you've created a detailed design only to realize upon implementation you'd gone about it entirely the wrong way.

You can tell if you're too fast if you find yourself spending more time cleaning up messes of poorly-thought-out spaghetti code.

Re:Too scared to write a line of code by andrewljohnson on Friday May 24, 2013 @08:55PM (#5765980)
This is like in go. If your walls are too brittle, play more conservatively. If your walls are too thick, loosen up. You can make generally better moves in go just by recognizing if you are too conservative or too aggressive. Same with poker.
Re:Too scared to write a line of code by avalaunch on Friday May 24, 2013 @11:44PM (#5766265)
Upvoted to make up for MBlume's mistake. :)
Re:Too scared to write a line of code by Zecc on Saturday May 25, 2013 @05:46AM (#5766815)
Upvoted again, because I felt like it. But it's still one upvote short, I guess.
Re:Too scared to write a line of code by mgkimsal on Saturday May 25, 2013 @07:05AM (#5766915)
Fixed. :)
Re:Too scared to write a line of code by MBlume on Friday May 24, 2013 @09:38PM (#5766057)
meant to upvote you and downvoted by accident, just so you know
Re:Too scared to write a line of code by stevenwei on Friday May 24, 2013 @10:19PM (#5766115)
Yes. To add to that:

Sometimes you'll want to plan ahead because the nature of the problem means you'll have a harder time cleaning things up later.

Other times, you want to move quickly because creating castle-in-the-sky architectures is not the best use of your time (especially if it becomes paralyzing).

Experience helps you determine which type of situation you're dealing with.

Re:Too scared to write a line of code by beckje01 on Friday May 24, 2013 @08:57PM (#5765984)
The problem that comes up is by the time you hit the tipping point of more cleaning then feature additions you end up having to do the fast solutions to fit deadlines.

I think when you take on the debt of poor code you have to remember that what it will take to fix that problem will continue to increase the longer you are away from that code.

Now I don't think you should consider fixing things for scale poor code that is it's own beast which makes the most sense to handle once you have the problem as you can't really tell what the problem will be until you have it.

Re:Too scared to write a line of code by plorkyeran on Friday May 24, 2013 @10:53PM (#5766187)
You're going to see a lot more code from people who err on the side of too little thought rather than too much though for a pretty obvious reason: people who are too scared to start coding... aren't writing any code for you to read later.

Also, while it's easy to identify code which has had too little thought put into it, it's often very difficult to identify code which has had too much thought put into it. The best solutions don't look like the result of lots of hard work, but rather make hard problems look like simple problems that would be trivial to solve.

Re:Too scared to write a line of code by ay on Friday May 24, 2013 @07:11PM (#5765730)
Great advice IMHO. One trick that I found useful for myself (good chunk of my code is "hobby" code that I myself revisit some months later, so not really "production" problem but nonetheless almost the real-world maintenance example):

- make the function fit in your head. Literally. Make it fit in one screen, if you can. If it does not, think how you can make it fit on the screen - everything, including variable definitions.

- avoid branching as much as possible unless it is the 'exit' branches. Avoiding branching sometimes can be done by tricks like passing function pointers. Do it - it makes code much more expressive and easier to read.

These two heuristics allow me to make the code much simpler to understand for $(me+6months). I saw the same patterns in real-world code at $work as well - using the above two principles makes the code dramatically easier to support later.

Re:Too scared to write a line of code by jes5199 on Friday May 24, 2013 @09:50PM (#5766074)
One screen is way too big! Make each function do a single thing. Even in verbose languages, you should be able to get most of them down to 5 lines or less - and this isn't actually difficult (except for coming up with names for all of them) the transformation is actually very mechanical, so much so that if you're using an IDE it can probably "extract method" for you, but even doing it by hand is trivial once you get the knack of it.
Re:Too scared to write a line of code by ay on Saturday May 25, 2013 @05:33AM (#5766794)
Indeed. 25 is the maximum - mostly the shorter, the better.

Though at least for me there is somewhere a limit (2-3 lines?) where the returns start to diminish - there's more noticeably more typing involved, and more mental context switching while debugging.

Re:Too scared to write a line of code by garysweaver on Saturday May 25, 2013 @12:06AM (#5766309)
> Improve your code by making it do less not more.

Exactly. The problem is that code can tend to be either crap (spaghetti) or too much code (too many methods and layers of extraction). So, I'd go one step further: code should do less, not more, and should be clear and understandable, without making unnecessary sacrifices. What is clear to me isn't clear to everyone else, but striving for clarity and simplicity isn't a bad thing.

Re:Too scared to write a line of code by marekmroz on Saturday May 25, 2013 @12:41AM (#5766369)
> too many methods and layers of extraction

Surely, you mean abstraction?

Re:Too scared to write a line of code by garysweaver on Sunday May 26, 2013 @01:36PM (#5771751)
Yes, was writing that in a hurry. :)