 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
09-06-2007, 11:26 PM
|
#1 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Posts: 164
Thanks: 0
|
[Tip] Variable Prefixes
Something i learnt, fairly recently but has already been a huge timesaver, and keeps scripts to a strict standardised programming method is variable prefixes.
Variable prefixes are 1 to 2 characters prior to the variable name, in the variable, for example:
$szString instead of $string.
The following are prefixes i use:
m_ Member, this is used as a pre-prefix to show a member variable and is used like: $this->m_iInteger;
g_ Global, this is used as a pre-prefix to show a global variable and is used like: global $g_aGlobal;
a Array ($aArray)
sz String ($szString, $sz looks better than $s before you ask)
i Integer ($iInteger)
f Float ($fFloat)
b Boolean ($bBoolean)
p Object/Pointer ($pPointer)
m Mixed (i.e. could be any value - $mMixed)
Think of the following peices of code:
PHP Code:
<?php
$numbers = array(1, 5, 10, 15);
$count = count($numbers);
$addition = $numbers[1]+$numbers[2];
$division = $numbers[1] / $numbers[2];
$integer = $addition+$division;
?>
With the previous snippet of code, you may of noticed that i have thought the $integer would be an integer, but looking a little closer you would notice that division could cause non-integer numbers.
Now if you take the following peice of code:
PHP Code:
$aNumbers = array(1, 5, 10, 15);
$iCount = count($aNumbers);
$iAddition = $aNumbers[1]+$aNumbers[2];
$fDivision = $aNumbers[1] / $aNumbers[2];
$iInteger = $iAddition+$fDivision;
Now it is made very clear that the $iInteger would not be an integer, due to the $fDivision clearly not being an integer, thus causing a float result which could cause problems within your application.
This is obviously a very simple example of the benefits of variable prefixes, but imagine using these within multiple thousand line applications, with variables being used across multiple includes, variable prefixes come in much more handy then! Other benefits include, the ability to type-cast/type-enforce and definately readability!
|
|
|
|
09-06-2007, 11:30 PM
|
#2 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
Thanks Bluesaga.
I always wondered what sz stands for.
|
|
|
|
09-06-2007, 11:41 PM
|
#3 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Posts: 164
Thanks: 0
|
No problem, it is a little hard to get used to, from your normal programming, but since i have got use to it, its a lifesaver! Honestly
|
|
|
|
09-06-2007, 11:44 PM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,182
Thanks: 88
|
Just to elaborate on Bluesaga's point about the automatic typecasting. I wrote a function earlier to automatically extract the prefix from GET, POST, COOKIE and SESSION arrays. You can then enforce the types how you wish - automatically.
It's common knowledge that most sites are hacked because a user is allowed to set the values of GET and POST (And less so COOKIE) to whatever they desire and thus causing all sorts of problems such as SQL injection attacks.
PHP Code:
function getPrefix($szPointer) { $aVariable = str_split($szPointer); $szPrefix = ''; foreach($aVariable as $szVariable) { $szOrd = ord($szVariable); if($szOrd >= 65 && $szOrd <= 90) { break; } $szPrefix .= $szVariable; } return $szPrefix; }
This function will return the prefix. I would use it the following scenario:
PHP Code:
function g($szPointer) { $szValue = $_GET[$szPointer]; switch(getPrefix($szPointer)) { /* Code Typecasting */ } }
The switch will contain the array pointer's prefix, such as sz or i. I would recommend then using the CTYPE_* functions.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
09-07-2007, 06:26 AM
|
#5 (permalink)
|
|
The Contributor
Join Date: Apr 2007
Location: Hampshire
Posts: 28
Thanks: 1
|
Wow thanks both of you, I never knew anything about this so I will be reading again tonight when I get home from school and will hopefully learn alot of information about it, Thanks again!
|
|
|
09-07-2007, 10:36 AM
|
#6 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,112
Thanks: 3
|
Quote:
Originally Posted by Wildhoney
PHP Code:
function getPrefix($szPointer)
{
$aVariable = str_split($szPointer);
$szPrefix = '';
foreach($aVariable as $szVariable)
{
$szOrd = ord($szVariable);
if($szOrd >= 65 && $szOrd <= 90)
{
break;
}
$szPrefix .= $szVariable;
}
return $szPrefix;
}
|
I'm just wondering why you went the route of split/foreach when a simple RegEx would be more readable (personally, I find it easier to glance at a pattern than work through a sequence of loops) and probably quicker? Also, in the case of 'pointers' without an uppercase character (e.g. random) the full string would be returned -- I'd have thought that in that sort of case, there'd logically be no prefix.
|
|
|
|
09-07-2007, 10:54 AM
|
#7 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 438
Thanks: 22
|
I agree that the RegEx could probably be more readable (if you know a bit of RegEx, that is), however, I was always under the impression that you can't beat regular loops in terms of speed. Afterall, by looping yourself you know exactly how many loops are required. In all honesty though, I have no idea how many loops a RegEx makes. This question probably deserves a new thread to itself, what is quicker, custom loops or RegEx :)
|
|
|
|
09-07-2007, 12:18 PM
|
#8 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,112
Thanks: 3
|
Quote:
Originally Posted by Karl
I agree that the RegEx could probably be more readable (if you know a bit of RegEx, that is), however, I was always under the impression that you can't beat regular loops in terms of speed.
|
PHP Code:
// My version of Wildhoney's getPrefix function function getPrefixSalathe($szPointer) { if (preg_match('/^([a-z_]+)[A-Z]?/', $szPointer, $aVariable)) return $aVariable[1]; return ''; }
After a quick test here are my results based on calling the function with the single argument "szString".
Calling with "szString" over 100,000 runs:
Wildhoney - 1.3253 seconds
Salathe - 0.9695 seconds
Then again, what real difference is 0.00000356 seconds per function call going to make? :p
As far as being, " under the impression that you can't beat regular loops in terms of speed," that is a good position to hold as a general rule. Sometimes looping through every character (or every item in an array, or whatever) is the clearest, simplest and fastest way to get things done. It is quite easy to get the RegEx parser bogged down - with great power comes great responsibility (to keep things moving slickly).
Finally, it would be slightly more efficient (~0.1141s faster over 100,000 runs) for Wildhoney's function to forgo the str_split and loop through the characters in the string directly if you didn't want to go the RegEx route.
|
|
|
|
09-07-2007, 12:23 PM
|
#9 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 438
Thanks: 22
|
Thanks for that information Salathe, it's very interesting to know! I must admit, I honestly thought RegEx would have been slower :o
|
|
|
|
09-07-2007, 12:25 PM
|
#10 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,182
Thanks: 88
|
Quote:
Originally Posted by Karl
Thanks for that information Salathe, it's very interesting to know! I must admit, I honestly thought RegEx would have been slower :o
|
Me too, actually. Will definitely go down the RegEx path now though.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
09-07-2007, 03:28 PM
|
#11 (permalink)
|
|
The Contributor
Join Date: Apr 2007
Location: Hampshire
Posts: 28
Thanks: 1
|
Wow, That's not a very big time difference... All the books I have ever read say that Regex is slower... Maybe not :p
|
|
|
09-07-2007, 10:01 PM
|
#12 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Posts: 164
Thanks: 0
|
It depends on what usage, if you have look aheads and look behinds regex will definately be slower, it just depends how you want to use it.
|
|
|
|
10-24-2007, 01:28 AM
|
#13 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,182
Thanks: 88
|
Here's a question for you people which I've been pondering and musing over. What prefix would you use for an unknown variable type?
PHP Code:
$vMyVar = 'Unknown'; // Varies $uMyVar = 'Unknown'; // Unknown
What else is there? It really needs one that looks good - v and u don't, to me.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
10-24-2007, 09:36 AM
|
#14 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Posts: 164
Thanks: 0
|
Doesn't that really get classified by:
PHP Code:
$mMixed = "Mixed Variable, could be array, int, float etc....";
|
|
|
|
10-24-2007, 10:24 AM
|
#15 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 438
Thanks: 22
|
I agree with Bluesaga, I always use the m prefix for mixed vars.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
|
|
|
|
10-24-2007, 11:58 AM
|
#16 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,112
Thanks: 3
|
The PHP manual uses the term "mixed" to denote uncertain type. So yes, an m prefix would seem the most appropriate choice (and is the one I use).
__________________
|
|
|
|
10-24-2007, 12:05 PM
|
#17 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,182
Thanks: 88
|
In that case, m it is!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
11-05-2007, 03:14 AM
|
#18 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
|
Thanks for the tip. I've been using it lately, and it makes my code seem much more "professional". :)
|
|
|
11-05-2007, 11:57 AM
|
#19 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,182
Thanks: 88
|
Yea :) it does make code a lot more professional looking - and also a lot easier to read! It shows that you know what the functions return if you set data types, and so when you skim over it you can instantly deduce what's holding what.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
11-12-2007, 12:35 AM
|
#20 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
|
What prefix would you guys use for MySQL resources such as a query result?
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|