Thumbnail

7 Tips To Write Clean And Better Code & Firmware

Introduction

Being a developer of code doesn’t mean just being good with the programming language. In fact a good developer is the one whose code not only efficient, but also Clean and Readable so lets get into better coding.

So in this article we will discuss Tips to write Cleaner & better code. Furthermore show you various practices and standards to follow while writing a good code. Which will be beneficial for the developer who has written the code. As well as for the community who might go through the code for further development. This will in return benefit the development of the code as well as for the future development.

So without any further ado let’s start coding!

What is a Clean Code?

Here are some of the points which you may call as a criteria for cleaner coding.

  1. Code should be Readable. Which means that by reading the name of function, variable & the File name. Code should give an idea about logic in it or for what that variable is used for.
  2. It should be elegant. Your code should not only be efficient but also follow good standards of coding like MISRA C. Use different case for file name, variable name and function names.
  3. Should be simple, easy to understand and easy to change.

How to Write Cleaner and Better Code?

Below are some of the Coding Styles, Patterns or Way which I Personally follow while writing Code/Firmware for different device.

1. Never use hard coded variables

The main reason hardcode is bad because it assumes that value will not change in future. Trust me I have done hardcoding in my early days (Please don’t tell anyone) assuming my client wont ask for any changes in future. And what else to say my past code came back and haunted me for days trying to figure out which god dam value to change.

So that’s why I always try to avoid hard code because, the value could probably be changed later, but it is not know by whom and how, so you don’t know where it belongs.

2. Use Pascal Case (PascalCase) for Variable

Now this tip and next two are not a standard but a practice for Readable coding.

3. Use Lower case of snake_case for file name

4. Use Upper case of SNAKE_CASE for #define and enum

5. Function name should be FileName_FunctionName()

Again it not a Standard or a rule but this method helps one understand. Where to look for that particular function as well as it gives a brief idea about what that function might be used for.

As you can observer form the below example code that these functions are form MBUS file. And they are used to process incoming frame form MODBUS and set the values of MODBUS Registers.

6. Use descriptive names for Variable & Function

Readable code starts with code that you find easy to read. Having good naming scheming throughout the code instead of Frankenstein naming will make your code Readable.

7. Comments must be used only for Description

Its a habit of many programmers to comment out something. Which is no longer needed instead of removing the code. Well now that we have github it a good idea to remove. Any part of code which is no longer required. This will make your code more readable.

So that’s pretty much it for this article. If you liked this article you make also like others. Make sure to share this article and more tips are always welcomed in the comments section below!

Leave a Comment