Articles

Programming Etiquettes – 4 best practices

By August 27, 2020 No Comments
Programming Etiquette
3 min read

Programming etiquette is a set of rules that governs the way programmers write codes.  Nobody loves a badly written codebase.

Have you been in a situation where you worked on a particular project and you revisit the project after 6 months or 1 year and you don’t even understand your codebase anymore?

Have you also been in a situation where you inherited a codebase and you feel like rewriting the codebase because the codebase is badly structured and not well written? but there is no time available to rewrite the codebase.

Writing good code is an art that every professional programmer should master. A bad codebase is a nightmare for everyone including the programmer who wrote it.  While we continually grow as software developers/programmers/engineers, it is important we cultivate the habit of writing good quality codes.

In this article I will be sharing programming etiquettes every software developer should know and strive to make it a habit

  1. Structure your code properly
  2. Good and consistent naming convention
  3. Keep it Simple Stupid (KISS)
  4. Don’t repeat yourself (DRY)

 

1. Structure your code properly

There are different code structures depending on the programming language you are using and its community, but one of the most popular coding structures that is been used for backend programming languages and frameworks like (Php/Laravel, Ruby/Rails, Javascript/ExpressJS) is MVC code structure.

One of the major advantages of a well-structured codebase is that it is easy to maintain. The larger the codebase the more complex the codebase becomes and when the codebase is not well-structured, it becomes a nightmare to work on.  Working on an unstructured codebase is like walking on eggshells, you don’t know what part of the codebase could be broken while working on different parts of the codebase.

A well-structured codebase makes it possible for multiple people to work on a project simultaneously without having many conflicts on the codebase. It also allows a new member of the team to know what the codebase is all about without going deep into the codebase. Below are the benefits of a well-structured codebase

Benefits of a well-structured codebase

  1. It is easier to maintain
  2. It is easier to test
  3. A new teammate or newbie can understand the roles of the different parts of the codebase
  4. A structured codebase is scalable – meaning that new features can be added to the codebase without breaking the functionality of existing features.
  5. it is easier to debug

 

2. Good and consistent naming convention

It is common practice that variables, functions, and class names should be descriptive and meaningful. It eliminates ambiguity in the codebase and gives a proper understanding of a variable or function’s purpose in the codebase.

Bad example

let x = "David";

let foo = ["Nissan", "Toyota", "Audi", "Bentley"];

function users(){};

 

Good example

let userName = "David";

let listOfCars = ["Nissan", "Toyota", "Audi", "Bentley"];

function getAllUsers(){}

 

While the naming of variables/functions/classes is descriptive, the naming style should also be consistent. There are different naming style convention in software development, which includes

1. Pascal Case – Words are delimited by capital letters.

Ex. UserName, ListOfCars, NumberOfDays

2. Snake Case – Words are delimited by an underscore.

Ex. user_name, list_of_cars, number_of_days

3. Kebab Case – Words are delimited by a dash.

Ex. user-name, list-of-cars, number-of-days

4. Camel Case – Words are delimited by capital letters, except the initial word.

Ex. userName, listOfCars, numberOfDays

 

Benefits of good and consistent naming convention

  1. Increases readability and makes the codebase understandable
  2. Reduces the cost of transferring the project to a new teammate
  3. Reduces the time and cost of onboarding a new teammate into the project/codebase
  4. Promotes consistency in the codebase

 

3. Keep It Stupid Simple

The KISS principle is all about keeping the codebase simple, easy to understand, and maintainable. For example, a method should only perform one function. If there are multiple condition statements in a method, then it signifies that the method should be broken into smaller methods, which becomes easier to maintain.

Benefits of the KISS Principle

  1. Ability to produce high-quality code
  2. It makes the implementation of test automation become easy
  3. The codebase is easier to maintain
  4. It makes easy onboarding of new teammates into the project
  5. Ability to solve complex problems without making the codebase complex to understand
  6. The codebase becomes flexible, easier to refactor and add more features without fear of breaking the application

 

4. Don’t Repeat Yourself (DRY)

Don’t Repeat Yourself is a software development principle aimed at reducing code repetition during software development. It encourages abstraction of the commonly used block of codes.

Every line of code that is written in a codebase must be maintained and unnecessary duplication of a code block adds to the maintenance cost of the codebase. As a rule of thumb, once a piece of code is repeated more than once in the codebase, it becomes important that the piece of code is abstracted into a separate function and then called where it is necessary.

Example of the DRY principle.

Imagine you are building the frontend of a finance application. The application would display user balance in multiple parts of the application in this format (USD 1,000.00). The user balance stored in the backend database is not formatted, e.g (1000.oo). Instead of repeating the same code format logic in multiple parts of the codebase, an abstract function would be created that accepts an unformatted user balance and then output a formatted user balance.

Benefits of DRY

  1. Readability – It is easier to read and understand the codebase
  2. Flexibility – Changing parts of the codebase based on clients or project manager feature request won’t become a nightmare
  3. Extensible – A block of code can be used multiple times in the codebase without increasing the lines of codes in the codebase enormously.
  4. Cost-efficient – It is easier to maintain a codebase that follows the DRY Principle
  5. It is easier to write test automation scripts for the codebase

 

In conclusion, The programming etiquettes shared in this article will improve the way you write and architect your codebase, and make you a better software developer.

If you enjoyed this article, please like, comment, drop your suggestions, and share.

See you in the next article, cheers!!! ✌️

 

Leave a Reply

%d bloggers like this: