Function
Ideally, a function should be less than 15 ~ 20 lines.
Describe the function by describing it as a brief TO paragraph: "To ..., we have firstly ..., then, ..."
Check our previous example in Session 1.
Sections within Functions
If one function is divided into sections like declarations, initializations, and sieve. It's wrong.
Again, functions should do one thing.
One Level of Abstraction per Function
Mixing levels of abstraction within a function is always confusing.
Reading Code from Top to Bottom: The Stepdown Rule
Connect with "TO paragraphs".
Vertical distance of function definitions.
Use Descriptive Names
You know, it's important.
Don't be afraid to make a name long.
Function Arguments
Less is better.
Arguments are even harder from a testing point of view.
The function and argument should form a very nice verb/noun pair.
Have No Side Effects
Side effects are lies. Your function promises to do one thing, but it also does other hidden things.
Can you find the effect side in this function?
Prefer Exceptions to Returning Error Codes
When you return an error code, you create the problem that the caller must deal with the error immediately.
But, if we use try/catch:
Don't Repeat Yourself
Duplication may be the root of all evil in software.
Last updated
Was this helpful?