Sei sulla pagina 1di 3

9/28/2017 Programming Rules in C / C ++

Start News My account User list downloads Tutorials Questionnaires Home The Contact Search...

Cool C / C ++
Programming in C / C ++

Home Tutorials Programming C / C ++ Programming Rules in C / C ++


MAIN MENU
Programming Rules in C / C ++
Start
Written by adrianvaca
News
Sunday, 20 March 2011 12:48
My account
Introduction
User list

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

Types and variables courses

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

Short-range variables, such as a counter, should have short, simple names.


TAGS

milliseconds convert ordering


The variables and global functions that belong to a reusable module or library must be demarcated, thus taking care of the
functions matrix keyboard
namespace. In C this means that a prefix will be used in front of the names. In C ++ the namespaces of the language should be
windows visual recursive
used.
algorithms time files number
winbgi library

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:

/ ** Divide two integers.


* This function divides two integers and does no more than that.
* @param numerator what goes up
* @param denominator what goes down

Do not over-document. Documentation should usually talk about what, and not about how. Anti-example:

Code:

/ * We assign the variable to the value 3 * /


a = 3;

/ * We invoke the function that cuts the pasture * /

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

The code should not cause warnings to be compiled.

All functions should always have their prototypes.

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:

char * error = NULL;


price = give me price (article, & error);
if (error)
{

This has the advantage of facilitating the propagation of errors upwards. As an example, the function price_price might look like this:

Code:

double dame_price (const char * article, char ** error)


{
return get_value (article, "price", error);
}

If there was an error in obtaining_value , it is passed up without further problems.

Const should be used in the corresponding cases. For example:

In string parameters that are input and not touched in the function.

In string variables to which a "literal" string will be assigned.

In member functions of a class that do not alter the object.

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

Programming Rules in C / C ++ Optimizations in C ++


Creating and using a dynamic library (.dll) C functions with variable argument list
Sort algorithms C ++ operator overload

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

Rest: 1000 symbols

Subscribe to notification of new comments

Refresh

Submit

JComments

Copyright Cool C / C ++ :: Programming in C / C ++


Webmaster: Adrin Vaca

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

Potrebbero piacerti anche