DEV Community

Cover image for Clean Code Principles - Function

Posted on • Updated 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.


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


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.


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


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.


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


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.


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

Top comments (0)