Guest User

Untitled

a guest
Jul 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. ///////////////////////////////////////////
  2. // method 1
  3.  
  4. // stuff that can go wrong;
  5.  
  6. if (test1 == failed)
  7. {
  8. // print error;
  9. // exit;
  10. }
  11. else
  12. {
  13. // more stuff that can go wrong;
  14.  
  15. if (test2 == failed)
  16. {
  17. // print error;
  18. // exit;
  19. }
  20. else
  21. {
  22. // ... and so on...
  23. }
  24. }
  25.  
  26. ///////////////////////////////////////////
  27. // method 2
  28.  
  29. // stuff that can go wrong;
  30.  
  31. if (test1 == failed)
  32. {
  33. // print error;
  34. // exit;
  35. }
  36.  
  37. // more stuff that can go wrong;
  38.  
  39. if (test2 == failed)
  40. {
  41. // print error;
  42. // exit;
  43. }
  44.  
  45. // ... and so on...
  46.  
  47. if (0)
  48. {
  49. ERROR:
  50. // Handle errors, and exit/return after potentially freeing resources
  51. }
  52.  
  53. #define LOCAL_ASSERT(COND) if (COND) {
  54. /* Handle errors, and exit/return after potentially freeing resources */
  55. }
  56.  
  57. // method 1
  58. if (error) goto ERROR; // no else
  59.  
  60. // method 2
  61. LOCAL_ASSERT(cond);
  62.  
  63. if (test1 == failed)
  64. {
  65. // print error;
  66. // exit;
  67. }
  68. else if (test2 == failed)
  69. {
  70. // print error;
  71. // exit;
  72. }
  73. else
  74. {
  75. // ... and so on...
  76. }
  77.  
  78. const Bool funcFoo(int someval, int someval2, int someval3)
  79. {
  80. if(someval == okval)
  81. { // We're ok
  82. if(someval2 == okval2)
  83. { // Still ok.
  84. if(someval3 == okval3)
  85. { // Yippee! We made it!
  86. return True; // <===== ONLY SUCCESS RETURN POINT
  87. }
  88. }
  89. }
  90. // Houston, we had a problem.
  91. return False; // <===== ONLY FAIL RETURN POINT
  92. }
  93.  
  94. const Bool funcFoo(int someval, int someval2, int someval3)
  95. {
  96. if(someval == okval)
  97. { // We're ok
  98. if(someval2 == okval2)
  99. { // Still ok.
  100. if(someval3 == okval3)
  101. { // Yippee! We made it!
  102. return True; // <===== ONLY SUCCESS RETURN POINT
  103. }
  104. else
  105. { // someval3 is bad.
  106. //...maybe handle, not return.
  107. }
  108. }
  109. else
  110. { // someval2 is bad.
  111. // ...maybe handle, not return.
  112. }
  113. }
  114. else
  115. { // someval is bad.
  116. // ...maybe handle, not return.
  117. }
  118. // Houston, we had a problem.
  119. return False; // <===== ONLY FAIL RETURN POINT
  120. }
Add Comment
Please, Sign In to add comment