5 ms·
a) All sorts of shitty code and text formatters will ignore or clobber whitespace. b) When reformatting code like for a in x: f(a) if something:
by otabdeveloper4 3y ago
a) All sorts of shitty code and text formatters will ignore or clobber whitespace.
b) When reformatting code like
for a in x:
f(a)
if something:
g()
you're liable to make a typo and commit a grave logic error.
- jerpint 3y agoWhy would curly braces stop you from making the same logic error?
- jen20 3y agoThey don’t. They stop it being an accident though - except in languages like C where braces are optional for single line blocks which can lead to similarly poor outcomes. Whitespace sensitivity is the top of Python’s pretty exhaustive list of flaws.
- yunwal 3y agoBraces definitely do not stop accidents of this type, in my experience. All it takes is refactoring some code where you can’t feasibly count the number of open braces/parentheses (HCL tends to get like this), and misplacing a brace somewhere.
- jen20 3y ago> where you can’t feasibly count the number of open braces/parentheses (HCL tends to get like this) HCL is fine for this. Editors highlight matching braces, typically, and the parser will typically fail if they are mismatched.
- yunwal 3y ago> Editors highlight matching braces Until it doesn’t fit on the page anymore so you can’t see the highlight > parser will typically fail if they are mismatched. The concern is that your braces are technically matched, but not in the intended way, which happens a lot. Probably more than tab errors in python in my experience
- jen20 3y agoCan you give an example of HCL which is ambiguous under these conditions?
- Kim_Bruning 3y agoOf course, on the other hand, you can just as easily misread this example if you don't notice the incorrect formatting. for (let a of x) { f(a); if (something) { g(); } } If you're a programmer who looks primarily at indentation for their syntax cues, it's the parens that can lead you into traps. So there's arguments either way. Now that I'm used to it, I do feel that indentation errors (or at least an indentation/paren mismatch) should probably be a syntax error either way. Possibly people who argue for belts and suspenders might be on to something?
- rstuart4133 3y agoI've had a team of programmers come to me and ask why some C code wasn't working after spending some time looking at it. It was a very badly formatted piece of code and yes, they were looking at the indenting rather than the braces. This was decades ago, before the rise of modern IDE's which might make such errors obvious. To me, that was a definitive proof that while people may prefer braces over white space the majority of those same people read code, they are using indentation to guide their mental parse tree - not the braces. One obvious reason for doing that is they may not even be able to see the braces in the editor window. It also seems obvious that the compliers should parse the same way humans do. It makes life so much simpler when we both see the same thing, so the code the compiler is emitting matches the semantic tree we are building in our heads. Which means if your goal is correct, readable code python got it right, and just about every other language gets it wrong. The article notwithstanding, there is not a lot of wriggle on that point.
- xigoi 3y agoWhy are you using shitty tools?