The main issues of code quality with if-then-else
,
switch
, and select case
are proper
indentation and the use of curly braces in C. Improperly
indented code, and code that does not use curly braces for
nested if
s, can be easily misread. Use of curly
braces is often a good idea even if they are not needed to group
multiple statements, since they can clarify the code.
// Misleading if ( x == 1 ) if ( y == 2 ) z = 3; else // Belongs to if ( y == 2 ), not if ( x == 1 ) z = 4;
// Clearer if ( x == 1 ) if ( y == 2 ) z = 3; else z = 4;
// Clearest if ( x == 1 ) { if ( y == 2 ) z = 3; else z = 4; }