7 Replies - 381 Views - Last Post: 02 July 2012 - 06:17 PM Rate Topic: -----

#1 Alyssa Saila  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 07-January 12

Best Practice For Naming Variables (Particularly Using Arguments)

Posted 02 July 2012 - 02:34 PM

I'm caught sometimes between deciding to reassign statements as new variable names (so that I can read the shorter more clear names easier), or simply leaving the statements as they are to avoid unecessarily new created objects.

Of the two examples beneath, which is better and why? What is conventional best practice?

this way...
switch(myArrayList[line].ToString().Trim().Substring(0, 1).ToString())
{
	case "A":
	break;
	case "B":
	break;
	case "C":
	break;
}




or this way...
string easierReadShorter = myArrayList[line].ToString().Trim().Substring(0, 1).ToString();

switch(easierReadShorter)
{
	case "A":
	break;
	case "B":
	break;
	case "C":
	break;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Best Practice For Naming Variables (Particularly Using Arguments)

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3877
  • View blog
  • Posts: 11,416
  • Joined: 18-April 07

Re: Best Practice For Naming Variables (Particularly Using Arguments)

Posted 02 July 2012 - 03:08 PM

Questions to ask yourself...

1) Which is easier to read for others?
2) Would you use the statement assigned to the variable in more than one place?


You should always strive for readability and to simplify complexity. If you take a long statement and assign it to an easier to read variable that you are going to use in more than one spot, wouldn't the second way be the best?

The only way I would really do the first one is if assigning it to a variable would cause a grossly large object in memory that could be pasted around by copy etc and just eat up resources.

:)

Edit: Would a statement like int length = "string".Length really serve to make things easier to read or help reduce complexity? Not really so I wouldn't do something like that... unless of course it can save it being recalculated over and over again which is another benefit of the first one. Set the variable before a loop and it will be calculated once and can be used in a loop without further recalculation on each iteration.

This post has been edited by Martyr2: 02 July 2012 - 03:11 PM

Was This Post Helpful? 1
  • +
  • -

#3 Alyssa Saila  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 07-January 12

Re: Best Practice For Naming Variables (Particularly Using Arguments)

Posted 02 July 2012 - 03:12 PM

Gotcha, just felt a little guilty about the reassigning.

Thanks that puts me at ease about it now, and yes I agree the second way is best.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1940
  • View blog
  • Posts: 5,775
  • Joined: 05-May 12

Re: Best Practice For Naming Variables (Particularly Using Arguments)

Posted 02 July 2012 - 03:22 PM

Also try to choose descriptive variable names.
string easierReadShorter = myArrayList[line].ToString().Trim().Substring(0, 1).ToString();



Doesn't tell the reader much but:

string firstLetter = myArrayList[line].ToString().Trim().Substring(0, 1).ToString();



is a bit more helpful, albeit misleading. firstLetterAsAString would be more accurate.

While on tangent, why not have the code as:
char firstLetter = myArrayList[line].ToString().Trim()[0];


Was This Post Helpful? 0
  • +
  • -

#5 Alyssa Saila  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 07-January 12

Re: Best Practice For Naming Variables (Particularly Using Arguments)

Posted 02 July 2012 - 03:52 PM

Definitely, and yes I agree with using char, I just threw something together for the sake of the question.

But while similarly on the same subject-tangent, do I absolutely have to rename all of my constant variables TO_UPPER_CASE?

I loathe all caps and underscores, and I know you never wanna go littering your code with undefined constants everywhere (because if they change you'll have to hunt all of them down -plus you run the risk of potentially forgetting what the numbers mean themselves in the first place when using constant ints for example).. ... but...

yet still, are there more "conventional aesthetically pleasing" ways of naming constant variables?

I don't wanna break best practice & convention (I'm building a solid foundation correcting my mistakes and trying never to repeat them), even if it hurts, but just wanted to know is it okay to name constants any other way by chance.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1940
  • View blog
  • Posts: 5,775
  • Joined: 05-May 12

Re: Best Practice For Naming Variables (Particularly Using Arguments)

Posted 02 July 2012 - 03:56 PM

The contants as uppercase naming convention is a C/C++ naming convention. C# naming convention advises against it. Just use plain old PascalCase on constants.

This post has been edited by Skydiver: 02 July 2012 - 04:06 PM

Was This Post Helpful? 0
  • +
  • -

#7 Alyssa Saila  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 07-January 12

Re: Best Practice For Naming Variables (Particularly Using Arguments)

Posted 02 July 2012 - 04:28 PM

Excellent ; ) Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4929
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Best Practice For Naming Variables (Particularly Using Arguments)

Posted 02 July 2012 - 06:17 PM

Personally I wouldn't be using strings if I could avoid it.
That's what enums are for.

case Mode.User:
  // Do this
  break;
case Mode.Admin:
  // do this
  break;
case Mode.SuperUser:
  // do this
  break;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1