{"id":19,"date":"2009-06-07T18:09:52","date_gmt":"2009-06-07T16:09:52","guid":{"rendered":"http:\/\/dirk.forbiddendream.be\/?p=19"},"modified":"2016-01-07T10:50:33","modified_gmt":"2016-01-07T10:50:33","slug":"coding-standards","status":"publish","type":"post","link":"https:\/\/dirk.schuermans.me\/?p=19","title":{"rendered":"Coding standards"},"content":{"rendered":"<p>It has come to my attention that most of the people that learn how to program in highschool \/ college \/ university have bad habbits when it comes to writing their code down.<br \/>\nI&#8217;m not saying the code itself is incorrect, i&#8217;m pretty sure they teach the correct stuff, but i&#8217;m talking about other things that usually get ignored&#8230;<br \/>\nIn order to program effectively, it&#8217;s important that you keep to a set of rules.<br \/>\nThe ones that irritate me the most are:<\/p>\n<ul>\n<li>Variable naming<\/li>\n<li>No indent in your code<\/li>\n<\/ul>\n<p>Okay, most of the time code written by people that don&#8217;t really have an interest in coding, but just take those classes because &#8220;they have to do something to get a diploma&#8221; looks like this:<\/p>\n<p><!--more--><br \/>\n<\/p>\n<pre name=\"code\" class=\"php\">\r\n\/\/ PHP Variable Naming example\r\n$mystring = \"Hello World!\";\r\n$myarray = array(\"item1\", \"item2\", \"item3\", \"item4\");\r\n$mynumber = 1;\r\n$mybool = true;\r\n\r\n\/\/ Output: Hello World! My array holds 4 items, my number is 1 and this all is 1\r\nprintf(\"$mystring My array holds \".count($myarray).\" items, my number is $mynumber and this all is $mybool<br \/><br \/>\");\r\n\r\n\/** Output:\r\n* item1\r\n* item2\r\n* item3\r\n* item4 **\/\r\nforeach($myarray as $item){\r\nprintf($item.\"<br \/>\");\r\n}\r\n<\/pre>\n<p>\nNow imagine that part of code x 2000.<br \/>\nI know i&#8217;ve used pretty obvious variable names, like <i>mystring<\/i> which indicates it&#8217;s a string etc, but i could&#8217;ve used any other name such as <i>&#8220;$dog&#8221;<\/i> where you won&#8217;t know what type the variable really is.<br \/>\nI mean, it could be a string where you define the dog name, or the dog type,<br \/>\nbut it could also be a integer or a boolean: dog = true (yes, it&#8217;s a dog) or dog = 4 (A certain pre-defined dog type or whatever).<\/p>\n<p>Anyhow, back ontopic:<\/p>\n<p>If I would be working at a company with a programming team, and one of my team members would send me a large file filled with code and variables names like that, i&#8217;d spent more time figuring out what type what variable is.<\/p>\n<p>That&#8217;s why it&#8217;s important that you make sure that people can tell by the name of your variable what type is stored inside the variable.<br \/>\nWhen I was learning OO in PHP, i&#8217;ve forced myself to keep using this set of standards, and i&#8217;ve been using them in all my programs since then in all the coding languages i know (PHP, Java, VB.NET, NASC, &#8230;)<\/p>\n<p>According to the Hungarian Naming Convention all variable names should be built as following:<\/p>\n<ul>\n<li>A letter for the scope of the variable (g = global, m = member, l = local, p = parameter)\n<ul>\n<li>Mind you, that for local they usually don&#8217;t put a l infront of it (Ex: l_sMyString) but leave it out (Ex: sMyString). This is due to the fact that all local variables are being used local, and all variables that orginate from outside the local scope, already have a scope indication<\/li>\n<\/ul>\n<\/li>\n<li>An underscore after each scope letter<\/li>\n<li>A &#8220;few&#8221; letters that describe the type of the variable, these are all lowercase\n<ul>\n<li>a = Array<\/li>\n<li>b = Boolean<\/li>\n<li>c = constant<\/li>\n<li>d = Date<\/li>\n<li>fn = Function<\/li>\n<li>n = Numeric\n<ul>\n<li>f = Float<\/li>\n<li>i = Integer<\/li>\n<li>l = Long<\/li>\n<\/ul>\n<\/li>\n<li>o \/ obj = Object<\/li>\n<li>r = reference<\/li>\n<li>s = String<\/li>\n<li>v = variant<\/li>\n<\/ul>\n<\/li>\n<li>The describing name of the variable, starting with a capital letter at each seperate part of the name (Ex: FillString, MyVariable, MyString, MyInteger, &#8230;)<\/li>\n<\/ul>\n<p>I&#8217;ll show you a quick example of a class in PHP:<\/p>\n<pre name=\"code\" class=\"php\">\r\n\t\/\/ Organized example\r\n\tclass Foo\r\n\t{\r\n\t\tprotected $m_sName;\r\n\t\tprotected $m_nAge;\r\n\r\n\t\tpublic function getInfo( $p_bAge )\r\n\t\t{\r\n\t\t\t\/\/ Show name with age or without age\r\n\t\t\tif( $p_bAge == true )\r\n\t\t\t{\r\n\t\t\t\tprintf( $this->m_sName.\" is \".$this->m_nAge.\" years old!\" );\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tprintf($this->m_sName.\" doesn't want to show his\/her age!\");\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n<\/pre>\n<p>As you can see, i&#8217;ve used my own standards and anyone that knows PHP and reads this code will know exactly what kind of value is inside each variable.<br \/>\nI hope you&#8217;ve also noticed that i&#8217;ve indent my code, including the make-up for the if-else etc<\/p>\n<p>The basic idea behind indent is to have a much better overview of your code, it&#8217;ll be easier to determine what belongs to where.<br \/>\nLet&#8217;s say we got the above code, but I won&#8217;t indent it nor make it pretty:<\/p>\n<pre name=\"code\" class=\"php\">\r\n\/\/ Organized example\r\nclass Foo\r\n{\r\nprotected $m_sName;\r\nprotected $m_nAge;\r\n\r\npublic function getInfo( $p_bAge )\r\n{\r\n\/\/ Show name with age or without age\r\nif( $p_bAge == true ){\r\nprintf( $this->m_sName.\" is \".$this->m_nAge.\" years old!\" );\r\n}else{\r\nprintf($this->m_sName.\" doesn't want to show his\/her age!\");}\r\n}\r\n}\r\n<\/pre>\n<p>Now imagine that again x 2000.<br \/>\nI&#8217;ll bet you $100 USD you&#8217;ll get confused before you&#8217;ve read through the first 100 lines of the code \ud83d\ude1b<\/p>\n<p>In cases like these, it&#8217;s not easy to determine which closing tag belongs to which opening tag, unless you&#8217;re using a special software to write your code in (Notepad++ for example highlights start &#038; ending tags if you click on them)<\/p>\n<p>Bottom line is: use correct names and learn to ident your code, make a habbit out of it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It has come to my attention that most of the people that learn how to program in highschool \/ college \/ university have bad habbits when it comes to writing their code down. I&#8217;m not saying the code itself is incorrect, i&#8217;m pretty sure they teach the correct stuff, but i&#8217;m talking about other things &hellip; <a href=\"https:\/\/dirk.schuermans.me\/?p=19\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Coding standards<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[38,6],"tags":[],"_links":{"self":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/19"}],"collection":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19"}],"version-history":[{"count":3,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":791,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions\/791"}],"wp:attachment":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}