As in Adam's example in the comments, an alternative with unconditional assignment could look like this:
let position = 0;
while (true) {
position = source.indexOf('something interesting', position);
if (position < 0) {
break;
}
// do something interesting with something interesting
position += 1;
}
The syntax could become even more compact if you disable curly
, which enforces braces on if
/for
/do
/while
statements even though K&R Ch 3 does not mandate those braces at all*. This invites a good comparison regarding the motivation for these rules in general. TSLint defines its purpose on its homepage:
TSLint is an extensible static analysis tool that checks TypeScript code for readability, maintainability, and functionality errors.
Is it entirely possible to use a brace-free if
statement correctly, particularly if you are diligent about indentation and formatting? Of course. However, it is also entirely possible to misuse that feature in a subtle way, like in the docs for curly
:
if (foo === bar)
foo++;
bar++;
Your question supposes "apparently somebody has decided that it is undesirable", but the more-accurate statement is that this pattern is a significant source of bugs across codebases, enough that the authors of tslint
have offered built-in detection and feedback for that particular pattern. Rather than "undesirable", "not 'allowed'", or "discouraged", the pattern is simply rare-enough and misused-enough to have its detection on by default.
As with all rules in tslint
, you can configure the default rule set using tslint.json
or tslint.yaml
, and you and your team may choose to disable rules like curly
and no-conditional-assignment
. This may be especially useful if this cases is often a false positive in your coding style (as it seems to be), or if you are applying tslint
to an existing codebase where that pattern is common and correctly-used. However, I think it is entirely reasonable for a team to claim (as others have done on this question) that the pattern is prone to =
/==
typos and other errors and that the correct uses are insufficient to justify allowing the pattern in their codebase.
*Note: Though K&R doesn't mandate braces, it does note on p56 that "The indentation shows unequivocally what you want, but the compiler doesn't get the message, and associates the else
with the inner if
. This kind of bug can be hard to find; it's a good idea to use braces when there are nested if
s." This suggests that even the original K&R text is supportive of reasonable style rules or limits beyond what their syntax supports.
They also can be an indicator of overly clever code which decreases maintainability.
- Gotta say, what you have written there makes my eyes bleed and I've been at this for a while. palantir.github.io/tslint/rules/no-conditional-assignment – Joinvillecorrect
code pattern, but the following will make your co-workers hate you (a little) less:let position = 0; while(true) { if(source.indexOf('something interesting', position) <= 0) break; // do something interesting with something interesting position+=1; }
– Joinville