Cleaner JavaScript Code? Read this...
Tags: javascript, webdev, programming, codenewbie
Published: 2023-01-03 15:24:09

As coding newbies, we all just want to write codes that work, to begin with, not minding the sanity of others who will read the codes now or later. The better we get at coding, the better we realize most of the codes we wrote in our early days were suicidal, Lol.

The difference between Senior programmers and Juniors is years of experience, familiarity with different code readings and how both write their codes. Senior programmers have been in the industry for close to a decade and more, and so they have written lots of codes, made mistakes and gotten better at writing codes as the years rolls by. Also, they have read hundreds and thousands of codes from different levels of programmers, so it’s very difficult for them not to differentiate a code newbie from a more professional one when they read the codes.

Here, I will highlight some of the things you can do to write cleaner and better codes, just like the pros do.

1. Naming Variables and Functions: Asides from following the Naming Conventions, how do you name your variables, classes, IDs and functions? Do you just name them according to your mood at the time of writing the codes or do you name them so it’s easier when someone else reads your code? This is an important step if you want to achieve a clearer and cleaner code.

Learn to name your variables and functions according to the action they will be performing. That gives a clearer description to whoever is reading the code. If you have an HTML element that is supposed to return the value in a form, let the variable name describe the action that is expected to take place. Name Functions as Verbs, and classes as Nouns.

const userName = document.querySelector(‘.userNameInput’).value

const getUserDetails = () = > {}

I am sure if anyone is reading this code snippet, it would be easy for them to understand what the code is about.

2. Maintain a Code Structure: Writing a heavy line of code in a function isn’t frowned upon, but you can easily be judged by it. So also, writing a one-liner instead of a heavy line of code doesn’t qualify one to be a senior developer. Because the code works regardless isn’t the end of it, can it be easily maintained? Can someone else understand it when they stumble on it? Can it easily be refactored? Those are the questions.

If you can plan your project by writing them down before coding, it will help you be able to write your functions into smaller chunks instead of heavy ones, they would be easily readable, maintainable and can easily be refactored. Asides from these, Unit Testing would easily be achievable. Code structuring helps you write modifiable codes.

3. Don’t Repeat Yourself (DRY) Principle: This principle is self-explanatory, and this is one of the things Code Structuring can help you avoid. When you can keep multiple smaller functions, they are easily reusable in other functions, instead of having to repeat the same code over and over. The less code you write, the easier it is to Test, and the fewer bugs you find.

It’s easier to conclude that one is a Junior developer or a beginner developer when there is repetition in their code. Don’t copy and paste the same code in different functions or components, better still, make them a smaller reusable function that can be called over and over wherever they are needed.

4. Use Comments: Even though your codes should be self-explanatory, it is advisable to use comments at intervals. Codes don’t die. They can be there for years and decades, and definitely would be modified as the year goes by. Because there are always modifications to programming languages, there would always be changes to codes written before the modifications were made. Using comments can save whoever is changing or refactoring the codes several hours of their time.

5. Read Other People’s Code: This is one other big difference between us and the pros. They have been able to read hundreds and thousands of lines of code from different levels of programmers, and so they have gotten better at it. They have seen different methods developers write codes, and it’s easier for them to build on that knowledge to get better themselves.

The better you do something, the better you get at it. Reading and learning from other people’s codes is valid to help us get better ourselves.

6. Be Consistent: This is the most important of it all. The longer you keep doing something, the more familiar you get with it, and the better you get at it over time.