Prefixing names using some notation

Started by
96 comments, last by phresnel 13 years, 11 months ago
Quote:Original post by rip-off
I think its funny that there are some people who consider no prefixes to be pretty much the same thing as no coding convention.


Yes, that stuck me as well; aside from the 'g_' prefix to highlight just how much of a bad idea it is I don't use prefixes, yet I have a 'convention' all the same.

Structs,Classes,types and functions are CamelCase. For example; RendererCommand or RenderingAgent or even typedef std::function<void (ID3D11DeviceContext*)> DeferredFunction;

Variables use "lower case camelCase"/"mixed case"; example: DrawingCommandType cmdID;

Enums are prefixed with their Enum name; this is a crutch for the lack of namespacing on C++ enums, otherwise I would use Enum::value instead. Example;
enum DrawingCommandType{	DrawingCommand_Render,	DrawingCommand_Function,	DrawingCommand_Present,	DrawingCommand_Waiting,	DrawingCommand_NOP,	DrawingCommand_Quit,		DrawingCommand_MaxCommandTypes};


oh, and { and } always ALWAYS go on their own line, with the scope 1-tab inside to show scope.
(honestly, there is a special place in hell for the people who decided that having the opening { on the same line as the function name/for/if was a good idea and in some way 'clear').

Functions should not go beyond a page break; if they do refactor the hell out of them. Chances are you'll catch bugs and improve the clarity anyway.

I think those pretty much cover the rules I code by and I feel my code is very clear to read.
Advertisement
Quote:Original post by phantom
(honestly, there is a special place in hell for the people who decided that having the opening { on the same line as the function name/for/if was a good idea and in some way 'clear').

Haha... ++this.
Quote:Original post by phantom
Quote:Original post by DevFred
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.


Amen to this.

If you are writing functions which are so big that you can't track the local variables in use then you should refactor it.

I make a point of writing functions where I can see the whole thing on the screen at once; locals a clear, parameters are clear and members are clear. (I avoid globals, infact the 'g' prefix is the only one I'll use because it happens so rarely and it's a mental hint to myself to refactor that).

Frankly, if you've hit functions which are so long you can't see them all on screen you are going to have bigger problems than 'finding the type of a variable' because in scope that big you don't know whats happened to the variable by the time you get to it.

Maybe I need to reiterate this on a per-page basis.

Quote:Original post by YogurtEmperorI just want to be clear.


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.


If I had had my own code in mind when thinking about the evils of no conventions, I would have already left the field of programming decades ago.
Your rebuttal suggests that naming conventions are an excuse for poor overall variable names. In fact someone even posted an example of it:
Quote:Original post by djz
double dInput;int iInput;byte bInput;//is much less descriptive than this...double OriginalInput;int Input_IntRange;byte Input_ByteRange;

Why do people assume that adding a prefix means you are unable to also add a descriptive name? These things cannot coexist? One extra character makes the name too long? Even though you are always going to copy-paste the name every time you re-access it anyway which means you only have to type the whole name once?
Wouldn’t this be even more descriptive?
double dOriginalInput;int iInputRange; // Also shorter, but if you had a reason for the long name you could also use iInput_IntRange.byte bInput_ByteRange; // Could probably be shortened!  If "Byte" in the name is trying to tell me it IS a byte (as apposed to just working WITH bytes),                       // wouldn't a "b" prefix convey the same meaning with fewer characters?  Easier to read and type.                         // You just don't want to do it because if you do it once you have to do it everywhere.

Now when I look at you code long after you have died I can easily tell the types of each, and I will know that bInput_ByteRange is a byte and so can count only a limited number of items. Input_ByteRange doesn’t tell me that it is a byte. Maybe it works with bytes in one of the numerous possible ways it could, but is actually an int.

#1:
IDE’s are not meant to be a crutch. Not everyone is using the same IDE. You can’t argue how easy it is for you to use your IDE to accomplish a task that is still slower than reading a prefix (for example). That has nothing to do with people who are going to use your code later.

#2:
What seems to be a descriptive name to you may easily be misleading (speed vs. velocity) or at least non-descriptive to someone else.
a: Naming conventions have clearly defined rules. Everyone knows m_ = member, p = pointer, etc. This is not subjective.
b: Naming conventions coexist with “descriptive” names quite well. Why would you assume that using a prefix (for example) is going to cause the rest of the name to be less descriptive? The only time when a prefix would cause a change in the overall name is when the prefix duplicates information that was in the name, such as Input_ByteRange, in which cases the prefix is always shorter anyway, allowing those precious shorter names everyone wants.

#3:
You are not coding for yourself. You are coding for the others who are going to maintain your code after you die. It doesn’t matter how aesthetically beautiful your code is to you. As I said before, we already know you can read your own code. This is like some guy coding in Swahili (variable names, comments, etc.) and then saying, “My code reads like prose, is very clear, and uses descriptive names.” That’s super, but he coded for himself, and he graded his own work subjectively. No one else can read his work or carry on after him. Code is not meant to be pleasing to your own eye. After functionality, it is meant to convey as much information as possible about what it is doing, so that when someone else comes along to add something to it, he or she has all that information ready at his or her fingertips.

#4:
Nothing is redundant about naming conventions.


Aesthetic coders tend to omit prefixes (as an example) or use conventions that do not relay that much information. The first time they see prefix-based conventions they think, “Ew, that’s ugly.” They prefer code like this:
res = left + right;

rather than code like this:
iRes = iLeft + uiRight;

The names (without prefixes) are equally descriptive. And we have information in the latter case that might cause us to do some error checking and possibly improve stability.
The only reason people would choose not to use this type of convention is that it is simply not “beautiful”. Aesthetics matter more than information to aesthetic coders.

I still remember the time years ago my old boss asked me to add a _ prefix to function parameters. I did it to follow orders, but it felt “ugly”, and I cringed every time I did it.
People don’t like cringing when they code. It’s that simple. That is the root cause of people avoiding prefixes.
They saw them at a young age, held “code beauty” at a very high importance, and decided, “that looks ugly; I won’t do it.”

This is also exactly why there are so many styles out there. The number of styles of coding there are is a testament to how widely people’s taste for “beautiful” varies.
If you don’t believe it, answer this: Do you think your code is ugly?
Obviously not. We all like our own styles. No one writes code he or she thinks is horrendously ugly.
So then, why not use prefixes (for example)? Because they clearly do no harm. So why not?
Because you think they are ugly. They make you cringe from an aesthetic point and you don’t want to cringe at your own code.

Consistency is still the most important thing. When I work on a project that has no clear convention, or at least no prefixes, I code that way too, just for the sake of having all of the code look the same. I cringe when I code, and I hate it. We are all susceptible to the cringe factor, but at the end of the day, consistency and the relay of information is simply more important.




I am clearly a proponent of naming conventions, and prefix-based ones are fine-by-me, but people don’t change after they have made up their minds.
I hope at least the original poster takes everything into consideration.
I feel I have made strong points, but there is no way to make everyone agree on this subject and taking it beyond this is too much of a waste of time (no really, look at how much I have typed in these last few posts!)


L. Spiro

[edit by phantom:adjusted code block to fix forum layout]

[Edited by - phantom on April 29, 2010 3:48:49 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 phantom
(honestly, there is a special place in hell for the people who decided that having the opening { on the same line as the function name/for/if was a good idea and in some way 'clear').


I wonder what's so bad about this convention, and what's better about your style.
It doesn't break the visual "flow" as your style does (highly subjective of course) and it saves screen space.
Quote:Original post by Konfusius
I wonder what's so bad about this convention, and what's better about your style.
It doesn't break the visual "flow" as your style does (highly subjective of course) and it saves screen space.


I find it much more difficult to locate where scopes begin and end when the opening bracket is on the same line as the control statement/function name. And it just looks ugly :)

It's really just preference.
Quote:Original post by Konfusius
I wonder what's so bad about this convention, and what's better about your style.
It doesn't break the visual "flow" as your style does (highly subjective of course) and it saves screen space.


Speaking for myself, I simply can't read it. It seriously gives me a headache. I have to consistently scan up and search for a line with an lesser indentation. Then I have to make sure that it's the correct scope by searching for the opening '{', since oftentimes programmers check in code with inconsistent indentation.

Even small (~10 line scopes) give me a hard time :P It's simply much faster to glance up, locate the "{", and then move on. The begin/end is consistent, looks the same, follow the same rules, and so it's a pretty simple no-brainer to be aware of scope.

Though I suppose if I were taught to use this particular I wouldn't have issue with it, but I'm personally quite glad I'm not ;)

Btw - Effective C++ highly recommends (and almost all but pleads) to abandon that style of coding (having the beginning brace on the end of the line).
How do you go reading entirely brace-free python code?
Quote:Original post by Hodgman
How do you go reading entirely brace-free python code?


My editor has those nifty scope lines ;) That, and Python's style is such that indentation defines scope, so I found it very easy to get used to the rule/style. If the indentation is off, the behavior of the program changes. So the scoping follows a very consistent begin/end paradigm.

C++ not so much - everything can be on one line and still be properly scoped.
It is so easy for you guys, who can see well. But the problem with '{' at the at of same line: what happens if you have to break that line, because you have to use a font in the IDE that you can see? And hell, I wouldn't scroll horizontally, even if someone was screwing my guts with a hook.
if( isSomethingDoneBySomethingRight(withThisVector)     && someIndexOfWhatever != -1     && SomeArrayOfStruct[someIndexOfWhatever].some_member     == SOME_CONSTANT ){...}

if( isSomethingDoneBySomethingRight(withThisVector)     && someIndexOfWhatever != -1     && SomeArrayOfStruct[someIndexOfWhatever].some_member     == SOME_CONSTANT ){...}
Yes, that stuff have to be broken for me just like that to fit to the screen. Or should I make an own function for that?

And I don't think an extra empty line is a problem. I code with lot of empty lines. Programs should be red like a book, but more likely like a catalog.
And sometimes (some of you will scream in pain) I even violate consistent indentation (for the same reason: to save horizontal space).

BTW that's why I code in camel case, to save horizontal space (whenever it's possible).

Okay, I'm not a real programmer (and my eyes suck). And I said that these arguments are rather pointless, but now I participate in it anyway...
szecs: I use the latter example in such cases, but usually the first if it fits the line nicely. So ima flexible there and don't follow absolutisms, just usuallynisms, argh :)

Quote:Original post by Ariste
Quote:Original post by Konfusius
I wonder what's so bad about this convention, and what's better about your style.
It doesn't break the visual "flow" as your style does (highly subjective of course) and it saves screen space.


I find it much more difficult to locate where scopes begin and end when the opening bracket is on the same line as the control statement/function name. And it just looks ugly :)


Of course it begins where there is an +indendation of 8 space characters in the line below. Everything else is blatantly teh uglz!!!!1

Quote:It's really just preference.

Hehe, yeah :)

This topic is closed to new replies.

Advertisement