Why would you?
By rickvdbosch
- 2 minutes read - 249 wordsLast week I ran into a variable called @continue in the sourcecode of a project I’m reviewing. At first I had to think a bit before I knew what the @ was for. Of course I knew it’s an escape character when used in front of a string value. But when used in front of a variable name? In that case, it is an escape character also, but this time for the name of your variable. This way you can use a reserved word as a variable name.
Why would you do this? What’s so important about that exact name of the variable that it is not possible to think of a slightly different name which is not a reserved word? In this particular case, the boolean was used to indicate if the method it was declared in should continue parsing the next value in a foreach loop. I would call the variable ‘continueParsing’ or ‘continueLooping’ or something like that*. Those more clearly state the purpose of the variable, and don’t make you escape your variable name. Furthermore it doesn’t baffle 75% of the developers maintaining or reviewing your code because they don’t know why the variable starts with an @.
Maybe I’m missing something. Maybe there are certain situations where this an absolute must. I can’t think of one right now though…. can you?
* By the way, the best way would probably be to use a break statement, but that’s not the reason I’m posting this for. 😉