Many times when i'm writing a piece of logic, i face this dilemma while calling a function. If a function needs to be invoked under certain condition, should i put the conditional check inside the function and do early return, OR first do an conditional check and the invoke.
Both have their pros and cons, IMO
- Pros - My execution path remains free of branches and the flow looks linear and simple to read.
- Cons - The intent and actual execution would differ in some cases.
- Pros - Intent is clear, i want to invoke this function only when a certain condition is met.
- Cons - Branches and readability.