|
In Pavel's defense, I too prepend variable names with their type. I find it generally helps the readability of the code. And yes - if I change a variable type, I do change the variable name accordingly - otherwise doing it in the first place is useless!!
You talk about having get/set functions for data hiding, but what about within the class itself?
I think after using it for a while you can really benefit. My colleague recently named some variables sBlah (CString) instead of saBlah (CStringArray) and it caused no end of confusion!
-Paul
|
|
|
|
|
PaulMdx wrote:
You talk about having get/set functions for data hiding, but what about within the class itself?
I use a strongly C++, object oriented notation with a data hiding and others cool stuff, inside the class too.
Pavel Sokolov,
CEZEO software,
LanTalk Network,
http://www.cezeo.com
http://www.lantalk.net
|
|
|
|
|
|
Marc Clifton wrote:
And if you change the DWORD to a WORD, do you diligently search and replace all references to the variable name with the corrected name? Maybe you do, but I bet a lot of other people don't.
Yes you should. And it is easy to do. Simply change the declaration and the compiler will not compile until you have made all the neccessary changes. This is about as close to forced self documenting code as it gets. Comments in context are far less easier to find and change.
Marc Clifton wrote:
Plus, why code the storage method as part of the name?
OK, so you see the following line of code, what does it mean?
bone = currentBone;
Is bone a string or a number or a class? So now you have to look at it's declaration. OK, so now you say, "Well, I wouldn't have named it bone or currentBone."(Hey, neither would I.)"I would have named it boneIndex or boneName." OK, so is boneIndex an iterator or a number or a key in map template? As for boneName, is it const char * or string? All this information is very important in debugging.
Marc Clifton wrote:
Isn't this why we have getter/setter methods, so we don't need to concern ourselves with internal implementation, and therefore don't care about the storage mechanism?
This helps for setting/getting members of a class, but what if you are just dealing with variables within code.
Also, remember cast conversion operators are inheirent in even basic types so...
const char * currentBone = "leg";
int bone = 0;
bone = currentBone;
This compiles, but it would far less likely to be written if it was:
const char * pszCurrentBone = "leg";
int nBone = 0;
nBone = pszCurrentBone;
Jason King
|
|
|
|
|
Pavel Sokolov wrote:
Bad example
WNDENUMPROC
Good example
LPTHREAD_START_ROUTINE
Yep - I'm with you Pavel. The more verbose a name the better.
cheers,
Chris Maunder
|
|
|
|
|
I agree.
Edward_Francis_Gadziemski
|
|
|
|
|
I also make every effort to make my code as neat as possible. But at the end of the development cycle I always end up wondering who the heck wrote that pile of crap while I wasn't looking. I think that there is some kind of thermodynamic principle governing how this happens. It certainly isn't my fault!
I'm not a real reverend, I just play one on CP.
|
|
|
|
|
You are quite right
I have learned in my software engineering lectures, that there is some "rising entropy" in every software project. Big and long-living projects' code size increases about 10-20% per year - just because of maintaince entropy.
Themoynamics can explain us the whole world
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
Reverend Stan wrote:
I always end up wondering who the heck wrote that pile of crap while I wasn't looking.
You too? Man - whoever it is sure gets around.
cheers,
Chris Maunder
|
|
|
|
|
Yeah, I feel physically sick when I look at my old code also.
Regardz
Colin J Davies
Sonork ID 100.9197:Colin
You are the intrepid one, always willing to leap into the fray! A serious character flaw, I might add, but entertaining.
Said by Roger Wright about me.
|
|
|
|
|
Reverend Stan wrote:
I also make every effort to make my code as neat as possible. But at the end of the development cycle I always end up wondering who the heck wrote that pile of crap while I wasn't looking
You should have seen me agonising over every line before I submitted that CP+ thing. While I was writing it I was thinking "this rocks", but once it was ready to be submitted I checked over my code and though "god, they are going to have a good laugh at this."
And I kept looking for the Format Code button for C++ in VS.NET. No such luck... lol.
regards,
Paul Watson
Bluegrass
Cape Town, South Africa
|
|
|
|
|
Paul Watson wrote:
And I kept looking for the Format Code button for C++ in VS.NET. No such luck... lol.
Are you sure? Boy, they really screwed the IDE then...
|
|
|
|
|
I can't see how anybody would go for an option other than this. All code must go through a development/debug/maintenance cycle.
So go for anything else would be professional suicide, but thats just my opinion.
Sure, depending on peformance you *may* have to compromise*, but with the speed of todays and future computers, this should be less of an issue as time goes by.
* I have never yet had to compromise, and I write real time apps for instrument control.
Roger Allen
Sonork 100.10016
If I had a quote, it would be a very good one.
|
|
|
|
|
Roger Allen wrote:
Sure, depending on peformance you *may* have to compromise*, but with the speed of todays and future computers, this should be less of an issue as time goes by.
I have never seen a situation that has suffered reduced performance due to formatting concerns. And, I honestly believe that anyone who greatly reduces the performance of code (note that certain temporaries are usually optimized out) by doing formatting changes likely is someone that is not qualified to make those changes!
Peace!
-=- James.
"There is nothing worse than being oblivious to the fact that you do not know what you are doing."
[Get Check Favorites 1.4 Now!]
|
|
|
|
|
If there are different people involved in one project it seems to me that much better to have the code well-formatted and use Hungarian notation than headache translating it to understand, imagine everyone in the street began to drive just the way he likes
#define __ARMEN_H__
|
|
|
|
|
I agree with the formatting (but NOT with the Hungarian Notation idea!!! ). However, I've seen this go way over the edge, too, with "style guides" that mandate a particular number of spaces to indent, that all constants (even 0 and 1) must be #define 'd (even when 0 and 1 would make more sense), or other things so piddly and against the development environment that it becomes more work to maintain the formatting vs. maintaining the code.
(Regarding my anti-Hungarian Notation thing, in my 20+ years of experience, I've rarely needed to know at a particular spot what type a variable is supposed to be, but I've often needed to know what the purpose for a variable is. Sorry, but lpszText doesn't tell me beans. What's the text supposed to contain? Besides, it can be a maintenance nightmare when you discover that you need to change the datatype for whatever reason (e.g., it comes from an external source, and it changes from an int to a float ).)
|
|
|
|
|
But lpszAppPath tells.
#define __ARMEN_H__
|
|
|
|
|
So does appPath , and you don't have to worry about changing it from char* to CString .
|
|
|
|
|
The name lpszAppPath makes clear its type and what methods to use without going to its definition. And how often do you change char* to CString ?
#define __ARMEN_H__
|
|
|
|
|
Armen Hakobyan wrote:
The name lpszAppPath makes clear its type and what methods to use without going to its definition.
IMHO, I'd rather go back to the definition (and be sure somebody didn't change the type without changing the name!) than to wade through the garbage just to find the purpose of the identifier.
Armen Hakobyan wrote:
And how often do you change char* to CString ?
During prototyping, more often than one might think.
|
|
|
|
|
Ok ok.
#define __ARMEN_H__
|
|
|
|
|
appPath doesn't say nothing to me except that this is a path and suppose to be some sort of string, thought it doesn't have. We, for example, have paths defined in Database, so the path can be actually a long variable - ID in DB. See? Now I saw appPath and have NO idea what you mean by that, I have to go and search where it was defined, and if you hate HN, you won't use "m_" declaration for class members also, so your variable can be define anywhere. This increases programming time of your product for example.
BUT if you had declared your variable lpszAppPath , I can be sure that this is a string (LPCTSTR ), or if you declared nAppPath (in our particular case) I know that this is an ID to path in Database.
Simple as is
Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
|
|
|
|
|
appPath tells me that it's the path to some sort of application; the details would depend on context. However, that gives me significantly more useful information than, say, lpszText , which tells me the datatype, but not how the stupid thing is used.
HN requires the developers to keep all occurrences of the identifier consistent with its datatype. I'd much rather go back to its definition, where I can see in detail what the compiler thinks it's supposed to be, than risk making a bad assumption.
But we've gone way off topic here.
|
|
|
|
|
Well, noone talking about lpszText , but about lpszAppPath , which tells you type AND what this variable for.
UltraJoe wrote:
I'd much rather go back to its definition,
I suppose that you mean a small project, maybe 1-2 classes/cpp files in it. But think about something bigger. Example is not very far - in my company, there are like 30 different modules (.dll and .exe) written by different people. And you use someone else's module and you have no idea in it, you just use it. So to know the type you should find where the variable is declared. It can be global, it can be member of class or just local to the function. Using your method, you can't do that without searching for definition, which can take a lot of time, or maybe you should go to that programmer who did the module and ask him? I would fire you for disturbing people by asking such questions lol, or maybe that programmer who has no idea in HN, depends on situation.
I tested that on my own skin, we had some people who liked to call their variables like database_connection . Yeah it says what is that, but what type? ADO Connection object? MFC connection? Just a connection string? And is this a pointer?
Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
|
|
|
|
|
Philip Patrick wrote:
Well, noone talking about lpszText , but about lpszAppPath , which tells you type AND what this variable for.
Okay, I'm probably not playing fair by changing examples in mid-stream; I should've used lpszText from the start.
Still, I'd much rather search for the definition (more on that in a second) than trip over the semantics of a line of code by trudging through all the Hungarian junque. For me, the understandability of the code decreases exponentially as I see more "lpsz" garbage.
Numerous people have noted that searching for a definition can be tedious. I disagree; it won't be tedious IF the project is organized well, not just in terms of classes or a single file, but also in terms of what goes into what files. An undecorated name is either an argument to the current function/method, in the class or a superclass thereof, or a global . (No, I'm not trying to claim that globals are good, nor am I condemning them. Completely different subject.) That makes 3 places to look, and that's it. More often than not, I'll need that file anyhow, for comments relating to the variable (when necessary), or other information that won't come from Hungarian Notation anyhow.
|
|
|
|
|