I just lost quite a bit of time because I changed a JavaScript function that read (something like)
function F(a,b,c) {
return x(a,b,c) +
y(a,b,c) +
z(a,b,c);
}
to
function F(a,b,c) {
return // x(a,b,c) +
y(a,b,c) +
z(a,b,c);
}
when I needed to test something.
The changed function returns undefined
, of course, because the language does not require a semicolon and assumes the return
to be a plete statement.
Unfortunatly, when I mented out x(a,b,c)
I didn't think of this implication. So, is there a way to prevent such stupid misstakes in the future.
I just lost quite a bit of time because I changed a JavaScript function that read (something like)
function F(a,b,c) {
return x(a,b,c) +
y(a,b,c) +
z(a,b,c);
}
to
function F(a,b,c) {
return // x(a,b,c) +
y(a,b,c) +
z(a,b,c);
}
when I needed to test something.
The changed function returns undefined
, of course, because the language does not require a semicolon and assumes the return
to be a plete statement.
Unfortunatly, when I mented out x(a,b,c)
I didn't think of this implication. So, is there a way to prevent such stupid misstakes in the future.
- 5 Yep, think of the implication of what you're doing. – Demian Brecht Commented Sep 7, 2011 at 18:04
- 1 At the risk of being blunt, it sounds like you're ranting since you lost a lot of time on this minor error. Because of that, I think you're already less likely to make it in the future and don't need any special tricks to prevent this. – Peter Commented Sep 7, 2011 at 18:06
- You can read about automatic semi-colon insertion: es5.github./#x7.9 – James Allardice Commented Sep 7, 2011 at 18:06
- I do unserstand, why that happened, I just want to make sure, it doesn't happen again. – René Nyffenegger Commented Sep 7, 2011 at 18:10
- I'm not sure it helps with semi-colons, but javascript has a strict mode: developer.mozilla/en/JavaScript/Strict_mode Could help prevent other similar-typed mistakes, maybe? – mqchen Commented Sep 7, 2011 at 18:12
4 Answers
Reset to default 8Lazy debugger's solution:
function F(a,b,c) {
return (
// x(a,b,c) +
y(a,b,c) +
z(a,b,c)
);
}
JSLint and a unit testing framework.
Problem at line 3 character 11: Expected ';' and instead saw 'y'.
Semicolon insertion is particularly nasty with return
. A workaround for this case: don't return
multiline statements.
function F(a,b,c)
{
var toReturn x(a,b,c) +
y(a,b,c) +
z(a,b,c);
return toReturn;
}
Integrate JSLint into your build, and fail builds when you detect
Problem at line 7 character 12: Unreachable 'y' after 'return'.
Make this generic for line and character, of course. What you're really looking for is "Unreachable after return".