code_junkie

Should a function have only one return statement? [closed]

Nov 10th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. public void DoStuff(Foo foo)
  2. {
  3. if (foo != null)
  4. {
  5. ...
  6. }
  7. }
  8.  
  9. public void DoStuff(Foo foo)
  10. {
  11. if (foo == null) return;
  12.  
  13. ...
  14. }
  15.  
  16. function()
  17. {
  18. HRESULT error = S_OK;
  19.  
  20. if(SUCCEEDED(Operation1()))
  21. {
  22. if(SUCCEEDED(Operation2()))
  23. {
  24. if(SUCCEEDED(Operation3()))
  25. {
  26. if(SUCCEEDED(Operation4()))
  27. {
  28. }
  29. else
  30. {
  31. error = OPERATION4FAILED;
  32. }
  33. }
  34. else
  35. {
  36. error = OPERATION3FAILED;
  37. }
  38. }
  39. else
  40. {
  41. error = OPERATION2FAILED;
  42. }
  43. }
  44. else
  45. {
  46. error = OPERATION1FAILED;
  47. }
  48.  
  49. return error;
  50. }
  51.  
  52. return a > 0 ?
  53. positively(a):
  54. negatively(a);
  55.  
  56. if (a > 0)
  57. return positively(a);
  58. else
  59. return negatively(a);
  60.  
  61. int f(int y) {
  62. int value = -1;
  63. void *data = NULL;
  64.  
  65. if (y < 0)
  66. goto clean;
  67.  
  68. if ((data = malloc(123)) == NULL)
  69. goto clean;
  70.  
  71. /* More code */
  72.  
  73. value = 1;
  74. clean:
  75. free(data);
  76. return value;
  77. }
  78.  
  79. int g(int y) {
  80. value = 0;
  81.  
  82. if ((value = g0(y, value)) == -1)
  83. return -1;
  84.  
  85. if ((value = g1(y, value)) == -1)
  86. return -1;
  87.  
  88. return g2(y, value);
  89. }
  90.  
  91. function isCorrect($param1, $param2, $param3) {
  92. $toret = false;
  93. if ($param1 != $param2) {
  94. if ($param1 == ($param3 * 2)) {
  95. if ($param2 == ($param3 / 3)) {
  96. $toret = true;
  97. } else {
  98. $error = 'Error 3';
  99. }
  100. } else {
  101. $error = 'Error 2';
  102. }
  103. } else {
  104. $error = 'Error 1';
  105. }
  106. return $toret;
  107. }
  108.  
  109. function isCorrect($param1, $param2, $param3) {
  110. if ($param1 == $param2) { $error = 'Error 1'; return false; }
  111. if ($param1 != ($param3 * 2)) { $error = 'Error 2'; return false; }
  112. if ($param2 != ($param3 / 3)) { $error = 'Error 3'; return false; }
  113. return true;
  114. }
  115.  
  116. function isEqual($param1, $param2) {
  117. return $param1 == $param2;
  118. }
  119.  
  120. function isDouble($param1, $param2) {
  121. return $param1 == ($param2 * 2);
  122. }
  123.  
  124. function isThird($param1, $param2) {
  125. return $param1 == ($param2 / 3);
  126. }
  127.  
  128. function isCorrect($param1, $param2, $param3) {
  129. $toret = false;
  130. if (!isEqual($param1, $param2)
  131. && isDouble($param1, $param3)
  132. && isThird($param2, $param3)
  133. ) {
  134. $toret = true;
  135. }
  136. return $toret;
  137. }
  138.  
  139. resulttype res;
  140. if if if...
  141. return res;
  142.  
  143. public void DoStuff(Foo foo)
  144. {
  145. if (foo == null) return;
  146. }
  147.  
  148. void foo (int i, int j) {
  149. A a;
  150. if (i > 0) {
  151. B b;
  152. return ; // Call dtor for 'b' followed by 'a'
  153. }
  154. if (i == j) {
  155. C c;
  156. B b;
  157. return ; // Call dtor for 'b', 'c' and then 'a'
  158. }
  159. return 'a' // Call dtor for 'a'
  160. }
  161.  
  162. A foo () {
  163. A a1;
  164. // do something
  165. return a1;
  166. }
  167.  
  168. void bar () {
  169. A a2 ( foo() );
  170. }
  171.  
  172. sub Int_to_String( Int i ){
  173. given( i ){
  174. when 0 { return "zero" }
  175. when 1 { return "one" }
  176. when 2 { return "two" }
  177. when 3 { return "three" }
  178. when 4 { return "four" }
  179. ...
  180. default { return undef }
  181. }
  182. }
  183.  
  184. @Int_to_String = qw{
  185. zero
  186. one
  187. two
  188. three
  189. four
  190. ...
  191. }
  192. sub Int_to_String( Int i ){
  193. return undef if i < 0;
  194. return undef unless i < @Int_to_String.length;
  195. return @Int_to_String[i]
  196. }
  197.  
  198. public void hello()
  199. {
  200. if ( ....)
  201. {
  202. ....
  203. }
  204. }
Add Comment
Please, Sign In to add comment