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.
// Attack if you press spacebar
attackTimeCounter = attackTime;
isAttacking = true;
myRigidbody.velocity = Vector2.zero;
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:
else if (Input.GetKeyDown(KeyCode.LeftControl))
That being said, if a problem can be solved easily, go for it. Don’t sacrifice too much productivity writing the cleanest code ever.
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.
#region #endregion instead. The curly braces might get optimized away, but they aren’t really meant for fomatting
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.
I would use either #region #endregion as well. The curly braces will get optimized. they are really not for formatting. Best of luck