Code in a way that helps others to understand your code. Any fool can write code that a computer can understand. Good programmers write code that humans can understand.(Martin Fowler)
Since CMSimple_XH 1.6 the pear coding standards have been adopted for the core. As of XH 1.7 the core has switched to PSR-2 which is more appropriate for contemporary code. Extension writers may consider adopting either of these coding standards also, but of course that is no requirement.
Because some people get religious when talking about their coding style, don't take this list as something like the Ten Commandments, but just as ten suggestions to increase readibility of your plugins and addons.
// bad (concerning point #2 as well): if ($t == '')e('undefined', 'file', $fl); // better: if ($temp == ''){ error_message('undefined', 'file', $file); } // or: if ($temp == '') { error_message('undefined', 'file', $file); }
//bad: echo "<a href=\"http://www.cmsimple-xh.org\" title=\"CMSimple_XH\">CMSimple_XH</a>"; //better: echo '<a href="http://www.cmsimple-xh.org" title="CMSimple_XH">CMSimple_XH</a>';
// bad: function some_function($foo,$bar){ if($foo==bar){ return $foo+$bar; } } // better: function some_function($foo, $bar){ if($foo == bar){ return $foo + $bar; } } // or: function some_function($foo, $bar) { if($foo == bar) { return $foo + $bar; } }
If there are a lot of parameters or conditions use linebreaks and indentation.
// bad: if(isset($foo) && $foo > 1 && $foo < 45 && $bar == true){ //... } // better: if( isset($foo) && $foo > 1 && $foo < 45 && $bar == true ) { //... }
// bad: $foo = explode('§', preg_replace("/(<h[1-".$cf['menu']['levels']."][^>]*>)/i", "§\\1", str_replace('§', '§', rf($pth['file']['content'])))); // better: $foo = rf($pth['file']['content']); $foo = str_replace('§', '§' $foo); $foo = preg_replace('/(<h[1-' . $cf['menu']['levels'] . '][^>]*>)/i', '§\\1', $foo); $foo = explode('§', $foo);
While writing short lines place the concatenation dot in multiple lines exactly under the “=“
// bad: $foo = $foo1.$foo2.$foo3.(my_super_cool_calculation).etc; // better: $foo = $foo1 . $foo2 . $foo3 . (my_super_cool_calculation) . etc;
class Toy { //... }