How to write clean code in C, C++ or in java
First of all, I'm not a c or a c++ programming expert, but their are some rules that could make the code simpler to read, most of them are not time consumming when you write down the code, but they will allow you to save a huge amount of time. The list is not exhaustive, but I think I put the most important rules.
- Keep the indentation of the code, this is very important in order to know when a instruction start's and when it's end, it could be a good idea to put a newline just before the opening brackets
- Sometime, for not important rules, we could write them in one line, this will make people pay less attention on what is not so much important, commenting this line just before using them could help to understand for what they are intended
- When you comment the code, you could use the commenting of the comment end region, in such way :
/*
code....;
//*/
In such we could uncomment faster, it's generally also a good idea not use this kind of comments : // for several lines
-
if the language allow it, use the variable only when it's required, and for loop index, use all time the same loop index. I have a strong preference for i,j and k for loops, indirect results from them could be ii,jj and kk. For instance : ( this could be a java or a C++ code )
for(int i;i
{
int ii=list[i];
}
This way of writing the code, will allow to reuse i in another part of the function, ii will also be specific to the block of code. Most of the time their is no reason to reuse the variable, it's also not a question of performance, the compiler could realize that we use twice the two integer, and could use the same memory place for the calculation.
-
Other good symbole to use are for instance in java for it iterator, jl for JLabel, jtp for the JTextPane...
-
Commenting the function, this should be done in the function definition file, so that we could understand in one blink what the code is doing. A coworker told me that he write first down the explanation, and then write the code.
-
Have a preference for the for loop instead of the while, the last one separate the stopping condition, and the state changing from the code that the block contain.
-
Avoid storing data in a extra variable when it's not necessary, if the question is had we been in this part of code, we could keep a trace from it using simply a boolean. The less we write code, the easyer it will to be understood ( this don't mine in the opposite that we should use wired function sideeffects ).
-
Don't use speciallized coding type, function priority that nobody know's about, this will bring more confusion than anything else.
-
Strong typing helps generally to understand what is happening : using 0.0 instead of 0 will recall the reader that this variable is a integer ( I believe that it'll not change anyhow the code ). Increasing operator should only be used on integer, this brings more specificity to the code.
-
When overloading function ( same function with different parameter ), ensure that they are doing the same thing. C++ do a lot of work without necessary our agrement, it could convert a type B to a type A when A has a "copy" constructor like this : A(B b). This could be quite surprising.
-
Do the variable cleanup as early as possible
-
Making functions as small as possible could help to understand what they do. The performances are not a problem when we inline the functions. Generally final functions or private function are the equivalent in java but having such functions will have another effect.
-
Rather use inline function than macro, the latest could causes several errors
-
Variable should not start with a underscore '_', use your own convention for copying variable on the constructor.
class C{
private:
int c;
public:
C(int c_){
c=c_;
}
-
When overloading function, ensure that they do the same work
-
you could define accesor and mutator function, it's generally a good idea to use get and set name for this function.