The best piece of coding advice I’ve ever received

“One thing* does just one thing.”

(Not exact quote though)

That’s what buddy of mine told me many many years ago. And this is just great piece of advice that helps in making everyday decisions in coding. I was thinking of adding something to “init” function today and was bothering if I make it “quickly”, but then instead of making it quickly I cleaned the codebase a bit. Already feeling great about this small thing.

You can replace first “thing*” with “method”, “function”, “class”, “module” depending on what you are currently doing.

7 thoughts on “The best piece of coding advice I’ve ever received

  1. My favorite code advice is “if you need something >3 times, make it a function/object. Otherwise, re-code it inline.” Without this rule, I 1) underestimate the cost of developing reliable objects; 2) overestimate the effort required to code the same thing a second time; and 3) overestimate the likelihood that I’ll need to that function again (in that language/context) in the future.

  2. It’s all in the single responsibility principle! Most of the time when I feel my code is inflexible, it’s because I’ve shoehorned too much functionality into the same “thing”.

    http://en.wikipedia.org/wiki/Single_responsibility_principle

  3. I use this kind of one thing does one thing a lot, because it makes programs much cleaner and more logical. For example today I was writing a simple GUI for Monkey, and I implemented buttons which can stick down, and be hovered. So I created simple hovered, down and stick boolean fields into the Button class, and the rendering code relies on those values only. The update code then handles the logic which is very simple once you have established those “one thing” flags in your class.

  4. I recommend Code Complete 2, epic book.

  5. @Igor: not thing.

    @Kamil: thanks for the hint. goes to my potential shopping list

  6. I recommend reading “Clean Code: A Handbook of Agile Software Craftsmanship”. You will find this one plus much more advices there. Worth reading really.

  7. What thing should I replace the second “thing” with?