4 ms·
Can {} (or any symbolic scoping) be considered a syntactic redundancy if it allows auto-formatting and auto-indentation of your code? It may be a redundancy to
by interpol_p 11y ago
Can {} (or any symbolic scoping) be considered a syntactic redundancy if it allows auto-formatting and auto-indentation of your code? It may be a redundancy to the compiler, but to the code editor it is useful information.
While I tend to write code to match whatever style guide I am following, I know a few programmers who write extremely messy, poorly indented code and then just hit the magic Clang-auto-format hotkey every now and then to clean up their file. In a whitespace significant language it is not possible to infer the correct indentation if not explicitly provided.
- Nullabillity 11y agoYou can just use tabs or indent by single spaces, and have the autoformatter fix it afterwards. Alternately, use a proper editor.
- emn13 11y agoThere are editors that do such things. However, I've never seen one that does it well. It tends to break down if you're editing code and have a temporary intermediate stage that's syntactically invalid. It's also not great in the face of copy-pasting, especially if you're rearranging code in ways that breaks blocks. You're easily left with code that's been "autocorrected" resulting in everything after a certain point being indented one level too much or too little. In a brace-using language that would correspond to an extra closing brace (or a missing closing brace), and it's easy to fix. In an indent language, fixing it is more of a hassle. Also if you have unmatched braces, that's generally a trivial parse error - the compiler (or linter) will check that and find that every time. But with indents, if the outcome of some code refactoring accidentally broke the indent and everything after some point is over-indented, that tends to superficially work (I've actually made that mistake several times in coffeescript). Additionally, diffing tools I know of can be set to ignore whitespace, which makes common code changes much easier to understand. If, on the other hand, you do that with an indent-sensitive language you're going to miss indent-changes entirely. That's just a practical tooling problem - you could write a diff that works just as well for indent-based languages - but it's a practical problem nonetheless. All in all, I think it's a much better idea to have braces. If you think they're ugly, use your syntax highlighter to deemphasize them. A smart code editor could even omit the braces entirely in normal, syntactically valid code (which I'm not suggesting is necessarily a good idea, but it's possible).