Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
terralib:convencaoprograma [2008/04/02 09:23]
laercio
terralib:convencaoprograma [2008/04/02 16:22] (current)
laercio
Line 17: Line 17:
 } }
 </​code>​ </​code>​
 +
 ====== Preprocessador ====== ====== Preprocessador ======
   * Use '#​include " ... "’ for collocated header files and '#​include <​...>'​ for external header files   * Use '#​include " ... "’ for collocated header files and '#​include <​...>'​ for external header files
-  * Place preprocessor include guard in header files+  * Place preprocessor include guard (macros associadas a arquivos include) ​in header files
   * 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 "#​define"​ to define constants – declare static const variables instead   * Do not use "#​define"​ to define constants – declare static const variables instead
 +
 +===== 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
 +</​code>​
 +
  
  
Line 30: Line 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::​pair<​TeLine,​ double> TeLineLength;​
 +typedef std::​vector<​lineLength>​ TeLineSizes;​
 +
 +enum TeSelectionMode
 +{
 +  TeSelectionModeDefault,​  ​ //!< default selection
 +  TeSelectionModeTePointed,​  ​ //!< object pointed
 +  TeSelectionModeTeQueried,​  ​ //!< object queried
 +  TeSelectionModeTePointedQueried //!< object pointed and queried
 +}
 +</​code>​
 +
  
 ====== 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++) 
 +</​code>​
  
  
Line 100: Line 130:
 } }
 </​code>​ </​code>​
 +
 +
  
  
 ====== Segurança, Cast e Conversões de Tipos ====== ====== Segurança, Cast e Conversões de Tipos ======
  
-  * Use C++ casting operators instead of C-style casts +  * Use C++ casting operators instead of C-style casts. Ex:<code cpp> 
-  * Avoid type casting and do not force others to use it +short a = 2000; 
-  * Use static-cast<>​ to impose non-intuitive implicit conversions+int b; 
 +b = (int) a;    // c-like cast notation - Ok only for fundamental data types 
 + 
 +double d = 3.14159265;​ 
 +int i = static_cast<​int>​(d);​ // No need for this cast 
 +</​code>​ 
 +  * Avoid type casting and do not force others to use it. 
 +  * Use **static-cast<>​** to impose non-intuitive implicit conversions
   * Do not use reinterpret-cast<>​ in portable code   * Do not use reinterpret-cast<>​ in portable code
-  * Only use const-cast<>​ on "​this"​ or when dealing with non-const-correct code +  * Only use **const-cast<>​** on "​this"​ or when dealing with non-const-correct code 
-  * Never use dynamic-cast<>​ as a substitute for polymorphism +  * Never use **dynamic-cast<>​** as a substitute for polymorphism 
-  * Use dynamic-cast<>​ to restore lost type information +  * Use **dynamic-cast<>​** to restore lost type information 
-  * Always treat string literals as const char* +  * Always treat string literals as **const char**
-  * Use C++ streams instead of stdio function for type safety+  * Use C++ streams instead of stdio function for type safety. Exemplo: <code cpp> 
 +const char* myMessage = "Hello World!!!";​ // A string literal 
 + 
 +printf("​%s",​ myMessage); // Stdio function 
 +std::cout << myMessage << std::endl; // Use this instead 
 +</​code>​
   * Test all type conversions   * Test all type conversions
 +
  
  
Line 128: Line 173:
   * Declare and initialize static variables within functions   * Declare and initialize static variables within functions
   * Zero pointers after deletion   * Zero pointers after deletion
-  * Use the new and delete operators instead of malloc() and free()+  * Use the **new** and **delete** operators instead of **malloc()** and **free()** 
  
  
Line 137: Line 183:
   * Do not test for equality with true   * Do not test for equality with true
   * Replace repeated non-trivial expressions with equivalent methods   * Replace repeated non-trivial expressions with equivalent methods
-  * Use size-t variables for simple loop iteration and array subscripts+  * Use **size-t** variables for simple loop iteration and array subscripts
   * Use a dummy template function to eliminate warnings for unused variables   * Use a dummy template function to eliminate warnings for unused variables
 +
  
  
 ====== Fluxo de Controle ====== ====== Fluxo de Controle ======
  
-  * Avoid break and continue in iteration statements +  * Avoid **break** and **continue** in iteration statements 
-  * Avoid multiple return statements in functions +  * Avoid multiple ​**return** statements in functions 
-  * Do not use goto +  * Do not use **goto** 
-  * Do not use try . .throw. .catch to manage control flow +  * Do not use **try .. throw .. catch** to manage control flow 
-  * Never use setjmp() or longjmp() in a C++ program +  * Never use **setjmp()** or **longjmp()** in a C++ program 
-  * Always code a break statement in the last case of a switch statement+  * 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 +  ​* ​Use assertions to enforce a programming contract 
-153. Do not silently absorb or ignore unexpected runtime errors +  ​* ​Do not silently absorb or ignore unexpected runtime errors 
-154. Use assertions to report unexpected or unhandled runtime errors +  ​* ​Use assertions to report unexpected or unhandled runtime errors 
-155. Use exceptions to report errors that may occur under normal program execution +  ​* ​Use exceptions to report errors that may occur under normal program execution 
-156 Manage resources with RAII for exception safety +  ​* ​Manage resources with RAII for exception safety 
-157. Catch exceptions by reference, not by value +  ​* ​Catch exceptions by reference, not by value 
-158. Do not discard exception information if you throw a new exception within a catch block +  ​* ​Do not discard exception information if you throw a new exception within a **catch** block 
-159. Avoid throwing exceptions in destructors+  ​* ​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 +  ​* ​Reuse objects to avoid reallocation 
-162. Leave optimization for last+  ​* ​Leave optimization for last
  

Navigation