<?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[Stories by Andy Cohen on Medium]]></title>
        <description><![CDATA[Stories by Andy Cohen on Medium]]></description>
        <link>https://medium.com/@andycohen_25321?source=rss-21765238f703------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1xiSJ8t_flFJKJbE.</url>
            <title>Stories by Andy Cohen on Medium</title>
            <link>https://medium.com/@andycohen_25321?source=rss-21765238f703------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 03:09:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@andycohen_25321/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Are Java interfaces always a good thing?]]></title>
            <link>https://medium.com/@andycohen_25321/are-java-interfaces-always-a-good-thing-85e46d59424c?source=rss-21765238f703------2</link>
            <guid isPermaLink="false">https://medium.com/p/85e46d59424c</guid>
            <category><![CDATA[java-basics]]></category>
            <category><![CDATA[programming-basic]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[programming-style]]></category>
            <dc:creator><![CDATA[Andy Cohen]]></dc:creator>
            <pubDate>Mon, 05 Mar 2018 21:27:21 GMT</pubDate>
            <atom:updated>2018-03-05T21:27:21.138Z</atom:updated>
            <content:encoded><![CDATA[<p>TL;DR: If there’s only one implementation of your interface, throw that interface away.</p><p>Here’s another issue that came up in a code review at work: We have an interface which has a single implementation. We expect to change that implementation in the near future (to make it read from a database instead of a config file, or vice versa, or to change some other behavior entirely — it doesn’t really matter). Should we keep that interface, or throw it away, in favor of using the concrete class directly? When should we use an interface?</p><p><strong>Use an interface when you don’t know which implementation to use at coding time.</strong></p><p>Are you writing a JDBC driver framework? In that case, at coding time, you don’t know which database-specific classes will be used at runtime. Use an interface for each of those database-specific classes.</p><p>Are you writing code that uses different algorithms to handle different types of data, where the type of each datum is not known until runtime? Use an interface to provide those different algorithms with a common API.</p><p>Are you writing some application-specific class? Will there be only one concrete implementation of that class at runtime? Don’t use an interface for that class.</p><p><strong>But why not use interfaces all the time?</strong></p><p>Because premature abstraction is the root of all evil. Seriously, that unnecessary interface is just more code to read, understand, and maintain. Readers of your code will need to take a detour every time they come across a usage of that interface: “This code calls Foo.bar(). OK, Foo is an interface. Where are all of the classes that implement that interface? I <em>think </em>there’s only one — let’s hope that’s true. OK, I’ve found the code that probably runs in this case. What was I trying to figure out, again?”</p><p>Worse yet, there may be two or more implementations, because one of them represents the way we <em>used</em> to do things, or it represents the way we <em>might</em> want to do things in the future, even though the code uses only one of those implementations. In that case, you have to figure out which implementations are never, ever used, and remember to ignore them.</p><p>Now, multiply that detour times 10, or 100, or whatever the number of unnecessary interfaces in your code.</p><p><strong>Are you saying that interfaces are bad?</strong></p><p>No, they are incredibly useful, when they’re needed. It’s kind of like a stepladder: when you need to carry it around, it’s definitely worth it. Just walking around town? You’d be better off leaving it at home.</p><p><strong>But I want to keep the different parts of my code loosely coupled!</strong></p><p>A laudable goal, but when an interface has only one implementation, which is known at coding time, then that interface does nothing to change your degree of coupling. If you write your class in a way that its callers need to know which collections it contains, and how and when to modify those collections directly, then that’s a tightly-coupled relationship, whether or not it’s hiding behind an interface. Similarly, if you write your class in a way that its callers are completely ignorant of its implementation details, then it’s loosely coupled, whether or not it’s hiding behind an interface.</p><p><strong>But this is just the initial implementation of my class — very soon, we’ll need to stop reading from a text file, and start reading from the database.</strong></p><p>That’s fine, but you still don’t need an interface. If you’re discarding an old implementation in favor of a new one, then there’s still only one implementation at runtime. If you’re considering leaving the old implementation in the codebase, don’t. Obsolete, unused code needs to be deleted, to avoid the maintenance and complexity burden of code that does nothing for nobody. If you ever really need it back, Git never forgets.</p><p><strong>But someday, we may come up with some requirement to have multiple implementations at runtime. Shouldn’t we just code for that possibility now?</strong></p><p>No. Lots of code is full of premature abstractions for requirements that never actually came true. Those premature abstractions result in an unnecessary complexity and maintainability burden. Add that layer of abstraction when you need it, but not before.</p><p><strong>But I’ve had experiences where somebody coded without an interface, and we needed it later.</strong></p><p>Yup. That will happen. It’s still better to add interfaces (and abstractions in general) <em>after</em> they’re needed, not before.</p><p>So, that’s the story: Use interfaces when you need them, and avoid or delete them when you don’t.</p><p>Have fun coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85e46d59424c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Empty collection, or null?]]></title>
            <link>https://medium.com/@andycohen_25321/empty-collection-or-null-45099ad392c?source=rss-21765238f703------2</link>
            <guid isPermaLink="false">https://medium.com/p/45099ad392c</guid>
            <category><![CDATA[programming-style]]></category>
            <category><![CDATA[programming-basic]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Andy Cohen]]></dc:creator>
            <pubDate>Mon, 05 Mar 2018 01:17:49 GMT</pubDate>
            <atom:updated>2018-03-05T01:17:49.468Z</atom:updated>
            <content:encoded><![CDATA[<p>TL;DR: Use empty collections, not nulls.</p><p>This question came up the other day at work during a code review, so I thought this might be a good opportunity to spell out my reasons for preferring empty collections over null collection references.</p><p>When an object contains a collection, and that collection has no members, should that be represented by a zero-length collection, or null? A reasonable first reaction would be to just leave the thing null, but that means that everyone who ever uses that collection needs to check first to see whether or not the collection actually exists:</p><pre>if (myCar.getTires() != null) {<br>  foreach (Tire tire : myCar.getTires()) {<br>    tire.inflate();<br>  }<br>}</pre><p>If we know that the collection always exists, even though it may be empty, then we can avoid that null-check:</p><pre>foreach (Tire tire : myCar.getTires()) {<br> tire.inflate();<br>}</pre><p>Less code is better, right? But that’s not the only advantage: When the collection may be null, every consumer of that collection must always remember to perform that null-check, or NullPointerExceptions will happen. Those exceptions probably won’t happen until the code is in production, and in some scenario that would never happen (but does). But everyone will always remember to do that check everywhere, forever, right? Yeah, no.</p><p>Let’s look at a slightly fancier example. In this case, we want to determine the sum of all of the terms of a mathematical expression. Each term may have its own subterms. In this case, we’ve represented the expression as a tree of terms, each with its own subtrees of terms. Here’s the code to calculate the value of a single term, along with all of its sub, sub-sub, and sub-sub-sub terms:</p><pre>int sumTerms(Expression expression) {<br>  return expression.value + term.getTerms()<br>                                .stream()<br>                                .mapToInt(t -&gt; sumTerms(t))<br>                                .sum();<br>}</pre><p>See, no null-checking, no NullPointerExceptions, no nothin’. Using null collections here would only make the code a little longer, a little harder to read, and a little more error prone. Only a little, right? But who needs any of that, when it’s not necessary?</p><p>Have fun coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45099ad392c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>