{"id":896,"date":"2008-08-15T14:25:05","date_gmt":"2008-08-15T12:25:05","guid":{"rendered":"http:\/\/planetozh.com\/blog\/?page_id=896"},"modified":"2008-08-15T17:10:20","modified_gmt":"2008-08-15T15:10:20","slug":"make-clean-and-readable-sources-why-and-how","status":"publish","type":"page","link":"https:\/\/planetozh.com\/blog\/articles\/make-clean-and-readable-sources-why-and-how\/","title":{"rendered":"Make Clean and Readable Sources &mdash; Why and How"},"content":{"rendered":"<p>In August 2008 I made a quick review of 50 plugins (<a href=\"http:\/\/planetozh.com\/blog\/2008\/08\/2008-plugin-competition-review-part-one\/\">part 1<\/a>, <a href=\"http:\/\/planetozh.com\/blog\/2008\/08\/2008-plugin-competition-review-part-2-of-2\/\">part 2<\/a>) and was really surprised by the number of coders who ship plugins with barely readable code. Working, even clever, but ugly code.<\/p>\n<p>Making clean sources is not a useless mania of a compulsive obsessive geek, it&#39;s truly a requisite for maintainable code. It won&#39;t make your code good &#8212; just better.<\/p>\n<p>For less experienced coders here are a few rules of thumb I try to follow. That&#39;s just my word and my coding habits, nothing is to be considered as <em>the<\/em> definitive best practice, but I hope this will make anyway a good pointer for novices.<\/p>\n<h2>Good Plugin Habits: Make clean and readable sources<\/h2>\n<p>As a <a href=\"http:\/\/planetozh.com\/blog\/my-projects\/\">WordPress plugins<\/a> coder, mostly everything I know, I&#39;ve learnt it by reading sources: find about a plugin that does something, wonder how I would do that or even how such a thing is possible, then read the plugin source to compare their solution and mine, or even just learn about a new trick, a new API or a function I didn&#39;t know about. I barely read instructions or readmes, but I always read the sources.<\/p>\n<p>And as a <a href=\"http:\/\/planetozh.com\/blog\/my-projects\/\">WordPress plugins<\/a> releaser, I maintain a number of scripts. I wrote some of them four years ago, and anyway most of the time when I haven&#39;t worked on a plugin for more than a couple of months, I totally forget how I coded it. Maintaining plugins means updating them once in a while: either someone finds a bug, or the WordPress code and APIs evolve in a way that breaks the plugin, so I have to rewrite parts of it. Basically, this means rediscovering my own plugin: read the sources, wonder how I did this and why I did that. Needless to say, this is much less cumbersome if the source is clean.<\/p>\n<p>To me, making clean sources is twofold: help others reading my code, but primarily help the future-me maintaining outdated stuff. If someone finds your source hardly readable, you will too.<\/p>\n<style type=\"text\/css\">div.thumb {-moz-border-radius:10px;-webkit-border-radius:10px;background:transparent url(http:\/\/planetozh.com\/blog\/images\/thumb_up.png) no-repeat scroll 5px 5px;border:2px solid #6DB6F4;color:#ee6b89;padding:0.2em 1em 0.2em 30px;margin-bottom:1em;}<\/style>\n<p>This said, how do you keep your plugin sources tidy and clean? Nothing rocket science and just common sense, really:<\/p>\n<ol>\n<li><a href=\"#_indent\">indent<\/a><\/li>\n<li><a href=\"#_describe\">describe<\/a><\/li>\n<li><a href=\"#_comment\">comment<\/a><\/li>\n<li><a href=\"#_split\">split<\/a><\/li>\n<li><a href=\"#_stick\">stick<\/a><\/li>\n<li><a href=\"#_group\">group<\/a><\/li>\n<\/ol>\n<h2 id=\"_ident\">Indent your code<\/h2>\n<p>When reviewing a couple dozens of plugins for the Plugin Competition 2008, I was truly <em>shocked<\/em> by the number of coders who don&#39;t indent correctly, or consistently, or even who don&#39;t indent at all. <\/p>\n<p>I&#39;ve seen several bits like the following:<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$bool<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">foreach<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$vars<\/span> <span style=\"color: #b1b100\">as<\/span> <span style=\"color: #000088\">$var<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span>check_var<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$var<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$that<\/span> <span style=\"color: #339933\">=<\/span> process_var<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$var<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">return<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$that<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span> <span style=\"color: #b1b100\">else<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">do_collect<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$var<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">return<\/span> <span style=\"color: #009900;font-weight: bold\">false<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span> <span style=\"color: #b1b100\">else<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">return<\/span> <span style=\"color: #009900;font-weight: bold\">true<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>How ugly is that? A few in-front tabs would make things much more readable, helping identify where blocks start and end.<\/p>\n<div class=\"thumb\">Indent blocks like <tt>if<\/tt>, <tt>else<\/tt>, <tt>elseif<\/tt>, <tt>for<\/tt>, <tt>foreach<\/tt>, <tt>while<\/tt>, <tt>do-while<\/tt>, <tt>switch<\/tt>.<\/div>\n<div class=\"thumb\">Make blocks visually identifiable, put a few blank lines between distinct actions, function declarations. Space is cheap, put some where it makes sense.<\/div>\n<h2 id=\"_describe\">Be descriptive<\/h2>\n<p>For most of the code, things are self explanatory if well named. Reading a block of PHP code should be almost like reading basic English. A loop increment can be named <tt>$i<\/tt>, but give all your function and variables a name that will unmistakably tell the reader what they do or what they are for.<\/p>\n<p>Ugly: <tt>joe_check($string1, $string2)<\/tt><br \/>\nCool: <tt>joe_check_and_validate_inputs($username, $password)<\/tt><\/p>\n<div class=\"thumb\">Give functions, important variables and constants a self explanatory name.<\/div>\n<h2 id=\"_comment\">Comment the source<\/h2>\n<p>Don&#39;t state the obvious : don&#39;t comment trivial things (everybody will understand what <tt>function myplugin_create_default_database()<\/tt> is for) but you have to add some insight to its tricky parts. Comments are not there to turn your work into a coding tutorial where you explain the basis, you write comments primarily for you.<\/p>\n<p>When reading a code block, one should understand all of the 3 following key points:<\/p>\n<ul>\n<li><em>What<\/em>: the aim<\/li>\n<li><em>How<\/em>: the method<\/li>\n<li><em>Why<\/em>: the reason<\/li>\n<\/ul>\n<p>The What and the How may not require even require comments. Sometimes, giving the function a self explanatory name will be enough.<\/p>\n<p>The How could either be a detailed comment at the beginning of the block, or several one liners inside it, before each key part (that should be visually separated by a blank line)<\/p>\n<p>The Why is really the crucial part. Whenever you&#39;re making a design choice, this has to be explained and justified, for two reasons. First, you&#39;ll want to understand a tricky bit of code in 2 years when you read it again. And second, you&#39;ll want to be able to understand the reasoning behind this decision, and eventually be able to make another, and hopefully better, design choice. Whenever you spent some time in your code on a problem, spend an extra minute explaining how you handled it.<\/p>\n<div class=\"thumb\">Comments are primarily for the future you.<\/div>\n<div class=\"thumb\">If not obvious by the code, comments should say What and How. If it required some thoughts, comments must say Why.<\/div>\n<h3>Bonus: special comments<\/h3>\n<p>PHP supports <a href=\"http:\/\/www.php.net\/manual\/en\/language.basic-syntax.comments.php\">various types of comments<\/a>, either inline or blocks, such as:<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ this function will do ....<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> something<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$var<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$var<\/span> <span style=\"color: #339933\">=<\/span> somefunc<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$var<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #666666;font-style: italic\">\/\/ inline comment<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/* Now we have to sanitize<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">&nbsp; &nbsp; &nbsp; &nbsp;and make sure that ...<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">&nbsp; &nbsp; *\/<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #339933\">...<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>A comment can be really anything you feel comfortable with: brief explanations, more detailed stuff, formal or informal, as long as it doesn&#39;t make your code slower to read (explain, don&#39;t distract). Still, there is a rather widely adopted convention over a few keywords in comments, and amongst them: <strong><tt>TODO:<\/tt><\/strong> and <strong><tt>FIXME:<\/tt><\/strong><\/p>\n<ul>\n<li><tt>TODO:<\/tt> indicates that a part can be extended, adding features for instance. Additional code should go here at some point of the development.<\/li>\n<li><tt>FIXME:<\/tt> shows parts that must be improved: fix a bug or potential bug, rewrite a time consuming loop&#8230;\n<\/ul>\n<p>Leave these comments as notes for the future you or for someone else.<\/p>\n<p>Another set of strange keywords you&#39;ll find, in most of WordPress&#39; core files now, are what is called <strong>Javadoc<\/strong>. These look like the following:<\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009933;font-style: italic\">\/**<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009933;font-style: italic\">&nbsp;* do_action() - Execute functions hooked on a specific action hook.<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009933;font-style: italic\">&nbsp;*<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009933;font-style: italic\">&nbsp;* @since 1.2<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009933;font-style: italic\">&nbsp;* @param string $tag The name of the action to be executed.<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009933;font-style: italic\">&nbsp;* @param mixed $arg,... Optional additional arguments <\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009933;font-style: italic\">&nbsp;* @return null Will return null if $tag does not exist in $wp_filter array<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009933;font-style: italic\">&nbsp;*\/<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> do_action<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$tag<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #000088\">$arg<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">''<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #339933\">...<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/08\/comment-phpxref.gif\" alt=\"\" title=\"comment-phpxref\" width=\"348\" height=\"135\" class=\"alignleft size-full wp-image-901\" srcset=\"https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/08\/comment-phpxref.gif 348w, https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/08\/comment-phpxref-300x116.gif 300w\" sizes=\"auto, (max-width: 348px) 100vw, 348px\" \/> This standard way of commenting allow some tools to generate automated documentation (see image here) and also enforces attention to details and strictness when coding: did I provide a function description? Am I sure it will only accept strings? Will this always return a boolean?<\/p>\n<p>This comment syntax is really valuable on larger team projects, but I find it a bit overkill for my personal plugin coding needs.<\/p>\n<h2>Split in several files<\/h2>\n<p>Make several smaller files instead of one big file, group functions in different files by topic, as this will have several virtues<\/p>\n<p>First, it makes things prettier, which will make you prouder. As simple as this :) One short main plugin file, and various directories for images, CSS, javascripts, PHP includes, translation files,&#8230;<\/p>\n<p>Second, when a particular WordPress API evolves and breaks your plugin (and this <b>will<\/b> happen), you&#39;ll know which small file needs work: the one with comments function, the one dealing with the admin interface? It&#39;s much less burdensome to rework a tiny homogenous file than a big one.<\/p>\n<p>Third, split for performance. Making various small files will allow you conditionally include them only if they are needed. For instance, group all the admin interface functions in <tt>plugindir\/includes\/admin.php<\/tt> and in your main plugin file where the plugin initializes everything use something like:<\/p>\n<div id=\"ig-sh-4\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span>is_admin<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ we're in wp-admin\/<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">require_once<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">dirname<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900;font-weight: bold\">__FILE__<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">.<\/span><span style=\"color: #0000ff\">'\/includes\/admin.php'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<div class=\"thumb\">Start splitting and organizing your files from the start, not when you have 15 different files in the same directory.<\/div>\n<h2>Stick to one syntax<\/h2>\n<p>Once you are comfortable with a coding style, stick to it. If you prefer to indent with 4 spaces instead of 1 tab, naming with CamelCase (<tt>joeCreateDatabase()<\/tt> or underscore separation (<tt>joe_create_database()<\/tt>), putting curly brackets before of after a line break&#8230; it&#39;s all up to you, as long as you&#39;re consistent across your files.<\/p>\n<p>As reference, the adopted coding style for WordPress core files seems to be:<\/p>\n<div id=\"ig-sh-5\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> do_something <span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$var1<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #000088\">$anothervar<\/span> <span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$foo<\/span> &nbsp; <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$var1<\/span><span style=\"color: #339933\">;<\/span> &nbsp; &nbsp; &nbsp; &nbsp;<span style=\"color: #666666;font-style: italic\">\/\/ this is $foo<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$foo34<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$anothervar<\/span><span style=\"color: #339933\">;<\/span> &nbsp;<span style=\"color: #666666;font-style: italic\">\/\/ this is $foo34<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$condition<\/span> <span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; do_this<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; do_that<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'foo'<\/span> <span style=\"color: #339933\">==<\/span> <span style=\"color: #000088\">$bar<\/span> <span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; do_foo<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #0000ff\">'something'<\/span> <span style=\"color: #339933\">==<\/span> <span style=\"color: #000088\">$foo<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">?&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>That is:<\/p>\n<ul>\n<li>No shorthand PHP start tags (&lt;? &#8230; ?> or &lt;?=$var?>) but full PHP tags (&lt;?php &#8230; ?>)<\/li>\n<li>Lowercase and underscore function names<\/li>\n<li>Spacing before and after parenthesis, and between parameters<\/li>\n<li>Real tabs in front, and spaces to align elements where that improves readability<\/li>\n<li>Braces used for multiline blocks but avoided when possible (single line)<\/li>\n<li>Variables on the right side of logical comparisons<\/li>\n<\/ul>\n<div class=\"thumb\">Use the same coding style as in the WordPress core files, if possible.<\/div>\n<h2>Group hooks<\/h2>\n<p>My personal preference is to group hooks (<tt>add_action()<\/tt> <tt>and add_filter()<\/tt>) all together at the end of the main plugin file, rather than spread all over the file. I&#39;ve found that it makes things a lot easier when you try to figure out what&#39;s going on, and what is the typical &quot;flow&quot; of events, especially when they refer to aptly named functions.<\/p>\n<div class=\"thumb\">Grouping hooks makes like a summary of your plugin: what it does, and when.<\/div>\n<h2>References and More to Read<\/h2>\n<ul>\n<li>Common sense<\/li>\n<li>Wasted hours because of crappy code on first iteration :P<\/li>\n<li><a href=\"http:\/\/codex.wordpress.org\/WordPress_Coding_Standards\">WordPress Coding Standars<\/a> and a <a href=\"http:\/\/comox.textdrive.com\/pipermail\/wp-hackers\/2006-July\/006930.html\">mail by Mark Jaquith<\/a> on coding style<\/li>\n<li><a href=\"http:\/\/particletree.com\/features\/successful-strategies-for-commenting-code\/\">Successful Strategies for Commenting Code<\/a><\/li>\n<li><a href=\"http:\/\/www.devtopics.com\/13-tips-to-comment-your-code\/\">13 tips to comment your code<\/a><\/li>\n<li><a href=\"http:\/\/en.wikipedia.org\/wiki\/Naming_conventions_%28programming%29\">Naming conventions in programming<\/a> on Wikipedia<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In August 2008 I made a quick review of 50 plugins (part 1, part 2) and was really surprised by the number of coders who ship plugins with barely readable code. Working, even clever, but ugly code. Making clean sources is not a useless mania of a compulsive obsessive geek, it&#39;s truly a requisite for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":282,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-896","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/pages\/896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=896"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/pages\/896\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/pages\/282"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}