Writing Clean Code

Let's ask the question first, is writing a clean code important?

Remember that time, when you came back to your project after a week of vacation and can not figure out what the heck is going on there. Now, Think about a project that more than 50 devs worked on, and you have to add/fix a feature on that codebase? Looks Exhausting even with the docs, Right?

Actually not, it won't be that much problematic to work on a codebase that is written using clean code techniques. So, here are a few tricks for beginners on how to write clean code to make your codebase understandable and keep your development steady, even with a larger codebase

Before we begin, these are not hard-coded rules, you may follow or break them according to your need. This blog is highly biased with my and Uncle Bob's view.

Naming Conventions

Naming is actually an important part of any programming language, you have to name most of the classes, functions, variables, etc. So, Giving a good name is actually pretty much important.

  • Make It Explanatory - When naming the variables or functions, make sure the names explain the implementation of that variable or function.
  • Length of the name - When giving an explanatory name, make sure to understand how much the name explains itself. Like this example

    const len = arr.length //calculate length of array arr
    const lenOfArrayArr = arr.length //Does the same
    

    Now, both of the names explain themselves but the second one goes deeper. Now, when to use which one? For Variables the more scope it has the more explanatory its name should be. As you may have to call more scoped variables more often and you have a chance to mess up there

    const len = arr.length //(for normal scopes)
    const lenOfArrayArr = arr.length //when the scope is larger (i.e. global scopes)
    

    But, for Functions or Classes the less scope it has the more explanatory its name should be. Because for functions it is the other way around.

  • Ambiguity - try to avoid ambiguity in names as much as possible.

    function arrayCalculate(){....}
    function arrayCalculates(){....}
    //try to avoid naming like this
    
  • Make the names easy to pronounce and search, so that they can be easy to remember. Avoid adding prefixes or type information within the name.

Writing Functions

Functions or methods are the backbones of any language, you have to work with them in some techniques, so that, you or anyone else have to decipher the cryptic work of them every time they return to the codebase.

  • Do single thing - Make sure your function does only one simple thing not more than that, if your function makes more than one thing extract another function from the function and keep your function as smaller as possible.
  • Side Effects - If your function cause side effects, try to return nothing from the function (make the return type void, if your language support function return types). If your function returns something try to keep it as a pure function (with no side effects).
  • Number of Arguments - Control the number of arguments you pass as parameters to your function, try to keep it less than 4 . Don't send default values as arguments, rather than implement the values inside your function. You can also use currying. Try not to pass flag arguments(i.e. booleans) try to implement two functions to implement them.
  • KISS - No, don't kiss your codebase but Keep It Simple Stupid (or Stupid SImple). Don't write unnecessary complex codes, if it can be achieved in a simple stupid efficient way, do it that way.
  • Try to declare variables, functions close to their usage, and the function that depends on them.
  • Try to write less to more important functions in a downwards direction, Try to keep lines of the code short.
  • Follow Indentation.

Writing Comments

We, as a beginner try to comment on every line to help our future selves, But, yet they became unuseful. Here are some tips to get the most out of them.

  • More comment is not good code - The more comments you have, the more good codebase you got, it doesn't work in this way. Comments don't pay for bad code. At first, try to explain the working of your code using the code only, if you are not satisfied, with how your code is structured only then put informative comments. Don't put comments describing silly things or self-explanatory codes.
    console.log("Hello World") //printing Hello World
    // Don't put comments like that
    
  • Make it noise-free - Don't add comment noise to the code. Most of the teams use version controls now(git etc.). So, don't push a codebase to the main repo where an old code is commented out, but not deleted. Don't comment unnecessarily like "I added this code", git knows who added what. Thus making the comments noise-free.

  • Make it Informative - while writing comments make sure this is informative. You can comment where something can cause consequences etc.

Check the Codebase

  • Make sure, the code is not difficult to change, or a simple change can cause the code to be broken in many places.
  • Make sure, it is easy to understand the codebase.
  • Your codebase should not be too needless complex.
  • Your codebase should not be too repetitive.

In the end, as Uncle Bob says, we cannot write clean code, we always write messy codes. But we should always have the time to clean our code, So, that it can be easier to collaborate and work on the project to improve our software. Practicing to write clean code from the beginning of an SDE career is much more important.

I Hope, I helped you in some way.