Prefixing names using some notation

Started by
96 comments, last by phresnel 14 years ago
Quote:Original post by nullsquared
Quote:Original post by Giedrius
saves you the time of going to see their declarations,


You mean the 0.2 second mouse-over tool-tip delay? Or the 0.2 second right-click -> go to declaration/definition?


Sorry for the late reply. If you know the right keystrokes (for me F12 under MSVC (at work!), F2 in QtCreator, e.g.), it is just 0.036 seconds.
Advertisement
Quote:Original post by AristeOr:

final = ballVel.Dot(dir);
final = ballVel.Cross(dir);
final = ballVel * dir;

And this way I don't have to backwards-deduce the nature of the operation by means of its return type. The only place where ambiguity might arise is in the last line (where ballVel could be either a float or a vector), but this is easily fixed by properly naming the variable:

final = ballSpeed * dir.

Speed vs. velocity is exactly what I meant by misleading names.
Basically, the problem boils down to people not being reliable. You can’t trust them to write meaningful names, lots of comments, non-ambiguous code, etc.
These are real-world examples that people commonly do, like it or not.

Even with naming conventions, people will be unreliable and make mistakes, but naming conventions are concrete in that the “what to do” can be clearly and explicitly defined. That is, “use meaningful and non-misleading names on variables” is a lot harder to follow than, “put a ‘p’ in front of pointers”. Aside from “misleading” being a relative term, some people simply don’t know about or think about the differences between “speed” and “velocity”.
If a person is likely to make mistakes on simply following a naming convention, he or she is probably more likely to also make mistakes related to improper naming, ambiguous overloading, etc.
Combine these mistakes with no naming conventions for a brew that will knock you off your feet in seconds.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Quote:Original post by YogurtEmperor
fFinal = vBallVel * vDir;

vFinal = vBallVel * vDir;

vFinal = fBallVel * vDir;



I still don't know what the hell is final.
Also, in the third case fBallVel is disinforming. It should be fBallSpeed.
I personally prefer:
whateverTheHellThisBe = ballVelocity * direction;

whateverTheHellThisBe = ballSpeed * direction;

I even find direction a bit vague. I guess this appreciation would depend on context.
Quote:Original post by YogurtEmperor
nothing pisses me off more than not being to immediately recognize the types and scopes of the variables I am seeing.
Types of variables are only one problem, but it is frustrating seeing a value being used and not immediately knowing whether it is a local, a parameter, a member, or, God forbid, a global.

If you cannot tell immediately whether a variable is local to the function, you have a problem: the function is too big. If it does not fit on one screen, it is too big. It does too much. Split it up. I would even argue that functions with more than 10 lines are too big.

Same with classes. If your class has more than, say, 100 lines, it is too big. Remember the single-responsibility principle. Let your class do exactly one thing. If it does multiple things, split it up.

By the way, modern IDEs such as Eclipse display differently scoped variables differently.

Quote:Original post by YogurtEmperor
If members were clearly marked m_*, I could easily scan the code and spot lines where the class itself is being modified.

Every method that is not marked as const is modifying your member variables. That's the whole point of encapsulation. If this is not the case, refactor. Utility functions do not belong in classes.

Quote:Original post by YogurtEmperor
If I want to quickly find places where a certain variable is accessed or modified

I simply click on it, and Eclipse will automatically highlight each occurrence of the variable.

Quote:Original post by YogurtEmperor
Why even argue that naming conventions do not make code clearer?

Because clean code should read like prose. That's what humans understand best. You should choose good names that speak for themselves. That's way more important than distracting prefixes.
Quote:Original post by DevFred
If you cannot tell immediately whether a variable is local to the function, you have a problem: the function is too big. If it does not fit on one screen, it is too big. It does too much. Split it up. I would even argue that functions with more than 10 lines are too big.

Same with classes. If your class has more than, say, 100 lines, it is too big. Remember the single-responsibility principle. Let your class do exactly one thing. If it does multiple things, split it up.

By the way, modern IDEs such as Eclipse display differently scoped variables differently.


That's totally valid when writing new code and you're responsible for that code. It's a completely different issue when you're dealing with Somebody Else's Code well after they've written it. TheDailyWTF is full of such examples :)

Case in point, I am currently porting a *massive* codebase to Android Native. Eclipse point-blank refuses to do anything but the basic syntax highlighting. I am, not kidding, minus any form of intellisense. For example, to find a variable's declaration I have to search the entire codebase using regex based on what I think the type of the variable is.

Oh, and did I mention there are several "key" files in this project that measures around 20k lines, each? One particular function gives me hand cramps every time I scroll through it. It takes about 30 seconds to compile just that one file :(

Luckily the developer used 'm' to designate scope, unluckily he used it inconsistently. Sometimes it means a local variable, sometimes it means a class variable, sometimes it's a global. More often than not it's a class variable but I can't count on it.

Anyway, the point is, if you have a programmer that insists on writing monolithic functions / classes / files, at least enforce a coding convention that will at least help folks without any good tools to navigate the code :P
How would you prefix a member variable that is array of pointers to classes?

mapcItems?
Quote:Original post by Ariste
Quote:Original post by YogurtEmperor
...

Or:

final = ballVel.Dot(dir);
final = ballVel.Cross(dir);
final = ballVel * dir;

And this way I don't have to backwards-deduce the nature of the operation by means of its return type. The only place where ambiguity might arise is in the last line (where ballVel could be either a float or a vector), but this is easily fixed by properly naming the variable:

final = ballSpeed * dir.


Or perhaps better still:

final = dot(ball_velocity, direction);
final = cross(ball_velocity, direction);
final = ball_velocity * direction;

Member functions don't always make for the most readable APIs IMO -- In general, I only use member functions when I'm modifying the internal state of the invoking instance, and everything else is a non-member, non-friend inside the same namespace as the class it operates on -- which makes it as much a part of the class interface as a member function.

throw table_exception("(? ???)? ? ???");

I just want to be clear.

Revisions to my sample are not unwelcome, but I don’t want the point of the code to be lost.
There are millions of ways to write code when you are in control, but, as Grafalgar reiterated, reading code is usually hardest when looking at code someone else wrote, which was the context of my example(s).
Without a doubt, you should be able to read your own code, regardless of any bad practices you may have used (ambiguous operators or not) or not used.

My examples are what you really may encounter when browsing other peoples’ code, if you are a professional game developer. And yes I threw in that misleading name (speed vs. velocity) on purpose as a compound example of mistakes people make. It’s common. People do that all the time, and you can’t always go by names to clarify code.


[EDIT]
I also want to reiterate the purpose of naming conventions in the first place. Naming conventions are first and foremost for the benefit of other people who have to read your code.
It is assumed that you yourself can read any style you use, and you can look at code and recall the idea you were expressing at that time, with or without name prefixes.
Additionally, code that “reads like prose” is subjective. Everyone thinks his or her code is clear and beautiful. A clear name to one may not be a clear name to another (speed vs. velocity comes to mind again here). What is prose to you may be nails on a chalk board to another.
And this applies to art, music, and everything else. We can’t grade our own code. We can’t rate ourselves.

Forget the fact that you are writing the code. Think about others who are going to read it later. Other people may not have the same IDE as you. Other people might not be as fast as you at clicking this or that. Other people don’t hold an encyclopedia of variable names + types in their heads as well as you do, and may end up having to re-check the type information of a variable several times.
[/EDIT]


L. Spiro

[Edited by - YogurtEmperor on April 28, 2010 6:27:23 AM]

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Quote:Original post by YogurtEmperor
reading code is usually hardest when looking at code someone else wrote

You have no control over how other people have written code in the past, so the entire discussion becomes rather meaningless.
Quote:Original post by DevFred
so the entire discussion becomes rather meaningless.
It should be clear now for sure.

I mean this shit always goes this far. At least once in every months. Aren't you bored of this already? (You means everybody, even the 'veteran' users participate it these meaningless flamewars. All the time...)

This topic is closed to new replies.

Advertisement