/ home / Programming / Style /

Braces

Curly braces { } should be used on all conditionals, and thensome.

How do I place my braces? ยง
  1. // Bad
  2. if (sneed)
  3. {
  4. feed();
  5. seed();
  6. }
  7. else
  8. {
  9. chuck();
  10. }
  11. // Good
  12. if (sneed) {
  13. feed();
  14. seed();
  15. } else {
  16. chuck();
  17. }

  1. // Bad
  2. void sneed() {
  3. feed();
  4. seed();
  5. }
  6. // Good
  7. void sneed()
  8. {
  9. feed();
  10. seed();
  11. }

  1. // Bad
  2. void feed() { seed(); }
  3. void formerly() { feed(); seed(); }
  4. void sneed() { chuck(); seed(); }
  5. // Good
  6. void feed () { seed(); }
  7. void formerly() { feed (); seed(); }
  8. void sneed () { chuck(); seed(); }

  1. // Bad
  2. int sneed()
  3. {
  4. Chuck c;
  5. c.setSeed(1);
  6. c.setFeed(2);
  7. c.init();
  8. Chuck d;
  9. Farmer farmer;
  10. if ( farmer.isSneed() ) {
  11. c.setSeed(3);
  12. c.setFeed(4);
  13. } else {
  14. c.setSeed(5);
  15. c.setFeed(6);
  16. }
  17. Chuck e;
  18. e.clear();
  19. return c + d + e;
  20. }
  21. // Good
  22. int sneed()
  23. {
  24. Chuck c;
  25. {
  26. c.setSeed(1);
  27. c.setFeed(2);
  28. c.init();
  29. }
  30. Chuck d;
  31. {
  32. Farmer farmer;
  33. if ( farmer.isSneed() ) {
  34. d.setSeed(3);
  35. d.setFeed(4);
  36. } else {
  37. d.setSeed(5);
  38. d.setFeed(6);
  39. }
  40. }
  41. Chuck e;
  42. {
  43. e.clear();
  44. }
  45. return c + d + e;
  46. }

  1. // Bad
  2. Sneed::Sneed()
  3. {
  4. QString s;
  5. // Prepare Seed
  6. m_Seed.clear();
  7. m_Seed.append( s_Type );
  8. m_Seed.setMax( mn_Limit );
  9. m_Seed.setMin(0);
  10. s.remove( m_Seed.toString() );
  11. // Add feed
  12. if ( m_Feed == nullptr ) { m_Feed = new Feed(); }
  13. m_Feed->grabAll();
  14. m_Feed->splice( &m_Seed );
  15. m_Feed->waitForSneed();
  16. s.append( m_Feed->readAll() );
  17. // Finish chuck
  18. m_Chuck.setFinal(s);
  19. this->clear();
  20. // Finish Sneed
  21. g.submode = NoSubMode;
  22. g.subsubmode = NoSubSubMode;
  23. g.movetype = MoveInclusive;
  24. g.gflag = false;
  25. m_Register = '"';
  26. g.rangemode = RangeCharMode;
  27. g.currentCommand.clear();
  28. resetCount();
  29. }
  30. // Good
  31. Sneed::Sneed()
  32. {
  33. QString s;
  34. /* Prepare Seed */
  35. {
  36. m_Seed.clear();
  37. m_Seed.append( s_Type );
  38. m_Seed.setMax( mn_Limit );
  39. m_Seed.setMin(0);
  40. s.remove( m_Seed.toString() );
  41. }
  42. /* Add feed */
  43. {
  44. if ( m_Feed == nullptr ) { m_Feed = new Feed(); }
  45. m_Feed->grabAll();
  46. m_Feed->splice( &m_Seed );
  47. m_Feed->waitForSneed();
  48. s.append( m_Feed->readAll() );
  49. }
  50. /* Finish Chuck */
  51. {
  52. m_Chuck.setFinal(s);
  53. }
  54. this->clear();
  55. /* Complete Sneed */
  56. {
  57. g.submode = NoSubMode;
  58. g.subsubmode = NoSubSubMode;
  59. g.movetype = MoveInclusive;
  60. g.gflag = false;
  61. m_Register = '"';
  62. g.rangemode = RangeCharMode;
  63. g.currentCommand.clear();
  64. }
  65. resetCount();
  66. }