function-exit

Should a function have only one return statement?

Are there good reasons why it's a better practice to have only one return statement in a function? Or is it okay to return from a function as soon as it is logically correct to do so, meaning there may be many return statements in the function? ...

Javascript: Trigger action on function exit

Is there a way to listen for a javascript function to exit? A trigger that could be setup when a function has completed? I am attempting to use a user interface obfuscation technique (BlockUI) while an AJAX object is retrieving data from the DB, but the function doesn't necessarily execute last, even if you put it at the end of the func...

What to put in the IF block and what to put in the ELSE block?

This is a minor style question, but every bit of readability you add to your code counts. So if you've got: if (condition) then { // do stuff } else { // do other stuff } How do you decide if it's better like that, or like this: if (!condition) then { // do other stuff { else { // do stuff } My he...

Quick question about returning from a nested statement

If I have something like a loop or a set of if/else statements, and I want to return a value from within the nest (see below), is the best way of doing this to assign the value to a field or property and return that? See below: bool b; public bool ifelse(int i) { if(i == 5) { b = true; } else { b = false; } return b; } ...

Why is "else" rarely used after "if x then return"?

This method: boolean containsSmiley(String s) { if (s == null) { return false; } else { return s.contains(":)"); } } can equivalently be written: boolean containsSmiley(String s) { if (s == null) { return false; } return s.contains(":)"); } In my experience, the second form is se...