What about naming conventions?

I use JetBrains’ Rider IDE for my Unity scripts (instead of Visual Studio). I prefer JetBrains IDEs for many reasons, one of which is its Code Inspection features.

I’ve noticed that Rider issues a warning for many of the variable names being used in these Unity courses and wonder if there is a reason Rick doesn’t adhere to the standard C# naming conventions.

I come from a Java background, so I prefer Rick’s version of naming, personally, but should we be changing our names to match the standards instead of sticking with what Rick is showing us?

For example, Rick uses all caps with snaking (underscores) for spaces, for const variables. It seems the standard would be to use PascalCaseVariables instead, though.

Is this just a case of C# and Unity having different conventions?

This is definitely not a criticism; just worried I may be learning bad habits I’ll need to unlearn later. :slight_smile:

Hi @zephyr,

What do you mean by “standards”? When you look at other people’s C# scripts, especially C# scripts for Unity, you’ll notice that most of them do not apply Microsoft’s C# naming convention.

If you stick with the following rules, it’s fine:

  • Method: DoSomething()
  • variables/properties usually like this: private int myNumber or (_myNumber), public int MyNumber (or myNumber)

Method names starting with a minuscule character usually indicate that somebody tries to apply the rules of another language to C#. In most cases, it’s Javascript. The same goes for underscores in names, for instance, my_number.

However, you’ll even see different naming conventions in the Unity API when reading the examples. You often see YAML/Python names with underscores there, which is very uncommon elsewhere.

Do what you feel is right for you. I provide support for Ben’s and Rick’s Unity 2D and 3D courses, not the RPG course, though. If you stick with Microsoft’s C# naming convention, I’m totally fine with it. Just do not use Javascript or Python naming conventions because that’ll make your scripts a bit difficult to read for me.

Where does Rick use underscores?

Rick uses underscores in the Unity2D course for const names (section 8).

I’m fine with Rick’s naming and I believe the most important thing is to just be consistent with whatever I choose. I just wanted to get some feedback on whether there was a general rule regarding this. I want my code to be as readable as possible to others
as well.

Thank you for your reply!

I checked his “Tile Vania” repo on GitHub but I was not able to find any constant with underscores. :confused:

In C#, it is fine to use uppercase and underscores to define the name of a constant. The underscore makes sense there to increase the readability of a name, e.g. START_LEVEL_INDEX instead of STARLEVELINDEX, LAST_LEVEL_INDEX instead of LASTLEVELINDEX, and so on. In scripts for Unity, I do not see the uppercase variant so often.

Long story short: I agree with you. As long as you stay consistent with your naming, it’s fine. However, I would always recommend to consider using the naming convention of the framework you are using, here: Unity.

In my personal opinion, it’s easier to grasp the concepts of a framework, if you simply follow the rules of that framework if possible. This way you can usually guess how things could be done and look them up in the API. Furthermore, you can spot mistakes more easily and avoid certain mistakes. A typical mistake is to type update(), start(), onCollisionEnter2D(), etc. and expect that Unity will call them anyway. It won’t.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms