Page 82 - C-Language
P. 82
Chapter 11: Common C programming idioms
and developer practices
Examples
Comparing literal and variable
Suppose you are comparing value with some variable
if ( i == 2) //Bad-way
{
doSomething;
}
Now suppose you have mistaken == with =. Then it will take your sweet time to figure it out.
if( 2 == i) //Good-way
{
doSomething;
}
Then, if an equal sign is accidentally left out, the compiler will complain about an “attempted
assignment to literal.” This won’t protect you when comparing two variables, but every little bit
helps.
See here for more info.
Do not leave the parameter list of a function blank — use void
Suppose you are creating a function that requires no arguments when it is called and you are
faced with the dilemma of how you should define the parameter list in the function prototype and
the function definition.
• You have the choice of keeping the parameter list empty for both prototype and definition.
Thereby, they look just like the function call statement you will need.
• You read somewhere that one of the uses of keyword void (there are only a few of them), is
to define the parameter list of functions that do not accept any arguments in their call. So,
this is also a choice.
So, which is the correct choice?
ANSWER: using the keyword void
GENERAL ADVICE: If a language provides certain feature to use for a special purpose, you are
better off using that in your code. For example, using enums instead of #define macros (that's for
https://riptutorial.com/ 58

