DEV Community

Cover image for Clean Code Principles - Function
codebond
codebond

Posted on • Edited on

Clean Code Principles - Function

I am reading a book Clean Code by Robert C. Martin and It is amazing so far. some principles he made need to be highlighted.

I am thinking of sharing other principles too as I go ahead in reading. because it will help me and you to learn and implement.

In this post, i covered clean code principles for functions.

1

Function should do one thing. They should do it well. They should do it only.

2

If a function does only those steps that are one level below the stated name of the function, then the function is doing one thing.

3

The smaller and more focus a function is, the easier it is to choose a descriptive name.

4

Another way to know that a function is doing more than "one thing" is if you can extract another function from it with a name that is not merely a restatement of its implementation.

5

A long descriptive name is better than a long descriptive comment.

6

The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification-- and they shouldn't be used anyway.

7

Function should either do something or answer something, but not both. either your function should change the state of an object, or it should return some information about that object. doing both often leads to confusion.

Happy coding.

This post first published on codebond.co

Top comments (0)