Your word on notation?

M

Matthias

I have been told not to use preceding underscores when notating data
members for example. What kind of notation do you use in general? I have
seen Microsoft uses hungarian notation a lot. Is that the most common one?
I also recognized that the more abstract a language becomes, the less
people care about proper notation (i.e. I haven't seen any C# or Java
code using anything even close to hungarian notation).
I have even recognized this behavior for myself. The less C-like C++
code I write, the less I care about notating it. For example, I found
that notating pointers with a preceding p helped a LOT not to confuse it
with anything else but being just a pointer.
In my more recent code I have to look really hard to find any pointers
at all, so I don't care about it anymore. Same for strings.

Do you use any specific notation at all?
 
V

Victor Bazarov

Matthias said:
I have been told not to use preceding underscores when notating data
members for example. [...]

Leading underscores can mean you're using a reserved identifier and
I, for example, try not to tempt my compiler.
Do you use any specific notation at all?

Some old habits die hard and I still use 'p' prefix for pointers and
'n' for counters. When I get to write from scratch I try not to use
leading underscores. They just make everything less readable for me.
At this point in my career I maintain a lot of somebody else's code,
so I try to use their notation to keep the code consistent unless it
is inconsistent to begin with in which case I try to make the least
amount of changes to bring it to a consistent state.

Don't get hung up on style unless your boss tells you to. Pick one
and follow it. Consistency is more important than any particular way
of naming your objects.

V
 
M

Mike Wahler

Matthias said:
I have been told not to use preceding underscores when notating data
members for example.

IMO that's good advice. In many contexts (but not the
one you cite), names beginning with an underscore are
reserved to the implementation. Rather than try to remember
these rules, I also advise avoiding use of leading underscores
altogether. Or perhaps whoever told you the above was simply
dictating 'shop rules'.
What kind of notation do you use in general?

I try to use names which strike a balance between
descriptiveness and conciseness.
I have
seen Microsoft uses hungarian notation a lot.

Yes, their code is full of it. :)
Is that the most common one?

Not for all C++ code in general, I doubt it.
I also recognized that the more abstract a language becomes, the less
people care about proper notation

The only 'proper' part of notation C++ cares about is correct
syntax. Subject to a few rules about reserved names, it
doesn't care what you name your identifiers. But again,
I recommend being descriptive. One reason I dislike
HN is that it unnecessarily binds a name to a type.
E.g. if I create an object of one type (e.g. 'int') and
give it some prefix to indicate 'int', and later I need
to change the type (to e.g. 'long'), I must remember
to change the prefix (and grep all the code where it's
referenced). A maintenance nightmare.

(i.e. I haven't seen any C# or Java
code using anything even close to hungarian notation).

Notation (as you're describing it) has nothing to do
with which language you use, except that the languages
rules about e.g. reserved names, etc. must be observed.
I have even recognized this behavior for myself. The less C-like C++
code I write, the less I care about notating it.

IMO one should simply care about making the code readable
and maintainable (to and by others, not only the author).
For example, I found
that notating pointers with a preceding p helped a LOT not to confuse it
with anything else but being just a pointer.

That's one case where I'm ambivalent about such a prefix.
(Since 'p' indicating 'pointer' is more generic that indicating
a specific type). However, I don't use this prefix either
(but I'll often name a 'temporary' pointer object as simply
'p' in a simple routine).
In my more recent code I have to look really hard to find any pointers
at all,

A pointer is easily recognized by reading its declaration (because
of the asterisk).
so I don't care about it anymore.

I don't know if this is the case for you, but I see many
folks using pointers in C++ where they're not necessary,
where a better, simpler solution exists.
Same for strings.

What do you mean? I don't see any need to indicate in an
identifer that an object has a string type. I just name
it what it means, e.g.:

std::string customer_name; /* imo the identifer makes it obvious
that 'customer_name is a string */

Do you use any specific notation at all?

Yes. Descriptive.

-Mike
 
V

Victor Bazarov

Mike said:
[...]
In my more recent code I have to look really hard to find any pointers
at all,


A pointer is easily recognized by reading its declaration (because
of the asterisk).

Notations exist not to aid reading declarations. They exist to aid
reading and understanding code while declarations are not in the view.
Often those declarations are in the header, especially when members
are concerned.
 
M

Mike Wahler

Victor Bazarov said:
Mike said:
[...]
In my more recent code I have to look really hard to find any pointers
at all,


A pointer is easily recognized by reading its declaration (because
of the asterisk).

Notations exist not to aid reading declarations. They exist to aid
reading and understanding code while declarations are not in the view.
Often those declarations are in the header, especially when members
are concerned.

Yes, you're right, I suppose I misunderstood the context
of the question.

But I still feel that pointers aren't really hard to spot
because context usually indicates that they're pointers.
Of course I'm saying this from a perspecitive of reading
a particular piece of code for meaning, not arbitrarily
scanning code, looking for 'where are the pointers'.

Finally, I'm sure you'll agree that there will never be
a consensus on what is the 'best' notation.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top