Re: Where To Find Resources About Programming Style (my bleedin' code is so darn ugly)
| From: | Christian Blichmann | Date: | Sun, 17 Feb 2002 19:05:17 +0000 |
| Subject: | Re: Where To Find Resources About Programming Style (my bleedin' code is so darn ugly) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-85112@lists.php.net to get a copy of this message | ||
Hi there!
This is my suggestion for code formatting/commenting/etc.:
if ($var > 0 && ( // Shortcut, since your're not
calling anything
// that "does stuff" in
the conditional.
strlen($var) == strlen(intval($var)) || // String length is equal
or one less
strlen($var) - 1) == strlen(intval($var)) // than the length of its
integer value
)) {
do stuff; // Then: Do stuff...
}
Btw, there are canonical texts on this topic, see the PEAR (PHP
Extension and Application Repository) web site:
http://pear.php.net
http://pear.php.net/manual/en/standards.php
I am using the following indentation (as opposed to PEAR):
- One - and only one - tab 4 spaces wide for one level of
indentation.
- One tab througout the <?php ?>- tag.
- if-statements:
<?php
if (condition) one_statement();
if (condition)
one_statement();
else
other_statement();
if (condition)
one_statement();
elseif (condition) // or use: else if
other_statement();
else
anything();
if (condition) {
// Multiple Statements
}
if (condition) {
// Multiple Statements
} else {
// Multiple Statements
}
if (condition) {
// Multiple Statements
} else if (condition) {
// Multiple Statements
} else {
// Multiple Statements
}
?>
- One blank line between variable declarations / inclusions:
<?php
$a = "don't know";
function do_stuff() {
global $a;
// use $a;
}
?>
You should also have a look at the operator precedence (this can
be really annoying, but it saves you brackets - thus resulting in
cleaner code).
Its also always a good idea to comment your code (yes, that's
annoying, too, but saves you _much_ time when resuming your
work after a longer interruption / when working in a team). My
suggestions for comments are as follows:
<?php
/*
* Caption of a larger section or a function
*/
function do_stuff() {
echo "Now, this does stuff...";
}
?>
<?php
/* Multi-line comment to explain why you do certain things the
way you do them, if this belongs to a function call, then nicely
arrange it to 80 chars on the right, otherwise leave it
left-aligned
to the indentation.
*/
do_stuff();
do_more_stuff();
?>
<?php
// One line comment as the caption of a small code block
// that doesn't need comments in each line;
do_stuff();
do_more_stuff();
?>
<?php
do_stuff(); // Longest comment right-aligned to 80
chars |||
do_more_stuff(); // But: "//" should be in one column.
?>
Ok, sorry for this mega-posting, hope it helps.
P.S.: Your code _always_ works? -Wow...
--
Christian Blichmann
_____________________________________________
don't hesitate - email me with your thoughts:
e-mail: feedback.nospam@blichmann.de
- please remove the ".nospam" from address.
_____________________________________________
do you want to know more?
web: http://www.blichmann.de