Episode 4: Function Structure
Function signatures should be small and have few arguments as possible. In this episode, the various types that should or should not be passed was discussed.
Functions are supposed to do one thing – and passing booleans would invalidate this rule because once you pass booleans, the function then does two things – one if the arg is true, and one if the arg is false.
The three prominent programming paradigms were discussed earnestly as well.
Overall, the episode was really great and informative.
- Arguments
- Arguments should be treated as a liability (not an asset), hence it should be limited. Uncle Bob tolerates as much as 3 maximum arguments in a function. And, no booleans, please. Lastly, no output arguments and null defenses as well.
- Defensive programming is definitely a smell because it means that you don’t trust in your unit tests OR, you haven’t even built them because the tests would have prevented you to code them in the first place.
- The Stepdown Rule
- We impose an order on methods – typically in a step-down manner where we step-down one level of abstraction at a time.
- Paradigms
- Functional Programming
- Basically means there are no side effects. However, we do not hate side effects all the time. In fact, we need desirable side effects because we also want to change / update our databases, for instance. The goal is to impose disciplines on where and how the side effects happen.
- Command Query Separation (CQS)
- Functions returning values should have no side effects and functions that returns void (or does not return any values) changes state (has side effects)
- Tell Don’t Ask (TDA)
- TDA for state. Do not violate the Law of Demeter. Do not call methods on objects that are returned from a previous method call.
- Structured Programming
- Sequence — Selection — Iteration
- Errors first
- Prefer Exception
- Use Unchecked Exceptions
- Null is not an error
- Trying one thing – Again, function are supposed to do one thing – error handling IS one thing.
- Object oriented Programming
- Functional Programming
The best comment is the comment you don’t have to write – Uncle Bob