Clean Code : Functions

Some rules for functions:

  1. First rule for function is they are small.
  2. Second rule they are smaller than that.
  3. Lots of smaller function with better names will help you and your team because they will help you like a sign board and navigate you through your code.
  4. Don’t worry about function call overhead. It is overly misplaced.
  5. Remember making function small will save you and everybody.
  6. Classes hide in larger functions.
  7. Functions do one thing. To make sure your function do one thing is to extract till you drop. If you can extract from your function that means your function was doing more than one thing.
  8. Function signature should be small;  they should have less than 3 arguments
  9. Do not pass Boolean or null into a function
  10. No output arguments
  11. Organize our method into step down principle, public methods on top followed by private method with no backward references.
  12. Switch statement break OO principle and doesn’t allow independent deploy ability and development ability. Replace it by Polymorphism or move it to safe independent deployable modules like Main inside of a factory method.
  13. Temporal coupling like file open & close operation must be in order. It can be solved by passing a block into a method.
  14. Command Query Separation: Command return must be void and Query must return a type.
  15. Tell Don’t Ask.
  16. Law of Demeter: Danger of too much knowledge in single line.
  17. Structure Programming:  Program should have single input and single output.
  18. Complex mid loop return tends to have problem rather avoid it.
  19. Error Handling: Consider error handling first before we continue with any other part of function.
  20. It’s better to have Exception instead of returning error code.
  21. Exception should be scoped to our class for better understanding.
  22. It’s better to have unchecked exception then check exception. E.g.: derive from runtime exception
  23. Null object pattern or Special case pattern is often better than exception.
  24. Null value is not an exception.

Leave a comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: