so after reading lots of forums and books. I learned Curly brackets is a option. But most programmers say use curly brackets because this can prevent bugs or errors in code also make it easy to read for other programmers. So after a week reading about it I say its best choice to always use curly brackets don't matter how small the code is

example

Code:
if(ds is awesome)
    return;

now this can lead into a bug or error.
If I coded it like this

Code:
if(ds is awesome)
{
     return;
}

now I have less of a chance to cause a bug or error and I just made it clearer for the next programmer to read.

Any thoughts or comments on this post or something I missed please reply.
 
Most scripts seem to be scripted this way, so if you leave one out or place an incorrect one you get that nice error :)
 
I don't know, I would not agree that it can lead to bugs. If there is just one line after the if or any loop I personally perfer not to do the brackets.

That being said adding them in doesn't change the code but it is depending on preference.
 
Yeah, I don't really see an issue with potential errors. But, I will say that you should be consistent in whichever style you choose to use. If you are using curly braces part of the time and then other times you forego them, then you create a readability issue for those that may need to edit your code.
 
the real difference is that not using curly brackets, it will only read the first line of code after an if statement

i prefer to use them almost all the time, because if I or anyone else wants to add something, they're already there
plus I started using the first bracket is on the same line as the statement to save room

if( blah blah blah ) {
stuff happens​
}

I'm use to coding this way, but if I code someone else, I'll do it usual way
 
DS is right, it can lead to bugs, but usually only if the code is written in a such a way that it's hard to read and change.

Using the curly braces to define scope is almost essential when it comes to switches with cases requiring multiple lines, if a case body isn't scoped properly, then it may not execute the code as expected. One-line cases are pretty safe.

The other reason to use proper scope is circumstantial - C# prevents you from defining new local variables in a non-scoped statement;
C#:
if( hello != world )
    string helloWorld = hello + world;
Will produce a compile exception, whereas;
C#:
if( hello != world )
{
    string helloWorld = hello + world;
}
Is correct.

It's not a very common circumstance that you would need to define a local variable and then not use it though, but it's still a limitation worth mentioning :)

In summary, I would always use the scope braces - for all the positive reasons noted in DS' post.
 
Nice points, especially when using Cases. I always use them myself as I think it looks better and easier to read.

if( blah blah blah ) {
stuff happens}

I know this is your personal choice, but I really hate trying to read code with the first brace at the end of the line. It's easy to miss it when trying to read it(for me at least.) Good point on having them already there in case someone needs to add additional lines ZeroD.
 
The argument that curly brackets around one-line statements prevents bugs is incomplete. The why is usually never fully argued. In my experience with very large, long-lived code bases, this practice helps prevent future maintainers from introducing a defect. Take this real-world example (I've generalized the variable names as it's from a proprietary product). Code is in C.

Previous:
Code:
if(PRODUCT_FEATURE_FLAG == 'N')
  return;

Changed to:
Code:
if(PRODUCT_FEATURE_FLAG == 'N')
  DEBUG5(DEBUGLOG("Product Feature DISABLED due to property configuration"));
  return;

Obviously this can be avoided if the maintainer is diligent. However I see this exact issue occur in my day job time and again. It is so frequent in our legacy code base that we have changed our coding style guide to require all statements to be scoped except Case statements.

I never have run into an issue with Case statements, but our code base doesn't use them much. It's in C and most of our switch-like logic is based on string comparison, which C's switch doesn't do.
 
The ReSharper profile provided with ServUO's repository has settings that allow you to clean-up code.
These settings can be applied to the entire project, or any selection of files and will maintain strict styles such as wrapping one-line statements with curly braces. There are a lot of other formatting options too, but when an entire team uses one shared profile, it can be very beneficial.
 
There are many free (both kinds) of source code formatting utilities we could use for those folks that don't have ReSharper. Many projects enforce this as part of their style guides. This is typically used on insanely large projects like the Linux Kernel Project that has so many contributors that tools are required to collaborate on even the simplest of points.

However for a project of our size I would encourage collaboration over tools and process.
 
I agree. ServUO used to have a free license for ReSharper, so it was easy to keep all the core devs in sync, but ReSharper isn't an easy tool to license unless you own your own open-source project. That being the case, a quick email to JetBrains will get you a single user open-source non-commercial license. If you are a developer on a team that develops and open-source project, you can apply too, though I'm not sure the chances of being granted a license. The folks at JetBrains are pretty cool though and usually answer within 24 hours :)

It would be nice to accumulate a list of free alternatives to ReSharper, I don't really know of any TBH.
 
I have my own Resharper license and recommend everyone who is serious about coding to get one too! I used coderush and justcode in the past but they are nowhere near as powerful as resharper is.

Our license expired about a week ago, they have changed the way you have to apply for open source licenses now. In the past the key was an unlimited user license. Now it is a per user license. I have applied for some keys. I hope to get enough for our core developers.
 
Just an FYI. If you are a student, you can apply for a Re-Sharper key and get one.
Also if you work on an open source project as well.

So, if you have a student email address from your school, you can apply for it using that.
 
Surely that's a bad thing to do? You're making loops when there is no reason to.

It's the same as doing this in pascal:

Code:
if bBoolean then
begin
	bIsBoolTrue := true;
end;

Which is just bad. You're adding in useless code. Sure, it may not be too much of a problem if it's only done a couple times, but that stuff adds up.

Better off coding properly, and only doing loops when you need to. If it's an issue of readability, just be smart with your tabs, and comment your code.
 
Adding braces does not create a loop in c#.

Code:
if (condition)
	action


and

Code:
 if(condition)
{
	action
}

Are both functionally the same and require the exact same amount of cpu cycles to process.

I was at a .net conference last year and a person asked the panel should they omit the braces or not in such a case they were told to always use the braces and infact it is in the internal coding guidelines for .net internal developers to always use the braces or VS will yell at them.
 
Adding braces does not create a loop in c#.

Code:
if (condition)
	action


and

Code:
 if(condition)
{
	action
}

Are both functionally the same and require the exact same amount of cpu cycles to process.

I was at a .net conference last year and a person asked the panel should they omit the braces or not in such a case they were told to always use the braces and infact it is in the internal coding guidelines for .net internal developers to always use the braces or VS will yell at them.

Aye, as far as I know. The only difference is condition being able to execute multiple lines of code, as opposed to only one line of code.
Curly braces should always be your go to, because it's easier to read.

If you are more advanced and are only executing one line of code after the IF statement, then you can forego the curly braces.
 
The braces denote scope in C#
Scope is an important term to learn about in any language.

I use braces all the time, even for single-line statements.

Braces can be used anywhere too, this code is perfectly valid:
C#:
{
    {
          Console.WriteLine("Hello World!");
    }
}
 
I am very new to coding with C++, but every tutorial and book I have seen shows to use the brackets. I thought it was just standard practice. It does seem to help keep things organized.
 
There isn't too much to add that has not already been said. However for those who are new to programming or those who have not built up a certain level of experience I would recommend always sticking with the curly braces. Being consistent will help reduce mistakes as well as saving you the mental effort to evaluate when to or not to have braces. As well as code that is easier to read if your collaborating with others.
 
Well there is one lesson left to be mentioned.
A sad, but funny, true story, about what the OP did after making this post.

So began his project wide sweep, bound and determined, to introduce the era of curly braces!
Yet there was one important element he was forgetting on his mission to save us from ourselves..
He had no idea how to even code.

So began the project wide introduction of countless bugs he had no idea were even there.
For he had no clue that..
Code:
if (x == 1)
	y = 2;
z = 3;

becoming..

if (x == 1)
{
	y = 2;
	z = 3;
}
Is not the same thing.

The morale of this story (There may be several, but we'll stick with one)

Never make project wide changes like this, most especially when you have no clue what you are doing.
 
Last edited:
If you don't mind me asking, what exactly happens with that. That is very interesting, obviously as a beginner to programing that would look like it would work. So why doesn't it?

Thank You

Code:
if (x == 1)
	y = 2;
z = 3;

becoming..

if (x == 1)
{
	y = 2;
	z = 3;
}
 
In the first example, if X is 1, then Y is set to 2. Z is always set to 3. In the second example, if X is 1, then Y is set to 2 and Z is set to 3. If X is not 1 Z is never set.
 
Another way to look at it is that when you create a conditional it will apply to any code that is either in scope (In brackets/braces) or until the terminator character is used (a semi colon)

I might also do something like this from time to time.
Code:
 if (x == 1) y = 2;

The compiler does not respect white spaces except for those in a string so you may format your code any way you wish (Or by the projects rules, usually)

I recommend though that scope is kept by using brackets when working in a team as things can get very unreadable very fast.
 
Back