<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ameya Karve</title>
    <description>The latest articles on DEV Community by Ameya Karve (@ameyakarve).</description>
    <link>https://dev.to/ameyakarve</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F48841%2F92f05a64-1597-4913-9684-c17b775a9d27.jpg</url>
      <title>DEV Community: Ameya Karve</title>
      <link>https://dev.to/ameyakarve</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ameyakarve"/>
    <language>en</language>
    <item>
      <title>Design for Readability</title>
      <dc:creator>Ameya Karve</dc:creator>
      <pubDate>Sun, 10 Dec 2017 19:11:17 +0000</pubDate>
      <link>https://dev.to/ameyakarve/design-for-readability-598</link>
      <guid>https://dev.to/ameyakarve/design-for-readability-598</guid>
      <description>&lt;p&gt;I feel strongly about writing good code; for me, it is an art that is honed over time. It needs to be written well, structured nicely, and easy to understand; one aspect that often goes neglected though, is the readability of the code itself. Intelligent formatting is such an underrated aspect of code; there are benefits in so many areas. It makes it very easy for a peer to review your code, for one. Quoting from &lt;em&gt;David Bryant Copeland’s&lt;/em&gt; &lt;a href="http://naildrivin5.com/blog/2013/05/17/source-code-typography.html"&gt;&lt;em&gt;blog&lt;/em&gt;&lt;/a&gt;,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;But, it’s not just the content — the code itself — that affects readability. How it’s presented matters and if we’re going to talk about presentation, we have to talk about typography.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Today, there is a lot of work going on around automating this area; there are tools to ensure consistent formatting to some degree. &lt;em&gt;Intellij-Idea&lt;/em&gt; has an auto-format feature for Java code that does most things right and saves time too. &lt;em&gt;Checkstyle&lt;/em&gt; for Java is a god-send too; catches most inconsistencies early. I am a huge fan of &lt;a href="https://prettier.io/"&gt;Prettier&lt;/a&gt; too; while I rarely write JavaScript code nowadays, I incorporate a lot of ideas from there in what I write. While automated styling does work at most times, one can always do better. The following quote from Practical Typography applies very well here:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Good ty­pog­ra­phy is mea­sured by how well it re­in­forces the mean­ing of the text, not by some ab­stract scale of merit. Ty­po­graphic choices that work for one text won’t nec­es­sar­ily work for an­other. (Corol­lary: good ty­pog­ra­phers don’t rely on rote so­lu­tions. One size never fits all.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My workflow when writing something new normally involves one step of automatic formatting followed by tweaks to enhance readability before I raise a code review request. I havetried to crystallize some of the techniques I use; I’ll list them below:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**All code is lifted from random open-source repositories&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function Definitions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider the following snippet:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;While it is readable, it could be made a lot better:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here is what I did:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lexicographical sorting by class-name to make things easier to find&lt;/li&gt;
&lt;li&gt;The first line was excessively long; long lines make it harder to focus the code at hand; this is solved very easily by simply starting the arguments in the next line&lt;/li&gt;
&lt;li&gt;Vertical lines: by ensuring that the argument names are all aligned in a single line, we give them the look of a table, which one is more comfortable with consuming, especially in print media. I have given out two variants: one with the class names right-aligned and the other with left-align; I prefer the right alignment, but I feel both approaches serve the purpose well.&lt;/li&gt;
&lt;li&gt;Finding matching brackets can be one of the most annoying things one goes through while reading code. A simple rule of thumb that I have found to work for me is to have the same indentation for the closing brace as the line containing the opening brace.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Classes, inheritance and interfaces:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How many times have we seen a code snippet that looks like the following:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Formatting it as below makes it a lot more readable:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Again, notice the table-like alignment similar to #3 from the previous section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function invocations:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I feel there is no need to reinvent the wheel here; &lt;a href="http://jlongster.com/A-Prettier-Formatter"&gt;Prettier&lt;/a&gt; does a solid job in this regard; I’ll highly recommend reading the paper by &lt;a href="http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf"&gt;Wadler&lt;/a&gt; too.&lt;/p&gt;

&lt;p&gt;Lifting the example straight from Prettier,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo(arg1, arg2, arg3); // Concise enough; no need to change

// Can do better
foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());

// Much cleaner!
foo(
  reallyLongArg(),
  omgSoManyParameters(),
  IShouldRefactorThis(),
  isThereSeriouslyAnotherOne()
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Comment blocks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Code comments are always a great way to enhance the readability of code; they help augment the reader’s understanding of the code with the relevant context. I do not have too many strict rules about comment blocks; there are a few that I do stick to though:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Keeping the maximum line-width of comments to &lt;em&gt;80 characters&lt;/em&gt; (or even fewer). This results in nice blocky comments that the reader can consume easily. This fits in very well with &lt;a href="https://practicaltypography.com/summary-of-key-rules.html"&gt;Butterick’s Practical Typography rules &lt;/a&gt;: “&lt;em&gt;The av­er­age&lt;/em&gt; &lt;a href="https://practicaltypography.com/line-length.html"&gt;&lt;em&gt;line length&lt;/em&gt;&lt;/a&gt; &lt;em&gt;should be 45–90 char­ac­ters (in­clud­ing spaces)&lt;/em&gt;”&lt;/li&gt;
&lt;li&gt;Use comment tags where necessary; I like to use &lt;strong&gt;TODO&lt;/strong&gt; , &lt;strong&gt;TECHDEBT&lt;/strong&gt; &lt;a href="https://en.wikipedia.org/wiki/Comment_%28computer_programming%29#Tags"&gt;https://en.wikipedia.org/wiki/Comment_%28computer_programming%29#Tags&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I’ll end my post here; do drop a comment with patterns that you use that help in this regard!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>prettier</category>
      <category>typography</category>
    </item>
  </channel>
</rss>
