Page 1 of 1

Coding standard

Posted: Wed Dec 05, 2007 5:19 am
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?

Re: Coding standard

Posted: Wed Dec 05, 2007 6:28 am
by onion2k
fastfingertips wrote:I limit my variable name to a length of maximum 20 characters
I can't see any advantage in that.

Posted: Wed Dec 05, 2007 7:44 am
by feyd
Have you, ~fastfingertips, read previous threads on this subject? There's a rather long one created by ~Hockey.

Re: Coding standard

Posted: Wed Dec 05, 2007 7:51 am
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.

Posted: Wed Dec 05, 2007 9:47 am
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.

Posted: Wed Dec 05, 2007 11:22 am
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?

Re: Coding standard

Posted: Wed Dec 05, 2007 12:47 pm
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.

Posted: Wed Dec 05, 2007 12:49 pm
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.

Posted: Wed Dec 05, 2007 2:10 pm
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 :)

Posted: Wed Dec 05, 2007 2:15 pm
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 ?

Re: Coding standard

Posted: Wed Dec 05, 2007 2:46 pm
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 :)

Posted: Wed Dec 05, 2007 3:07 pm
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.