- Home /
Is it okay to organize c# code with curly brackets?
So I am newish to programming and am having a hard time keeping track of my code once it gets really large. I started adding curly brackets around chunks of code to organize it in chunks, and find it really useful as i can minimize chunks i'm not working on, and so far it seems to work really well for me. I am just curious if this is okay to do, will it cause problems in the long run, or is this considered a bad coding habit? It works really good for me but I don't want to get used to it if its in anyway wrong for me to do, as I don't see others doing anything similar.
Example:
// Attack if you press spacebar
{
if (Input.GetKeyDown(KeyCode.Space))
{
attackTimeCounter = attackTime;
isAttacking = true;
myRigidbody.velocity = Vector2.zero;
anim.SetBool("Attack", true);
}
}
You mean you need more? Lol. Well, I wouldn't use anything that has the chance of interfering with syntax. I believe Visual Studio has the ability to collapse chunks of code. That is probably what you're looking for. Other than that, you can use empty lines or /////////////. Oh, and also... your code should never 'get really large'. Separate things into smaller simpler classes, don't bunch things all into one... Hope that is helpful
$$anonymous$$onodevelop has that ability too. Tools/Options/Text Editor/General/Code Folding
Answer by JedBeryll · Apr 17, 2018 at 05:19 AM
It works so it's ok, but I usually try to avoid long and complex methods which has hundreds of lines. I prefer breaking it into smaller methods because when you go back to this code 3 months from now, you'll have a hard time understanding it. With that in mind, you can completely avoid curly brackets with if statements because the following code works too:
if (Input.GetKeyDown(KeyCode.Space))
OnSpacePressed();
else if (Input.GetKeyDown(KeyCode.LeftControl))
OnControlPressed();
else
OnAnyKeyPressed();
That being said, if a problem can be solved easily, go for it. Don't sacrifice too much productivity writing the cleanest code ever.
Answer by yaezah · Apr 17, 2018 at 06:37 AM
I usually organize my code with line spacing and a 1 or 2 line comment right above the code to help me remember what the specific snippet of code is for . I'm still pretty new to programming too and sometimes missing or adding a curly brace resulting in an error can be frustrating so I personally I wouldn't do it this way.
Answer by MrMatthias · Apr 17, 2018 at 06:54 AM
use #region #endregion
instead. The curly braces might get optimized away, but they aren't really meant for fomatting
Answer by Zodiarc · Apr 17, 2018 at 09:01 AM
Instead of organizing with curly brackets I'll suggest to extract a method if you can identify a block of code which works "on it's own". Your example is perfect for this. Just make a new method called "Attack" (or similar), move the content of the brackets to that method and call it. Curly brackets are less for organizing, but more for defining a scope.
Answer by bunnynsnake · Apr 17, 2018 at 06:24 PM
I would use either #region #endregion as well. The curly braces will get optimized. they are really not for formatting. Best of luck
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make method that gets multiple types? 1 Answer
Flip over an object (smooth transition) 3 Answers
What do VFXManager scripts usually do? 0 Answers