JavaScript Programmer’s Reference Where this causes particular problems

JavaScript Programmer’s Reference Where this causes particular problems is in the maintenance phase where you might perhaps be adding another line of code. In cutting and pasting an existing line, it can be easy to overlook an operator and accidentally increment something twice or assign a value inadvertently. Special care is necessary with iterators and conditional execution blocks. A particularly nasty habit is to have a condition that when true, executes one statement. This is frequently written into the source text without any enclosing braces. Those braces are important because they group the block of code into a single syntactical unit. When you later try to unpick someone else’s script, the indentation may fool you into seeing several lines that appear to be conditionally executed when in fact only one is. The same applies to iterators as well. It is highly recommended that you put in the braces where they are required for multiple line conditional code and iterator blocks even when there is only one line of code being executed. This safeguards against errors when more lines are added to the conditional or iterated code block later on. This is not recommended practice: if(aCondition) someCode; while(aCondition) someCode; for( … ) someCode; This is slightly better but requires more effort when adding lines to the code block: if(aCondition) someCode; while(aCondition) someCode; for( … ) someCode; This is less dangerous than having no braces but makes the line long and twice as hard to scan visually: if(Condition) { someCode } This is fashionable, but the braces are hard to balance visually: if(aCondition) { someCode } 1568

Leave a Reply