Hungarian Notation

This forum is reserved for everything you want to talk about. It could be about programming, opinions, open source programs, development in general, or just cool stuff to share!
Post Reply
priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Hungarian Notation

Post by priyank_bolia » Wed May 23, 2007 3:51 pm

What notations you use for variable types:
wxString, m_str
std::string, m_str
int, m_n
short, m_s
double, m_ld
bool, m_b
long, m_lg
long long, m_ll
float, m_f
wxChar, m_ch
wxImage *, m_image
byte, m_byte
__int32, m_n32
__int64, m_n64
void *, m_p
size_t, m_sz
ptrdiff_t, m_ptrdiff
intptr_t, m_ptr
uintptr_t, m_uptr
char, m_ch
I don't know what to use, so I am asking you people, what should be the standard according to you if you use that. As this will go in next version of wxVS2005Integration code generator, so I need something that the most people uses.

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Wed May 23, 2007 4:33 pm

I use none. Also I don't use m_, since variables within methods are mostly membervariables, or local.

Only exception are some wxClasses:
wxTextCtrl txt_
wxStaticText stxt_
wxButton btn_
wxListBox lb_
wxComboBox cb_
wxChoice ch_
etc.

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Wed May 23, 2007 5:24 pm

IMHO Hungarian notation is bad. It is a result of not reading the API where types should be documented if they are needed. it takes too much time figuring out what is what, I simply use a couple of rules:

- classes always start with an upper case letter and one uppercase break on word barriers e.g. Class, DataManager etc.
- member variables start with m_ or _ followed by a lowercase, and an uppercase to break for a multiple word variable such as m_variableName
- methods always start with an upper case letter
- sometimes get / set methods start with a lower case letter, but I found that more confusing myself, like getName() or GetName()

I think this is easily remembered, since hungarian notation originates from ANSI C period, today with more classes then basic types, it is kind of confusing to mix and match.

To add to this, a variable should be descriptive, making it m_imageIconDisabled is over the top for a wxImage, I assume m_iconDisabled is more then enough, even when it is a wxBitmap.

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

mispunt
Experienced Solver
Experienced Solver
Posts: 59
Joined: Tue Oct 19, 2004 3:23 pm
Location: Ede, Holland

Post by mispunt » Wed May 23, 2007 9:36 pm

Jorg wrote: - classes always start with an upper case letter and one uppercase break on word barriers e.g. Class, DataManager etc.
- member variables start with m_ or _ followed by a lowercase, and an uppercase to break for a multiple word variable such as m_variableName
- methods always start with an upper case letter
- sometimes get / set methods start with a lower case letter, but I found that more confusing myself, like getName() or GetName()
I almost got the same rules, except I like to start with lower case method names. some times I use m_psomeName for pointer types. But not always.
OS: win XP pro
Compiler: MingW
wxWidgets version: 2.6.2

priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post by priyank_bolia » Thu May 24, 2007 6:44 am

I have a practice from the MFC days to use m_str or m_n at least. These provide much more clarity. although I don't use for every variable types, just for strings and int.

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Thu May 24, 2007 7:11 am

I agree I used them as well, for int (n) and string (s) mostly like nCount, sName but in the time where no char * is common to be passed around, but higher class objects like std::string or wxString or CString, what is there to recognise? if I type sName it can be either one of those (given someone still uses crappy MFC for the latter ;-) )

It depends on what you want. Maybe you should leave this up to the user and don't force a standard upon them. Maybe you can make prefix placeholders they can add if they like, e.g. when the user wishes to prefix a wxString, and your wizard / generator recognizes wxString as a type, it can put the prefix there.

I would go with such a scheme based design where the user can pre-select a number of them, with an example matrix how they look, and maybe the ability to make a custom scheme

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post by priyank_bolia » Thu May 24, 2007 7:34 am

Jorg wrote:I would go with such a scheme based design where the user can pre-select a number of them, with an example matrix how they look, and maybe the ability to make a custom scheme - Jorgen
Agreed, but it will place a heavy burden for the initial release. That can be future versions. But there should be some default values always and that what I am asking what should be the default values.
Jorg wrote:Maybe you should leave this up to the user and don't force a standard upon them.
No, I am not forcing a standard. It just that when user adds a private variables say m_strJorgen, a get/set function also gets added, like GetJorgen, SetJorgen :D provided Get/Set checkbox is checked. So, the software should automatically remove the prefix from the variable name, for meaningful function names, else the user had to do that manually in the generator itself.

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Thu May 24, 2007 8:35 am

I also agree with Jorgen, that the user should be able to edit this with a Optionsdialog or something.
In my own Tool for codegeneration I solved it that way.

chrismcb
Knows some wx things
Knows some wx things
Posts: 26
Joined: Fri Nov 02, 2007 11:58 am

Re: Hungarian Notation

Post by chrismcb » Fri Nov 02, 2007 12:20 pm

priyank_bolia wrote:What notations you use for variable types:
wxString, m_str
std::string, m_str
int, m_n
short, m_s
Types like short, int, float don't have a direct hungarian notation. Think of Hungarian like a unit. I can tell you I have a number, and the number is an int. But that doesn't tell you if the number is a kilometer or a mile!

If it is an int, what sort of int is it? What is it representing. Hungarian Notation tells you.

killerbot
Experienced Solver
Experienced Solver
Posts: 75
Joined: Wed Dec 26, 2007 1:13 pm

Post by killerbot » Wed Dec 26, 2007 2:11 pm

hungarian notation is rather old. Microsoft started out with that, and in the meantime doesn't use it anymore (old C days).

It's kind of putting information in there twice :
1) the declaration
2) the hungarian notation

guess which one will always be correct and which one will run out of sync ;-)

Strong typed languages don't need that. In the "c++ coding standards" book of Sutter and Alexandrescu they say, a coding standard should NOT enforce hungarian notation, a coding standard *may* even enforce to prohibit the use of it.

One last remark, the often used :
m_variable
_Variable
Variable_
is suggested by many authors for scoping reasons.
A simple Setter will show it's purpose:

Code: Select all

class Test
{
public:
  void SetVar(int Variable)
};

void Test::SetVar(int Variable)
{
  m_Variable = Variable;
}
otherwise we have to think of other names for the abstraction but which makes clear the relations etc ...
The m_ and others solve this very cleanly.

priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post by priyank_bolia » Wed Dec 26, 2007 4:54 pm

Thanks for the info, I finished reading the book "c++ coding standards" just 2 weeks back. Also I stopped using hungarian notation, but its a habit of many years :D , so I guess will take time to fully leave that.

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Wed Dec 26, 2007 8:00 pm

We all want to develop the most efficient and best we can. But when it doesn't really add anything anymore, you tend to find a path in the middle.

I had the same when coding with too much safe guards in my code (checking every pointer for NULL etc). But at some point your code becomes unreadable, so swithcing to asserts or check macro's is more efficient and readable. When the code is debugged properly, most null pointer checks can be omitted.

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

wxwalker
In need of some credit
In need of some credit
Posts: 5
Joined: Fri Dec 28, 2007 10:39 pm
Location: Ancona, Italy (or Schleswig, Germany)
Contact:

Post by wxwalker » Sun Dec 30, 2007 11:36 pm

I use a mix between Hungarian and personal one. Member variables are always "m_"-flagged if they belong to objects/classes. But if I delcare directly variables of some type i use, str, dw(ubsigned 32-bit INTEGER), i(Signed 16-Bit INTEGER) and so on.

Code: Select all

Class MyCLass: Base
{
   ...

   private
   wxString m_caption; // string variable of type wxString

   public
   wxString GetCaption();

   ...
};

int _tmain(int argc, wchar_t *argv[])
{
   short iCounter; // Define counter of type 16-bit signed integer
   wxString strInputLine; // Any string not related to an object

   ...

   return (0);
}
As iCounter doesn't belogn to any class I flag it "i".

Post Reply