Do you prefix?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 20763 Views - Last Post: 14 September 2012 - 05:55 PM

Poll: Do you prefix? (29 member(s) have cast votes)

Do you prefix your variables name?

  1. Yes (18 votes [62.07%] - View)

    Percentage of vote: 62.07%

  2. No (11 votes [37.93%] - View)

    Percentage of vote: 37.93%

Vote Guests cannot vote

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,410
  • Joined: 29-May 08

Do you prefix?

Post icon  Posted 28 April 2011 - 07:01 AM

Show use what you use.

Member Variables: _VariableName
Is This A Good Question/Topic? 0
  • +

Replies To: Do you prefix?

#2 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

Re: Do you prefix?

Posted 28 April 2011 - 07:21 AM

For me (in general programming), I do not use prefixes as it lowers the reason for having a meaningful variable name. Microsoft helped this hate grow by introducing meaningless (for me) prefixes that make no sense. I mean. How the hell am I supposed to know that a lpsz- prefix means a long pointer to a zero-terminated string? This kind of stuff is not needed, it only produces more wasted memory.
Was This Post Helpful? 1
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Do you prefix?

Posted 28 April 2011 - 08:12 AM

Private class variables with a corresponding public property get the "m_" treatment. Local variables inside of methods get nothing.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8908
  • View blog
  • Posts: 33,395
  • Joined: 12-June 08

Re: Do you prefix?

Posted 28 April 2011 - 08:17 AM

UI controls:
ux<control type><name>

variables and objects:
l<Name> = integer
s<Name> = string
b<Name> = bool
dt<Name> = datetime
etc..

global variables:
same as above but with an underscore before the type lettering.

ux = 'user experience' plus it keeps them all in the same area... so if I have a form with a grip of textboxes I know that "uxTextbox" has them all clustered in the same area.
Was This Post Helpful? 1
  • +
  • -

#5 bflosabre91  Icon User is offline

  • go sabres

Reputation: 105
  • View blog
  • Posts: 1,439
  • Joined: 22-February 08

Re: Do you prefix?

Posted 28 April 2011 - 08:50 AM

for variables i do the data type in front like sMyString or iMyInt. I put an underscore my public properties that are set from different classes. i do that because i like knowing if the variable may have be initialed somewhere else.

For controls i do stuff like txtMyTextbox, cbMyCheckbox, radMyRadioButton. I find its much easier to keep track of names to do it this way.
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Do you prefix?

Posted 28 April 2011 - 09:04 AM

Private members get an _underScore naming.

Public properties get regular NormalCasing naming.

Controls/UI get something like:

  • btnSubmit
  • rbtMale
  • rbtFemale
  • chkConfirm

Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4425
  • View blog
  • Posts: 7,693
  • Joined: 08-June 10

Re: Do you prefix?

Posted 28 April 2011 - 09:31 AM

Let's see what MS has to say about this:

http://msdn.microsof...y/ms229045.aspx

Quote

Do choose easily readable identifier names. For example, a property named HorizontalAlignment is more readable in English than AlignmentHorizontal.

Do favor readability over brevity. The property name CanScrollHorizontally is better than ScrollableX (an obscure reference to the X-axis).

Do not use underscores, hyphens, or any other nonalphanumeric characters.

Do not use Hungarian notation.

Hungarian notation is the practice of including a prefix in identifiers to encode some metadata about the parameter, such as the data type of the identifier.

Avoid using identifiers that conflict with keywords of widely used programming languages.


So no, I don't prefix my variables with anything.

I try to match published standards whenever I can. At work, we don't have a strict set of guidelines, so I follow the ones Microsoft sets out.

The only time I'll use an underscore prefix is when I'm hiding a field by a property of the exact same name, capitalization included.

I do break these rules for UI controls. I name my control after what it is. For example, if there's a textbox that accepts query text, it'll be named queryTextBox, but this is simply for my own sake, in immediately being able to tell UI controls from anything else.

There are lots of useful guidelines here for naming all kinds of things:

http://msdn.microsof...y/ms229002.aspx

And a more general style guide:

http://msdn.microsof...y/ms229042.aspx

I believe the reason MS is saying not to use Hungarian notation is because 1) static typing, and 2) Visual Studio itself. A huge reason Hungarian notation got popular was in languages where variables weren't statically typed, so people prefixed their variables with types, to prevent themselves from assigning a string to a variable they were using for an integer.

These days, in most languages, you can't do that. And even if you could, Visual Studio lets you know what the type of a variable is. You can hover over it, or you can press F12 to go to it's definition. Trust in your tools! They're so much better than they used to be.

This post has been edited by Curtis Rutland: 28 April 2011 - 09:40 AM

Was This Post Helpful? 3
  • +
  • -

#8 nathanpc  Icon User is offline

  • SCIENCE!
  • member icon

Reputation: 112
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Do you prefix?

Posted 29 April 2011 - 09:13 PM

My prefixes for UI controls:
  • lbl - Label
  • bt - Button
  • chk - Checkbox
  • lst - Listbox
  • gau - Gauge
  • img - Image


For variables it's all a mess :P
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Do you prefix?

Posted 30 April 2011 - 01:25 PM

Only my UI controls have the standard prefix. All variables just have names that make sense.

Public Class zone 
'defined members
End Class

Private Zones As New List(Of zone)

Zones.Add(New Zone With {.name = "foobar"})

Was This Post Helpful? 0
  • +
  • -

#10 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2528
  • View blog
  • Posts: 4,630
  • Joined: 24-September 10

Re: Do you prefix?

Posted 02 May 2011 - 07:49 AM

I'll exemplify mine in VB as this is the VB forum... rather standard stuff that I've noticed a lot of other people use (though I've heard hard criticism of the underscore, which I don't understand. If you don't like it, don't use it...)

Public Class PrefixEample

#Region "Fields"
    Public Const CONST_VAL As Integer = 0

    Public SomeValue As Integer ''though I rarely have public fields
    Private _anotherValue As String
    Protected _protectedValue As Double
#End Region

#Region "CONSTRUCTOR"
    Public Sub New(ByVal sStringParam As String, ByVal iIntParam As Integer)

    End Sub
#End Region

#Region "Public Properties"

    Public Property AnotherValue() As String
        Get
            Return _anotherValue
        End Get
        Set(ByVal value As String)
            _anotherValue = value
        End Set
    End Property

#End Region

#Region "Public Methods"

    Public Sub Foo()

        Dim sVal As String = ""
        Dim iVal As Integer = 0
        Dim dVal As Double = 0.0
        Dim fVal As Single = 0.0F
        Dim oVal As Object = Nothing
        Dim bVal As Boolean = False
        Dim ofName As Form = Nothing

    End Sub

#End Region

End Class



Arithmetic is relinquished from a lot of this though. In methods arithmetic will usually receive single letter names that look more like algebra.

a,b,c... constants
i,j,k... indices
x,y,z... variables
ix,iy,iz,jx,jy,jz... vector or matrix members, cartesian coords, etc
f0,f1,f2, x0, x1, x2... values in a sequence
fN, xN... final or very large value
n... series count (for summations, product series, loops, etc)
l, len... length from an array or list
l,w,h... length width and height
dx,dy,dz... change in some value (usually position, 'd' is for delta)
f (as prefix)... denotes a functional result


I have other prefixes for hackish code. For example I'll use $ and double underscore (__) to denote vars used in what I consider hacky or odd code. No offense to the PHP users, but I use '$' for hacky code because it reminds me of PHP and I consider PHP to be very slapdash and hacky at times.

Furthermore my prefixes might change if in certain languages or if I'm writing code for someone other than myself. For instance at work they love to use the 'm' prefix, I personally dislike it, but they've been using it for years (I also do not know what the 'm' signifies, but I see it used by many people in the industry). This also includes the prefixes for controls like:

btn_ButtonName
lbl_LabelName
cbo_ComboBoxName
grd_GridName
chx_CheckBox
pnl_PanelName
img_ImageName
lst_ListBox
drp_DropDownBoxName

etc...





As for those stricter suggestions (like those found on MSDN), I only apply that to public members on the interface. I see no problem with the underscore as a private or protected variable, but as a public one I don't like it. But to not use it at all... screw that, these symbols make the code MUCH more readable.

That's basically my distinction between interface and implementation. And I go by the principal of 'program to the interface'.

This post has been edited by lordofduct: 02 May 2011 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#11 SurfingShark  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 188
  • Joined: 18-April 11

Re: Do you prefix?

Posted 04 May 2011 - 03:20 PM

I'm really suprised that MSFT doesn't like hungarian notation. I'm a huge proponent of it. I would rather see three extra letters in from of a variable name instead of having to reference the definition of a variable to find out the datatype.
Was This Post Helpful? 0
  • +
  • -

#12 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Do you prefix?

Posted 09 May 2011 - 02:35 AM

Microsoft are weird like that. I always prefix my variable with <type><Name> like so:
dblValueX;
btnOK;
strName;
clsEnemy;


This post has been edited by NoBrain: 09 May 2011 - 02:36 AM

Was This Post Helpful? 0
  • +
  • -

#13 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Do you prefix?

Posted 09 May 2011 - 07:09 AM

I used to prefix everything with its type but now I try to keep my prefixing down to a minimum and use meaningful names as much as possible and let the IDE keep track of type.
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4425
  • View blog
  • Posts: 7,693
  • Joined: 08-June 10

Re: Do you prefix?

Posted 09 May 2011 - 11:34 AM

If you guys really do care about the negatives of Hungarian notation (and not just bagging on MS), you can read here:

http://en.wikipedia....n#Disadvantages

Also note the section immediately following it: "Notable Opinions."
Was This Post Helpful? 0
  • +
  • -

#15 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2528
  • View blog
  • Posts: 4,630
  • Joined: 24-September 10

Re: Do you prefix?

Posted 09 May 2011 - 11:44 AM

Every disadvantage I've ever read have been mostly aesthetic and/or personal taste. Never have I seen a deal breaking reason to avoid it. In the same respect using it in modern type safe languages also doesn't come with anything but aesthetic and/or personal reasons for using it.

In the end... take it or leave it.

... see Advantages found just above disadvantages. Which hilariously some of are pretty much the same thing repeated just with a different connotation put on it.

This post has been edited by lordofduct: 09 May 2011 - 11:49 AM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2