views:

105

answers:

9

Hello!

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

+1  A: 

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
+1  A: 

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
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
@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
+1  A: 

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
+8  A: 

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

Yada
VB (both classic and .NET) has types, it's just that the default type is the generic Variant type.
R. Bemrose
Nevertheless the notation came from VB .net world because of the variant type.
Yada
+1  A: 

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
A: 

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
+1  A: 

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
A: 

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
A: 

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