You are here: start » coding_style

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

coding_style [2017/11/01 15:22]
127.0.0.1 external edit
coding_style [2018/10/26 15:33]
Line 1: Line 1:
-====== Coding Style ====== 
  
-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.//​([[http://​www.amazon.com/​Refactoring-Improving-Design-Existing-Code/​dp/​0201485672|Martin Fowler]]) ​ 
- 
-Since CMSimple_XH 1.6 the [[http://​pear.php.net/​manual/​en/​standards.php|pear coding standards]] have been adopted for the core. Extension writers may consider adopting 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. 
- 
-  - Alway **use <?php ?>,** never the shorthands <? ?> or <?= ?>. 
-  - Choose **understandable names** for functions and variables and avoid using new global variables as far as possible. And prefix your functions with e.g. the plugin'​s name (because we can't use namespaces for compatibility with PHP < 5.3).  
-  - **Indent your code** with 4 spaces or tabs 4 spaces long in a logical manner. 
-  - It is recommended to use **curly brackets,** even if they are not required. <code php> 
-// 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); 
-} 
- 
-</​code>​ 
-  - Use **single quotes**, at least in case when there is nothing to evaluate in the string. <code php> 
-//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>';​ 
-</​code> ​ 
-  - Separate parameters and operators with **spaces** <code php> 
-// 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; 
-    } 
-} 
-</​code>​If there are a lot of parameters or conditions **use linebreaks** and indentation. <code php> 
-// bad: 
-if(isset($foo) && $foo > 1 && $foo < 45 && $bar == true){ 
-    //... 
-} 
-// better: 
-if(    isset($foo) 
-    && $foo > 1 
-    && $foo < 45 
-    && $bar == true 
-) { 
-    //... 
- 
-</​code>​ 
-  - **Do not nest** too many things, and write **short code lines** (max. 80 characters is usually recommended). <code php> 
-// bad: 
-$foo = explode('​§',​ preg_replace("/​(<​h[1-"​.$cf['​menu'​]['​levels'​]."​][^>​]*>​)/​i",​ "​§\\1",​ str_replace('​§',​ '&#​167;',​ rf($pth['​file'​]['​content'​]))));​ 
- 
-// better: ​ 
-$foo = rf($pth['​file'​]['​content'​]);​ 
-$foo = str_replace('​§',​ '&#​167;'​ $foo); 
-$foo = preg_replace('/​(<​h[1-'​ 
-     . $cf['​menu'​]['​levels'​] 
-     . '​][^>​]*>​)/​i',​ '​§\\1',​ $foo); 
-$foo = explode('​§',​ $foo); ​   
-</​code>​While writing short lines place the concatenation **dot** in multiple lines exactly **under the "​="​**<​code php> 
-// bad: 
-$foo = $foo1.$foo2.$foo3.(my_super_cool_calculation).etc;​ 
- 
-// better: ​ 
-$foo = $foo1 
-     . $foo2 
-     . $foo3 
-     . (my_super_cool_calculation) 
-     . etc; 
-</​code> ​ 
-  - **Function names** are better too **long** than too short. A descriptive phrase as function name makes understanding the code easier. Start with **lower case letter** and separate words using camelBack or underscores. 
-  - **Class definitions** should start with **uppercase** and have a linebreak before the opening bracket. <code php> 
-class Toy 
-{ 
-   //... 
-} 
-</​code>​ 
-  - **Indent** the assignments **in arrays.** <code php> 
-// bad: 
-array('​drink'​ => '​coffee',​ '​do_not_watch'​=>'​tv','​eat'​=>'​bread'​);​ 
- 
-// better: 
-array( 
-    '​drink' ​       => '​coffee',​ 
-    '​do_not_watch'​ => '​tv',​ 
-    '​eat' ​         => '​bread',​ 
-); 
-</​code>​ 
 
You are here: start » coding_style
Except where otherwise noted, content on this wiki is licensed under the following license: GNU Free Documentation License 1.3
Valid XHTML 1.0 Valid CSS Driven by DokuWiki