code_junkie

Most crucial elements in a light-weight C++ coding standard

Nov 11th, 2011
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. if (p = 0)
  2.  
  3. if (p == 0)
  4.  
  5. if (0 == p)
  6.  
  7. // bad example - what the writer wrote
  8. if( i < 0 )
  9. printf( "%dn", i );
  10. ++i; // this error is _very_ easy to overlook!
  11.  
  12. // good example - what the writer meant
  13. if( i < 0 ) {
  14. printf( "%dn", i );
  15. ++i;
  16. }
  17.  
  18. float x = (y > 3) ? 1.0f : -1.0f;
  19.  
  20. float x = foo(2 * ((y > 3) ? a : b) - 1);
  21.  
  22. if (5 == variable)
  23.  
  24. struct MyStruct // BAD
  25. {
  26. int i ; bool j ; char * k ;
  27. }
  28.  
  29. struct MyStruct // GOOD
  30. {
  31. MyStruct() : i(0), j(true), k(NULL) : {}
  32.  
  33. int i ; bool j ; char * k ;
  34. }
  35.  
  36. MyStruct oMyStruct = { 25, true, "Hello" } ; // BAD
  37. MyStruct oMyStruct(25, true, "Hello") ; // GOOD
  38.  
  39. void doSomething()
  40. {
  41. MyStruct s = { 25, true, "Hello" } ;
  42. // Etc.
  43. }
  44.  
  45. void doSomethingElse()
  46. {
  47. MyStruct s = { 25, true, "Hello" } ;
  48. // Etc.
  49. }
  50.  
  51. // Etc.
  52.  
  53. if (bla) {
  54. for (int i = 0; i < n; ++i)
  55. foo();
  56. }
  57.  
  58. if (bla) {
  59. for (int i = 0; i < n; ++i)
  60. foo();
  61. }
  62.  
  63. if (bla) {
  64. for (int i = 0; i < n; ++i)
  65. foo();
  66. }
  67.  
  68. // char * d, * s ;
  69. strcpy(d, s) ; // BAD
  70.  
  71. // std::string d, s ;
  72. d = s ; // GOOD
  73.  
  74. int * i = (int *) malloc(25) ; // Now, I BELIEVE I have an array of 25 ints!
  75. int * j = new int[25] ; // Now, I KNOW I have an array of 25 ints!
Add Comment
Please, Sign In to add comment