Series Clean Code - Functions

Continue the Clean Code series, today I'll talk about Functions topic. About Clean Code in Functions, there are some rules to apply for writing Functions like below:
  • Small
  • Do one thing
  • One Level of Abstraction per Function
  • Use Descriptive Names
  • Function Arguments
  • Have No Side Effects
  • Command Query Separation
  • Don’t Repeat Yourself
  • How Do You Write Functions Like This?
Small
  • First principle of function is small
  • Second principle of function is smaller
  • Function should not exceed 20 lines and one line should not exceed 150 characters
  • Function should not have nested structures
  • The indentation of the function should be one or two

Do One Thing
  • Function should only do one thing. It should do that for good. It should do one thing only. 
  • It is important to distinguish "one thing" from "multi steps":
    - A function is not just one step that involves many steps.
    - Each step can be a function.
    - The goal is to decompose a large concept, in other words function is a set of steps at the next level of abstraction.
One Level of Abstraction per Function
  • To make sure our function is performing a "single" thing, make sure that the statements in the function are the same the level of "abstract".
  • Reading code from Top to Bottom: The Stepdown Rule
    - Each sort function is followed by the level of abstraction from the top down.
    - Gradually reduce the level of abstraction as we read from the top down.
Use Descriptive Names
  • Use the name to describe what the function does. 
  • Do not be afraid because it is a long name. 
  • Long description is better than a mysterious short name. 
  • Longer descriptions are better than descriptive commentary. 
  • Use a naming convention that uses easy-to-read words as a function name and uses keywords to describe what it does.
Function Arguments
  • The ideal number of arguments used in the function is 0 (niladic), followed by 1 (monadic), followed by 2 (dyadic). 
  • Three (triadic) parameters should be avoided if possible. 
  • More than three (polyadic) requires a very special explanation and should not be used. 
  • Many parameters make it harder to stand in the test. 
  • Imagine how difficult it would be to write test cases to ensure that all combinations of parameters work properly:
    - If there is no parameter, this is normal.
    - If there is a parameter it is not too difficult.
    - If two parameters, then the problem becomes a bit more difficult.
    - With more than two parameters, this is really a problem
  • The output parameter is more difficult to understand than the input parameter. 
  • When we read a function, we use the idea of ​​input information through the parameters, and the return value in the output. 
  • Monadic argument- 2 common forms when using a parameter are:   + Argument function: returns true or false.   + Argument function: Variables the input into something else and returns it through the output parameter. 
  • Flag argument- Nothing is worse when the input parameter is a flag argument, because it does more than one thing: once the flag is valid, and the other when the flag is false.- This should be separated into two functions. 
  • Dyadic arguments- The two-parameter function is more difficult to understand than the one-parameter function.- There are cases where two parameters are perfectly reasonable, eg Point p = new Point (0,0)- However, the two parameters in this case are arranged as a component of a single value.- The two parameters must have a natural bond or arrangement of a natural order. 
  • Triadic arguments- Functions that give up to three parameters are harder to understand than two.- Be careful when giving a function with a set of three parameters. 
  • Object argument- When a function actually needs more than two or three parameters, there are likely to be some parameters that can be encapsulated in a particular class.
  • List argument- Sometimes we want to pass a parameter list to a function.
 Command Query Separation
  • Query string and execution. 
  • The function will do something or answer something, but not including both, which will both lead to confusion.
Don’t Repeat Yourself (DRY)
  • The goal is to minimize duplication. 
  • Duplication is a problem because it exaggerates the code and will have to change many times if the algorithm changes. 
  • It also multiplies the number of times for a shortcoming. 
  • Duplication can be the root of all sins in software.
How Do You Write Functions Like This
  • Write software as well as write any text:- When you write an article or article, you get the idea first, then you shape it for better reading.
    - The first sketch can be clumsy and unorganized, but you can do it, refactor and refine it until it's read the way you want. 
  • And when writing a function:
    - It starts very long and complicated.
    - It has many dents and nested loops.
    - It has a long list of parameters.
    - Name is arbitrary and has duplicate code.
    - So, refining and refining code, splitting functions, changing names, eliminating duplication.
    - Shorten and rearrange.
See you in next chapter about Comments in Clean Code...

0 nhận xét:

Đăng nhận xét

Được tạo bởi Blogger.