Best practices of programming (Code this 🌈 not 💩)


Best practices for all programming languages.


Introduction

Do you want to sound like the real senior developer? Here's where you can get started. You may be a senior developer, a tech lead, or a junior developer. Your programming style can determine the quality of your work. You may have 10 years of programming experience. But your real quality worth can be determined in seconds by looking into your code. Here are some best practices you should follow, in order to sound like a pro.


Contents

  • Using appropriate names while declaring variables, functions, or creating a file.
  • Using camel and pascal case convention for variable, functions, and class name declarations.
  • Formatting/Prettifying your code.
  • Avoiding unwanted whitespaces.
  • Adding comments wherever required.
  • Avoiding Hardcodes.
  • Shortening your code as much as possible.
  • Do not Repeat Yourself aka DRY principle.
  • Refactoring and optimization of your code.
  • Measuring the performance of your code.

Explanations

1. Using appropriate names while declaring variables, functions, or creating a file.

When you declare the new variables or when you add/alter column names in the database, or when you creating a file please use appropriate names, etc. It will help you and your colleagues in the future to understand what's going on.


2. Using camel and pascal case convention for variable, functions, and class name declarations.

While declaring variables or adding/altering any table columns in the database, use the camel case. For class names, use the Pascal case.

🚀 Example:

CAMEL CASE: Starts with lower case and following the first letter of the words start with uppercase.

PASCAL CASE: Each and every word starts with uppercase without any spaces.

Type of case 🚀

Purpose 👓

Noob’s code 😶

Pro’s code 👨‍💻

CAMEL CASE

Stores created date

createddate

createdDate

CAMEL CASE

The function that returns even or not.

isthis_even(int num)

isEven(int num)

CAMEL CASE

User is active or not as boolean

active

isActive

PASCAL CASE

The class name of the car

class car

class Car

PASCAL CASE

The class name of the task queue

class task_queue

class TaskQueue



3. Formatting or Prettifying your code.

Formatting or prettifying the code is the process of adding indentations (spaces) to the code we wrote. It helps to increase the readability of the code. More prettier is more easier to understand. Nowadays, almost all of the ide and text editors provides prettier or formatting code functionality out of the box.

😎 More prettier is clean ✨.


4. Avoiding unwanted whitespaces.

Adding unwanted whitespaces increases the lack of readability, understandability and even increases the file size. Most of the code reviewers including me hate unwanted whitespaces. It makes it more complex while reviewing the code and the rejection rate will increase.

🤑 Quick tip: Format your code while saving or enable auto-formatting while saving will help you to reduce unwanted whitespaces on the go.

 

5. Adding comments wherever required.

Adding comments is something which is not only beneficial for other developers but also for you in the future. I have suffered a lot in this area. I did a module that is huge in terms of functionality. After a month, there was a change required in that functionality. I spend a lot of time understanding the code and flow. If I wrote some appropriate comments on important areas, I would have saved a lot of time there. 

6. Avoiding Hardcodes.

One of the common complaints that code reviewers will give is hardcoding some static values. Hardcoding will make the codebase more complex while maintaining. If we want to change some values the devs have to track back to each and every file of code to get into the relavant file and line of code.

🤑 Quick tip: Create a new file in a separate folder like statics or environment to keep all of your hardcoded values such as response codes, response messages, color codes, some standard values etc.,    

 

7. Do not Repeat Yourself aka DRY principle.

Do not repeat any codes, let say if you have redundant code, it is very hard to maintain and it will increase the lines of code and when you want to modify any functionality, you have to trace back the redundant functions and modify the same in many places.

Instead, try to create a common function, component, module, or file. Whenever the changes happens, it is very easy to trace back and you have to modify only in single place. This is one of the most vital steps to be followed by a senior programmer.  

8. Shortening your code as much as possible.

This is the most professional quality where your code reviewer or an interviewer expects. This is the quality where experience comes in. There are a lot of things you must keep in mind while shortening the code. Such as removing unused variables, imports, assignments, unwanted comments, whitespaces, using functions, modularizations, etc ., We will see code shortening in upcoming stories. 

🤑 Quick tip for Javascript developers: Try to use ES6 features and Array methods to shorten your code as much as possible rather than using traditional javascript features.

 

9. Refactoring and Optimization of your code.

This might sound like point no.8, which covers shortening the size of the code. But refactoring/optimization covers the reduction of loading time or response time of an application, function, database query, response, or even UI, etc., This helps you to bring you a rich user experience and bring more audience to your application. 

🤠 Quick fact from google analytics: Bounce rate will be higher for websites/applications whose response time is slower. 

 

10. Measuring the performance of your code.

This quality is a must if you are learning data structures and algorithms. It predicts the scalability of the logic or algorithm you wrote. Big O notation helps you to measure the performance of your code. This helps to measure the time, space complexity as the no. of input grows. As a result, it will tell you whether your algorithm is
  • Constant O(1) - 😍
  • Logerthemic O(log n) - 😀
  • Linear O(n) - 😒
  • Quadradic O(n²) - 😨
  • Exponential O(2) - 🤯

Emojis denotes the reactions of your CPU. We will see the Big O notations in a separate story.

 

🎉 Thanks for reading this article. Hope I have given you pro tips to make your code clean. Practice these steps to sound like a real pro senior developer. I am very happy that you spent your time on something good. Please comment and share this article with someone who really needs this. Thanks again and stay connected for more interesting posts.

And special thanks to my seniors for encouraging me to write this article.






6 Comments

  1. Replies
    1. I am so glad that I helped you 😊. 🎉 Congratulations for spending your time more valuable.

      Delete
  2. Replies
    1. Thanks for the comment 😎. Keep away from 💩.

      Delete
  3. Thanks for your support 🙏. This kind of support really motivates me for posting further articles. Stay connected for more posts !!!.

    ReplyDelete
  4. Very informative, keep posting

    ReplyDelete
Previous Post Next Post