Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Start News My account User list downloads Tutorials Questionnaires Home The Contact Search...
Cool C / C ++
Programming in C / C ++
The next compendium of ideas for C programming should not be seen as a rigid rule body. It is intended to be a set of suggestions so we Notes
can all work better and create more readable, efficient and with less errors. Compilers
Sources
Hungarian notation is not welcome in new code. It contributes to bad programmer practices, and does not participate in the idea that Tutorials
the programmer must manipulate abstractions to be able to devote himself better to the problem that really concerns him. Questionnaires
Home
The
Each set of values that deserve a type, must be handled with a separate type, this way the programmer is abstracted from the detail
of what that type should be. It also reserves one the possibility of altering the type that is used for those values. Related Links
Guestbook
Contact
Native language types should be used as much as possible. That is to say that in C ++ will use bool and not some strange macro
BOOL or BOOLEAN .
ACCESS
User
Logical or Boolean variables should, if possible, have names that are viewed as predicates. Example: If there is a variable, we can
write: if (are_fritos) abort (); . Anti-example: On the other hand, if the variable is called state_friture , the written code is not as Password
obvious: if (state_friture) ....
Remember
LOGIN
Global functions and variables must have descriptive names, regardless of whether they are long. Example: close_conexion () . Forgot your Username / Password?
No account yet? Registry
Enums should be preferred over the preprocessor macros. They provide more type checking and are seen by debuggers.
Documentation
Each function must be documented in the code: What it does. What are its parameters. This should be done using the syntax
required by the automatic document generator. For example if you use doxygen , this would look something like this:
Code:
Do not over-document. Documentation should usually talk about what, and not about how. Anti-example:
Code:
http://www.programacionenc.net/index.php?option=com_content&view=article&id=59:normas-de-programacion-en-cc&catid=37:programacion-cc&Item 1/3
9/28/2017 Programming Rules in C / C ++
general
All necessary .h must be included in order to never depend on the compiler implicitly assuming how a function is defined.
The compilers should be configured (for example from the corresponding Makefile) to emit a generous amount of warnings.
Any function whose scope is limited to the file in which it is being defined must be declared static. This avoids name collisions with
other parts of the application, in addition some compilers can take advantage of this and improve warnings and / or generate more
efficient code.
It is preferred that a static function is defined before its use, to avoid the need for prototypes which in this case are redundant.
A function should not extend over more than one screen or two. Functional units must be isolated and coded in separate functions
even if those functions will only be called in one place. This contributes greatly to the subsequent reading of the code.
It is preferable to use the function return to actually return what the function calculates, instead of returning an error code and
putting the result in a parameter passed by reference. Example: A price dame function is preferred to be price = price_name ()
instead of st = price_name (& price); . If you want to return an error code, better do it as an output parameter:
Code:
This has the advantage of facilitating the propagation of errors upwards. As an example, the function price_price might look like this:
Code:
In string parameters that are input and not touched in the function.
Macros should not be abused. If a string appears only once, and it's never going to change a definition it will probably only make the
code reading more complex. Parameterized macros are often more convenient to code as inline functions.
Other articles
WRITE A COMMENT
http://www.programacionenc.net/index.php?option=com_content&view=article&id=59:normas-de-programacion-en-cc&catid=37:programacion-cc&Item 2/3
9/28/2017 Programming Rules in C / C ++
Name (required)
E-mail (required)
Website
Refresh
Submit
JComments
http://www.programacionenc.net/index.php?option=com_content&view=article&id=59:normas-de-programacion-en-cc&catid=37:programacion-cc&Item 3/3