Answered by:
For a single If I am trying to make a case to always use { }

Question
-
User113730038 posted
So a few of our devs do the following:
If (a == b) do c;
if (a == b) do c;
I want to make a case to do it ALWAYS this way:
if (a == b) { do c; }
Any links or best practices?
Tuesday, March 31, 2020 5:50 PM
Answers
-
User409696431 posted
In addition to what you mention, studies have shown that including the braces improves/speeds up reading and understanding the code. And, obvioiusly, not putting in the braces opens a door to introduce a bug with a quick update to the if when adding another line to execute.
if( true ) doSomething(); Updated to if( true ) doSomething(); doAnotherthing(); WHOOPS!
I like consistency. "Here's an if statement", not "Here's an if statement of type one-line, and here's an if statement of type not-one-line".
You can see that https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions does not mention the option to leave off the braces, so I would take it as implicit that when they show the layout with braces, they are recommending they always be used.
However, at the end of the day, it's just a convention, and you and your fellow coders can pick whichever you use - but be consistent if you ever expect to read or modify each other's code.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 31, 2020 7:40 PM -
User475983607 posted
More readable, less prone to thinking the 3rd line goes in the if. To name a few. Hoping there is more. I was taught to do it the way I have been doing it. But it's been years so I don't remember why I was told to do that.
Explain that you your team as well as Kathy's input.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 31, 2020 8:01 PM
All replies
-
User475983607 posted
What is your case?
Tuesday, March 31, 2020 6:28 PM -
User113730038 posted
More readable, less prone to thinking the 3rd line goes in the if. To name a few. Hoping there is more. I was taught to do it the way I have been doing it. But it's been years so I don't remember why I was told to do that.
Tuesday, March 31, 2020 7:06 PM -
User409696431 posted
In addition to what you mention, studies have shown that including the braces improves/speeds up reading and understanding the code. And, obvioiusly, not putting in the braces opens a door to introduce a bug with a quick update to the if when adding another line to execute.
if( true ) doSomething(); Updated to if( true ) doSomething(); doAnotherthing(); WHOOPS!
I like consistency. "Here's an if statement", not "Here's an if statement of type one-line, and here's an if statement of type not-one-line".
You can see that https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions does not mention the option to leave off the braces, so I would take it as implicit that when they show the layout with braces, they are recommending they always be used.
However, at the end of the day, it's just a convention, and you and your fellow coders can pick whichever you use - but be consistent if you ever expect to read or modify each other's code.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 31, 2020 7:40 PM -
User475983607 posted
More readable, less prone to thinking the 3rd line goes in the if. To name a few. Hoping there is more. I was taught to do it the way I have been doing it. But it's been years so I don't remember why I was told to do that.
Explain that you your team as well as Kathy's input.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 31, 2020 8:01 PM -
User113730038 posted
Thanks! Just what I was looking for!
Tuesday, March 31, 2020 8:04 PM