Jump to content

coding standards


joquius

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
Link to comment
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.
Link to comment
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.
Link to comment
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 :\
Link to comment
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
Link to comment
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.
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.