PHP - Worth including the data type in the variable name?
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'; in开发者_运维技巧stead 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
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
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.
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.
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.
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.
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
- Could this make my code more understandable?
- No, it's useless
- Should I actually use this or stick with "normal" variable names?
- Stick with normal names
- Do you know scripts where this is used?
- Yes, but it does not matter
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
.
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.
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 :)
精彩评论