|
|
| FAQs >> What is 'good
code'? |
'Good code' refers to the code that works properly, does not
have any bugs and is easily readable, understandable and
maintainable. Usually, some organizations have defined
coding standards for their employees which should be
followed by them, but every single person has a different
idea about what is good and what is best, and everyone tends
to follow his/her own set of rules. Many theories and
metrics exist, e.g. McCabe Complexity metrics. But at the
same time, it should also be kept under consideration that
too much use of rules and standards can constrain your
productivity and creativity. 'Peer reviews', 'buddy checks'
code analysis tools, etc. are the examples of the tools that
can be used to check for problems and enforce standards. For
C and C++ coding, following are some ideas to consider while
setting the rules and standards for coding. However, these
may or may not be applicable to every situation:
• reduce or completely eliminate the use of global
variables.
• keep your function and method names as descriptive as
possible – try using upper and lower cases, avoid the use of
abbreviations if possible, the names should not be very long
or very short. And finally, be consistent in following the
naming scheme.
• keep your variable names as descriptive as possible -
use both upper and lower case, avoid too much abbreviations,
use as many characters as required to be sufficiently
descriptive. Finally, be consistent in following your naming
conventions.
• The functions and methods should be smaller in size,
less than 100 lines of code is good to have, while less than
50 lines of code is preferable in most situations.
• There should be well defined comments preceding a function
that describe the function and the functionality it
provides.
• The code should be well organized for readability.
• The use of whitespace should be generous both
vertically and horizontally
• One line of code should not exceed more than 70
characters.
• One line should have one statement.
• Your coding style should follow a pattern throughout
the program. The coding style includes stuff like using
indentation, brackets, naming conventions etc.
• A common rule of thumb is that there should be as many
lines of comments in your code as many lines of code are, so
include as many comments as you can.
• The application should have a proper documentation
irrespective or how small or how big the application is. The
documentation should define process and flow of the program,
and even a few paragraphs would serve the purpose, or if
possible, we should focus on preparing a separate flow chart
and detailed program documentation.
• the exception and error handling processes should be
used as much as possible along with the status and error
logging mechanisms.
• In C++, in order to reduce the complexity to the
maximum and increase maintainability to maximum, avoid a lot
of inheritance mechanism usage (these are unavoidable if the
application actually requires them). Multiple inheritance
should be reduced to maximum, and operator overloading
should also be avoided.
• In C++, the class methods should be kept small, less
than 50 lines of code is usually preferable.
• In C++, exception handlers should be extensively used.
|
| Sponsored Links: |
|
|
| << back |
|
|
|