7

I'm trying to write my code as maintainable and easy to understand as possible, and I thought of including the type of data a variable holds in its name.

e.g:

$intCurrentLine = 1; instead of $currentLine = 1;
$strUser = 'blah'; instead of $user = 'blah';

I don't think it is really necessary in the above example, but may it be helpful in some cases?

  • Could this make my code more understandable?
  • Should I actually use this or stick with "normal" variable names?
  • Do you know scripts where this is used?

best regards, lamas

lamas
  • 4,528
  • 7
  • 38
  • 43

9 Answers9

9

Adding the type to the name of the variable is called Hungarian Notation.

It's a matter of style in typeless languages such as PHP and VB. I prefer not to use because there isn't a real need.

This is a good article on the topic. http://www.joelonsoftware.com/articles/Wrong.html

Community
  • 1
  • 1
Yada
  • 30,349
  • 24
  • 103
  • 144
1

I was tought that in college programming classes, but in the real world found it useless. I just try to make the variable name descriptive and that usually conveys what type it is. For example firstName means string to me, or in your case currentLine means int.

I think as long as you're descriptive and accurate in your naming that's enough.

Parrots
  • 26,658
  • 14
  • 59
  • 78
1

It definitely helps you to know what datatype your variables are without having to use ctype or similar.

If you try to give your variables names that implicitly define their type, such as $userName, you shouldn't have too many issues.

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • Things like $userName would be the things that make the least problems.. Think of stuff like $usersCurrent - it could be an array, an integer or just a string containing a seperated list of users :O – lamas Jan 22 '10 at 13:57
  • 3
    @lamas, that was pretty much his point. $usersCurrent is an example of a poorly named variable. $currentUserCount or $currentUsers would be more indicative of the data. – Langdon Jan 22 '10 at 14:03
1

My rule of thumb is that as long as your variable has an unambiguous name there's no need to annotate it with the type as well.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

This is called hungarian notation and considered bad practice.

According to Robert C. Martin, author of "Clean Code", type encodings "make it harder to change the name or type of a variable, function, or class. They make it harder to read the code. And they create the possibility that the encoding system will mislead the reader."

Don't use them. Choose your variable names wisely.

blinry
  • 4,746
  • 4
  • 26
  • 31
1

I'm not a fan of Hungarian notation. This is my personal opinion and I'm deliberately subjective in this matter.

Strip out hungarian notation from a PHP file

  1. Could this make my code more understandable?
    • No, it's useless
  2. Should I actually use this or stick with "normal" variable names?
    • Stick with normal names
  3. Do you know scripts where this is used?
    • Yes, but it does not matter
Peter Lindqvist
  • 10,122
  • 3
  • 41
  • 60
0

I don't think that the type of a variable adds to the understanding that much. I suggest you should use meaningful names like

$currentLineNumber = 1; instead of $currentLine = 1;
$userName = 'blah'; instead of $user = 'blah';

Although in this case $currentLine makes pretty clear what it means.

In contrast even if I add the type, it is not necessarily easier to understand what the variable actually does, e.g. $floatNumber vs. $numberOfStores.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

usually, I'm using $sVar for strings, $iVar for integers, $aVar for arrays, etc. (where Var is ofc replaced with the variables name). It's a small addition and a pretty bit clearer for me.

Eggie
  • 127
  • 1
  • 10
0

You can specify it in your comments.

Something like

/*
*
* @var string
*/

$your_var = '';

If you have a good ide it will pick up on these things :)

AntonioCS
  • 8,335
  • 18
  • 63
  • 92