This is an old revision of the document!


Convenção de Programação

Recomendações para programação em C++.
Baseado em “The Elements of C++ Style - T. Misfeldt, G. Bumgardner, A. Gray

Namespace

  • Usar sempre o namespace TerraLib
  • Usar sempre os namespaces externos explicitamente

Exemplo

namespace TerraLib
{
  ....
  std::string myString = getName();
  ....
}

Preprocessador

  • Use '#include “ … “’ for collocated header files and '#include <…>' for external header files
  • Place preprocessor include guard in header files
  • Use #if. . #endif and #if def . . #endif instead of ”/* . . .*/” comments to hide blocks of code
  • Use macros sparingly
  • Do not use “#define” to define constants – declare static const variables instead

Declarações

  • Use typedefs to simplify complicated type expressions
  • Create a zero-valued enumerator to indicate an uninitialized, invalid, unspecified or default state
  • Do not define enumerations using macros or integer constants
  • Declare enumerations within a namespace or class

Escopo

  • Declare for-loop iteration variables inside of for statements

Funções e Métodos

  • Use an enumeration instead of a Boolean to improve readability
  • Use an object pointer instead of a reference if a function stores a reference or pointer to the object
  • Accept objects by reference and primitive or pointer types by value
  • Pass enumerator values, not integer constants
  • Do not use void* in a public interface
  • Use inline functions instead of macros
  • Inline only the simplest of functions

Classes

  • Define small classes and small methods
  • Declare the access level of all members
  • Declare all member variables private
  • Avoid the use of friend declarations

Membros de Classes

  • Declare an explicit default constructor for added clarity
  • Always declare a copy constructor, assignment operator, and destructor if the class can be instantiated
  • Always implement a virtual destructor if your class may be subclassed
  • Make constructors protected to prohibit direct instantiation
  • Make constructors private to prohibit derivation
  • Declare a private operator new() to prohibit dynamic allocation
  • Declare a protected or private destructor to prohibit static or automatic allocation
  • Declare single-parameter constructors as explicit to avoid unexpected type conversions
  • Use default arguments to reduce the number of constructors
  • Do not overload non-virtual methods in subclasses
  • Declare virtual methods protected and call them from public non-virtual methods
  • Keep your functions “const-correct“
  • Use object pointers and references in class declarations

Operadores

  • Adhere to the natural semantics of operators
  • Do not overload operator&&() or operator||()
  • Invoke the superclass assignment operator(s) in the assignment operator of a subclass
  • Implement copy-safe and exception-safe assignment operators
  • Define binary operators outside of a class
  • Implement a Boolean operator in terms of its opposite

Templates

  • Use templates instead of macros to create parameterized code
  • Do not use CV-qualified types as template parameters

Exemplo

// Example of bad macro
#define MAX(a,b) ( ((a) > (b)) ? (a) : (b) )
 
// Use template instead
template<class T>
inline T max (const T& a, const T& b)
{
  return (a > b) ? a : b/
}

Segurança, Cast e Conversões de Tipos

118. Use C++ casting operators instead of C-style casts 119. Avoid type casting and do not force others to use it 120. Use static-cast<> to impose non-intuitive implicit conversions 121. Do not use reinterpret-cast<> in portable code 122. Only use const-cast<> on “this” or when dealing with non-const-correct code 123. Never use dynamic-cast<> as a substitute for polymorphism 124. Use dynamic-cast<> to restore lost type information 125. Always treat string literals as const char* 126 Use C++ streams instead of stdio function for type safety 127 Test all type conversions

Inicialização e Construção

128 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

139. 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

145. 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

151. 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

160. Use lazy evaluation and initialization 161. Reuse objects to avoid reallocation 162. Leave optimization for last


Navigation