{"id":43,"date":"2007-05-23T04:01:00","date_gmt":"2007-05-23T11:01:00","guid":{"rendered":"http:\/\/sp2hari.com\/?p=43"},"modified":"2020-06-20T09:04:54","modified_gmt":"2020-06-20T09:04:54","slug":"coding-style","status":"publish","type":"post","link":"https:\/\/sp2hari.com\/index.php\/2007\/05\/23\/coding-style\/","title":{"rendered":"Coding Style &#8230;"},"content":{"rendered":"<div style=\"text-align: justify;\">I have never thought much about coding style before i did my <a href=\"http:\/\/www.gnomebangalore.org\/?q=nosip\">NOSIP<\/a> in <a href=\"http:\/\/www.novell.com\/\">Novell<\/a>. But once i started coding for <a href=\"http:\/\/ldtp.freedesktop.org\/\">ldtprecord<\/a>, according to the coding style suggested to me by <a href=\"http:\/\/nagappanal.blogspot.com\/\">nags<\/a>, i was surprised to see how nice and neat the final code looks.<\/div>\n<p>Some tips\/tricks for nice coding skills are,<\/p>\n<div style=\"text-align: justify;\">1. Do spend some time to think about the variable names and the function names. This sometimes might be bit boring, especially when you want to concentrate much on the program logic and performance. But this is <span style=\"font-style: italic;\">Rule 0<\/span> for coding conventions. A variable name &#8220;k&#8221; can imply anything like &#8220;kappa, kozhukattai, katthu, kaadhal, kerala, kozhuppu&#8230;&#8221; to someone who might have to read your code later. This is again mentioned <a href=\"http:\/\/developer.gnome.org\/doc\/guides\/programming-guidelines\/code-style.html\">here<\/a> clearly. Many thanks to emacs, you can always use the auto complete, if your variable name is too long. \ud83d\ude42 .<\/div>\n<div style=\"text-align: justify;\">2. The actual coding convention depends much on the language and the standards your team is using already. The following style won&#8217;t work for someone, whose team is already using a totally different style.<\/div>\n<p>A few examples for C is posted here .<\/p>\n<p><span style=\"font-weight: bold;\">Sample Code 1 :<\/span><\/p>\n<p><span style=\"font-style: italic;\">if (a == 5)  {<\/span><span style=\"font-style: italic;\"><br \/>\n<\/span><\/p>\n<div style=\"text-align: left;\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"font-style: italic;\">b = 10;<\/span><\/div>\n<p><span style=\"font-style: italic;\">}<\/span><br \/>\n<span style=\"font-style: italic;\">else {<\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"font-style: italic;\">b = 20;<\/span> <span style=\"font-style: italic;\"><br \/>\n}<\/span><\/p>\n<p>Things to be noticed in the above snippet are.<\/p>\n<p>1. A space between if and &#8220;(&#8221; .<br \/>\n2. Space in both the sides of the comparison operator.<br \/>\n3. Space between &#8220;)&#8221; and &#8220;{&#8221;<br \/>\n4. Space between both the sides of assignment operator (line 2 &amp; 5) . This is true for almost all the operators.<br \/>\n5. Proper indentation of lines 2 &amp; 5. If you are using emacs or vi, check <a href=\"http:\/\/developer.gnome.org\/doc\/guides\/programming-guidelines\/code-style.html\">here<\/a> for your .emacs or .vimrc file .<\/p>\n<div style=\"text-align: justify;\">Well, your code will compile and run even if you don&#8217;t give these spaces, but a program coded with a bad coding style is equivalent to an inefficient code.<\/div>\n<p><span style=\"font-weight: bold;\">Sample Code 2<\/span><\/p>\n<p>Let us have a function which takes two integers and returns their sum .<br \/>\nThe code should be like<br \/>\n<span style=\"font-style: italic;\"><br \/>\nint add_numbers (int num1, int num2) {<\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"font-style: italic;\">return (num1 + num2);<\/span> <span style=\"font-style: italic;\"><br \/>\n}<\/span><\/p>\n<p>The function call will be something like,<\/p>\n<p><span style=\"font-style: italic;\">int sum;<\/span> <span style=\"font-style: italic;\"><br \/>\nsum = add_numbers (10, 20);<\/span><\/p>\n<p>Things to be noticed in the above snippet is<\/p>\n<p>In the first line in the function declaration,<\/p>\n<p>1. The function name should be as clear as possible.<br \/>\n2. A space between the end of function name and &#8220;(&#8221; .<br \/>\n3. Spaces are given after every &#8220;,&#8221; in the function argument list.<br \/>\n4. A space is given between &#8220;)&#8221; and &#8220;{&#8220;.<\/p>\n<p>In the second line in the function declaration,<\/p>\n<p>1. A space before &#8220;(&#8220;.  [ This rule is almost global. Apply it everywhere whenever you use &#8220;(&#8221; ] .<\/p>\n<div style=\"text-align: justify;\">2. There is a space on both the sides of the addition operator. This is again almost global. A space between both the sides of operator makes the code look real neat.<\/div>\n<p>3. The indentation about which was mentioned earlier.<\/p>\n<p>But yes, if your girl friend is a geek or a nerd or a psycho or a fundoo, then you better go for this. \ud83d\ude09<\/p>\n<p><span style=\"font-style: italic;\">#define MAGIC &#8220;eilouvy43605321&#8221;<\/span><br \/>\n<span style=\"font-style: italic;\">#define _(p,o,q) (t o#p[0])?(q)<\/span><br \/>\n<span style=\"font-style: italic;\">#define __(p,o,q) _(p,o,t-q)<\/span><br \/>\n<span style=\"font-style: italic;\">int main(){int t, i; for(i=8;i&gt;0;i&#8211;)printf(&#8220;%c&#8221;, MAGIC[(((t=(MAGIC+7)[i-1])==&#8217;_&#8217;)?62:_(.,==,63):_(@,==,64):__(a,&gt;=,&#8217;a&#8217;+36):__(A,&gt;=,&#8217;A&#8217;+10):(t-&#8216;0&#8217;))]);}<\/span><\/p>\n<p>Note :: I wont say the coding style i use is the perfect one. It always depends upon what your team was using till now and how easy it is to read, debug and maintain the code.<span style=\"font-style: italic;\"><\/span><\/p>\n<p><span>Useful Links :<br \/>\n<\/span><\/p>\n<div style=\"text-align: justify;\">The guide coding standards in <a href=\"http:\/\/developer.gnome.org\/doc\/guides\/programming-guidelines\/book1.html\">GNOME<\/a> is really a nice one.<br \/>\nEven better was <a href=\"http:\/\/www.possibility.com\/Cpp\/CppCodingStandard.html\">this one<\/a> i found recently. Though i didn&#8217;t read it completely, it was quite interesting.<br \/>\n<a href=\"http:\/\/www.arcticlabs.com\/codingstandards\/ArcticLabsCodingStandardsCpp.html\">This<\/a> article was short and sweet.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I have never thought much about coding style before i did my NOSIP in Novell. But once i started coding for ldtprecord, according to the coding style suggested to me by nags, i was surprised to see how nice and neat the final code looks. Some tips\/tricks for nice coding &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":2,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":1475,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/43\/revisions\/1475"}],"wp:attachment":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}