{"id":1494,"date":"2011-07-14T13:06:10","date_gmt":"2011-07-14T17:06:10","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/?p=1494"},"modified":"2011-07-14T13:06:10","modified_gmt":"2011-07-14T17:06:10","slug":"stuff-everyone-should-do-part-2-coding-standards","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2011\/07\/14\/stuff-everyone-should-do-part-2-coding-standards\/","title":{"rendered":"Stuff Everyone Should Do (part 2): Coding Standards"},"content":{"rendered":"<p> Another thing that we did at Google that I thought was surprisingly effective and useful was <em>strict<\/em> coding standards.<\/p>\n<p> Before my time at Google, I was sure that coding standards were pointless.  I had absolutely no doubt that they were the kind of thing that petty bureaucrats waste time writing and then use to hassle people who are actually productive.<\/p>\n<p> I was seriously wrong.<\/p>\n<p> At Google, I could look at <em>any<\/em> piece of code, anywhere in Google&#8217;s codebase, and I could read it. The fact that I was <em>allowed<\/em> to do that was pretty unusual in itself. But what was surprising to me was just how much the standardization of style &#8211; indents, names, file structures, and comment conventions &#8211; made it dramatically easier to look at a piece of unfamiliar code and <em>understand<\/em> it. This is <em>still<\/em> surprising to me &#8211; because those are all trivial things. They <em>shouldn&#8217;t<\/em> have much impact &#8211; but they <em>do<\/em>. It&#8217;s absolutely shocking to realize how much of the time you spend reading code is just looking for the basic syntactic structure!<\/p>\n<p> There&#8217;s a suite of common objections to this, all of which I used to believe.<\/p>\n<dl>\n<dt> It wastes time!<\/dt>\n<dd> I&#8217;m a good coder, and I don&#8217;t want to waste time on stupidity. I&#8217;m good enough that when I write code, it&#8217;s clear and easy to understand. Why should I waste my time on some stupid standard? The answer is: because there is a value in uniformity. As I alluded to earlier &#8211; the fact that every piece of code that you look at &#8212; whether it was written by you, by one of your closest coworkers, or by someone 11 timezones away &#8212; will always demarcate structures in the same way, will always use the same naming conventions &#8211; it really, genuinely makes a big difference. You need so much less effort to read code that you haven&#8217;t looked at in a while (or at all), because you can immediately recognize the structure.<\/dd>\n<dt> I&#8217;m an artist!<\/dt>\n<dd> This is phrased facetiously, but it does reflect a common complaint. We programmers have a lot of pride in our personal style. The code that I write really does reflect something about me and how my mind works. It&#8217;s a reflection of my skill and my <em>creativity<\/em>. If I&#8217;m forced into some stupid standard, it seems like it&#8217;s stifling my creativity. The thing is, the important parts of your style, the important reflections of your mind and your creativity aren&#8217;t in trivial syntactic things. (If it is, then you&#8217;re a pretty crappy programmer.) The standard actually makes it easier for other people to see your creativity &#8211;  because they can actually see what you&#8217;re doing, without being distracted by the unfamiliar syntactic quirks.<\/dd>\n<dt>One size fits all actually fits none!<\/dt>\n<dd> If you have a coding standard that wasn&#8217;t designed specifically for your project, then it&#8217;s probably non-optimal for your project. That&#8217;s <em>fine<\/em>. Again, it&#8217;s just syntax: non-optimal doesn&#8217;t mean <em>bad<\/em>. The fact that it&#8217;s not ideal for your project doesn&#8217;t mean that it&#8217;s not worth doing. Yeah, sure, it <em>does<\/em> reduce the magnitude of the benefit for your project, but at the same time, it <em>increases<\/em> the magnitude of the benefit for the larger organization. In addition, it frequently makes sense to have project-specific code styles. There&#8217;s nothing wrong with having a project-specific coding standard. In fact, in my experience, the best thing is to have a very general coding standard for the larger organization, and then project-specific extensions of that for the project-specific idioms and structures.<\/dd>\n<dt>I&#8217;m too good for that!<\/dt>\n<dd> This is actually the most common objection. It&#8217;s sort-of a combination of the others, but it gets at an underlying attitude in a direct way. This is the belief on the part of the complainer that they&#8217;re a better programmer than whoever wrote the standard, and lowering themselves to following the standard written by the inferior author will reduce the quality of the code. This is, to put it mildly, bullshit. It&#8217;s pure arrogance, and it&#8217;s ridiculous. The fact of the matter is that <em>no one<\/em> is so good that any change to their coding style will damage the code. If you can&#8217;t write good code to any reasonable coding standard, you&#8217;re a crappy programmer.<\/dd>\n<\/dl>\n<p> When you&#8217;re coding against a standard, there are inevitably going to be places where you disagree with the standard. There will be places where your personal style is better than the standard. But that doesn&#8217;t matter. There will, probably, also be places where the standard is better than your style. But that doesn&#8217;t matter easier. As long as the standard isn&#8217;t totally ridiculous, the comprehension benefits are significant enough to more than compensate for that. <\/p>\n<p> But what if the coding standard <em>is<\/em> totally ridiculous? <\/p>\n<p> Well, then, it&#8217;s rough to be you: you&#8217;re screwed. But that&#8217;s not really because of the ridiculous coding standard. It&#8217;s because you&#8217;re working for idiots. Screwing up a coding standard enough to really prevent good programmers from writing good code is hard. It requires a sort of dedicated, hard-headed stupidity. If you&#8217;re working for people who are such idiots that they&#8217;d impose a broken coding standard, then they&#8217;re going to do plenty of other stupid stuff, too. If you&#8217;re working for idiots, you&#8217;re pretty much screwed no matter what you do, coding standard or no. (And I don&#8217;t mean to suggest that software businesses run by idiots are rare; it&#8217;s an unfortunate fact, but there&#8217;s no shortage of idiots, and there are plenty of them that have their own businesses.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another thing that we did at Google that I thought was surprisingly effective and useful was strict coding standards. Before my time at Google, I was sure that coding standards were pointless. I had absolutely no doubt that they were the kind of thing that petty bureaucrats waste time writing and then use to hassle [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[54],"tags":[129,313,232,236],"class_list":["post-1494","post","type-post","status-publish","format-standard","hentry","category-programming","tag-comprehensibility","tag-programming","tag-shut-up-and-do-it","tag-standards"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-o6","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/1494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=1494"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/1494\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=1494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=1494"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=1494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}