do you guys use Hungarian notation?

Started by
38 comments, last by davepermen 13 years, 9 months ago
Quote:I also use m_ prefix for class members. Is that ok?
I think quite a few folks who otherwise avoid name prefixes still use 'scope' prefixes such as m_, s_, and g_. There have been some good arguments made against them here on the forums, but if you find them helpful, I'd just use them and not worry about it too much.
Quote:When I ask "is this OK" I'm asking "is this what most expert programmers and greatest companies do?"
What the 'greatest companies' do probably isn't terribly relevant.

Lots of big companies have codebases and coding standards that have been around for years, and therefore may incorporate styles and practices that aren't so much in favor now. For someone who's starting a new project from scratch though (or otherwise doesn't have to contend with a lot of inertia), there's really no need to adopt such practices.

As for what expert programmers would do, I think most experienced coders writing modern, idiomatic C++ would avoid type prefixes of the kind under discussion here. That's just the impression I get though (I don't really have any data to back that up).

In any case, I wouldn't worry too much about what others are doing. If you can clearly quantify how and to what degree type prefixes help you, personally, develop software more effectively, then use them. However, I would not recommend using them just because you've seen it done that way elsewhere.
Advertisement
I use an intense bastardisation of it and drop it whenever it makes more sense XD

I mostly just use it to group things together in the auto-complete. It's easy to find all the private variables in a class when they begin with _pv_ ^^

In C++ I denote "usable" classes typically with C and classes intended to be inherited with I. In C# and Java, aka classes lacking multiple inheritance, I use I just for explicit interfaces, with B (base) for classes intended to be inherited but not interfaces because they implement some functions.

I also have a concept of "static interface" in C++ (SI) which is an implementation of the Singleton pattern but where only children can access the global data.

Private variables begin with _pv_, and static private variables _spv_ =P Protected variables are just preceded by _. Functions have no markings identifying them as such except beginning with a capital letter :P

Integers are also prefixed with n, and if the size of the integer is considered "important" I then mark the amount of bytes. This includes "char" when it is used for integers, not characters. When it's used for characters it's c. Things like that are simple enough to tell ^^ Pointers p then the type pointed to's identifier.

It sounds complicated but it's simple enough and like I said, almost entirely for auto-completion and keeping things organised on screen and in my head.
Quote:Sometimes there's no escape. Many libraries I use work with pointers to virtual classes (TinyXML, AngelScript and others).
True, sometimes when working with third-party APIs, you'll be stuck using pointers.

However, keep in mind that even then, 'raw' pointers can often be avoided. (Just as an example, resources that are managed via 'create' and 'free' functions - things like SDL surfaces and so on - can still be managed using smart pointers. So, just because an API deals with raw pointers doesn't necessarily mean that you have to deal with them directly in your own code.)
Quote:Original post by mind in a box
Just a little side question:

_Myt& replace(size_type _Off,		size_type _N0, size_type _Count, _Elem _Ch)		{	// replace [_Off, _Off + _N0) with _Count * _Ch		if (this->_Mysize < _Off)			_Xran();	// _Off off end		if (this->_Mysize - _Off < _N0)			_N0 = this->_Mysize - _Off;	// trim _N0 to size		if (npos - _Count <= this->_Mysize - _N0)			_Xlen();	// result too long		size_type _Nm = this->_Mysize - _N0 - _Off;		if (_Count < _N0)			_Traits::move(_Myptr() + _Off + _Count,				_Myptr() + _Off + _N0, _Nm);	// smaller hole, move tail up		size_type _Num;		if ((0 < _Count || 0 < _N0)			&& _Grow(_Num = this->_Mysize + _Count - _N0))			{	// make room and rearrange			if (_N0 < _Count)				_Traits::move(_Myptr() + _Off + _Count,					_Myptr() + _Off + _N0, _Nm);	// move tail down			_Chassign(_Off, _Count, _Ch);	// fill hole			_Eos(_Num);			}		return (*this);		}


What notation is this? Is this Hungarian, too? I'm asking because I can't understand in less than 5 minutes looking at it what this code should do.



Btw, why are all the standard headers so f***ing ugly?! Looking at standard C++ headers, I often get the impression those guys ran their code through an obfuscator program to make everything as hard to read as possible.

Quote:Original post by Red Ant
Btw, why are all the standard headers so f***ing ugly?! Looking at standard C++ headers, I often get the impression those guys ran their code through an obfuscator program to make everything as hard to read as possible.
Yeah it would be nice if it was a bit more readable, like this.
I can't figure out if you're agreeing or being sarcastic. I think it's probably the latter? :p
=D Sorry, that was agreement.
Actually, I was using that example because it's your usual cryptic STL code, but it's not full of underscores and capital letters and nested typedefs... Compared to the earlier example of STL code, I find this RDESTL code much more readable :P
Quote:Original post by Red Ant
Btw, why are all the standard headers so f***ing ugly?! Looking at standard C++ headers, I often get the impression those guys ran their code through an obfuscator program to make everything as hard to read as possible.


Yup I agree. It's a complete mess. Some times bugs in my code crash in STL code, then I need to know why, but I can't! The STL is such a mess, it's like reading optimized JS code with no newlines or white spaces...

The Boost library isn't either build for readability though. I really have to take a day to wrestle through some of their code...
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:Original post by Red Ant
Quote:Original post by mind in a box
Just a little side question:

_Myt& replace(size_type _Off,		size_type _N0, size_type _Count, _Elem _Ch)		{	// replace [_Off, _Off + _N0) with _Count * _Ch		if (this->_Mysize < _Off)			_Xran();	// _Off off end		if (this->_Mysize - _Off < _N0)			_N0 = this->_Mysize - _Off;	// trim _N0 to size		if (npos - _Count <= this->_Mysize - _N0)			_Xlen();	// result too long		size_type _Nm = this->_Mysize - _N0 - _Off;		if (_Count < _N0)			_Traits::move(_Myptr() + _Off + _Count,				_Myptr() + _Off + _N0, _Nm);	// smaller hole, move tail up		size_type _Num;		if ((0 < _Count || 0 < _N0)			&& _Grow(_Num = this->_Mysize + _Count - _N0))			{	// make room and rearrange			if (_N0 < _Count)				_Traits::move(_Myptr() + _Off + _Count,					_Myptr() + _Off + _N0, _Nm);	// move tail down			_Chassign(_Off, _Count, _Ch);	// fill hole			_Eos(_Num);			}		return (*this);		}


What notation is this? Is this Hungarian, too? I'm asking because I can't understand in less than 5 minutes looking at it what this code should do.



Btw, why are all the standard headers so f***ing ugly?! Looking at standard C++ headers, I often get the impression those guys ran their code through an obfuscator program to make everything as hard to read as possible.


If you replace the ugly identifiers with beautiful ones, it becomes nicer. If you look closely, you'll recognize that they are very consistent with their style, and after some minutes of training, it is even possible to understand their code.

That is at least the impression I got when I dissected the GNU implementation of <valarray> to learn how their expression templates implementation looks and works like and how they organized their code (that was also the time when I learned that MSVC ppl did implement <valarray> naively and sub-performant).


Quote:Original post by Hodgman
Quote:Original post by Red Ant
Btw, why are all the standard headers so f***ing ugly?! Looking at standard C++ headers, I often get the impression those guys ran their code through an obfuscator program to make everything as hard to read as possible.
Yeah it would be nice if it was a bit more readable, [...]


Short story: The problem is that users can unknowingly override meanings of the standard library, and only if the identifiers of the libraries follow the reserved naming rules, and if clients will follow the rule of not using such naming schemes, it is guaranteed that everything is fine.

How it is like when vendors don't follow that rule can be seen at microsofts min()/max() #defines vs. std::min()/std::max().


Quote:[...] like this.


That's indeed fine code. But it would already fail for

#define pred -42[...]#include <sort.h>

Quote:
error: expected identifier near:  void quick_sort(T* data, long low, long high, TPredicate pred)  .........................................................^


even though the client did not use a reserved name / naming scheme.

But if you rename <sort.h>::... so that it is all liek

// Jump over elements that are OK (smaller than pivot)while (_Pred(_Data[_I], _Pivot))        ++_I;


instead of

// Jump over elements that are OK (smaller than pivot)while (pred(data, pivot))        ++i;


then users won't get into trouble as long as they follow the standard.

Of course <sort.h> is not part of the standard library, but imagine it would, and re-imagine how the standard library massively grows in the next revision of C++, and the more it grows, the larger the probability of conflicts.

Surely, if you avoid evil macros like the pest, you wouldn't have problems in the first place.

[Edited by - phresnel on July 19, 2010 3:10:52 AM]
There actually was a time when hungarian notation made sense. It was way back in ancient times of C.
The basic idea is that you can look at the name of the variable and see what kind of type it is. Since in C you have to define the variable at the beginning. But as the variable might be used a few pages futher down you still would be able to see that iCounter is supposed to be an int variable.
This is now all obsolete since you have nice IDEs. You can hoover over the varible and will get a nice popup showing the details. Also in C++ you can define the variable where you actually need it.

I am developing software for 25 years now and this is definitely something that has died the deserved death.

This topic is closed to new replies.

Advertisement