- I prefix my private and my protected with _
I prefix my variable with a letter that represents it's type (array: a, integer: i, global: g, mixed: m) this was handy when i used web-services but also to read my code easier
I use descriptive names: $iUserName for example instead of $name
I limit my variable name to a length of maximum 20 characters
Coding standard
Moderator: General Moderators
-
fastfingertips
- Forum Contributor
- Posts: 242
- Joined: Sun Dec 28, 2003 1:40 am
- Contact:
Coding standard
Hello from some time i'm using a coding standard (my own version) when i'm developing PHP, some of idead were taken from other sources, anyway i end it with something that helps me to move on on my projects and also makes my code (in my opinion readable). But i have doubts regarding variable notation so any input on this (were i'm doing wrong or good is great).
Re: Coding standard
I can't see any advantage in that.fastfingertips wrote:I limit my variable name to a length of maximum 20 characters
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Coding standard
That only seems useful (IMO) in languages that have strict-typing.fastfingertips wrote:I prefix my variable with a letter that represents it's type (array: a, integer: i, global: g, mixed: m) this was handy when i used web-services but also to read my code easier
I think those are all good practices except the variable name limiting. I can see where you're coming from - you don't want super long variables, it makes code harder to read. On the other hand, having to trim down variable names just to fit in 20 characters might make them obfuscated.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
#2 and #4 seem out of place in modern scripting languages. #3 is just obvious. I have been wondering about #1. It seems to be a standard more appropriate to PHP4, but with PHP5 you can't access them anyway -- so why distinguish them? Is it really important information to know within the class?
(#10850)
Re: Coding standard
I don't agree, because any self-respecting ide for languages with strict typing rules, would already make it apparent for the developer that a variable can't be used in a given context (eg: not having the correct type).superdezign wrote:That only seems useful (IMO) in languages that have strict-typing.fastfingertips wrote:I prefix my variable with a letter that represents it's type (array: a, integer: i, global: g, mixed: m) this was handy when i used web-services but also to read my code easier
I can imagine that this feature is harder to implement for php, but i believe that it's easer (as a consumer of some code) to read the documentation, than make assumptions based on variable names their prefix.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Generally speaking, I do:
not prefix my private and my protected with _ as PHP5 has visibility declaration. Maybe in PHP 4, but not in 5.
not prefix my variable with a letter that represents it's type (array: a, integer: i, global: g, mixed: m) because PHP is loosely typed and what starts as an iVariable could just as easily become a sVariable.
use descriptive names: $userName for example instead of $name
not limit my variable name to a length of maximum 20 characters for the sake of short variable names. I keep them usable and clear, and if they get long they get long.
not prefix my private and my protected with _ as PHP5 has visibility declaration. Maybe in PHP 4, but not in 5.
not prefix my variable with a letter that represents it's type (array: a, integer: i, global: g, mixed: m) because PHP is loosely typed and what starts as an iVariable could just as easily become a sVariable.
use descriptive names: $userName for example instead of $name
not limit my variable name to a length of maximum 20 characters for the sake of short variable names. I keep them usable and clear, and if they get long they get long.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I'm with Everah, I do the third of the four above. I don't I need to repeat the points aboveEverah wrote:Generally speaking, I do:
not prefix my private and my protected with _ as PHP5 has visibility declaration. Maybe in PHP 4, but not in 5.
not prefix my variable with a letter that represents it's type (array: a, integer: i, global: g, mixed: m) because PHP is loosely typed and what starts as an iVariable could just as easily become a sVariable.
use descriptive names: $userName for example instead of $name
not limit my variable name to a length of maximum 20 characters for the sake of short variable names. I keep them usable and clear, and if they get long they get long.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Many frameworks follow #1 and prefix ?Jcart wrote:I'm with Everah, I do the third of the four above. I don't I need to repeat the points aboveEverah wrote:Generally speaking, I do:
not prefix my private and my protected with _ as PHP5 has visibility declaration. Maybe in PHP 4, but not in 5.
(#10850)
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: Coding standard
I change my conventions quite often...as I learn what works and what doesn't...fastfingertips wrote:Hello from some time i'm using a coding standard (my own version) when i'm developing PHP, some of idead were taken from other sources, anyway i end it with something that helps me to move on on my projects and also makes my code (in my opinion readable). But i have doubts regarding variable notation so any input on this (were i'm doing wrong or good is great).
What do [s]u[/s] you think about my style? How can i improve it?
- I prefix my private and my protected with _
I prefix my variable with a letter that represents it's type (array: a, integer: i, global: g, mixed: m) this was handy when i used web-services but also to read my code easier
I use descriptive names: $iUserName for example instead of $name
I limit my variable name to a length of maximum 20 characters
Prefixing privates with '_' is good practice. Not so important in PHP 5 now that it supports access control, but I still find it useful to indicate the function I"m accessing is private. I don't follow that convention for protected members and I *rarely* allow member variables anything but private access.
Prefixing variable names...I did...for ages...having experience in C/C++ prior to starting in PHP didn't help (old habits die hard). It's not *really* that critical in PHP as there is no "type" per se. Although it can help clear up your code a bit, in most cases, I try an keep the context of the function terse, so there is no need for extraneous prefixing, such as $aEmails or $UserName as they are implied by the function context.
As for limiting you names to less than 20 characters...I'm not sure I see why??
I'm not a big fan of massive variable names, because as I said above, the function sets the context for the variables, so even three character abbreviations still make sense...but restricting to any length...I dunno. Some of my function/method names get pretty long.
I personally try to avoid changing the type at runtime...because that can lead to some interesting bugs...so in that regard I can see prefixing a type as being handy...seeing as PHP doesn't inform you...you could write a script to lint-check your code and make sure certain variables of type 'Array' were not being reused later as type 'String'not prefix my variable with a letter that represents it's type (array: a, integer: i, global: g, mixed: m) because PHP is loosely typed and what starts as an iVariable could just as easily become a sVariable.
use descriptive names: $userName for example instead of $name
Cheers
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA