Jump to content


Photo

coding standards


  • Please log in to reply
8 replies to this topic

#1 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 03:04 PM

I've been battling between going for super neat script and super short script for a long time and I can't seem to see which is the better of the 2. Here's a situation:

<?
if ($a == 1)
{
  $a = ($b == 1) ? 2 : 3;
  if ($b == 3)
  {
    if ($c == 1) { $b = 1 } else { $b = 4 }
  }
  if ($a == $b - 1) echo "blah"; else echo "bleh";
}
else
{
  ?>you're insane $a isn't real<?
}
?>
ok so which do you despise and which do you use yourselves
z..z..z..z..z..z..z..z..

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 14 June 2006 - 03:11 PM

I always aim for clarity through variable names and documentation (comments), unless the script is very temporary.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 03:14 PM

well let say you have

if (mysql_num_rows ($result) != 0)
{
echo "<table>"; // or ?><table><?, at what stage is it better to abandon php
...
}
z..z..z..z..z..z..z..z..

#4 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 14 June 2006 - 03:20 PM

I tend to write with phpDocumentor in my mind, so I code with a lot comments and make my variable and function names as clear as possible; like effigy said this would only be for code I expect to stay around. In terms of readability I try to keep a lot of white space around where I can. That means indenting, obviously, and also a line break between functions if there aren't any comments between them. I feel like you need something to make it clear where an if clause or function bracket ends without having to simply guess from indents.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#5 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 14 June 2006 - 03:34 PM

joquius, looking at again at your posts I think I might have misunderstood you, or at least I have a different answer. For printing, which it looks like you're doing in your two examples, I use a special object which controls the smarty templating object. It is a good idea to keep the visual logic separated from the business logic, at least for maintenance reasons. If you don't now what smarty is, it's worth checking out as it is really quite easy:
[a href=\"http://www.smarty.php.net/\" target=\"_blank\"]http://www.smarty.php.net/[/a]
Otherwise I suggest you create as many .html files as you can to include upon error or whatever other message you want to generate. It may also be worthwhile creating a class for this that can coordinate which files to include and what to print after say the include(header). That way you can at least have things slightly more organized.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 14 June 2006 - 04:37 PM

[!--quoteo(post=383844:date=Jun 14 2006, 10:04 AM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 14 2006, 10:04 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I've been battling between going for super neat script and super short script for a long time and I can't seem to see which is the better of the 2. Here's a situation:

<?
if ($a == 1)
{
  $a = ($b == 1) ? 2 : 3;
  if ($b == 3)
  {
    if ($c == 1) { $b = 1 } else { $b = 4 }
  }
  if ($a == $b - 1) echo "blah"; else echo "bleh";
}
else
{
  ?>you're insane $a isn't real<?
}
?>
ok so which do you despise and which do you use yourselves
[/quote]
It depends on the situation. If my condition is a simple if/else with one thing being done, i'll go for the ternary. also, i usually put the opening quotes on the same line as the if/else
if (blah) {
   blahblah;
} elseif (blahblah) {
   blahblah;
} else {
  blah;
}
that's the style my comp sci teacher taught long long time ago and it stuck :\
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 05:13 PM

[!--quoteo(post=383859:date=Jun 14 2006, 04:34 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 14 2006, 04:34 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
joquius, looking at again at your posts I think I might have misunderstood you, or at least I have a different answer. For printing, which it looks like you're doing in your two examples, I use a special object which controls the smarty templating object. It is a good idea to keep the visual logic separated from the business logic, at least for maintenance reasons. If you don't now what smarty is, it's worth checking out as it is really quite easy:
[a href=\"http://www.smarty.php.net/\" target=\"_blank\"]http://www.smarty.php.net/[/a]
Otherwise I suggest you create as many .html files as you can to include upon error or whatever other message you want to generate. It may also be worthwhile creating a class for this that can coordinate which files to include and what to print after say the include(header). That way you can at least have things slightly more organized.
[/quote]I hear you on this, regarding the html includes.
I usually try to acheive that, but fall short in the process. I suppose it's because I never really write any code which has multiple use (most my code only appears once) which means no reason for too many functions, and no classes at all.
For instance I have a forum code [a href=\"http://theflamingfist.com/forum.php\" target=\"_blank\"]this one[/a], which has absolutely no distinction between code and html because I couldn't be bothered to get into that mess. Doesn't mean I don't try :P
z..z..z..z..z..z..z..z..

#8 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 14 June 2006 - 05:20 PM

[!--quoteo(post=383851:date=Jun 14 2006, 10:14 AM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 14 2006, 10:14 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
well let say you have

if (mysql_num_rows ($result) != 0)
{
echo "<table>"; // or ?><table><?, at what stage is it better to abandon php
...
}
[/quote]

For this example I would go with echo '<table>';

1. If it's small, keep it simple. You're not echoing out a lot of text, and the text has nothing special happening, i.e., variable interpolation.
2. It's cleaner than having a bundle of ?> and <?php tags throughout your code.
3. I think it's more friendly to the eye in editors that use syntax highlighting.

Some other things:

1. I would use <?php instead of <?.
2. I use single quotes where I don't need interpolation, and double when I do. This may be nitpicky, but I see it as more disciplined and self-documenting.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#9 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 14 June 2006 - 06:52 PM

:edit:
sorry, i just noticed effigy has already mentioned two of my points above.
:/edit:


i think it was UNIX and Slackware that go with the philosophy: KISS = Keep It Simple Stupid :)



out of your example, i don't like:
1. PHP short tag for compatibility reasons.

2. i avoid having the value in an if condition on the right hand side in the event that i use a = instead of a == which i always do by mistake and wonder why my condition is always true :/
to illustrate:
//i prefer this
if(3 == $value)

//i don't like this
if($value == 3)

3. i hate breaking out of the PHP tags. i avoid it where possible. sometimes, i even use the heredoc syntax

4. i avoid double quotes and use single quotes as much as possible. i only use doublt quotes if i'm outputting a string literal along with a variable.


i'm neutral about the ternary operator. i think it has its uses in some places but overusing it would just make illegible code.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users