{"id":167,"date":"2021-02-08T09:35:12","date_gmt":"2021-02-08T07:35:12","guid":{"rendered":"http:\/\/victorrentea.ro\/blog\/?p=167"},"modified":"2021-02-08T09:43:24","modified_gmt":"2021-02-08T07:43:24","slug":"resurrection-of-the-coding-guidelines-document","status":"publish","type":"post","link":"https:\/\/victorrentea.ro\/blog\/resurrection-of-the-coding-guidelines-document\/","title":{"rendered":"Resurrection of the Coding Guidelines Document"},"content":{"rendered":"<div id=\"bsf_rt_marker\"><\/div><p>I recently got a lot of ideas from the comments I got to a <span style=\"text-decoration: underline;\"><strong><a href=\"https:\/\/www.linkedin.com\/feed\/update\/urn:li:activity:6745604504282042368\/\">recent post<\/a><\/strong><\/span> on LinkedIN.<\/p>\n<blockquote><p>The purpose of Code Review is learning, not blaming.<\/p><\/blockquote>\n<p>But I won\u2019t discuss code review nor pair programming today, they both deserve their own articles. Instead, I\u2019d like to talk about the Coding Guidelines Document. Every mid-large team ends up writing such a document sooner or later. It\u2019s a document written <strong>by developers for developers<\/strong>. It\u2019s a rare kind of beast, as developers are notoriously reluctant to writing documents. Maybe they refrain from writing documents because they realized how fast documents get obsolete in our branch. This idea was captured in the Agile Manifesto, 2nd item: \u201cWe have come to value Working Software over Comprehensive Documentation\u201d.<\/p>\n<p>So you might hear them saying that:<\/p>\n<blockquote><p>The next minute you finish writing a document about code, that document is already obsolete and should immediately be regarded as an archeology product.<\/p><\/blockquote>\n<p>That might be the default for detailed code architecture documents and documentation, but having this attitude towards a coding guidelines document will immediately defeat its purpose. The coding guidelines are supposed to represent the target coding style of the team, which all developers in the group follow every time they write code.<\/p>\n<p>Developers themselves put their best efforts into writing this document. It\u2019s true, the <strong>act of writing<\/strong> a coding standards document is extremely gratifying: you get to talk about clean code, design principles, and common issues frequently discussed in code reviews. You might start extracting typical code samples from your codebase, have brainstorms and architecture meetings. Then, after a while, you complete that document, and you email it to everyone. Or even better, you put it somewhere visible, on a wiki, and invite people to help to refine it.<\/p>\n<p>And there\u2019s when it all stops.<\/p>\n<p>Two years later, most people on the project aren&#8217;t even aware that such a document exists.<\/p>\n<p>What went wrong? We involved everybody in writing that document; we broadcast it to everyone; perhaps we&#8217;re still linking it in any email we regularly send to the team members (like the timesheets reminder email \ud83d\udca1). We even took the time to keep that document in sync with the patterns commonly occurring in code reviews.<\/p>\n<p>But somehow, everyone simply ignores it after a while.<\/p>\n<p>What to do? You need a reset.<\/p>\n<h2>Delete. Rewrite.<\/h2>\n<p>I want you to delete that document and then enjoy rewriting it <strong>together<\/strong>. A page with ten rules (\ud83d\udca1) that everyone takes into account is far better than a state-of-the-art 50-pages Clean Code book chapter that no one cares about.<\/p>\n<p>So every 6-12 months, delete it and start anew!<\/p>\n<p>Tell <strong>everyone in the team<\/strong> to write down 5-10 things they would like to see in the Team Coding Guidelines Document and leave them several days to think over.<\/p>\n<p>Every developer should compile her &#8220;personal wish-list of coding guidelines&#8221;, because every one of us struggles to master a different set of design goals as she progresses through her career. For sharpening your skills, it&#8217;s crucial to try to synthesize these goals in writing, put a name to the issues, and try to describe them in your own words. Plus, the simple fact of writing stuff down tends to make that stuff weigh more for oneself. So push hard: send reminders and insist that every single developer in the team squeezes these bullet points, no excuses.<\/p>\n<p>You may even put together a little contest: vote for the best topics and offer prizes for the best items or variants. The goal of the Team Coding Guidelines is to find a reasonable common denominator, neither bloated with trivial rules, nor subjective, or too advanced as for a super-senior.<\/p>\n<p>But again, delete it? Really?<\/p>\n<p>The team coding habits may have changed since you wrote those guidelines. The very fact that you already had such a document shows a concern that likely helped your developers improve, so they don\u2019t commit the same errors as they were one year ago. Perhaps you realize some rules became useless or evident to everyone. Or maybe you\u2019ve concluded that a particular guideline doesn\u2019t pay off in the long term (example: extract constants from every number in the code). Either way, a rewrite will demand that every rule in there to be strictly needed.<\/p>\n<p>But more importantly, deleting and rewriting the document anew is so much fun; it\u2019s a joint creative act. Plus, in the end, everyone will be among its authors and signers, and that will feel like a sort of Team Clean Code Manifesto. A joint, well-understood commitment tailored to your codebase and team skill level.<\/p>\n<blockquote><p>The Coding Guidelines Document is a Team Code Quality Manifesto.<\/p><\/blockquote>\n<p>You could cheat a bit and have a quick look at the old document before deleting it, but anything you propose to add to the new guidelines document should be needed <strong>today<\/strong>, and you must honestly convince the rest of your team about that. Then archive the old guidelines for historical purposes only.<\/p>\n<p>Ok, now you&#8217;ve (re)written it. What next?<\/p>\n<h2>Adjust in Regular Team Design Meetings<\/h2>\n<p>Every month sit together with your team and have a chat about every point in this document. Go through all of it in an hour. If it takes longer than that, that&#8217;s a sign you should shrink the document or repeat the meeting more often. This meeting doesn&#8217;t just allow to continuously adapt and update the rules, but more importantly, offers an opportunity to discuss any tricky design topics.<\/p>\n<blockquote><p>The best code happens when you discuss its design with someone else.<\/p><\/blockquote>\n<p>The simple fact of articulating the design ideas and tradeoffs is crucial to produce simpler, cleaner, and more expressive code. At the same time, hearing your colleagues talk about design is extremely beneficial in terms of learning but also motivating you to do a better job. &#8220;If they are talking about it, then it&#8217;s important&#8221;. But try to make everyone contribute to the discussion if you get to facilitate this meeting.<\/p>\n<p>Developers are famously reluctant to meetings. They rather spend weeks in code, suffer harsh code reviews, and play &#8220;I know better&#8221; than sit down in a meeting together. They might actually tell you that they &#8220;don&#8217;t have time for that&#8221;. But that\u2019s crap! <em>Weeks of rework can spend hours of design meetings<\/em> &#8211; but make them understand it\u2019s not their private time they spend there.<\/p>\n<blockquote><p>Time spent on a coding guidelines document is time earned.<\/p><\/blockquote>\n<p>Any time spent on constructively brainstorming and fine-tuning the guidelines to follow is by far more time- and stress-efficient than repeatedly arguing about recurring design flaws or painfully reworking the code you&#8217;ve put your best effort into writing. These meetings not only help with explaining those rules, but you might get to challenge some of the rules you don\u2019t like.<\/p>\n<p>On the other hand, you might find out that the vast majority of your colleagues are against one of your personal &#8220;best practices&#8221;. For example, they might not be so in love with the ternary operator (?:) as you are. Just saying \ud83d\ude09. This social pressure might convince even the most stubborn colleague to adjust his style. \ud83d\udca1<\/p>\n<p>And slowly, throughout these meetings, your Coding Guidelines Documents will tend to grow indefinitely over years, becoming harder and harder to digest for anyone joining the team. But it\u2019s exactly those new additions to the team that need this document more than any others, to either assimilate or challenge it. Throughout my career as a trainer and consultant, I&#8217;ve seen many projects having prohibitively long and complex coding standards documents that no one even dared to open.<\/p>\n<p>You might try several ideas to shrink it back to a tolerable size:<\/p>\n<ul>\n<li>remove the obvious<\/li>\n<li>extract the complex<\/li>\n<li>use tools for formal rules<\/li>\n<li>cluster by topic.<\/li>\n<\/ul>\n<p>The last idea simply means to split the document depending on the area they target: for example frontend code vs backend code. Even if there are similarities and you\u2019ll repeat some rules, the coding style is usually substantially different as the challenges they face are different.<\/p>\n<p>We\u2019ll briefly cover the other techniques below.<\/p>\n<h2>Upvote the Rules you Discuss<\/h2>\n<p>As a team grows in skills, some items in the coding guidelines document become so obvious that they can be removed. How do you know which ones?<\/p>\n<p>One trick is to have that guidelines document on a wiki and upvote the rules every time they are used\/referred in a code review or pair programming discussion. In other words, if two people end up discussing that rule, it should get a <code>+1<\/code>. You may even try to sort the topics based on those votes, to start with the &#8220;hottest&#8221; ones.<\/p>\n<p>Then, in a Team Design Meeting look at those counters, and if a rule was never the subject of a debate between developers, consider removing it, if you all agree that it&#8217;s a trivial one.<\/p>\n<p>Okay, but what if a junior developer joins the team and the trivial rules are gone? That&#8217;s why regularly deleting and rewriting the document is key. In addition, always consider Pair Programming with any junior developer joining the team for the first several weeks\/months.<\/p>\n<p><strong>Note<\/strong>: If you\u2019re Pair Programming\/Mobbing all the time in your project, then celebrate! You are among the several 10-20% most lucky developers on the Planet!<\/p>\n<h2>Extract Tutorials<\/h2>\n<p>Many teams working on complex legacy codebases end up writing elaborate tutorials and walkthroughs about how to redesign certain types of &#8220;typical mess&#8221; that occur frequently. Some examples I saw:<br \/>\n&#8220;How to replace ServiceLocator with Dependency Injection&#8221;<br \/>\n&#8220;How to move from programmatic to declarative transaction management&#8221; or<br \/>\n&#8220;How to replace side-effecting code with returning immutables&#8221;<\/p>\n<p>Should these sections be part of the coding guidelines?<\/p>\n<p>No.<\/p>\n<p>They would make the document hard to digest as each such tutorial needs intense focus to understand. It\u2019s a different mind gear (slow thinking), so don\u2019t mix them. Plus, if you delete and rewrite the document regularly to keep it alive, you wouldn&#8217;t want to lose them, right?<\/p>\n<p>You will probably store them in an internal wiki page and link them from the guidelines document, but I would like to challenge you to do much more than that. If you are fond of those tutorials, then I would say you&#8217;re the perfect person to start a blog. So, to the extent possible, anonymize and upload those tutorials to a public blog for others to benefit too. If starting a blog is overkill, you can try a blogging platform like <a href=\"https:\/\/dzone.com\/\">dzone.com<\/a> or <a href=\"http:\/\/dev.to\/\">dev.to<\/a> to get started. Just get it out there, share it with more people!<\/p>\n<p>I think our profession is missing that: blogs, reports, and talks named &#8220;What have we done wrong: &#8230;&#8230;.&#8221;, &#8220;The mistakes we did last year&#8221;. Very few developers are powerful enough to admit their\/their team\u2019s mistakes and speak openly about them. Have that courage, and let many others struggling with the same mistakes learn from you.<\/p>\n<p>PS: if you already know of such a blog, please leave a comment with the link below.<\/p>\n<h2>Use Tools for Formal Rules<\/h2>\n<p>Don\u2019t turn your Coding Guidelines Document into a list of numbers, use tools to check the static code analysis metrics like:<\/p>\n<ul>\n<li>length and max indentation of a function<\/li>\n<li>number of parameters<\/li>\n<li>max source code file size<\/li>\n<li>number of fields\/class, etc<\/li>\n<\/ul>\n<p>What\u2019s that <a href=\"https:\/\/www.youtube.com\/watch?v=BOF3x08s_8c\">sound in your mind<\/a>?<\/p>\n<p>Yup, Sonar is there for you. Use it to your advantage: tailor those rules to match the beliefs of your team. Restrict those too weak (7 max params\/function is the default?!), or relax those inapplicable to the current codebase (we have legacy 10K-lines files !!).<\/p>\n<p>If you live in terrible code but you \u201cwant to do a good job from now on\u201d, read about <a href=\"https:\/\/stackoverflow.com\/questions\/41550260\/what-does-the-leak-period-mean-in-sonarqube\">leak period<\/a>: it allows measuring the quality only for the code that you changed since a given milestone, for example since the last release.<\/p>\n<p>Install SonarLint plugin (available for both Eclipse and IntelliJ) and check the code every time before you commit. I\u2019m not a big fan of having the analysis continuously running in my IDE, it distracts me. But I do love how IntelliJ allows you to run SonarLint analysis on the lines you changed before every commit (a nice checkbox in the commit dialog). No matter what, it\u2019s far better to <strong>see the Sonar issues right in your IDE<\/strong> than on a webpage (aka The Hall of Shame), then ALT-TAB, search the file, line number, &#8230; edit.<\/p>\n<p>Be sure to link the SonarLint plugin to your (paid) SonarCube\/SonarCloud instance attached to your git repo to benefit from a lot more rules than those bundled with the free plugin. For example, <a href=\"https:\/\/rules.sonarsource.com\/java\/RSPEC-3749?search=injected\">this rule<\/a> prevents turning your Spring components into stateless beans. Plus, you can then adjust the rules on your project profile and those rules will propagate into developers\u2019 IDEs.<\/p>\n<h2>Free Tour for Newcomers<\/h2>\n<p>You should walk every developer joining the team through these guidelines, gently but firmly, a couple of rules every day (so you shouldn\u2019t have dozens of them). Don\u2019t rush, take your time to provide complex examples and detailed explanations, as well as exceptions to the rules and alternative solutions. Enjoy the moment as you, the mentor, are effectively teaching Clean Code, Simple Design, and\/or Unit Testing, and explaining these concepts will enhance your skillset too.<\/p>\n<h2>Conclusion: What Should go in a Coding Guidelines Document?<\/h2>\n<p>I deliberately left this topic for the end, as I wanted to make it clear: the Coding Guidelines Document should be a <strong>LIVE document<\/strong>, expressing the beliefs and present goals of the team. A dead one is no use. And we\u2019ve gone through several techniques to keep it alive. If you are using another trick that I didn\u2019t mention, please let me know in the comments.<\/p>\n<p>The topics it covers do indeed stem from the Clean Code, Object-Orientation, Functional Programming, Simple Design, and Unit Testing areas. But the exact content depends on the project\/people mindset. However, as a conclusion, here is a summary of my preferences.<\/p>\n<p>Coding Guidelines Document:<\/p>\n<ul>\n<li>Synthesize common code review discussions<\/li>\n<li>Focus on the pain points, avoid dull cargo-cult rules<\/li>\n<li>Don\u2019t copy-paste rules from other projects<\/li>\n<li>Extract detailed tutorials into wikis\/blogs<\/li>\n<li>Continuously massage the guidelines in Team Design Meetings<\/li>\n<li>Regularly delete-rewrite it to revive the commitment and keep it focused<\/li>\n<li>Treat it as a Manifesto for Good Code<\/li>\n<li>Use tools for simple metrics<\/li>\n<li>Gently walk every new developer through these guidelines<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I recently got a lot of ideas from the comments I got to a recent post on LinkedIN. The purpose of Code Review is learning, not blaming. But I won\u2019t discuss code review nor pair &hellip; <\/p>\n","protected":false},"author":1,"featured_media":234,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-167","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-practical-tips"],"_links":{"self":[{"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/comments?post=167"}],"version-history":[{"count":4,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":237,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/posts\/167\/revisions\/237"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/media\/234"}],"wp:attachment":[{"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}