The importance of producing neat code Coding Unity 3D

The importance of producing neat code

 

Aaaaah, nothing cheers me up more than a clean, tidy piece of code!

Jammin’ does not mean rushing, because hurry is a programmer’s worst enemy.
You’re like “hey, let’s to that any old way, I don’t have much time to waste on optimization, indentation, comments and crap like that, right?”

WRONG!

Every time you write a class without havin clear in mind what it has to do, the risk of making mistakes rise up to alarming levels!

This random kid approves bugfixing a neat code
This random kid approves bugfixing a neat code

You can’t obviously waste time by deep planning every single interaction, since it will take way too much time for a jam, but here comes the real thing. Just follow my reasoning.

If you can’t pay too much attention to the planning phase you’re going to make mistakes.
Then you’ll have to FIND those mistakes.
Neat code helps you having a better overview to the whole thing, which actually makes bug finding A LOT FASTER!

What are the tools you can use to change a shapeless hell to a well organized heaven?

Let’s look at them together!

COMMENTS

Beautiful invention, yet uttelry underestimated by a lot of coders.

Basically plain text inside your code whose job is to explain what are you going to do with a certain line or an entire block of code. It’s an explicit declaration of your intents

// I am a single line comment

/*
* I am a multiline comment
*/

There is also a special kind of comment, but I’ll talk about it in one of the next posts.

TOOLTIPS

Hi, I'm a tooltip!
Hi, I’m a tooltip!

You know that nice little gray boxes that appear when you hold your cursor above something in the inspector?

You can create them too with a ludicrously simple line of code above the variable you want it to appear above

[Tooltip(“Hi, I’m a tooltip!”)]
public int crap;

REGIONS

Regions are a C# preprocessor directive.

You can use them to envelop great portions of code to quickly identify them basing on your personal criteria.

Everything can be put inside a Region, from variables to functions. Here’s an example in my code (you can see it in the top image of this article.

#region GESTIONE_NEMICI
// I nemici vengono gestiti come coda circolare, in modo da mantenere una certa varietà nelle apparizioni a schermo
private GameObject[] enemiesList;
private int head;
#endregion

That’s all for now.neat code

15 Comments

  1. Sorry, But I personally disagree with using regions is a way to make your code cleaner. Every time I see regions in a code, I see a massive class that needs urgent refactoring. It’s what many consider an anti-pattern. Clean code also mean just that, less code, fewer lines, even without comments. I prefer reading 20 lines of just code, rather than hundreds with regions and comments. If your class has more than few hundreds of lines, it’s smells bad, you are probably not developing, you are hacking. 🙂

    1. I clearly see your point, but the phenomenon you refer to occurs only when you use regions to encapsule parts of a method, meaning that this particular method is long and intricated.

      My personal use of regions is to have a way to show only some methods and variables at the same time in classes like state machines.

      I like to keep every state-related methods “in the same room” to quickly run through my code and avoid distractions while working on them.

      My OCD really enjoys a well documented code and the possibility to hide everything I’m not working on.

      As always, there aren’t bad tools, just bad ways to use certain tools, also code tidiness strongly depends on coders’ habits.

      Except for the GOTO. Its existence doesn’t make any sense outside extremely low level environments.

      1. Your example (with state machine) calls for refactor. 🙂

        As for GOTO, it has it’s uses, as long as it’s within a method/function.

        Other then using regions, good advice for keeping your sanity.

        You should probably add proper naming convention. That will save you from writing bunch of comments. (self documenting code)

        1. My state machine mechanics are already properly divided in different methods, my update is nice and clean.

          What if I just finished working on the methods relative to the first state and I don’t want to have all the code hanging there and occupying my IDE screen with no purpose? all under a region called “STATEX_HANDLERS”, collapse and on with the next methods.

          I don’t see why should it be a malpractice, since I don’t use it to avoid refactor, but just to have a better overview of the elements I still have to handle. Let’s not split hairs.

          Plus, about the self documenting code part, I’ve already planned a post about good practices that save time, which includes also naming conventions, how a method should do one and one thing only, etc.

          About comments, my mind works a bit differently, the purpose of my comments is not to explain what a variable does, but what do I expect from that variable.

          It’s more a jam thing, plus it helps if someone else has to put his hands on my code, as he makes an idea about how the class works with just a glance at the variables.

    2. I mean … there obviously ARE cases where regions are used to hide coding malpractices, such as to avoid refactoring or to hide a bunch of code without a real purpose, just to keep it out of the way.

      The point is: I’ve got that part ready, I don’t need to touch it anymore except for some later bugfixing related to the testing phase, why do I have to keep it standing there without any real purpose?

      Keeping it open just raises the risk of accidental modifications while moving around the document and keeps the variables zone away from the screen, when I’d like to work on them while I work on my methods or functions.

      I’m not talking about over 500 lines classes, even 150-200 lines forces you to continuously scroll up and down you class, imho that’s a waste of time.

      Moreover, i think that talking about code smell and anti-patterns related to region is a bit out of place, since it’s just a visual expedient and doesn’t impact computational performance at all 😉

  2. I have been browsing on-line more than three hours nowadays, yet I never found any interesting article like yours. It’s beautiful value enough for me. In my view, if all website owners and bloggers made excellent content as you probably did, the internet will probably be much more useful than ever before.|

Leave a Reply

Your email address will not be published. Required fields are marked *