I posted this on another php forum recently and would like to see the results here on sitepoint.
I’ve been a delphi coder for years and when switching to php I noticed that the format by many coders is different. In delphi anything seems to go but in php I noticed people use the following format a lot:
A:
function name(){
function last(){
//Do something
}
}
I’ve always found that rather hard to read (EG It’s hard to identify which closing brace belongs to which opening brace when you have a lot of them).
I’ve always coded my code like this:
B:
function name()
{
function last()
{
//Do something
}
}
<?php
/**
* Class Description
*/
class MyClass extends YourClass {
const FOO = "foo";
const BAR = "bar";
private $variableName1;
protected $variableName2;
public $variableName3;
public $children;
/**
* Constructor
* @param $variableName1 int Description
* @param $variableName2 int Description
*/
public function __construct($variableName1, $variableName2 = 2) {
$this->variableName1 = $variableName1;
$this->variableName2 = $variableName2;
$this->children = new array();
}
/**
* This is my function description
* @return int Whatever
*/
protected function internalFunction() {
// Do Something Here
}
/**
* Adds a child item to the class
* @param $child variant Whatever
*/
public function addChild($child) {
array_push($this->children, $child);
}
/**
* Renders the output and prints to the page
*/
public function render() {
// Start with some basic output
$output = <<<HTML
<div>
<p>This is output attempt number {$this->variableName1} </p>
</div>
HTML;
// Print our foo/bar status
switch ($this->variableName2) {
case self::FOO:
$output .= "Some foo stuff";
break;
case self::BAR:
$output .- "Some bar stuff";
break;
default:
// No output
}
// Try to output children
try {
// Loop children and print each one
foreach ($this->children as $child) {
if (isset($child)) $output .= $child;
}
} catch (Exception $e) {
// Output error message
$output .= "oops!!! error!";
}
// Print output
print($output);
}
}
?>
I’m quite surprised because on the other site I ran this poll option B was slightly more preferred than option A.
I’ve always preferred B due to the ease of which you can see which code block you’re inside of and where the opening and closing braces are etc. The inline option A has always annoyed me as I’ve never been able to understand it properly.
With Option B and suitable indenting, you tend to have a clear line of sight from the start of a section its closing brace.
for ($i = 0; $i < $someLength; $i++) {
if ($i < $someValue) {
doSomething();
} else if ($i < $someOtherValue) {
doSomethingElse();
} else {
doSomeOther();
}
}
Option A tends to result in the code taking up too much vertical space. You’re good if you’re paid by the line of code, but going from 9 lines to 15 lines for the same code, what does that achieve?
for ($i = 0; $i < $someLength; $i++)
{
if ($i < $someValue)
{
doSomething();
}
else if ($i < $someOtherValue)
{
doSomethingElse();
}
else
{
doSomeOther();
}
}
I am tempted to inquire whether people who prefer the above code might require an eye-glass for proper reading.
But to be more politic about it, we all have our reasons as to why we prefer our own ways.
That’s because you inadvertently exploited the other forums’ post-parsing algorithm with just the right combination of code, memory, and timing necessary to throw one of the data mining bots from China into a Kafkaesque infinite voting loop…