Coding standard

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Coding standard

Post by fastfingertips »

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).
  • 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
What do [s]u[/s] you think about my style? How can i improve it?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Coding standard

Post by onion2k »

fastfingertips wrote:I limit my variable name to a length of maximum 20 characters
I can't see any advantage in that.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you, ~fastfingertips, read previous threads on this subject? There's a rather long one created by ~Hockey.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Coding standard

Post by superdezign »

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
That only seems useful (IMO) in languages that have strict-typing.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

#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)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Coding standard

Post by timvw »

superdezign wrote:
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
That only seems useful (IMO) in languages that have strict-typing.
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).

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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Everah 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.
I'm with Everah, I do the third of the four above. I don't I need to repeat the points above :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Jcart wrote:
Everah 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.
I'm with Everah, I do the third of the four above. I don't I need to repeat the points above :)
Many frameworks follow #1 and prefix ?
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Coding standard

Post by alex.barylski »

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).
  • 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
What do [s]u[/s] you think about my style? How can i improve it?
I change my conventions quite often...as I learn what works and what doesn't...

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.
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
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'

Cheers :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

arborint wrote:Many frameworks follow #1 and prefix ?
True. I just find it cleaner when I don't use them and instead rely on visibility declarations. That is just me.
Post Reply