Coding style convention in C language

C

We will try to list the things you need to keep in mind when talking about conventions; then we will go through all the C language instructions and give examples.

The code must be:
1. Clear and modular
The code will be divided into components, so there is a logical separation (e.g., multiple files, more functions, etc.). Each elemental logic piece will be moved to a function (which allows reuse of the code.) Functions that have something in common (e.g., mathematical functions) will be grouped together.

2. Expression
a. The names of the entities must be suggestive. The variable name must be suggestive and show what the variable is used for. It is perfectly ok to have names like i, j, k, p, q for the indexes/counters we use when working with paintings, but for non-vital things, the variable name must look more (eg int count_duplicates; – the first situation clearly states that this counter is used to count duplicates, instead of the second situation we can not understand what it is used without reading all the code that uses it). Function names also reflect the action it performs. Example: int count_duplicates (int n, int v [NMAX]); int (int n, int in [NMAX]); The first case says what the function does, in the second case the name of the function does not tell us anything.

So:
The names of variables and functions will be suggestive; they will be composed of small letters, digits, and underscore (if we want to have more words). Comments help to improve the expressiveness when the name and organization of the code are not enough. Example: a function that searches for duplicates in a vector, then deletes duplicate elements, and from the remaining ones deletes them all pare or divisible by 1007 and returns and how many numbers are prime. We can not express the entire functionality just by the name of the function, so we will call it update_array, and before the signature of the function, we will explain the update algorithm in a few words.

3. Legible
a. A very important component is the spacing. The code must be as airy as possible to contain spaces, empty lines where appropriate.
b. Functions must not be very long, up to 80 lines in C, lines must not be very long, up to 80 characters per line in C.

c. Indenting plays a very important role. Each time a pair of cloaks (“{}” opens), the code is indexed with a tab (4 spaces) to the right.
It is very important that we can write the code correctly, but in the same way, our code must be understandable to other people. Program conventions are important because 80% of the time allocated to a software component is maintenance. Very rarely a software product is maintained throughout its use by the same person. Code conventions improve the readability of the product and allow software engineers to understand a new program quickly.

Imagine the following scenarios: one or two people write code in different ways, and at some point, they have to combine it to make the final product. Since no one understands what the other person did because they wrote the code as they wanted, they now have to sit and make changes so that they come to a common denominator and can collaborate. That is why we believe that using common code and following rules in writing is appropriate.

Therefore, the following requirements should be followed:
a. Use of Comments without reservation: what do the procedures, what are the variables, explain the steps of the algorithm, etc.?
b. Use suggestive names for variables and procedures
c. Modular writing of the project
d. Use set / get, start / stop, add / delete, save / load pairs.

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories