Page 2 of 3

Posted: Sun Apr 02, 2006 8:01 pm
by Ambush Commander
Sounds like your style is very similiar to mine...you must be a top notch developer?
I wish. I'm really just a random 15-year-old who codes in his free time.
For instance...I always use single character identifiers for loop indices, but I always start with i

For now on...I think I'm gonna start with a and each time a new one is needed I increment the ordinal value and go from there.

I have run into bugs when dealing with deeply nested for loops, etc...and re-using a counter variable...so this new convention would eliminate that all together...
That is an interesting problem. Convention says that you use i, j and then k for loops, and i is pretty much synonymous with loops nowadays, but I always think, "Hmm... what letter comes after k? A, b, c..." I would propose using that only for deeply nested loops. Keep i for the other ones.

Edit due to post phasing

I hate it when people omit braces like this:

Code: Select all

if ($condition)
    do_something();
I'm sorely tempted to...

Code: Select all

if ($condition)
    do_something();
    do_something_else();
Which isn't right. I'm fine with:

Code: Select all

if ($condition) do_something();
Because it's pretty obvious that there are no braces.

Re: Coding conventions

Posted: Sun Apr 02, 2006 8:03 pm
by alex.barylski
arborint wrote:
Hockey wrote:So I decided that I am going to sit down and write a formal document explaining how and why I use certain conventions when writing PHP code.
Oh no ... we can't stop him can we. ;)

I think the issue of formatting variables, constants, functions, class names, etc. in PHP is generally settled. Like it or not, most everyone is using something close to the style guidelines defined by PEAR. The new Zend Framework was probably the last chance for a change of course and they ended up just conforming. So at this point anyone varying much from PEAR style in PHP is doing so either out of ignorance or to make some sort of personal statement. That means either you have better things to do, are clueless, or an iconclast.
On the contrary... :?

I think you should read up on programming guidelines/conventions...cuz in just a few short hours I've discovered tons ;)

Cheers :)

Posted: Sun Apr 02, 2006 8:04 pm
by Ambush Commander
I think you should read up on programming guidelines/conventions...cuz in just a few short hours I've discovered tons
Well, the thing is that all of this is common knowledge. It's just not centralized.

(imagines a code conventions wiki)

Posted: Sun Apr 02, 2006 8:12 pm
by alex.barylski
Ambush Commander wrote:
Sounds like your style is very similiar to mine...you must be a top notch developer?
I wish. I'm really just a random 15-year-old who codes in his free time.
For instance...I always use single character identifiers for loop indices, but I always start with i

For now on...I think I'm gonna start with a and each time a new one is needed I increment the ordinal value and go from there.

I have run into bugs when dealing with deeply nested for loops, etc...and re-using a counter variable...so this new convention would eliminate that all together...
That is an interesting problem. Convention says that you use i, j and then k for loops, and i is pretty much synonymous with loops nowadays, but I always think, "Hmm... what letter comes after k? A, b, c..." I would propose using that only for deeply nested loops. Keep i for the other ones.

Edit due to post phasing

I hate it when people omit braces like this:

Code: Select all

if ($condition)
    do_something();
I'm sorely tempted to...

Code: Select all

if ($condition)
    do_something();
    do_something_else();
Which isn't right. I'm fine with:

Code: Select all

if ($condition) do_something();
Because it's pretty obvious that there are no braces.
"i" is only synoymous with arrays because it's an alias for index which is what your doing when you iterate an array - not that didn't know that but i felt my answer needed more depth ;)

There is no reason you couldn't start at a...infact...my argument suggests that might be a good idea...

Perhaps you could start at i and continue with j and so on...but sub-consciencously I would be worried about running out of letters and having to resort to "ii" or something silly...not likely to happen, but how un-natural would that be if it did? I think starting from "a" is a much better alternative :P

Only 15 eh? Cool...keep on givin'er dude!!! 8)

When I was a young pup about your age I was writing a flight planner (for MSFS) in C++ using OWL and loosing my hair and my mind from stress :P

Stay with PHP...much less tormenting!!! :lol:

Posted: Sun Apr 02, 2006 8:15 pm
by alex.barylski
Ambush Commander wrote:
Sounds like your style is very similiar to mine...you must be a top notch developer?
I wish. I'm really just a random 15-year-old who codes in his free time.
For instance...I always use single character identifiers for loop indices, but I always start with i

For now on...I think I'm gonna start with a and each time a new one is needed I increment the ordinal value and go from there.

I have run into bugs when dealing with deeply nested for loops, etc...and re-using a counter variable...so this new convention would eliminate that all together...
That is an interesting problem. Convention says that you use i, j and then k for loops, and i is pretty much synonymous with loops nowadays, but I always think, "Hmm... what letter comes after k? A, b, c..." I would propose using that only for deeply nested loops. Keep i for the other ones.

Edit due to post phasing

I hate it when people omit braces like this:

Code: Select all

if ($condition)
    do_something();
I'm sorely tempted to...

Code: Select all

if ($condition)
    do_something();
    do_something_else();
Which isn't right. I'm fine with:

Code: Select all

if ($condition) do_something();
Because it's pretty obvious that there are no braces.
I typically on single statement if's have the statement follow right behind the if...save line space :)

It appears you like that additional space between keywords and brackets?

I guess thats more personal taste cuz no argument is even available...except maybe if you write in notepad without colorizing...that space isn't needed :)

Posted: Sun Apr 02, 2006 8:18 pm
by Ambush Commander
It appears you like that additional space between keywords and brackets?

I guess thats more personal taste cuz no argument is even available...except maybe if you write in notepad without colorizing...that space isn't needed
Personal taste. I don't know why I do it that way, I just do. I don't even do that for functions().
I typically on single statement if's have the statement follow right behind the if...save line space
Allows for some nice formatting too:

Code: Select all

if (!$this->validateName($this->first_name)) $errors['first_name'] = 'Invalid name.';
        if (!$this->validateName($this->last_name))  $errors['last_name']  = 'Invalid name.';
        if (!is_a($this->email, 'Email'))            $errors['email']      = 'Programmer error.';
            elseif (!$this->email->isValid())        $errors['email']      = 'Invalid email.';
(the only problem with this is that it goes beyond 80.

Posted: Sun Apr 02, 2006 8:22 pm
by alex.barylski
Todd_Z wrote:I wonder how difficult it would be to write a script to check the coding convention for consistency throughout a script.

It would be interesting to run a script on a set of files and see how many errors show up.

This could be an invaluable tool for n00bs to help them either develop their own style, or to help them stay away from errors
Seeing how that would require preservation of white space...and a few other things...I'm to lazy to get into it right now, but...

Pretty difficult in PHP without some parsing framework...or library...put it that way :)

Cheers :)

Posted: Sun Apr 02, 2006 8:28 pm
by Christopher
Hockey, I think you are tilting at windmills. I honestly think the differences you are talking about are trivial. Any issues with tabs or line endings are dealt with by editors. I work with all sorts of code every day and difference in code density and capitalization that claim are "deeply not a settled" have little effect on mine or anybody's ability read the code.

Posted: Sun Apr 02, 2006 8:31 pm
by Christopher
Ambush Commander wrote:Convention says that you use i, j and then k for loops, and i is pretty much synonymous with loops nowadays, but I always think, "Hmm... what letter comes after k? A, b, c..." I would propose using that only for deeply nested loops. Keep i for the other ones.
Here is a question for the newbies: do you know where that convention comes from?

Posted: Sun Apr 02, 2006 8:31 pm
by alex.barylski
Ambush Commander wrote:
I think you should read up on programming guidelines/conventions...cuz in just a few short hours I've discovered tons
Well, the thing is that all of this is common knowledge. It's just not centralized.

(imagines a code conventions wiki)
Centralized??

Never will be...IMHO...

Context free languages would be impossible to impose such limitations on...

However there much formal discussion on the subject:

http://www.google.ca/search?hl=en&q=Cod ... ions&meta=

What I was after, was programmer opinions which differed my own, so I could write a formal document which I would follow and anyone i contract work out to would follow...

I was not looking to standardize the industry as arborint (i think) was suggesting...I was looking to standardize my own codebase... :)

As it stands, my code is fairly organized, but even after 20 years of development there is always room for improvement :)

Roja...in case your wondering why I'm so standards gun-ho all of a sudden :)

I always have been, when standards affected me directly...I advocate them religiously...but xhtml and css compliance don't affect me directly, thus my previous opinion on the matter was what it was, until you convinced me otherwise ;)

Cheers :)

Posted: Sun Apr 02, 2006 8:48 pm
by Gambler
I agree with arborint about conding conventions. They're not a big issue.
Here is a question for the newbies: do you know where that convention comes from?
I do not consider myself a newbie, but it's a tough question. My best guess would be that "i" is taken from sigma notation of Reimann sum.

Posted: Sun Apr 02, 2006 9:00 pm
by alex.barylski
arborint wrote:Hockey, I think you are tilting at windmills. I honestly think the differences you are talking about are trivial. Any issues with tabs or line endings are dealt with by editors. I work with all sorts of code every day and difference in code density and capitalization that claim are "deeply not a settled" have little effect on mine or anybody's ability read the code.
Tilting at windmills :lol:

Not sure what that is supposed to mean, but regardless I've never heard of that before and it made me laugh. :)

Not sure where you stand as far as experience is concerned, but if programming has taught me anything over the years, it's that, the more you know, the more you realize the less you know...

I'm always looking at ways to improve coding practice, development, etc...

In this instance, it's my personal coding standards...

Yes there are existing standards, but who says I have to like them? If I can do it better I will.

I don't re-invent the wheel unless I'm convinced the end result will be better or satisfies me more. In this case PHP coding standards and PEAR are poor examples of standards

Ever notice that PHP uses both:

Code: Select all

mysql_query()
AND
substr() or addslashes()
That hardly what I would call a optimal standard...

As for PEAR...I hardly use it...it's a mis-mash collection of classes written by individual groups...I looked into Auth before and was disappointed so I never looked into it again.

Thats hardly what I would call a complete/optimal standard...

They make basic suggestions and I guess everyone comments using phpDocumentor or Doxygen or whatever?

I document with phpDocumentor too :)

There class names look really unorganized...some class have spaces and captilize acronyms...

Auth_HTTP

and others don't appear too seperate words at all...

LiveUser

They should have added yet another namespace like:

CxPear

So every class would follow a convention like:

CxPearAuth_HTTP

Also, from what I can tell, there is no one single base class which every class MUST derive from...if I'm right...thats a big mistake for many reasons and a poor standard to follow



Cheers :)

Posted: Sun Apr 02, 2006 9:01 pm
by John Cartwright
I find naming conventions to be extremely important, and pretty much required when dealing with frameworks considering most often files/classes are expected to be of a certain format (ie. IndexController/NewsGateway/ProfileView). Even if this wasn't required, I find sticking to standards makes my programming a lot simpler to understand considering it is consistant. I'm sure I could make unique and decriptive class names that accomplish this as well, but down the road I find it slightly easier to stick to my standards. I bolded "my" because I want to emphasis that you do not have to follow some one elses standard. If you genuinly believe yours is superior for whatever reason, then by all means use it. Perhaps other people will tag along with you.

On the other hand, I am often forced to read other people's code. Overall, this is not pleasant for me, considering I do have my own standards are have slightly more dificulty understanding others code. Most code I've seen looks relatively like mine, but every now and then I come across a snipplet worth the daily WTF. My biggest pet pieve for readability is ironically too much whitespace. I prefer to condence my code nearly as much as I can. Perhaps this is to do the fact I run my laptop on 1280x800 so my vertical space is limited.

Morale of the story -- do what works for YOU.

Posted: Sun Apr 02, 2006 9:07 pm
by alex.barylski
Gambler wrote:I agree with arborint about conding conventions. They're not a big issue.
Here is a question for the newbies: do you know where that convention comes from?
I do not consider myself a newbie, but it's a tough question. My best guess would be that "i" is taken from sigma notation of Reimann sum.
Whoa!!! 8O

It's possible...I wonder...

I read in an old C K&R book that "i" was just an alias for "Index" as that is usually what "i" is doing... :)

If you find out lemme know ;)

As for conventions not being a big deal...perhaps not a big deal...but I hate looking at code written by a newbie...

I can't stand HTML and PHP intermingled...or code with poor formatting...

It also drive men nutts when functions or variables have different naming schemes...

Like the mysql_query() or addslashes()

One OR the other please...not both...

I frequently did (and sometimes still do) the add_slashes() thing because thats how I know PHP to work...then get an error at runtime because add_slashes() doesn't exist. :?

Cheers :)

Posted: Sun Apr 02, 2006 9:11 pm
by alex.barylski
Now were all totally off topic :(

Did I just complain about that? :)

Anyways, it's obvious there is no right or wrong answer and everyones opinion differs slightly from the rest...

But lets stick to specifics here people...I wanna know what works for you and why...if you can even justify your answer...

Not just...cause whitespace sux...or I prefer statment trailing comments as opposed to newline comments :)

Cheers :)