How to write “Clean Functions” ft. Clean Code

Darshan kadu
4 min readJul 3, 2020

Clean Code….Naam to suna hi hoga (Famous SRK dialogue). A book which is followed by many engineers to write perfect code. Today we will cover Chapter 3 : Function . The purpose of this post is to make a summary of the chapter so that you can revisit it quickly. I strongly recommend to read the chapter entirely, this is just for revision purpose. So let’s begin.

  1. Length of the functions: Have functions size as small as possible, writing a single function with 100s of lines doesn’t make any sense as it makes code unmaintainable, hard to read.
  2. Blocks and Indenting: Blocks within statements like if, else, while should be in a one line which probably can be a separate function, which also implies that try not to have more than one to two levels of indentation.
  3. Do one thing: Make sure your function does one thing. But how to make sure it’s doing only one thing? Answer is function should have single level abstraction abstraction.

In the above example, to get a name we can call contactService or addressService, but for getting a name from an address service we are doing more than one thing. We are making level of abstraction as two. We can reduce the level of abstraction as in the below example

Now we can not replace the if statement with a function that will just restate the code ex. the name of the function replacing the if statement will be ifBlankNameGetNameFromAddressService(). One way you can make sure you have reached the optimal level abstraction is that you can’t further break a function.

4. Reading Code from Top to Bottom: When you have 1 level of abstraction you will need to arrange the function vertically so as to navigate through them easily. So placing the functions above with just less level of abstraction will make it easy to read the top down manner.

5. Switch Statements: Switch statements do N things and should be avoided. But it’s not always possible to avoid them. The next best thing we can do is not to repeat the same switch statement conditions more than once. How to do it? Answer is to use Polymorphism. Multiple polymorphic classes are implemented from an abstract class, the abstract factory uses the switch statement to return the polymorphic object and hence we have hidden the switch statement at one place.

Example: Without abstract factory getPaintingCost:

Now, by using abstract factory :

6. Names of the function: Have descriptive names of the function, it’s fine even if they are long. The naming of the functions should be consistent.

7.Function Arguments: Prefer to have as at least args as possible, 0 args are better than 1, 1 is than 2, 2 is than 3 args.

  1. Try to avoid having functions with 1 arg having void return statements.
  2. Try to avoid passing boolean arguments in functions
  3. Argument Objects: If the function has more than 2–3 args, try to wrap them into their own class.

8. HAVE NO SIDE EFFECTS : Make sure function is doing one thing and not doing the unexpected change in the state of the system

9. Exception vs Error Code: Prefer throwing exception and not the error codes. You need to deal with every error code immediately in the caller function rather than throwing an exception where happy path flow is uninterrupted and error is taken care separately in the catch block.

10. Extract Try/Catch Blocks: Try/Catch are meant to handle the error so better write the main code in another function and let the try do only error handling. Function should only do error handling, it should start with try and end with catch/finally

11. DON’T REPEAT YOURSELF: Don’t repeat the same code at multiple places, it’s better to create a function to do so. It will help in keeping the logic at the central place and future modification will be easy.

If you have any feedback, please feel free to comment.

If you like this and want to see more please share this and subscribe Newsletter .

Find me @darshan_kadu

--

--