Mar 23, 2010
Functions
According to Clean Code A Handbook of Agile Software Craftsmanship, functions should have the following characteristics.
- They should be small
- Do one thing
- Minimal amount of arguments
- No side effects
- Command Query Separation
- Exceptions over error codes
- Don’t Repeat Yourself
The explains the reasoning behind each of these very well, but the command query separation was very enlightening.
Functions should either do something or answer something, but not both. Either your function should change the state of an object, or if should return some information about that object. Doing both often leads to confusion.
I’ll admit that I have violated some of those rules numerous times and never though twice about why it was hard to follow. Most of the time I just passed it off as being some squirrelly code, but as I continue to read this book I am learning more and more how to not make rabbit raccoon chasing a cat chasing a yarn ball code.
About
Developing software should be simple and rewarding, not painful and overly complicated
Subscribe via RSS.