Skip to main content

Top 3 tips on writing cleaner code

Clean code, hmm sounds challenging. 91% of beginner coders worldwide struggle...

February 17, 20162 min read

Clean code

Clean code, hmm sounds challenging. 91% of beginner coders worldwide struggle to maintain their code. There are many reasons why this may be, I am going to explore a few right now.

Unclear naming

They never use clear variable names, names really do matter. For example, in JavaScript or other languages if you are creating a variable to store a visitor’s name, call the variable “username” or something related to name. Calling the name “gimmyhardnailes123” will most likely confuse you. Now some of you may be thinking psst totally can handle using proxy names as variables, that may be true now whilst the code is fresh in your head, however, most programs are updates and imagine the headache of remembering what “gimmyhardnailes123” stands for. You could use proxy names, or unclear variable names, however, it will just slow down development and make your code dirty, remember the idea is to make clean code. The cleaner the code the faster the development.

Commenting

Did you really think I would make a clean code tutorial without throwing in comments, many developers seem to hate commenting because people often overestimate their ability to remember the code and what it does in the future. Sometimes good naming will not help you as imagine you have a very long function that does some processing, commenting can be a good way of explaining what the code does.

Commenting is advantageous in many ways, if you were to come back to your code in 1 year, you can easily pick up where you last left and easily understand the code. Another advantage is, people viewing your code for the first time will have zero idea what the code does and what is the purpose of the code, comments help break the ice and allows potential developers to help contribute to your application.

Refactor code

What!!! Refactoring code. You just spend hours coding a nice nifty function that does a ton of processing. Refactoring involves breaking that function down into tiny functions that all do 1 job each. Functions are really advantageous as they allow code to be broken down, no one wants a function that can do 10 different things because that makes the code 10 times harder.

For example, if a function grabs data from the internet and parses the JSON received, then it's doing too much work. We can break this function down into two pars. First function grabs the data and passes it to the second function via call-back and passes it to the second function which parses the data. Once parsed we can additionally pass the data on to another function to add the data on to the DOM.

The above function can all be coded within one function, however, splitting code makes it easier to debug and fix errors since the code is all segregated we can pin point the exact location of the bug and fix it, instead of a trial and error approach we can know exactly where the bug. Refactoring, ensures faster development, as 1 month from know that super intelligent and long function will have you scratching your head and wondering what it does, yes comments help however too long functions can still be overwhelming.

Image

Thanks for reading my post today, hope you had fun.