• 0

Using non-descriptive variable names...


Question

on another note your lectures

the word "amt" is not descriptive and is very poor programming

he/she should have used the fullword "amount" as I did.  short hand variable make it difficult to read

We use 'amt' very often at work. Everybody knows what it means.

It would be bad programming to use 'a', but 'amt' equals amount in everyone's (who'll have to look at the actual code) mind so I would consider it a descriptive variable.

I would also consider using 'amt' in a 'banking' application as being a descriptive variable too. I would be surprised to find anyone going through a bank app. code to not know what amt would mean. Especially in a method called deposit()!

Granted, amt could mean something else, and it would be bad programming to use a amt variable to mean something else. But then, most complete words have multiple meanings too.

So I think that u can't say that using an abbreviation for a variable name is "very poor programming".

Just my 2 cents.

*edit: Now that I checked, I could add that java.sun.com often uses abbreviations for variable names in their examples. 'num', 'rect', 'fps', etc.

Edited by Mouton
Link to comment
Share on other sites

Recommended Posts

  • 0

it depends how you're using it i reackon, and if it would do anyharm to the length of method names as well, like getAmount is relatively nice and still in reasonable length, i think this is hard to debate subject, because to an average programmer and also an average person we would understand it still however you might adopt such habbit in the future, and assume others would get such an abbreviation too. I mean some words should be abbreviated based on it's length but some shouldn't, it does get hard in the future when someone else reads it, and they have to read it slowly, and besides amt could also conflict for some others, it could mean something else for them.

Link to comment
Share on other sites

  • 0

I think using "amt" is fine as well. As long as it's fairly obvious what it means and what the variable contains, it shouldn't be a problem. If you use comments throughout the code, it should be easy to work out what everything means.

I would use either: dAmount/dAmt or decAmount/decAmt

Good idea to prefix the variable name with the data type

Why? Hungarian notation (which is what this is) is the spawn of the devil. Microsoft no longer recommends its use for .NET programming. What happens if you decide to change the type of one of your variables (say from an integer to a long)? And it's usually perfectly clear from the context in which the variable is used what type it is (and you can always scroll up to check).

More on Hungarian notation/variable prefixes here and here.

Link to comment
Share on other sites

  • 0

Hmm I made that comment about "amt"

In terms of maintenance it is best to use the full word and not abbrivated ones, because it makes it easier to READ by other programmers. true setAmount(double amt), is already quite descriptive because of the function name, but I think a lecture teaching new students he/she should teach the use of descriptive fully named variables.

I remember being penalised for using short hand when I was a student.

names like "frm" could mean frame, form or something else.

Making your code read like a book reduces maintenance time/costs. especially when someone else has to update/maintain the code like I have to do at my work all the time.

Link to comment
Share on other sites

  • 0

I usually abbreviate the type name.

System.Xml.XmlTextWriter = xw

System.Xml.XmlTextReader = xr

System.Xml.XmlDocument = xd

System.String = str

System.String() = sa

System.Integer = i

System.Long = l

System.Short = st

System.Text.ASCIIEncoding = ae

System.Collections.Specialized.NameValueCollection = nvc

...and so on.

Link to comment
Share on other sites

  • 0

Wow. So many constructive comments!

I was a little scared this would start flame thread! :)

memodude: what happens when u need to use two System.Integer ? the 2nd one is names i2 ..???

using only variable type as variable name makes no sense whatsoever... makes code unreadable.

Link to comment
Share on other sites

  • 0
Wow. So many constructive comments!

I was a little scared this would start flame thread! :)

memodude: what happens when u need to use two System.Integer ? the 2nd one is names i2 ..???

using only variable type as variable name makes no sense whatsoever... makes code unreadable.

I use j then.

Link to comment
Share on other sites

  • 0

I try to use descriptive names, excluding the type... Hungarian notation puts the focus on the type, not the usage. If the name is descriptive enough, it should be evident what the type is used for. However, there are times when I use Hungarian for consistency, mostly in Win32 programming.

Indexers and temp variables, I usually don't care about them. It's obvious what an indexer is for since I generally declare it in the for-loop initializer. Temp vars are just that, temporary, and don't really need to be regarded.

As for abbreviations, as long as they're standard, like amt is standard in the English language, I'll use them. If it is some abbreviation/acronymn that I've come up with... well, I wouldn't subject anyone to than. :)

Ultimately, though, I'll code to whatever standards my team agrees to. We have a coding standard documented, so it's always easy to figure out what's supposed to happen when in doubt.

Link to comment
Share on other sites

  • 0
I would use either: dAmount/dAmt or decAmount/decAmt

Good idea to prefix the variable name with the data type

584705196[/snapback]

I've tried that before. It seems to make my code look ugly :s.

Link to comment
Share on other sites

  • 0
I would use either: dAmount/dAmt or decAmount/decAmt

Good idea to prefix the variable name with the data type

I personally find the prefixing messy if used with primitives such as double, int etc.

however prefixing large object I find makes it a little eaiser to read... like

frmMain - an instance of TForm called main

tblAbout - an instance of TTable called about

still

MainForm and AboutTable is easier to read than the above

Link to comment
Share on other sites

  • 0
I usually abbreviate the type name.

System.Xml.XmlTextWriter = xw

System.Xml.XmlTextReader = xr

System.Xml.XmlDocument = xd

System.String = str

System.String() = sa

System.Integer = i

System.Long = l

System.Short = st

System.Text.ASCIIEncoding = ae

System.Collections.Specialized.NameValueCollection = nvc

...and so on.

584706441[/snapback]

I am so glad I don't have to maintain your code mate!

No offence or anything, but in a complex program, I would find it difficult to read and maintian.

Link to comment
Share on other sites

  • 0
Sometimes i think it depends on the context your code is written for... i mean if the class is a bank account, amt is fairly straightforward.

584705499[/snapback]

Yes I agree, but I believe a teacher teaching new computer science students programming should always use full variable name when giving out code to students.

Link to comment
Share on other sites

  • 0
Indexers and temp variables, I usually don't care about them. It's obvious what an indexer is for since I generally declare it in the for-loop initializer. Temp vars are just that, temporary, and don't really need to be regarded.

I agree temp variable can be called

"i" or temp or str.

some lectures do tell students to call "i" in for loops a more meaningful name though

Link to comment
Share on other sites

  • 0

I think in terms of the i variable, they really oughta stop making for loop examples using i and using some other var name, some of my peers use to think you can only name it as i in a for loop.

Link to comment
Share on other sites

  • 0

I need to get in the habit of using good variable names. I usually just randomly hit a key on the keyboard.

dim x(20) As String
x = tmp.Split(",")
x(Asc(compname.Chars(5)) - 65) = ru
Dim q As String
For Each ggg As String In x
q += ggg & ","
Next
q = Left(q, q.Length - 1)
Dim hf As IO.File

:blink:

Link to comment
Share on other sites

  • 0

Ok. Sorry for the tripple post. I kept getting these, so I just tapped Reload. Silly me.

mySQL error: Too many connections

mySQL error code:

Date: Monday 11th of October 2004 01:51:31 PM

Link to comment
Share on other sites

  • 0
I am so glad I don't have to maintain your code mate!

No offence or anything, but in a complex program, I would find it difficult to read and maintian.

584709534[/snapback]

Which is where Intellisense comes in... Assuming you use VS.Net or similar, of course.

My code looks like this (from a calendar user control in .NET):

 /// <summary>
  /// Works out the range of dates to display.
  /// </summary>
  private void CalculateDates()
  {
 	 DateTime firstDay, lastDay;
 	 int currentDay, firstWeekday, lastWeekday, numOfDays, firstOffset;

 	 // Determine the number of days in the selected month.
 	 numOfDays = DateTime.DaysInMonth(SelectedValue.Year, SelectedValue.Month);

 	 // Make dates containing the first and last of the selected month.
 	 firstDay = new DateTime(SelectedValue.Year, SelectedValue.Month, 1);
 	 lastDay = firstDay.AddDays(numOfDays + 1);

 	 // Get the week day for each date.
 	 firstWeekday = (int)firstDay.DayOfWeek;
 	 lastWeekday = (int)lastDay.DayOfWeek;

 	 // Get the selected day.
 	 currentDay = SelectedValue.Day;

 	 // Work out the offset of the first day in the selected month.
 	 if (firstWeekday == 0 || firstWeekday == 1) firstOffset = 6 + firstWeekday;
 	 else firstOffset = firstWeekday - 1;

 	 _startDate = firstDay.AddDays(-firstOffset);
 	 _endDate = lastDay.AddDays(42 - (numOfDays + firstOffset));
  }

Variable names are fairly descriptive and there's plenty of comments. Don't underestimate the use of comments: in previous jobs, I've had to maintain code that's completely without comments. When the programmer in question isn't very good (which is most of those I've worked with) and their code is poor, the lack of comments makes for a frustrating time.

I use "_" as the prefix on class-level variables so as to distinguish them from local variables.

Link to comment
Share on other sites

  • 0
Which is where Intellisense comes in... Assuming you use VS.Net or similar, of course.

whats intellisense?

on another note: your example code I thought was quite good to read. As for comments, we we should write meaingful comment in our code !!!!

this was a good example of variable names :yes:

DateTime firstDay, lastDay;
 ?int currentDay, firstWeekday, lastWeekday, numOfDays, firstOffset;

a poor example which is harder to read could be

DateTime f_Day, l_Day;
 ?int thisDay, fWeekday, lWeekday, days, Offset;

Link to comment
Share on other sites

  • 0

I abbreviate as much as I'm comfortable with to get the code working quicker.

Once complete I use Find/Replace to change my abreviations into full blown descriptions.

for example, I'd use:

fwday, and lwday and then find and replace them to be first_Weekday, and last_Weekday, or what have you.

Link to comment
Share on other sites

  • 0
It's the auto complete drop down menu followed from a period.

e.g. Application.[dropdownmenushows]

584717565[/snapback]

Ok, so it is code complete function that microsoft provide.

Link to comment
Share on other sites

  • 0
Ok, so it is code complete function that microsoft provide.

584717660[/snapback]

Plus, with VS.Net 2003, if you hover the mouse over a variable, it tells you its type and whether it's local, a parameter or a class-level variable in a tooltip.

For example (the mouse pointer is over "currentDay" in the line below the first comment) :

267.png

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.