code_junkie

Is WHILE TRUE…BREAK…END WHILE a good design?

Nov 11th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. WHILE TRUE
  2. OpenAFile
  3. IF Failed
  4. BREAK
  5. ENDIF
  6.  
  7. DoSomething
  8. IF NOT OK
  9. BREAK
  10. ENDIF
  11.  
  12. ...etc...
  13.  
  14. BREAK
  15.  
  16. END WHILE
  17.  
  18. BREAK
  19.  
  20. BREAK
  21.  
  22. BREAK
  23.  
  24. BREAK
  25.  
  26. BREAK
  27.  
  28. #define BEGIN_AE_HANDLER while (true) {
  29. #define END_AE_HANDLER break; }
  30. #AE_CHECK_ERROR(exp) if ((exp) != noErr) { break; }
  31.  
  32. /* code snippet */
  33. OSErr err = noErr;
  34. BEGIN_AE_HANDLER
  35. AE_CHECK_ERROR(err = AECreateAppleEvent( /* ... */ ));
  36.  
  37. AE_CHECK_ERROR(err = AEPutAttributePtr( /* ... */ ));
  38. /* dozens of other lines, my AppleEvents are wordy */
  39. END_AE_HANDLER
  40. if (err != noErr) { /* tada */ }
  41.  
  42. OpenAFile
  43. IF Failed GOTO TheEnd
  44. DoSomething
  45. IF NOT OK GOTO TheEnd
  46. ...etc...
  47. TheEnd:
  48.  
  49. int f(int x, int y) {
  50. if (y < 0) { // or some other error condition
  51. return 0; // or throw exception
  52. }
  53. ... // perform operations
  54. }
  55.  
  56. int f(int x, int y) {
  57. if (0 <= y) { // or some other error condition
  58. ... // perform operations
  59. } else {
  60. return 0;
  61. }
  62. }
  63.  
  64. do { ... } while(false); // or whatever it's written in VB
  65.  
  66. do { ... } while(false); // or whatever it's written in VB
  67.  
  68. do { ... } while(false); // or whatever it's written in VB
  69.  
  70. WHILE TRUE
  71. OpenAFile
  72. IF Failed
  73. BREAK
  74. ENDIF
  75.  
  76. DoSomething
  77. IF NOT OK
  78. BREAK
  79. ENDIF
  80.  
  81. ...etc...
  82.  
  83. BREAK
  84.  
  85. END WHILE
  86.  
  87. try {
  88. if (!OpenAFile()) throw new StackOverflowException(); //throws an exception? otherwise the next line would be needed
  89.  
  90. if (!DoSomething()) throw new StackOverflowException();
  91.  
  92. etc
  93. } catch (StackOverflowException soex) {
  94. //do nothing, but it can only come from 2 places
  95. }
  96.  
  97. OpenAFile
  98. If Failed Then Throw New IOException("Unable to open file.")
  99. DoSomething
  100. If Not OK Then Throw New ApplicationException("Couldn't DoSomething.")
  101.  
  102. Public Function TryToDoStuff( ... ) As Boolean
  103. Try
  104. OpenAFile
  105. DoSomething
  106. Catch ioex As IOException
  107. Return False
  108. End Try
  109. Return True
  110. End Function
  111.  
  112. do {
  113. doSomething();
  114. if (error)
  115. break;
  116. et c.
  117. } while (false);
  118.  
  119. loopname:
  120. loop
  121. ...
  122. exit loopname when ...condition...;
  123. ...
  124. end loop loopname;
Add Comment
Please, Sign In to add comment