<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Code Style Terror - Medium]]></title>
        <description><![CDATA[A series of opinion articles regarding code style - Medium]]></description>
        <link>https://medium.com/code-style-terror?source=rss----71d2a65452cd---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Code Style Terror - Medium</title>
            <link>https://medium.com/code-style-terror?source=rss----71d2a65452cd---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 16:15:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/code-style-terror" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Code Style Terror III]]></title>
            <link>https://medium.com/code-style-terror/code-style-terror-iii-f9cabc928d43?source=rss----71d2a65452cd---4</link>
            <guid isPermaLink="false">https://medium.com/p/f9cabc928d43</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[code-practices]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Nuno Grilo Pinheiro]]></dc:creator>
            <pubDate>Mon, 27 Jun 2016 11:56:15 GMT</pubDate>
            <atom:updated>2016-06-27T11:56:14.854Z</atom:updated>
            <content:encoded><![CDATA[<h3>Code Style Terror III — How deep is your code?</h3><blockquote>Best feature of Java 8? Lamdas!</blockquote><blockquote>Worst Feature of Java 8? Lambdas!</blockquote><blockquote>Best feature of ES6? Arrow functions!</blockquote><blockquote>Worst Feature of ES6? Ok, you guessed it, arrow functions!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*X7LXvA0YHBQKGe2EUUCRnw.png" /><figcaption>Small code is good code!?</figcaption></figure><p>I really love lambdas, not because of their tiny little shiny syntax, but because of what they represent: They are pieces of code that can be assigned to variables, passed as arguments to functions, etc. Although this was already possible in Java (before 8) using the <a href="https://en.wikipedia.org/wiki/Strategy_pattern">strategy pattern</a>, the lambda syntax and the <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html">method references</a> really favor this style of programming due to the simplicity of the syntax.</p><p>What makes lambdas the worst feature of Java 8 is not the lambdas per se, but the usage given by the community. Writing all the logic in the same line is so easy, that this programming style is turning into a trend, but just because you can put everything in the same line, it does not mean you should!</p><p>People usually use small LOC (Lines of Code) as a programming language quality measure, but measuring the quality of a code block based on LOC makes as much sense as <a href="http://martinfowler.com/bliki/CannotMeasureProductivity.html">measuring productivity based on LOCs</a>.</p><p><strong>Only if it was a lambda problem</strong></p><p>As I said, it is not a lambda problem. Other scenario were code gets really complex, is in <a href="https://en.wikipedia.org/wiki/Fluent_interface">fluent interfaces</a>.</p><p>Fluent interfaces are methods which can be chained in the same line to provide better readability. And they do make code easier to read! Until the programmer decides to put complex logic or structures inside of the method calls.</p><p>Fluent interfaces usually go hand in hand with lambdas, turning it easier to create bad code. An example in several languages (e.g Java and Scala) are the map reduce operations over collections.</p><p><strong>Real Cases</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eeda520325ce56ce6d4e12637d523490/href">https://medium.com/media/eeda520325ce56ce6d4e12637d523490/href</a></iframe><p>As a rule of thumb, if you have a lamda inside a lambda, it is not easy to read!</p><p>Other big issue I see here is the fact that there is a big block in the middle of a fluent interface chaining. When you arrive at the 10th line, you don’t remember that you were in a map call, so you will probably need to re-start reading from line 2. Unless your lambda block is defined inside the last method call in the chain, your probably should not use it.</p><p>Just a side note, although I changed some parts of the code to present it in this post, the name “x” was used for the variables...</p><pre>db.collection(request.query.type).find({ “$text”: { “$search”: textToSearch }}, {“name” : 1}).skip(PAGE*PAGE_SIZE).limit(PAGE_SIZE)</pre><p>One case where it is really easy to break readability with no lambdas involved is in MongoDB queries. The call to the find method, which first argument corresponds to the WHERE clause in the SQL world, usually brings a big-depth query object, and you either write everything in the same line, and can’t read the line, or you end up breaking the expression in multiple lines, losing the purpose of the fluent interface.</p><h4>How to fix this?!</h4><p>Solving this issue is really simple: Simply refactor your noisy parts into variables.</p><p>For instance, refactor your lambdas to a variable if they are complex and are in a filter clause. If you start the name of that variable with the “by” word, your code can be read as “filter(byComplexLogic)”.</p><p>The same happens to the MongoDB queries. Just refactor your find block into a variable and you will end up with the following line most of the time</p><pre>db.collectionName.find(query, fieldsToReturn).skip(PAGE*PAGE_SIZE).limit(PAGE_SIZE)</pre><p>If you are a Java developer, and you use <a href="https://eclipse.org/downloads/packages/eclipse-ide-java-developers/mars2">Eclipse Java IDE</a>, you can use the <a href="http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-menu-refactor.htm">automatic refactoring commands</a>. Personally, I am a addicted to “ctrl + 1” which, besides allowing me to automatically fix some errors, allows me to choose “extract to local variable/field/method/constant”.</p><h4>What About You?</h4><p><strong>Have you ever been pursued by a Minotaur while you were lost in a maze of lambdas?</strong></p><pre>people.filter(person -&gt; person.isYou())</pre><pre>.map(you =&gt; you.pleaseComment(this.story).heart(this.story));</pre><p>(Please comment or heart this story) :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9cabc928d43" width="1" height="1" alt=""><hr><p><a href="https://medium.com/code-style-terror/code-style-terror-iii-f9cabc928d43">Code Style Terror III</a> was originally published in <a href="https://medium.com/code-style-terror">Code Style Terror</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Code Style Terror II]]></title>
            <link>https://medium.com/code-style-terror/code-style-terror-ii-1b5dc516bf10?source=rss----71d2a65452cd---4</link>
            <guid isPermaLink="false">https://medium.com/p/1b5dc516bf10</guid>
            <category><![CDATA[code-practices]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Nuno Grilo Pinheiro]]></dc:creator>
            <pubDate>Thu, 12 May 2016 12:40:36 GMT</pubDate>
            <atom:updated>2016-05-12T12:40:36.659Z</atom:updated>
            <content:encoded><![CDATA[<h3>Code Style Terror II — The Value of the Truth</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*Rxx-QQjAP8NA3ajvfIGajQ.png" /><figcaption>This is false in Java</figcaption></figure><p>Booleans are the most important concept in computer science. Whole processors are made based only on <a href="https://en.wikipedia.org/wiki/Boolean_algebra">Boolean Algebra</a> through logic gates, and conditional statements are what enable us to create logic in high level programming languages.</p><p>Since they are fundamental to programming, we usually master them in the early learning phases of programming , turning these weird zero and one operations into a simple and natural reasoning. But as every simple technique, it can be cryptic if used in excess or in the wrong contexts.</p><h4>Boolean equals True</h4><p>I remember once listening to a Professor criticizing a piece of code because, instead of returning a Boolean variable, returned the expression:</p><pre>return booleanVariable == True</pre><p>Even a programming student can see that this expression has the same result as the <em>booleanVariable</em> itself. In fact, knowing this is so easy, that the compiler/interpreter will probably ignore the equals operator and just return the variable.</p><p>But lets ignore code performance and focus on readability. You are reading a piece of code to check if a bill was paid, in a language where you do not need to declare your function and variable types, such as Python or Scala, and you read one of the two following return lines:</p><pre>1)</pre><pre>return paid </pre><pre>2)</pre><pre>return paid == true</pre><p>Without more context, from which code line can you understand better the return type of the function? The sample 1 is enough to achieve the right value, but the second case gives more information to the reader by saying that <em>paid</em> is a Boolean, the function returns a Boolean, and the function returns true if the variable <em>paid</em> is true.</p><p>Of course I do not want everybody to start writing code like this since most of the time it is a simple <a href="https://medium.com/code-style-terror/code-style-terror-i-1dbbf222f1da#.weak8fmlw">variable naming problem</a>. If you prepend “is” or “was” to your variable names the code will probably be perfect. Just change the variable in the presented code to “wasPaid” and it is fixed.</p><h4>The emptiness will haunt you</h4><p>I am mainly a Java programmer, and as a Java programmer I like my if statements to be based on Booleans. But sometimes I need to go JavaScript, and lately I am spending lots of time programming in Python. In these languages any value can be used inside <em>if</em> statements and any value ,or absence of a value, has a corresponding Boolean. An heuristic I use to understand them is the “emptiness” of the value. “Empty” values are false, non-empty values are true. For instance in Python:</p><ul><li>0 (zero) is an empty value (false). Any other number is non-empty.</li><li>An empty list… is empty (false). Non-empty list, not false. The same for dictionaries.</li></ul><p>This feature is so rooted in JavaScript language, that is usually used to conditionally initialize variables in an idiomatic way. By using <a href="https://en.wikipedia.org/wiki/Short-circuit_evaluation">short-circuit evaluation,</a> the following JavaScript sample <em>1</em> is equivalent to the <em>2</em>nd example:</p><pre>1)</pre><pre>var a = a || {}</pre><pre>2)</pre><pre>if(!a){</pre><pre>  a = {}</pre><pre>}</pre><p>The fact that we can use any variable in <em>if</em> statements, and according to the last section using “== True” is redundant and usually not done, this leads to curious code:</p><pre>if(paid):</pre><pre>  print “already processed”</pre><pre>else:</pre><pre>  processPayment()</pre><p>In this code, we do not know if paid is the “amount paid” or the fact that it “was paid”. Adding a simple “!=0” or “==True” will add some more semantic to the code.</p><h4>Boolean Algebra != Cool</h4><p>Boolean algebra in programming languages feels really natural, we have the <em>and</em>, <em>or</em>, <em>xor</em> and <em>not</em> operators and we can easily compose expressions that can solve complex logic.</p><p>The problem with this composition is its reader non-friendliness. For example, I once wrote some code that checked if a contact needed to be validated. Web pages didn’t need validation, and phone numbers only needed validation based on a configuration variable:</p><pre>def requiresValidation():<br>return !(isWebAddress || <br>         ((isMobilePhone || isPhone) &amp;&amp; !requiresPhonesValidation));</pre><p>The logic is easy to read, but based on this code ,and considering that a comment is not present, you cannot easily transpose the requirements from the code. Based on the code it is hard to get the human answer to “When does the contact needs validation?”</p><p>Simply abstracting parts of the logic into smaller pieces would lead to a better human readable code.</p><pre>def requiresValidation():</pre><pre>toValidatePhone = ((isMobilePhone || isPhone) &amp;&amp;     <br>                   !requiresPhonesValidation)<br>return !(isWebAddress || toValidatePhone);</pre><p>Of course, this is subjective, I consider the previous refactor to improve code reading, since I will probably focus on the last line of the method when skimming through the code, but other people may consider that the “toValidatePhone” assignment could be further refactored.</p><h4>What About You?</h4><p>Have you ever seen a Boolean expression so hard to understand that even Pinocchio&#39;s nose would not know if it was supposed to grow or not?</p><pre>youLiked &amp;&amp; please(heart || comment)</pre><p>If you liked this post, please heart or comment :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b5dc516bf10" width="1" height="1" alt=""><hr><p><a href="https://medium.com/code-style-terror/code-style-terror-ii-1b5dc516bf10">Code Style Terror II</a> was originally published in <a href="https://medium.com/code-style-terror">Code Style Terror</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Code Style Terror I]]></title>
            <link>https://medium.com/code-style-terror/code-style-terror-i-1dbbf222f1da?source=rss----71d2a65452cd---4</link>
            <guid isPermaLink="false">https://medium.com/p/1dbbf222f1da</guid>
            <category><![CDATA[code-practices]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Nuno Grilo Pinheiro]]></dc:creator>
            <pubDate>Wed, 13 Apr 2016 13:13:51 GMT</pubDate>
            <atom:updated>2016-04-13T13:13:50.886Z</atom:updated>
            <content:encoded><![CDATA[<h3>Code Style Terror I — Big Names Can Kill Your Network!</h3><blockquote>You need to write efficient code. Your page load has strict requirements since <a href="http://www.cedexis.com/blog/for-google-400ms-of-increased-page-load-time-results-in-044-lost-search-sessions/">each 100ms it takes you lose 1% of your sales</a>. What should you do?</blockquote><blockquote>̶U̶s̶e̶ ̶l̶e̶t̶t̶e̶r̶s̶ ̶a̶s̶ ̶v̶a̶r̶i̶a̶b̶l̶e̶ ̶n̶a̶m̶e̶s̶!̶!̶!</blockquote><blockquote>No! Minimize your code!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/264/1*sRnNnNfgbNClpMcqWJ1yDA.png" /><figcaption>a = b means nothing</figcaption></figure><p>There may have been a time in which variable and commands length took a very important role. Terminal commands needed to be small since <a href="https://books.google.pt/books?id=5W3f6IyIBQcC&amp;pg=PA509&amp;lpg=PA509&amp;dq=slow+teletypes+unix+commands+length&amp;source=bl&amp;ots=w9yNd4r3ro&amp;sig=0D0bmlumr-uxSktvgGxgWxnvpGY&amp;hl=en&amp;sa=X&amp;ved=0ahUKEwjbzOuNmPHLAhVFtxQKHRdLAwgQ6AEINTAE#v=onepage&amp;q=slow%20teletypes%20unix%20commands%20length&amp;f=false">terminals were slow</a>. Small commands were crucial for the typist’s productivity.</p><p>But in the 21st century, it is awkward that we still have to discuss the importance of variable naming with programmers. Even if you lose some productivity because you need to type more, <a href="https://www.goodreads.com/quotes/835238-indeed-the-ratio-of-time-spent-reading-versus-writing-is">we spend most of the time reading code, not writing it</a>, so it probably won’t be a bottleneck.</p><p>Besides, if you use an IDE, or almost any worthy code editor, you will have any kind of auto completion, so your 30 character variables only need to be written once! Do you think any Java developer has to write <a href="https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition">EnterpriseGradeFizzBuzzSolutionStrategyFactory</a> more than once? Even if you are using a dynamically-typed language, using a code editor such as <a href="https://github.com/atom/atom/">Atom</a> will give you pretty good suggestions of existing symbols.</p><h4><strong>But, But, The Network…</strong></h4><p>When developing web-applications in a large scale, every byte counts. If you are worried about the size of your code files and their delivery to the browsers, I am glad to inform you that this has been solved years ago! <a href="http://lmgtfy.com/?q=javascript+minification">Just minimize your code with any of the tools that exist for that</a>! These tools transform all your variable and function names into small symbols like ‘a’ and ‘b’, remove all the spaces, all the line terminators, etc. Some tools, such as <a href="https://developers.google.com/closure/compiler/#what-is-the-closure-compiler">Google’s Closure Compiler</a>, will even improve your code performance and remove non-used code!</p><p>If you use angular, you may still be worried about your <a href="https://docs.angularjs.org/tutorial/step_05">never minified controller names</a>, but I do believe those will not be a bottleneck.</p><h4><strong>Pull Requests are trending!</strong></h4><p>When you analyse code, the “unit” of code you need to read depends on your current context. If you are coding, you are probably reading a class or a function.</p><p>But if you are searching for a recent bug and need to check the latest changes, or you are reviewing the work the team did during your vacation, what you are reading is probably a commit, so the unit of code is lesser than a method, is just a diff in some file and a commit comment message.</p><p>The “cool” thing about diffs is the leak of context. For instance, in GitHub, unless you click to see the whole file, you probably will not see the function name if the changes were performed in the middle of a block.</p><p>The role of the variable name here isn’t even to improve code reading. If you are just skimming through recent commits, as I do in some third party projects, the variable names are a big hint to check if the commit is worth reading.</p><h4><strong>Some Recent Cases</strong></h4><pre>def doStuff(path):</pre><pre>   st = len(contextPath)</pre><pre>   for i in range(st, len(path)):</pre><pre>      #do the stuff</pre><p>This sample was a bit more complicated than the part I am showing here. Maybe I was in a bad day, but I really struggled to understand what the <em>st</em> variable meant. It’s easy to know from what is written that it is the size of the context path, but what does it mean in the context of the algorithm?</p><p>When I realized it meant “start” it made a big difference to understand the whole algorithm.</p><pre>var a = $(“someCSSSelector”);</pre><p>I spent some time thinking why would somebody name <em>a</em> to a variable. I don’t know if this variable name is a front-end standard, but I really think that <em>linkForSomething </em>would be more readable than <em>a</em>. (It was <em>a</em> as in <em>&lt;a&gt;</em>)</p><h4><strong>Historically Acceptable Cases</strong></h4><p>Of course there are some classic small variable names that are socially acceptable.</p><p>In any programming language where <em>for</em> loops exist, we have <em>i</em>, <em>j</em>, <em>k</em> variables. But even those should be carefully used. Having an <em>i</em> variable in a 2 line <em>for</em> block is ok, but if your block grows (as it shouldn’t), during a pull request the reader may not notice that the variable is inside a loop block, so it will be a meaningless name.</p><p>For all the generic fans, at least in Java, we have the <em>T</em>, <em>U</em>, <em>V</em> variables. Sometimes I feel that real names like <em>TypeOfObject</em> would be preferable. Generics based code is usually complex and written by advanced programmers. Having to guess what those type variables mean can be hard, specially if you have multiple generic types at the same time. In the middle of the code it may be hard to remember what the variables <em>T</em> and <em>U </em>stand for.</p><h4>What About You?</h4><p><strong>Do you have any variable name that you are still trying to figure out?</strong><br>pls com || hrt ths stry (Please comment or heart this story) :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1dbbf222f1da" width="1" height="1" alt=""><hr><p><a href="https://medium.com/code-style-terror/code-style-terror-i-1dbbf222f1da">Code Style Terror I</a> was originally published in <a href="https://medium.com/code-style-terror">Code Style Terror</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>