Best Practices
These comments were taken from the wiki markup:
- "Whose best practices are these? Complexity is never good. If the contents of a control structure are too complex, chances are they should be moved into separate functions."
- A simple statement is something like an assignment or function call - anything that's not a block statement or control flow statement. Control flow statements should never only contain simple statements because a single simple statement can always be wrapped in a block statement to improve readability and maintainability - and I have read several articles which agree on that point, although I don't have them available right now. Which one is easier for you to read?
while(condition) doSomething(); doSomethingElse();
while(condition){
doSomething();
}
doSomethingElse();
- Neither, because they aren't indented properly. With proper code formatting (indentation and blank lines around the whole thing), it becomes the very legible:
while (condition)
doSomething();
doSomethingElse();
- I think that we agree on the problem (control flow statements whose scope isn't delimited properly are illegible) but disagree on the solution (I would prefer to wrap the scope of the block in a separate function, neatly labelled, and format the code properly, while you would prefer to wrap it in braces). The lack of readability in your example stems from the lack of indentation, and the braces alone don't actually improve it very much, especially with more complex nesting:
while (foo) {
if (bar(3)) {
baz();
} else if (wub) {
if (flob()) {
quux();
}
} else {
do {
mubble();
} while (clob());
}
}
- Whereas proper indentation definitely does:
while (foo)
if (bar(3))
baz();
else if (wub)
while (flob)
quux();
else
do
mubble();
while (clob());
- Further evidence of this can be seen in Python, generally hailed as one of the more readable programming languages, in which there are no block delimiters other than indentation.
- This guideline exists to prevent huge blocks of any kind, since it makes the developer think, when adding more code to a block, whether perhaps this code would fit better in a separate function, which aids readability by keeping the outer code simple and concise, as well as by clearly labelling each action — properly applied functions can, in many cases, remove the need even for comments. Obviously there are times when the answer is 'no' and using a block statement is preferable (usually, I find, in order to define a variable to be used in the main statement), but they are rare.
- --Twey 04:53, 17 October 2008 (UTC)
- I have two problems with this. First, I run across code all the time, whether in actual files or in forum posts, which has no indentation whatsoever, and in some cases the indentation was actually stripped. If it used blocks, it would be much easier to indent without mistakes. Second, one of my lesser skills is zooming out from my code to view the big picture; "Should I put this in a function instead?" only crosses my mind when I begin to duplicate logic.--Jesdisciple (talk) 02:37, 18 October 2008 (UTC)
- In address to the first problem, I would recommend getting a better editor. Any good editor can scan a file and automatically indent it in a matter of milliseconds. I particularly recommend emacs, with js2-mode. The latter, of course, is exactly the problem the brace-free format is intended to solve. — Twey 07:31, 18 October 2008 (UTC)
- I have two problems with this. First, I run across code all the time, whether in actual files or in forum posts, which has no indentation whatsoever, and in some cases the indentation was actually stripped. If it used blocks, it would be much easier to indent without mistakes. Second, one of my lesser skills is zooming out from my code to view the big picture; "Should I put this in a function instead?" only crosses my mind when I begin to duplicate logic.--Jesdisciple (talk) 02:37, 18 October 2008 (UTC)
- "Labels are not useful enough to be included here, and what was originally here is not true: Javascript provides no 'goto' statement, so labels are useful only for use with 'break' and 'continue', and cannot be used to implement more complex structures."
- I feel certain that I removed the reference to
gotoafter researching it. Which revision was it removed in? --Jesdisciple (talk) 15:04, 16 October 2008 (UTC)- There was no direct reference to
goto, but it stated that any other control structure could be laboriously simulated by use of labels, which, withoutgoto, isn't true. - --Twey 04:53, 17 October 2008 (UTC)
- Actually, it isn't true anyway — you need at least
if. --Twey- I thought I noted that too, but I can't find any of this in the history... Bah. --Jesdisciple (talk) 02:37, 18 October 2008 (UTC)
- Actually, it isn't true anyway — you need at least
- There was no direct reference to
- I feel certain that I removed the reference to