C# variable naming

I recently viewed S&box wiki and noticed that some class variable names are shortened.

public class NoclipController : PlayerController
{
    public override void Tick()
    {
        // Face whichever way the player is aiming
        Rot = Input.Rot;

        // Create a direction vector from the input from the client
        var direction = new Vector3( Input.Forward, Input.Right, 0 );

        // Rotate the vector so forward is the way we're facing
        direction *= Rot;

        // Normalize it and multiply by speed
        direction = direction.Normal * 1000;

        // Apply the move
        Pos += direction * Time.Delta;
    }
}

Source

In this case there are shortened words “Pos”, “Rot” and it is ambigous how I can interpret them. Pos - is it position, positivity or possession? And Rot is completely matches with already existing word meaning decaying process. IMHO this is a bad pratice, because it makes code harder to read. Futhermore new modders can take it for a good thing and shorten every variable name more than 5 letters for example which will lead to a completely unreadable code. I would like to see no shortenings at least for class variables in S&box C# layer that Facepunch exposing as their S&box API.

4 Likes

I can’t really say I agree. Pos and Rot are pretty standard “Position” and “Rotation” when you’re near vectors, especially in game programming. It’d be the equivalent of complaining about “Ang”.

I don’t think you’d see the applications “RotTING, PosSESSION, PosITIVITY” on something that is handling vectors and angles.

I also don’t think I know a sane programmer who types out every letter of the word when making a variable, most of the time it’s short hand. But then again, variables are user defined, so if you wanted to type like this you COULD.

What level of beginner are you anticipating will have problems? Surely they would pretty naturally pick it up after a few classes

9 Likes

Fuck typing Position and Rotation every time. There’s no ambiguity there.

20 Likes

I usually use position and rotation but that’s only because I use autocomplete. The use of the shortened versions doesn’t bother me at all apart from being aesthetically displeasing. Practically it makes no difference.

1 Like

Why isn’t direction shortened to “dir”?
It gives me anxiety seeing that some variables are capitalized while others aren’t.

1 Like

I think in this case, lowercase variables are local, while capitalised are class variables.

3 Likes

I see, Rot and Pos are variables for the Noclip controller, my bad.

1 Like

This sounds like a complete non-issue. The context here makes it obvious what “Pos” and “Rot” are supposed to be, and generally these shorthand forms seem to be universal in game making. What other similar words would you use in game coding that would deserve to be shortened and could theoretically use these shorthands too?

Pos, Rot, Ang, Deg, among others are very commonly used shorthands, the only way you would not know what they mean is if you have never written any code at all.

2 Likes

Its 2021 we have autocomplete and intellisense since a while

I agree there is no ambiguity but it just makes it less readable imo

2 Likes

Personally I’ve never seen anyone fully write those out, especially when working with vectors. As Garry said f writing them out every time, common knowledge IMO.

  1. The variable name must describe the information represented by the variable. A variable name should tell you specifically in words what the variable stands for.
  2. Your code will be read more times than it is written. Prioritize how easy your code is to understand rather than how quickly it is written.
  3. Adopt standard conventions for naming so you can make one global decision instead of multiple local decisions.

Normally you’d follow these conventions as he did with direction. But I think this falls under 3.

2 Likes

Common knowledge for us maybe, but not to some new modder who wants to try their hand at it - and there’ll be a lot of those.

Also @ubre they’re probably pascal case because they’re properties, variables are likely to be camel case with private variables bring prefixed with an underscore. That’s the most common convention.

1 Like

dir is more commonly short hand for directory.

2 Likes

I wonder what kind of code you have seen in that case. This is not common at all in C#.

2 Likes

What about adding public Vector3 Position => this.Pos; to the entity class so we can choose which one we want to use. I’d much rather type out Position than Pos, looks a lot nicer imo.

2 Likes

I also don’t think I know a sane programmer who types out every letter of the word when making a variable, most of the time it’s short hand. But then again, variables are user defined, so if you wanted to type like this you COULD.

Do you mean a programmer that types the whole word is insane? Well, I have a completely different experience. And yes, I can choose names whatever I like for my variables but not for variables already defined by developers. Everyone is forced to reference them in theirs code.

Generally, there is one rule for writing good code - it should be clear to anyone reading it with no extra questions. This code creates such questions and that’s the main problem.

1 Like

@garry Do you have a set language style for the API? It would make reading posted code easier at a glance.

1 Like

I get that you want to make it readable but at the same time a programmer can make code readable without literally putting the entire word of what they’re writing in there. It’s just not efficient.

Comments would be a better spending of time.

2 Likes

Yeah this isn’t really an issue, nobody is going to run into problems because of some shortened variable names.

While I’m personally not a fan of them being shortened I can’t see myself giving a damn about it in practice.

2 Likes

If you are reading code without context then there’s something wrong I suppose…

Still, there are comments everywhere in the code.

3 Likes

Personally I think it looks stupid shortened. Adding both would just make it so people could choose which one they want to use

1 Like