string fooBar(string s, int? i) {string ret = "";if(!string.IsNullOrEmpty(s) && i != null) {var res = someFunction(s, i);
bool passed = true;foreach(var r in res) {if(!r.Passed) {passed = false;break;}}
if(passed) {// Rest of code...}}
return ret;}
将此与允许多个退出点是的代码进行比较:-
string fooBar(string s, int? i) {var ret = "";if(string.IsNullOrEmpty(s) || i == null) return null;
var res = someFunction(s, i);
foreach(var r in res) {if(!r.Passed) return null;}
// Rest of code...
return ret;}
int f(int y) {int value = -1;void *data = NULL;
if (y < 0)goto clean;
if ((data = malloc(123)) == NULL)goto clean;
/* More code */
value = 1;clean:free(data);return value;}
if (a is false) {handle this situation (eg. report, log, message, etc.)return some-err-code}if (b is false) {handle this situationreturn other-err-code}if (c is false) {handle this situationreturn yet-another-err-code}
perform any action assured that a, b and c are ok.
sub Int_to_String( Int i ){given( i ){when 0 { return "zero" }when 1 { return "one" }when 2 { return "two" }when 3 { return "three" }when 4 { return "four" }...default { return undef }}}
这样写会更好
Perl 6:很好的例子
@Int_to_String = qw{zeroonetwothreefour...}sub Int_to_String( Int i ){return undef if i < 0;return undef unless i < @Int_to_String.length;return @Int_to_String[i]}
void foo (int i, int j) {A a;if (i > 0) {B b;return ; // Call dtor for 'b' followed by 'a'}if (i == j) {C c;B b;return ; // Call dtor for 'b', 'c' and then 'a'}return 'a' // Call dtor for 'a'}