Why not put all braces inline in C++/C#/Java/javascript etc.?
- by DanM
Of all the conventions out there for positioning braces in C++, C#, Java, etc., I don't think I've ever seen anyone try to propose something like this:
public void SomeMethod(int someInput, string someOtherInput) {
if (someInput > 5) {
var addedNumber = someInput + 5;
var subtractedNumber = someInput - 5; }
else {
var addedNumber = someInput + 10;
var subtractedNumber = someInput; } }
public void SomeOtherMethod(int someInput, string someOtherInput( {
... }
But why not? I'm sure it would take some getting used to, but I personally don't have any difficulty following what's going on here. I believe indentation is the dominant factor in being able to see how code is organized into blocks and sub-blocks. Braces are just visual noise to me. They are these ugly things that take up lines where I don't want them. Maybe I just feel that way because I was weened on basic (and later VB), but I just don't like braces taking up lines. If I want a gap between blocks, I can always add an empty line, but I don't like being forced to have gaps simply because the convention says the closing brace needs to be on its own line.
I made this a community wiki because I realize this is not a question with a defined answer. I'm just curious what people think. I know that no one does this currently (at least, not that I've seen), and I know that the auto-formatter in my IDE doesn't support it, but are there are any other solid reasons not to format code this way, assuming you are working with a modern IDE that color codes and auto-indents? Are there scenarios where it will become a readability nightmare? Better yet, are you aware of any research on this?