Re: Proper code formatting
| From: | Shawn McKenzie | Date: | Mon, 23 Mar 2009 15:24:34 +0000 |
| Subject: | Re: Proper code formatting | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-290600@lists.php.net to get a copy of this message | ||
George Larson wrote:
> On Mon, Mar 23, 2009 at 8:23 AM, Shawn McKenzie <nospam@mckenzies.net>wrote:
>
>> Bob McConnell wrote:
>>> From: Michael A. Peters
>>>> Angus Mann wrote:
>>>>> Hi all, I'm fairly new to PHP so I don't have too many bad habits
>>> yet.
>>>>> I'm keen to make my code easy to read for me in the future, and for
>>>>> others as well.
>>>>>
>>>>> Are there any rules or advice I can use for formatting (especially
>>>>> indenting) code?
>>>>>
>>>>> for example how best to format / indent this ?
>>>>>
>>>> To each his own. Whatever floats your canoe.
>>>> Just whatever you pick, stick to it throughout your code.
>>>>> I'm using "PHP designer 2008" which does syntax coloring but if
>>>>> it
>>> has
>>>>> something to automatically indent - I haven't found it yet.
>>>> It probably allows you to either set a specify a tab as a real tab or
>>> a
>>>> specified number of spaces. Auto-indenting - this isn't python, the
>>>> compiler doesn't enforce it's way, you follow the convention of the
>>>> project you are working on - so I suspect many php editors tailored to
>>>> php don't have an auto indent.
>>>>
>>>> I've never of course tried that specific product. I use bluefish, vi,
>>>> and emacs.
>>> To take this question a step further, is there a PHP best practices
>>> document available? I am looking for one that I can give to a new
>>> programmer and tell her "do it this way until you can explain to me why
>>> you shouldn't."
>>>
>>> Bob McConnell
>> There are various coding standards. There is one for PEAR, the Zend
>> Framework and most frameworks/large projects that take contributions
>> have them. Here's Zend:
>>
>> http://framework.zend.com/manual/en/coding-standard.html
>>
>> --
>> Thanks!
>> -Shawn
>> http://www.spidean.com
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> Being a greenhorn, I too can benefit from this thread.
>
> Is that to say, Shawn, that you personally find this (Zend) standard as good
> or better than the rest?
>
Many of them are very similar, I just picked Zend as an example because
it is well known and they are the "PHP" company :-) I tend to follow
many of the common standards but not any one in particular. I am not a
professional developer and don't work with others really so its not as
big of a deal. If you are working with others then I see that it is
important first and foremost to have some consistency and readability.
Some things are more personal preference and some are meant to make the
code readable/portable. For example, the spaces vs. tabs is important
because some systems/editors/IDEs display tabs differently, but 4 spaces
should be 4 spaces almost everywhere. I use CakePHP mostly and try and
follow they way it is coded so that it is consistent, but I flip flop on
some things for no apparent reason sometimes without even realizing it.
* I used to do this:
function somefunc() {
//something
}
* and
if ($something) {
//do something
}
else {
//do something else
}
* Now I do this:
function somefunc()
{
//something
}
if ($something) {
//do something
} else {
//do something else
}
--
Thanks!
-Shawn
http://www.spidean.com