3 ms·
>naked if-else bodies For my own interest, do you have examples of the way other languages do it? I'm interested, and but I'm afraid to google that.
by SketchySeaBeast 8y ago
>naked if-else bodies
For my own interest, do you have examples of the way other languages do it? I'm interested, and but I'm afraid to google that.
- ainar-g 8y agoIn Go, one has to always write the if-else bodies in curly braces (and, the opening brace should always be on the same line). I think this is great for two reasons. Firstly, it prevents bugs where a very tired programmer needs to add something to the else body, which is written like this: else foo(); and writes else foo(); bar(); which compiles, but doesn't do what you think it does. And I have seen this bug in actual projects. Secondly, along with gofmt, it prevents style wars.
- jerf 8y ago"In Go, one has to always write the if-else bodies in curly braces (and, the opening brace should always be on the same line)." If you really want to twist the knife... even Perl 5 gets this right. Even Perl 5 thinks that naked if/else bodies is too crazy and error prone.
- renox 8y ago'even Perl5'? Perl5 has also 'use strict' which ensure that variables ate declared before usage, a feature that Python doesn't have AFAIK.
- krylon 8y agoThat is why I got into the habit, very early on, to always, always enclose blocks in curly braces (or begin-end, depending on the language). If it is ingrained deeply enough, one no longer has to think about it. But it is nice if the language enforces it. I would even go so far as to claim it is common sense.