• Create Account

## Naming conventions for math statements

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1ryt  Members

294
Like
0Likes
Like

Posted 23 January 2014 - 03:31 PM

How do you write variables for math names ?

For vectors for example I sometimes append v like vLocalPosition. But for some common vectors I use names like pos, dir, dist. Also for coords I sometimes use forward, up, right.

For matrices I append m in front or try to use a single uppercase letter like M.

How do you write names for transformations and rotations ?

Names like transformAirplane or rotationCar seem a little cumbersome.

### #2Álvaro  Members

20266
Like
4Likes
Like

Posted 24 January 2014 - 04:28 AM

I don't think there is anything special about vectors or matrices. Variables should have descriptive names, and stay away from abbreviations as much as possible. I work in an environment where around 100 people might have to read my code, and it might be obvious to me that "pos" stands for "position", but it could also be "positive" or something else.

Using "dist" for a vector is the type of thing that can make code impossible to read. A distance is a number, not a vector. Also positions are not really vectors, and I prefer to have a separate type called "Point" for them.

Single letters for matrices are OK if you are writing a function that treats the matrix disregarding what it represents (say, a function that computes the inverse of the matrix). But in code where the matrix means something, a name like model_to_world_transform' is much better.

### #3Buckeye  GDNet+

10739
Like
0Likes
Like

Posted 25 January 2014 - 10:17 AM

Naming variables depends greatly on who the intended audience is. If you're coding only for yourself, with it being unlikely that others will be viewing and trying to understand your code, name the variable something that will reduce the chance that you'll make an error in your code when you use the variable.

That being said, as Alvaro mentions, get in the habit of naming variables appropriately. Long variable names can improve the readability of your code (even to yourself). I understand longer names are a bit of a pain to keep repeating, but many code editors (e.g., Visual Studio) have an auto-completion feature. You need only type the first few letters before the choices narrow greatly. In addition to keeping spellings correct, that auto-completion is scope-based. I.e., if you start to type a variable name that isn't in the current scope of your code, it won't appear in the auto-complete dropdown. You'll have an indication that the variable you want to use will not be found during compilation.

In addition to naming variables appropriately, be liberal with comments. I wrote code as part of my job in a previous life and the guidelines included comments comprising no less than half of the total lines in a source file! For esoteric code, the guideline was TWO lines of comments for each line of code. There are times when you will be forever grateful to yourself for describing what the following code is intended to do.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #4Álvaro  Members

20266
Like
2Likes
Like

Posted 25 January 2014 - 01:42 PM

Documentation of interfaces (how to use some class, perhaps even with examples) is great. But, other than that, I am not a big fan of comments. In some sense, a comment is an admission of defeat: "I didn't manage to make the code clear enough, so here's an explanation".

[EDIT: I see how this can quickly become a religious war on all aspects of coding standards, even though it started with a very narrow focus. So perhaps we should not keep going...]

Edited by Álvaro, 25 January 2014 - 01:43 PM.

5832
Like
0Likes
Like

Posted 25 January 2014 - 02:14 PM

Matrices should be named for how they are used e.g. world_to_screen etc. Other abbreviations should be well understood e.g ori (orientation), rot (rotation), trans (translation), etc. Orthogonal matrices should have Right (or Left), Top, Face accessors too.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #6Dirk Gregorius  Members

2438
Like
1Likes
Like

Posted 27 January 2014 - 07:04 PM

I like to distinguish between game code and low level code. In game code I also use longer names like e.g. a descriptive player_position over just 'p'. For my low level collision or physics routines I prefer short names as I derived them on paper. I usually check in theses paper with the code and add a reference in the code. Then those can be used for debugging. E.g. a function to compute the intersection between a ray and a triangle would look like this:

float IntersectRayTriangle( Vector A, Vector B, Vector C, Vector P, Vector Q ).

If you need functions that take a triangle or ray structure you just overload the function and delegate to the leaf code function. Personally I found this very convenient to work, but YMMV.

As the point came up here I personally recommend against using a special point class. I have to use this lately a lot inside the Maya SDK and find this more confusing than helpful. E.g. you get the translation from a transformation matrix as vector and pass this as position (a point) for e.g. a manipulator you have to convert between types for this. Also the overloading of a multiplication operator between vectors and points which ignores the translation in the first place is very error prone. In this case I prefer explicit functions of the form TransformVector( Matrix m, Vector v ) and TransformPoint( Matrix m, Vector p ). This is my personal preference and it worked well for me, but again YMMV.

In general I find most most libraries that are available as open source these days way over-engineered for such a simple problem like a math library. E.g. using templates and extra point classes and what not else.

Edited by Dirk Gregorius, 27 January 2014 - 07:07 PM.

### #7Bacterius  Members

13102
Like
2Likes
Like

Posted 27 January 2014 - 07:42 PM

As the point came up here I personally recommend against using a special point class. I have to use this lately a lot inside the Maya SDK and find this more confusing than helpful. E.g. you get the translation from a transformation matrix as vector and pass this as position (a point) for e.g. a manipulator you have to convert between types for this. Also the overloading of a multiplication operator between vectors and points which ignores the translation in the first place is very error prone. In this case I prefer explicit functions of the form TransformVector( Matrix m, Vector v ) and TransformPoint( Matrix m, Vector p ). This is my personal preference and it worked well for me, but again YMMV.

Really? I found it quite convenient, and the point-vector transformation problem was a non-issue since you are supposed to know what you are doing with your vectors and points (translating a vector is never meaningful, adding two points together either, etc...). The fourth column of a 3x4 (or 4x4) matrix is mathematically a vector, which defines a translation when applied to a point, resulting in another point. To obtain the point corresponding to the origin translated by that vector, you add it to the origin, which is a well-defined conversion, not a ToPoint() kind of deal, even though programmatically it is probably equivalent. No wonder you're having issues if your framework isn't even self-consistent!

The alternative of having everything as vectors is of course viable, but at the same time you lose expressiveness because you cannot clearly distinguish between whether a vector is actually a point, except through metadata such as comments and variable names. Though for low-level library code it would probably make sense since that kind of code tends to have this information built into the function contracts and tends to operate blindly on tuples rather than carry out a physically meaningful calculation.

Just my opinion, of course, as you said YMMV.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #8ferrous  Members

5772
Like
2Likes
Like

Posted 30 January 2014 - 01:37 AM

I like to distinguish between game code and low level code. In game code I also use longer names like e.g. a descriptive player_position over just 'p'. For my low level collision or physics routines I prefer short names as I derived them on paper. I usually check in theses paper with the code and add a reference in the code. Then those can be used for debugging. E.g. a function to compute the intersection between a ray and a triangle would look like this:

float IntersectRayTriangle( Vector A, Vector B, Vector C, Vector P, Vector Q ).

I prefer it when the function definition makes better since off the start without having to read a separate document, and then the function can rename the variables in the code (the compiler will clean it up anyway)  So instead of what you have:

float IntersectRayTriangle( Vector rayOrigin, Vector rayDir, Vector triPt0, Vector triPt1, Vector triPt2 )
`

That way when someone is using your math library and intellisense\VisualAssist\SourceInsight pops up they can easily deduce what the variables are.  And in your code, you can still use the shortened A, B, Cs.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.