Home » Php » PHP – Worth including the data type in the variable name?

PHP – Worth including the data type in the variable name?

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&Answers:

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

Answer:

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.

Answer:

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.

Answer:

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.

Answer:

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.

Answer:

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

Answer:

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.

Answer:

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.

Answer:

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 🙂