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.

6 Responses to “The best piece of coding advice I’ve ever received”

  1. Igor Hardy says:

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

  2. Kamil Toszek says:

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

  3. Juuso says:

    @Igor: not thing.

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

  4. Jake Birkett says:

    I recommend Code Complete 2, epic book.

  5. Lumooja says:

    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.

  6. dsp4 says:

    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

Leave a Reply