RE: [PHP] PHP Standard style of writing your code
| From: | Ford, Mike | Date: | Tue, 02 May 2006 13:03:19 +0000 |
| Subject: | RE: [PHP] PHP Standard style of writing your code | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-235259@lists.php.net to get a copy of this message | ||
On 02 May 2006 10:07, Barry wrote:
> Richard Lynch schrieb:
> > On Tue, May 2, 2006 3:02 am, Barry wrote:
> > > Rafael schrieb:
> > > > IMHO, vertical aligned brackets can be messy when nesting
> > > > relatively-small blocks (and seems to me like a lot of
> wasted space)
> > > Huh?!
> > > Show an example. I don't think you will be able to show one.
> > > But this wasted space gives you a lot more insight into the code
> > > when looking for loops,whiles,functions and such.
> >
> > if (...)
> > {
> > if (...)
> > {
> > if (...)
> > {
> > //blah
> > }
> > //blah
> > }
> > //blah
> > }
> >
> Sorry, where is this here better?
> if (...) {
> if (...) {
> if (...) {
> // blah
> }
> //blah
> }
> //blah
> }
Very nice. Lovely, readable, compact code. (Apart form all those horrible curly brackets.)
> And then it goes like:
> if (...) {
> if (...) {
> // mmh
> } else {
> // oh
> }
> while (...) {
> if(...) {
> // oh
> } else {
> if (...) {
> // where am i?
> } else {
> // huh!?
> }
> // hmm
> }
> if (...) {
> if (...) {
> // blah
> }
> // blah
> }
> // blah
> }
Yes - still lovely, readable, compact code. And dead simple to spot you've failed to indent
the "else" section after "// where am i?". (Apart from all those horrible curly
brackets.)
> The same as above in the other style:
> if (...)
> {
> if (...)
> {
> //mmh
> }
> else
> {
> //oh
> }
> while (...)
> {
> if (...)
> {
> //oh
> }
> else
> {
> if (...)
> {
> //where am i?
> }
> else
> {
> //huh!?
> }
> // hmm
> }
> if (...)
> {
> if (...)
> {
> // blah
> }
> // blah
> }
> // blah
> }
Uurgh -- curly brackets -- I see curly brackets -- and, aargh, horrible curly brackets -- eeew, and
I think some code, maybe -- but definitely curly brackets.
> Your style:
> function findetag() {
> $wochentag = func_get_arg (0);
> if (func_num_args() >= 2) {
> $letztezeit = func_get_arg (1);
> } else {
> $letztezeit = time();
> }
> $count=0;
> $taggefunden = false;
> while (!$taggefunden) {
> $count++;
> $tagarray = getdate($letztezeit);
> if (strtolower($wochentag) ==
> strtolower($tagarray["weekday"])) {
> $taggefunden = true;
> } else {
> // tag drauf
> $letztezeit = $letztezeit+86400;
> }
> //endlosschleife abbrechen
> if ($count > 20) {
> $taggefunden = true;
> $letztezeit = false;
> }
> }
> return $letztezeit;
> }
Yep, nice -- still too many curly brackets, but readable.
> My style:
> function findetag()
> {
> $wochentag = func_get_arg (0);
> if (func_num_args() >= 2)
> {
> $letztezeit = func_get_arg (1);
> }
> else
> {
> $letztezeit = time();
> }
> $count=0;
> $taggefunden = false;
> while (!$taggefunden)
> {
> $count++;
> $tagarray = getdate($letztezeit);
> if (strtolower($wochentag) ==
> strtolower($tagarray["weekday"]))
> {
> $taggefunden = true;
> }
> else
> {
> // tag drauf
> $letztezeit =
> $letztezeit+86400;
> }
> //endlosschleife abbrechen
> if ($count > 20)
> {
> $taggefunden = true;
> $letztezeit = false;
> }
> }
> return $letztezeit;
> }
Oh my god -- curly brackets and excessive indentation -- and curly brackets.. Just a mo, where did
I put my curly-brackets-and-whitespace-glasses? Aaaahhh, that's better!!
As you might have guessed, I *HATE* curly brackets with a vengeance, which is why I eschew both
those styles and use PHP's alternative syntax:
if (...):
if (...):
// mmh
else:
// oh
endif;
while (...):
if (...):
// oh
else:
if (...):
// where am i?
else:
// huh!?
endif;
// hmm
endif;
if (...):
if (...):
// blah
endif;
// blah
endif;
// blah
endif;
Just beeeautiful! And, oh look, all the end tags tell me which kind of start tag they should match.
And the compiler. Which leads to much more focussed error messages when you cock your structure
up. I can't remeber the last time PHP told me I had a syntax arror at the end of the file --
if I've got unbalanced start/end tags, the error message usually points to a line in the middle
of the file, slap bang in the target area.
OK, heretic advocacy over now. I'll just crawl back under my rock and kill a few more curly
things...! ;)
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm