DEV Community

Cover image for A Guide to Coding Standards to Improve Code Quality
Suraj Vishwakarma for Documatic

Posted on

A Guide to Coding Standards to Improve Code Quality

Cover Image by Freepik

Introduction

Writing code is easy but writing readable code is tricky. When working as the sole developer, you might miss a lot of coding standards as you are the only one going to work on them. This is the scenario of every developer when they start writing code. But when you get into an organization where you have to code in the team, you might code like as usually you do. This is not a good practice in both scenarios.

There are coding standards that you can follow to enhance your code quality. Coding standards are a set of guidelines and conventions that developers follow when writing code. These standards ensure that the code is readable, maintainable, and consistent across a project or organization. This will not only elevate the performance of the code but also make it more readable by other developers on the team. This list is from a web developer's perspective. You will find examples that are more suited for a web developer.

So, today we are going to look into some coding standards that you should integrate into your programming. Let’s get started.

Indentation and Formatting

Prettier Demo

Make sure that your code is not only working but also readable. Readable code will let other developers easily work on the project. There are various methods that you can use to make your code readable. We will discuss a few of them in this list. For this part, we need to make the code well-indented and formatted.

Code that is well formatted does not require a lot of attention to go through. As the developers have worked with such indentation. This makes the developers read the code more easily. You can make use of tools such as Prettier to make the code well formatted as per the programming language.


Naming Convention

Naming Convention

One of the challenging tasks is to name variables and functions. There are various naming conventions that you can use. Here are some of them.

  • Camel Case: In camel case, the first letter of the identifier is lowercase, and the first letter of each subsequent concatenated word is capitalized.
Variables and Function Class
myVariable MyClass
  • Snake Case: Snake case uses underscores (_) to separate words.
Variables and Function Class
my_variable my_class
  • Upper Case: In some cases, identifiers are written entirely in uppercase letters.
Variables and Function Class
MY_VARIABLE MY_CLASS

There are other naming conventions too. I don’t go through all of them and also which is better. Just use one naming convention and stick to it.


Comments and Documentation

Better Comments

Commenting on your code is another way of making the code readable. This can be from your perspective or other developers'. You can explain the uses of function, TODO, bug explanation, Algorithm steps, and others.

Here are some of the best practices that you use to write comments:

  • Write your comments as such others can easily understand. It should be able to explain the purpose, functionality, or intent of the code.
  • Put comments as such it provides context. You should If you're referring to something specific, provide enough context for others to understand your comment. You should write a comment before the code. In-line comments should be used for small comments.
  • Follow a consistent commenting style as per the programming language. There are various ways of commenting but choose one and stick with that for readability purposes.
  • Be concise with your comments. Long and overly explained comments can become difficult to read.
  • Don’t write comments for each line. As it will overshadow the important comments and also make the code full of comments.
  • Self-explanatory steps and usually things should be exempt from commenting.

Tools such as Better Comments can help you in writing finer comments. Mintlify can help you in generating comments and documentation for your project.


Error Handling

Error Handling

Errors and exceptions are part of the code. It deals with identifying, capturing, and appropriately responding to any errors. It is a critical aspect of writing reliable code. Guidelines for handling errors and exceptions consistently throughout the codebase, ensuring proper error reporting, and logging.

Make sure that your code is always able to handle errors. For instance, while requesting an API. Your code should cover both scenarios that is success and failure. As per the response, create a log for developers and notify the user about the unsuccessful request. This is one such way of making your code error-proof.


Testing Application

Testing Application

Testing your application is a crucial step before shipping it into production. It simply involves the process of verifying that the application is behaving as it was intended. It also involves using the application to cover all the scenarios in that the user can interact with the application.

You can create a test plan that outlines the objective, test coverage, testing techniques, and timelines. This will help in sorting most of the things even before starting testing.

Some good practices that you can use while testing:

  • Test Cases: You can write different test cases for specific scenarios to test different aspects of the application.
  • Bug Tracking: During testing, it's important to track and document any identified issues or bugs.
  • Cover different tests: You should be able to test the application with various testing techniques to ensure that the application is reliable.
  • Automate testing: Wherever possible try to automate the testing with tools. It will check for common issues and will also save a lot of time.

You can use the following techniques while testing the application:

  • Unit Testing: It deals with testing individual units or components of the application in isolation. It involves testing functions, methods, or classes to ensure they work as intended.
  • Integration Testing: It involves testing the interaction between different components or modules of the application.
  • Regression Testing: Regression testing is carried out after making changes or enhancements to the software to ensure that existing functionality has not been adversely affected.
  • Security Testing: As its name suggests, it deals with identifying vulnerabilities and weaknesses in the application.

Security

Security

While writing code, you must follow security and performance-related guideline. You should avoid common vulnerabilities and also try to optimize the code for performance. Some common security vulnerabilities can be:

  • Input Validation: You should enforce robust input validation. It will prevent vulnerabilities like injection attacks.
  • Authentication and Authorization: A modern application needs both features today. You implement strong mechanisms such as using strong password hashing, enforcing password complexity, and role-based access.
  • Data protection: You should not leak any data whether it is a user or sensitive API key. You should emphasize protecting sensitive data through techniques such as encryption, secure key management, and secure storage practices.

Conclusion

Implementing coding standards such as these will help you in achieving code quality. This will also ensure consistency, readability, and maintainability of the codebase, making it easier for developers to collaborate and understand each other's code. This will minimize the occurrence of bugs and errors.

This will make the codebase more maintainable as there will be less work to be done after completing the project. Otherwise, you have to add many things after completing the code. I hope this article has helped you know some coding standards that you can implement in your code. Thanks for reading the article.

Top comments (16)

Collapse
 
middlekerb profile image
Bonifasius Edwin

For naming convention, here is my use case

  • camelCase -> method, variable name
  • PascalCase -> class name
  • snake_case -> database field name
  • kebab-case -> url, branch, folder name
Collapse
 
adarshrajpathak profile image
Adarsh Raj Pathak • Edited

Also, The identifiers with corresponding area that they are specifically used in(or found in)
camelCase ->Java(and many other)
PascalCase -> almost every language
snake_case -> database & python(and some other)
kebab-case -> web-development(HTML, CSS, Js and related tech)

Collapse
 
mrlectus profile image
LectusMan

Kebab for goats

Collapse
 
coderatul profile image
Atul Kushwaha

Hey @middlekerb
Welcome to the community of thriving developers, connect with people, gain knowledge by reading blogs and even create blogs to share your knowledge

All the best

Collapse
 
ekimcem profile image
Ekim Cem Ülger

We all follow this in our team ! exactly the same.

Collapse
 
joebordes profile image
Joe Bordes • Edited

nice article. I agree with almost everything except the comments. IMO comments should be scarse, write code that can be easily read, use comments only when the logic is hard to grasp or you need to convey something to your future self.

Collapse
 
manchicken profile image
Mike Stemle

The problem with writing articles like this is that you’ve got a really good set of stuff that does improve quality (testing, security, etc) coupled with subjective standards (formatting, naming, and comments) which do not improve quality.

The former are disciplines which can demonstrably improve code quality, security, and performance. The latter are opinions that many folks have, sometimes rather strongly, which are a matter of preference, and contribute very little of value to the code. The latter, however, does have the potential to improve the developer experience, which is important.

My advice to most folks: focus on what makes your code better, and then for the formatting just use something that does that for you. If you find yourself spending any meaningful amount of time focused on aesthetic qualities of the text files that make up your application, you’re likely wasting valuable time that you could be using to write more tests which will make your code better.

Collapse
 
panayiotisgeorgiou profile image
Panayiotis Georgiou

thanks for sharing

Collapse
 
jegonag profile image
jegonag • Edited

Follow secure coding practices to protect against common vulnerabilities, such as injection attacks, cross-site scripting (XSS), or insecure authentication. Sanitize user input, use parameterized queries, and validate and escape data to prevent security breaches.
Regards: apk crave

Collapse
 
akpi816218 profile image
akpi816218

I always keep constants and environment variables in UPPER_SNAKE_CASE
class/constructor camel case, capitalized first letter
Everything else camel case, first letter lower case

Collapse
 
adarshrajpathak profile image
Adarsh Raj Pathak

The identifiers with corresponding area that they are specifically used in(or found in)
camelCase ->Java(and many other)
PascalCase -> almost every language
snake_case -> database & python(and some other)
kebab-case -> web-development(HTML, CSS, Js and related tech)

Collapse
 
freddyhm profile image
Freddy Hidalgo-Monchez

Very good points! One thing that is also very important to remember is how readable is the code for the stakeholders (team, group, etc.)

I believe adhering to a style and coding convention can only improve the readability if the stakeholders agree that it does 😛

So having the buy-in from everyone involved is super critical.

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍

Collapse
 
og_dev profile image
OGBONNA SUNDAY

Thanks for this awesome article 🍕. Really insightful

Collapse
 
kalejaiye75 profile image
Kalejaiye75

Thanks for sharing, even if I haven't got to this level, at least I have the knowledge now..

Collapse
 
jerubaalking profile image
Gideon Sainyeye

I usually write ugly stuff that i only can understand then start separating core functions and methods so i can reuse them.... then i start commenting and prettifiyin.