Jump to content

Archived

This topic is now archived and is closed to further replies.

joquius

coding standards

Recommended Posts

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:

[code]
<?
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<?
}
?>
[/code]
ok so which do you despise and which do you use yourselves

Share this post


Link to post
Share on other sites
I always aim for clarity through variable names and documentation (comments), unless the script is very temporary.

Share this post


Link to post
Share on other sites
well let say you have

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--quoteo(post=383844:date=Jun 14 2006, 10:04 AM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 14 2006, 10:04 AM) [snapback]383844[/snapback][/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:

[code]
<?
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<?
}
?>
[/code]
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
[code]
if (blah) {
   blahblah;
} elseif (blahblah) {
   blahblah;
} else {
  blah;
}
[/code]
that's the style my comp sci teacher taught long long time ago and it stuck :\

Share this post


Link to post
Share on other sites
[!--quoteo(post=383859:date=Jun 14 2006, 04:34 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 14 2006, 04:34 PM) [snapback]383859[/snapback][/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

Share this post


Link to post
Share on other sites
[!--quoteo(post=383851:date=Jun 14 2006, 10:14 AM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 14 2006, 10:14 AM) [snapback]383851[/snapback][/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.

Share this post


Link to post
Share on other sites
: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:
[code]
//i prefer this
if(3 == $value)

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

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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.