{"id":155,"date":"2016-08-12T09:32:32","date_gmt":"2016-08-12T08:32:32","guid":{"rendered":"http:\/\/beta.devon.gov.uk\/styleguide\/?page_id=155"},"modified":"2016-08-12T09:32:32","modified_gmt":"2016-08-12T08:32:32","slug":"formatting-code","status":"publish","type":"page","link":"https:\/\/www.devon.gov.uk\/standards\/overview-coding-standards\/formatting-code\/","title":{"rendered":"Formatting code"},"content":{"rendered":"<h2>Formatting<\/h2>\n<h3>Annotations<\/h3>\n<p>All functions and methods should be annotated. Visit <a href=\"https:\/\/beta.devon.gov.uk\/styleguide\/overview-coding-standards\/comments-and-annotations\/\" rel=\"noopener\">Comments and Annotations<\/a> for more details on this topic.<\/p>\n<h3>Indentation<\/h3>\n<p>All code needs to be indented in multiples of four spaces, using the tab key not the space bar. This is to ensure consistency across IDEs and text editors. If the code spans multiple lines, make sure that the line is dropped and then indented.<\/p>\n<pre class=\"EnlighterJSRAW\">&lt;?php echo 'Bar'; ?&gt;\n\n&lt;?php\n    $foo = 'Foo';\n    echo $foo;\n?&gt;\n\n&lt;?php\n\u00a0\u00a0\u00a0 function() {\n        \/\/ some code\n    }\n?&gt;<\/pre>\n<h3>Spacing<\/h3>\n<h4>Breathing room in and around functions\/methods\/classes<\/h4>\n<p>Before and after functions\/methods\/classes, there should be at least one empty line. Inside of functions\/methods\/classes, there should be an empty line before the closing curly brace and after the opening curly brace.<\/p>\n<p>Bad practice:<\/p>\n<pre class=\"EnlighterJSRAW\">function\u00a0foo( $bar ){ \/\/\u00a0some\u00a0code }\nfunction\u00a0foo2( $bar, $bar2 ) {\n\u00a0\u00a0\u00a0 \/\/\u00a0some\u00a0code}\nif( true ){\/\/ run something\n}\nClass Foo(){\/\/ some code}<\/pre>\n<p>Good practice:<\/p>\n<pre class=\"EnlighterJSRAW\">function foo( $bar ) {\n\n    \/\/ some code\n\n}\n\nfunction foo2( $bar, $bar2 ) {\n\n    \/\/ some code\n\n}\n\nif\u00a0( true )\u00a0{\n\n    \/\/\u00a0run\u00a0something\n\n}\n\nClass\u00a0Foo() {\n\n    \/\/\u00a0some\u00a0code\n\n}<\/pre>\n<h4>Parsed arguments and conditional statements<\/h4>\n<p>After the opening and closing parentheses, if there is an argument passed in, add a space to act as padding for the arguments. If there are multiple arguments, add a space after each separating comma. The opening curly braces should be on the same line as the function\/conditional statement that requires them with a leading space.<\/p>\n<p>Bad practice:<\/p>\n<pre class=\"EnlighterJSRAW\">function\u00a0foo($bar){\n\n\u00a0\u00a0\u00a0 \/\/\u00a0some\u00a0code\n\n}\n\nfunction foo2($bar,$bar2)\n{\n\n\u00a0\u00a0\u00a0\u00a0\/\/\u00a0some\u00a0code\n\n}\n\nif(true){\n\n    \/\/ run something\n\n}\n\nfunction no_args( ){\n\n\u00a0\u00a0\u00a0\u00a0\/\/\u00a0run\u00a0something\n\n}<\/pre>\n<p>Good practice:<\/p>\n<pre class=\"EnlighterJSRAW\">function foo( $bar ) {\n\n    \/\/ some code\n\n}\n\nfunction foo2( $bar, $bar2 ) {\n\n    \/\/ some code\n\n}\n\nif\u00a0( true )\u00a0{\n\n\u00a0\u00a0\u00a0 \/\/\u00a0run\u00a0something\n\n}\n\nfunction\u00a0no_args(){\n\n\u00a0\u00a0\u00a0\u00a0\/\/\u00a0run\u00a0something\n\n}<\/pre>\n<h2>Coding principles<\/h2>\n<h3>jQuery no conflicts<\/h3>\n<p>All jQuery code must be wrapped in a no conflict. Examples of these are:<\/p>\n<pre class=\"EnlighterJSRAW\">( function( $ ) {\n\n    \/\/ Code goes here, using the jQuery $\n\n} )( jQuery );\n\njQuery( function( $ ) {\n\n    \/\/ Code goes here, using the jQuery $\n\n} );\n<\/pre>\n<h3>OOP (Object Orientated Programming)<\/h3>\n<p>Any custom plugins or extensions, written in either PHP or JavaScript, should be written following the OOP principles.<\/p>\n<p>Even though OOP can seem excessive, this is to make sure that anything written fits in to the principles of being:<\/p>\n<ol>\n<li>Readable<\/li>\n<li>Scalable<\/li>\n<li>Maintainable<\/li>\n<\/ol>\n<p>An example of a non-OOP code block:<\/p>\n<pre class=\"EnlighterJSRAW\">function sliced_add_funds( $balance, $amount ) {\n\n  $new_balance = $balance + $amount;\n  return $new_balance\n\n}\n\n$balance = sliced_add_funds( 5, 25 );\necho $balance \/\/ prints 30 to the screen\n<\/pre>\n<p>and the same code, in OOP:<\/p>\n<pre class=\"EnlighterJSRAW\">class\u00a0Account\u00a0{\n\n\u00a0\u00a0\u00a0\u00a0private\u00a0$_balance;\n\n\u00a0\u00a0\u00a0\u00a0function\u00a0__construct(\u00a0$balance\u00a0)\u00a0{\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;set_balance(\u00a0$balance\u00a0);\n\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0 public\u00a0function\u00a0set_balance()\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0$this-&gt;_balance;\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0get_balance()\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0$this-&gt;_balance;\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0add_funds(\u00a0$amount\u00a0)\u00a0{\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$old_balance\u00a0=\u00a0$this-&gt;get_balance();\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$new_balance\u00a0=\u00a0$old_balance\u00a0+\u00a0$amount;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;set_balance(\u00a0$new_balance\u00a0);\n\n\u00a0\u00a0\u00a0\u00a0}\n}\n\n$account =\u00a0new\u00a0Account(\u00a05\u00a0);\n$account-&gt;add_funds(\u00a025\u00a0);\n\necho\u00a0(\u00a0$account-&gt;get_balance()\u00a0); \/\/ prints 30 to the screen\n<\/pre>\n<h3>Single responsibility principle<\/h3>\n<p>All functions, methods should follow the &#8216;single responsibility principal&#8217; . If a function or method is carrying out multiple processes, split the processes in to multiple functions\/methods and use an encapsulating function\/method to call the overall process.<\/p>\n<p>For example:<\/p>\n<pre class=\"EnlighterJSRAW\">function withdrawMoney(\u00a0$balance,\u00a0$amount\u00a0)\u00a0{\n\n\u00a0\u00a0\u00a0\u00a0\/\/\u00a0Remove\u00a0parsed\u00a0amount\u00a0from\u00a0the\u00a0total\u00a0balance\n\u00a0\u00a0\u00a0\u00a0$balance\u00a0-=\u00a0$amount;\n\n\u00a0\u00a0\u00a0\u00a0\/\/\u00a0If\u00a0the\u00a0balance\u00a0is\u00a0reduced\u00a0below\u00a00,\u00a0apply\u00a0a\u00a0\u00a35\u00a0fine\n\u00a0\u00a0\u00a0\u00a0if\u00a0(\u00a0$balance\u00a0&lt;\u00a00\u00a0)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$balance\u00a0-=\u00a05;\n\n\u00a0\u00a0\u00a0\u00a0return\u00a0$balance;\n\n}<\/pre>\n<p>This should be split to reflect something more like this:<\/p>\n<pre class=\"EnlighterJSRAW\">\/\/ remove parsed amount from the total balance\nfunction\u00a0removeFunds(\u00a0$balance,\u00a0$amount\u00a0) {\n\n    $newBalance\u00a0=\u00a0$balance\u00a0-\u00a0$amount;\n\n\u00a0\u00a0\u00a0\u00a0return\u00a0$newBalance;\n\n}\n\n\/\/ if the balance is below 0, add a fine\nfunction\u00a0checkFine(\u00a0$balance\u00a0)\u00a0{\n\n    if\u00a0(\u00a0$balance\u00a0&lt;\u00a00\u00a0)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$balance\u00a0=\u00a0$balance\u00a0-\u00a05;\n\n\u00a0\u00a0\u00a0\u00a0return\u00a0$balance\n\n}\n\n\/\/ main function that calls other function to perform an action\nfunction\u00a0withdrawMoney(\u00a0$balance,\u00a0$amount\u00a0)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\n    $newBalance\u00a0=\u00a0removeFunds(\u00a0$balance,\u00a0$amount\u00a0);\n\u00a0\u00a0\u00a0\u00a0$newBalance\u00a0=\u00a0checkFine(\u00a0$newBalance\u00a0);\n\n\u00a0\u00a0\u00a0\u00a0return\u00a0$newBalance;\n\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Formatting Annotations All functions and methods should be annotated. Visit Comments and Annotations for more details on this topic. Indentation All code needs to be indented in multiples of four spaces, using the tab key not the space bar. This is to ensure consistency across IDEs and text editors. If the code spans multiple lines, [&hellip;]<\/p>\n","protected":false},"author":945,"featured_media":0,"parent":171,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-155","page","type-page","status-publish","hentry"],"acf":[],"publishpress_future_action":{"enabled":false,"date":"2026-06-01 09:05:38","action":"change-status","newStatus":"draft","terms":[],"taxonomy":"","extraData":[]},"publishpress_future_workflow_manual_trigger":{"enabledWorkflows":[]},"_links":{"self":[{"href":"https:\/\/www.devon.gov.uk\/standards\/wp-json\/wp\/v2\/pages\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devon.gov.uk\/standards\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.devon.gov.uk\/standards\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.devon.gov.uk\/standards\/wp-json\/wp\/v2\/users\/945"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devon.gov.uk\/standards\/wp-json\/wp\/v2\/comments?post=155"}],"version-history":[{"count":0,"href":"https:\/\/www.devon.gov.uk\/standards\/wp-json\/wp\/v2\/pages\/155\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.devon.gov.uk\/standards\/wp-json\/wp\/v2\/pages\/171"}],"wp:attachment":[{"href":"https:\/\/www.devon.gov.uk\/standards\/wp-json\/wp\/v2\/media?parent=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}