Jump to content

Recommended Posts

What's your coding style, or form?

 

Here are some examples grabbed off Wikipedia:

if (hours < 24 && minutes < 60 && seconds < 60)
{
    return true;
}
else
{
    return false;
}

if (hours < 24 && minutes < 60 && seconds < 60) {
    return true;
} else {
    return false;
}

if  (    hours<
24  && minutes<
60  && seconds<
60  )
{return    true
;}         else
{return   false
;}

 

 

Using that kind of example, my coding style/form is:

if (hours<24 && minutes<60 && seconds<60) {
  return true;
}
else {
  return false;
}

 

And with echos I do echo("text"); instead of echo "text";.

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/
Share on other sites

  • Replies 76
  • Created
  • Last Reply

Top Posters In This Topic

I use the 2nd one. Or rather, that's the closest one. Here's how I would write it:

 

// comment about what I want to be true
if ((hours < 24) && (minutes < 60) && (seconds < 60)) {
    // comment about what happens if true
    return true;
// comment about if it's false
} else {
    // comment about what happens if false
    return false;
} // end if 

 

I followed Daniel's link and it looks like I follow about 95% (give or take) of their coding standard.  Or rather, they follow me, seeing as how I've been writing code for much longer than they've been around ;)

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-569117
Share on other sites

I used to code like this:

 

if((hours < 24) && (minutes < 60) && (seconds < 60)){
    return true;
} 
else {
    return false;
}

 

But since a month or so we've got an an official coding standard at work, resulting in me adopting this style:

 

if((hours < 24) && (minutes < 60) && (seconds < 60))
{
    return true;
} 
else 
{
    return false;
}

 

It took some getting use to, but now I do think it is the most readable.

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-569182
Share on other sites

I could *probably* live with doing it this way:

// comment about what I want to be true
if((hours < 24) && (minutes < 60) && (seconds < 60))
{ // comment about what happens if true
    return true;
} // comment about what I want to be false
else 
{ // comment about what happens if false
    return false;
} // end if..else

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-569251
Share on other sites

I usually do this:

 

if(cond1 == true && cond2 == true && number > number2) {

}
else {

}

 

No commenting at all ;p.  I comment the tops of pages sometimes, but...  I'm not much of a commenter....

 

I follow a little bit of the ZF standards....  But, some of it I refuse to follow.  (I like my tabs, for example, dammit!)

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-569301
Share on other sites

I also use:

 

<?php

if (hours < 24 && minutes < 60 && seconds < 60)
{
return true;
}
else
{
return false;
}

?>

 

 

But only when the block contains more than one line.

For an example, I do:

 

<?php

for($i = 0; $i < 7; $i++)
if($i % 2 == 0)
	echo $i."<br>";

//And

while($i < 7)
{
if($i % 2 == 0)
	echo $i."<br>";
$i++;
}

?>

 

 

Orio.

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-569430
Share on other sites

Now I'm trying to do it more like this:

//Checks if hours is less than 24, minutes less than 60, and seconds less than 60.
if (hours<24 && minutes<60 && seconds<60) {
return true; //Returns true if the above statement is true.
}
else {
return false; //Returns false if the above statement is false.
}

 

Using tab makes it easier to intend for me. Comments I'm not really into the habit still.

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-569909
Share on other sites

Well I use coding style 2 since i find it takes up less space and makes the code seem less confusing.

if((hours < 24) && (minutes < 60) && (seconds < 60)){
    return true;
}else{
    return false;
}

 

Oh yes and also I only indent when im using dreamweaver (thats about 95% of the time). Before i started using actionscript i wouldn't indent my php at all!

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-571468
Share on other sites

My style

 

<?php
if(($hours < 24) && ($minutes < 60) && ($seconds < 60)) return true;
return false
?>

 

As for multiline things (I am not a fan of else):

 

<?php

class MyClass {
    private $foo = null;
    
    public function __construct() {
        $this->foo = "constructed";
    }

    public function getFoo() {
        if ($this->foo != null) return foo;
        // handle the 'else' statement here.
    }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-574673
Share on other sites

my coding style when i am just doing it for the fun of it i code like this...

<?php if ($blah== $blahh){ ?> what ever you would echo<?php }else{ ?> whatever <?php } ?>

i use var's more than i need too also

 

but i do every thing wierd lol when i lisson to music in head phones i only have one speaker in myear prolly because i dj as a hobby

 

and if am not using firefox i cant spell :P

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-574827
Share on other sites

I guess you didn't use firefox for that post..

 

As for multiline things (I am not a fan of else):

 

Why not? I mean, you never know what interesting ways people will come up with to circumvent your code, so wouldn't having a "catch-all" or "catch-all-the-rest" type thing be the best thing you can have for that?

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-575005
Share on other sites

I guess you didn't use firefox for that post..

 

As for multiline things (I am not a fan of else):

 

Why not? I mean, you never know what interesting ways people will come up with to circumvent your code, so wouldn't having a "catch-all" or "catch-all-the-rest" type thing be the best thing you can have for that?

 

I do.

 

If my if statement returns something, else will never get executed if the condition is true, so.. 'else' is really what happens If my if statement did not execute ;)

 

Generally, though, I try to keep conditional logic to a minimum. I like to keep my code as straight forward as possible.

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-575477
Share on other sites

I used to code like:

function myFunction(){
   if($me == "awesome"){
      echo "Damn straight.";
   }
   else{
      echo "You suck.";
   }
}

 

In the last month or so, I've changed to:

function myFunction()
{
   if($me == "awesome")
   {
      echo "Damn straight.";
   }
   else
   {
      echo "You suck.";
   }
}

 

It seems more readable to me, especially when using something like Notepad++ which highlights matching braces.

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-575906
Share on other sites

  • 2 weeks later...

function myFunction()
{
   if($me == "awesome")
   {
      echo "Damn straight.";
   }
   else
   {
      echo "You suck.";
   }
}

I hate making my code like that because i read an optimization article that if you put stuff very close together e.g.

if ($me == "awesome"){ echo "hi"; }else{ echo "you suck"; }

then the server will be able to run the code much faster. or so ive read.

Link to comment
https://forums.phpfreaks.com/topic/110866-coding-style/#findComment-586482
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.