terralib:convencaoprograma
Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
Ambos lados da revisão anteriorRevisão anteriorPróxima revisão | Revisão anterior | ||
terralib:convencaoprograma [2008/04/02 12:08] – laercio | terralib:convencaoprograma [2008/04/02 19:22] (atual) – laercio | ||
---|---|---|---|
Linha 2: | Linha 2: | ||
**Recomendações para programação em C++**. \\ | **Recomendações para programação em C++**. \\ | ||
Baseado em "//The Elements of C++ Style - T. Misfeldt, G. Bumgardner, A. Gray//" | Baseado em "//The Elements of C++ Style - T. Misfeldt, G. Bumgardner, A. Gray//" | ||
+ | |||
====== Namespace ====== | ====== Namespace ====== | ||
- | * Usar sempre o namespace TerraLib | + | * Usar sempre o namespace |
* Usar sempre os namespaces externos explicitamente | * Usar sempre os namespaces externos explicitamente | ||
+ | |||
===== Exemplo ===== | ===== Exemplo ===== | ||
<code cpp> | <code cpp> | ||
Linha 15: | Linha 17: | ||
} | } | ||
</ | </ | ||
+ | |||
====== Preprocessador ====== | ====== Preprocessador ====== | ||
* Use '# | * Use '# | ||
- | * Place preprocessor include guard in header files | + | * Place preprocessor include guard (macros associadas a arquivos include) |
* Use #if. . #endif and #if def . . #endif instead of "/* . . .*/" comments to hide blocks of code | * Use #if. . #endif and #if def . . #endif instead of "/* . . .*/" comments to hide blocks of code | ||
* Use macros sparingly | * Use macros sparingly | ||
* Do not use "# | * Do not use "# | ||
+ | |||
+ | ===== Example ===== | ||
+ | <code cpp> | ||
+ | #ifndef TE_CONNECTION_POOL_H | ||
+ | #define TE_CONNECTION_POOL_H | ||
+ | .... | ||
+ | static const float TeMaxFloat = 3.4e37; | ||
+ | .... | ||
+ | #endif // end TE_CONNECTION_POOL_H | ||
+ | </ | ||
+ | |||
Linha 28: | Linha 42: | ||
* Do not define enumerations using macros or integer constants | * Do not define enumerations using macros or integer constants | ||
* Declare enumerations within a namespace or class | * Declare enumerations within a namespace or class | ||
+ | |||
+ | ===== Example ===== | ||
+ | <code cpp> | ||
+ | typedef std:: | ||
+ | typedef std:: | ||
+ | |||
+ | enum TeSelectionMode | ||
+ | { | ||
+ | TeSelectionModeDefault, | ||
+ | TeSelectionModeTePointed, | ||
+ | TeSelectionModeTeQueried, | ||
+ | TeSelectionModeTePointedQueried //!< object pointed and queried | ||
+ | } | ||
+ | </ | ||
+ | |||
====== Escopo ====== | ====== Escopo ====== | ||
* Declare for-loop iteration variables inside of for statements | * Declare for-loop iteration variables inside of for statements | ||
+ | ===== Example ===== | ||
+ | <code cpp> | ||
+ | for (int i = 0; i < 10; i++) | ||
+ | </ | ||
Linha 98: | Linha 130: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | |||
+ | |||
====== Segurança, Cast e Conversões de Tipos ====== | ====== Segurança, Cast e Conversões de Tipos ====== | ||
- | 118. Use C++ casting operators instead of C-style casts | + | * Use C++ casting operators instead of C-style casts. Ex:<code cpp> |
- | 119. Avoid type casting and do not force others to use it | + | short a = 2000; |
- | 120. Use static-cast<> | + | int b; |
- | 121. Do not use reinterpret-cast<> | + | b = (int) a; // c-like cast notation - Ok only for fundamental data types |
- | 122. Only use const-cast<> | + | |
- | 123. Never use dynamic-cast<> | + | double d = 3.14159265; |
- | 124. Use dynamic-cast<> | + | int i = static_cast< |
- | 125. Always treat string literals as const char* | + | </ |
- | 126 Use C++ streams instead of stdio function for type safety | + | * Avoid type casting and do not force others to use it. |
- | 127 Test all type conversions | + | * Use **static-cast<> |
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | const char* myMessage = "Hello World!!!"; | ||
+ | |||
+ | printf(" | ||
+ | std::cout << myMessage << std::endl; // Use this instead | ||
+ | </ | ||
+ | * Test all type conversions | ||
+ | |||
+ | |||
====== Inicialização e Construção ====== | ====== Inicialização e Construção ====== | ||
- | 128 Initialize all variables | + | * Initialize all variables |
- | 129. Do not rely on the order of initialization of global objects | + | |
- | 130. Always construct objects in a valid state | + | |
- | 131. Initialize member variables in the initializer list | + | |
- | 132. Initialize member variables in the order they are declared | + | |
- | 133. Indicate when the declaration order of data members is significant | + | |
- | 134. Always list any superclass constructors in the initialize list of a subclass constructor | + | |
- | 135. Do not call virtual functions in constructors and destructors | + | |
- | 136 Declare and initialize static variables within functions | + | |
- | 137. Zero pointers after deletion | + | |
- | 138. Use the new and delete operators instead of malloc() and free() | + | |
+ | |||
====== Declarações e Expressões ====== | ====== Declarações e Expressões ====== | ||
- | 139. Do not rely on operator precedence in complex expressions | + | * Do not rely on operator precedence in complex expressions |
- | 140. Use block statements in control flow constructs | + | |
- | 141. Do not test for equality with true | + | |
- | 142. Replace repeated non-trivial expressions with equivalent methods | + | |
- | 143. Use size-t variables for simple loop iteration and array subscripts | + | |
- | 144. Use a dummy template function to eliminate warnings for unused variables | + | |
+ | |||
+ | |||
====== Fluxo de Controle ====== | ====== Fluxo de Controle ====== | ||
- | 145. Avoid break and continue in iteration statements | + | * Avoid **break** and **continue** in iteration statements |
- | 146 Avoid multiple return statements in functions | + | |
- | 147. Do not use goto | + | |
- | 148. Do not use try . .throw. .catch to manage control flow | + | |
- | 149. Never use setjmp() or longjmp() in a C++ program | + | |
- | 150. Always code a break statement in the last case of a switch statement | + | |
+ | |||
+ | |||
====== Manipulação de Erros e Exceções ====== | ====== Manipulação de Erros e Exceções ====== | ||
- | 151. Use return codes to report unexpected state changes | + | * Use return codes to report unexpected state changes |
- | 152. Use assertions to enforce a programming contract | + | |
- | 153. Do not silently absorb or ignore unexpected runtime errors | + | |
- | 154. Use assertions to report unexpected or unhandled runtime errors | + | |
- | 155. Use exceptions to report errors that may occur under normal program execution | + | |
- | 156 Manage resources with RAII for exception safety | + | |
- | 157. Catch exceptions by reference, not by value | + | |
- | 158. Do not discard exception information if you throw a new exception within a catch block | + | |
- | 159. Avoid throwing exceptions in destructors | + | |
+ | |||
====== Eficiência ====== | ====== Eficiência ====== | ||
- | 160. Use lazy evaluation and initialization | + | * Use lazy evaluation and initialization |
- | 161. Reuse objects to avoid reallocation | + | |
- | 162. Leave optimization for last | + | |
terralib/convencaoprograma.1207138139.txt.gz · Última modificação: 2008/04/02 12:08 por laercio