<?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 Jacob Goldfarb on Medium]]></title>
        <description><![CDATA[Stories by Jacob Goldfarb on Medium]]></description>
        <link>https://medium.com/@jacobgoldfarb?source=rss-89fe17d71eed------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lpbeMSfndIeDbRGLco4m0Q.jpeg</url>
            <title>Stories by Jacob Goldfarb on Medium</title>
            <link>https://medium.com/@jacobgoldfarb?source=rss-89fe17d71eed------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 01:53:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jacobgoldfarb/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[Single Responsibility App Design]]></title>
            <link>https://medium.com/@jacobgoldfarb/single-responsibility-app-design-33d1c1659f3d?source=rss-89fe17d71eed------2</link>
            <guid isPermaLink="false">https://medium.com/p/33d1c1659f3d</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Jacob Goldfarb]]></dc:creator>
            <pubDate>Thu, 11 Jul 2019 22:47:58 GMT</pubDate>
            <atom:updated>2022-04-01T17:14:04.405Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>The primary value of good software comes from its ability to accommodate changing user needs. Yes, that means the ability for software to change is more valuable than it being bug free, or even meeting current users’ needs. If a software is robust and modular, it can easily be made to fit whatever new feature a stakeholder may desire. <br>If a software is rigid and fragile, it will quickly become a burden to maintain. Updating rigid code is time consuming, difficult, and results in unintended side-effects and messy (or absent) unit tests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Uu3kKQ_lVDc3ct5NQSsgxw.png" /></figure><p>In software design, <strong>SOLID</strong> is an acronym coined by Bob Martin used to nicely encapsulate 5 important design principles.</p><p>From <a href="https://en.wikipedia.org/wiki/SOLID">the Wikipedia article on SOLID</a>:</p><ul><li><strong><em>Single responsibility principle</em></strong><br>“A class should only have a single responsibility, that is, only changes to one part of the software’s specification should be able to affect the specification of the class.”</li><li><strong><em>Open–closed principle</em></strong><br>“Software entities should be open for extension, but closed for modification.”</li><li><strong><em>Liskov substitution principle</em></strong><br>“Objects in a program should be replaceable with instances of their sub-types without altering the correctness of that program.”</li><li><strong><em>Interface segregation principle</em></strong><br>“Many client-specific interfaces are better than one general-purpose interface.”</li><li><strong><em>Dependency inversion principle</em></strong><br>One should “depend upon abstractions, [not] concretions.”</li></ul><p>These principles can all be employed in app design to decouple your code and create modular, robust apps. This article will focus on the <strong>single responsibility principle</strong>, heavily inspired by Bob Martin’s <em>Clean Code</em>.</p><blockquote>Functions should do one thing. They should do it well. They should do it only.<br>— Bob Martin</blockquote><p>Functions/classes/structs/whatever other software entities should have one responsibility. I’m sure most programmers have heard this quote or some variation already. It’s a nice sentiment, but without defining what a “thing” or “responsibility” is in this context, it’s difficult to utilize.</p><h3>What is a Responsibility?</h3><p><em>A responsibility is a reason for your code to change.</em></p><p>Each class, function, method, whatever should have only one reason to change: the request of a stakeholder to change some aspect of your application. Even though a project may be small, and you may even be the sole developer, it’s important to think of functionality in terms of stakeholders to address common sources of change and keep your app modular.</p><p>Let’s build a budgeting app.<br>Meet our team:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*vysYOxjrWreGFQ_RyiIXIg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*f9xMYkNgKwo9S88NtU3gAA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/516/1*vmQ0gYqCP8k4CwjhrZXrNA.png" /></figure><p><em>Matt</em>, the back-end engineer, <em>Eli</em>, the financial analyst, and <em>Terry</em>, the UX visionary. Each stakeholder is responsible for a different component of our app.</p><p><strong>Matt</strong> handles the back-end and scaling issues. What’s started as a Firebase project now has a distributed backend hosted on several AWS machine with a Django back-end and MongoDB database. Matt’s recently been talking about changing the REST API to a GraphQL API.</p><p><strong>Eli</strong> handles the algorithms. With a background in math and experience as a financial planner, Eli’s constantly employing the use of new algorithms and business logic to help your users save money.</p><p><strong>Terry’s</strong> a Figma wizard. Constantly interviewing users to find UI pain points and always on the scour for clean and trendy designs, Terry makes sure your app doesn’t look or feel like crap.</p><p>These are the stakeholders on our team, however, it’s important to consider absent stakeholders as well. It’s important to consider reasons your code may need to change down the line.</p><p>Is the app’s tagline on the sign-up splash page the responsibility of the designer? What if the company hires a dedicated copywriter? What if we want to create a Spanish version of our app as well?</p><p>Thinking carefully about these specifications is what the single responsibility principle is all about.</p><h3>BudgetScorpion™</h3><p>Here’s our app. The flow is simple:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2Zs6z5j15mIWjrbSrmks-g.png" /></figure><ol><li>At the beginning of each month the user creates a savings goal.</li><li>The user continuously adds transactions throughout the month. They can be debits, with negative dollar amounts or credits, with positive dollar amounts.</li><li>The app uses algorithms to <strong>a)</strong> predict the users’ payroll schedule to determine whether or not they’re on track to meet their monthly savings goal, and <strong>b)</strong> present daily insights.</li></ol><p>The example implementation will only look at some of this functionality</p><h4>The Responsibilities</h4><p>The obvious responsibilities are Matt, Eli, and Terry’s contributions. <br>Eli decides which insights are the most valuable, and how the apps main logic should work. <br>Terry creates the UI, making sure it’s in-line with the startup’s branding. <br>Matt makes sure the data will persist and is updated appropriately.</p><p>There is an important responsibility missing, however. The presentation of the information — not the UI design (as briefly talked about above). To release our app in China we need to replace the $’s with ¥’s, and the English with Chinese. These responsibilities are those of “the presenter”.</p><h3>Utilizing the Single Responsibility Principle</h3><h4><strong>The Model</strong></h4><p>This is the BudgetDashboardController‘s main model, “BudgetPlan”. It nicely encapsulates all the functionality any stakeholder could possibly desire.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c27b038fbd4935799453afe64e884de0/href">https://medium.com/media/c27b038fbd4935799453afe64e884de0/href</a></iframe><p>It stores and operates on business objects, can post itself to our back-end, has a simple algorithm to determine the user’s income, and contains logic to reset the users’ monthly transactions each month.</p><p>This is the Transaction object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f8c2aa562773c281916404f9df8d407/href">https://medium.com/media/2f8c2aa562773c281916404f9df8d407/href</a></iframe><p>Its sole purpose is to encapsulate important information to describe the transaction (i.e. date, amount, and vendor). It also has a method to easily format the transaction amount.</p><p>Nice right? Except these two structs have at least half a dozen responsibilities, many unrelated to each other.</p><h4>Multiple Responsibilities</h4><p>Let’s start with the minor violations before we include our stakeholders. <br><strong>(1) </strong>BudgetPlan’s addTransaction method does two things, courtesy of isNewMonth. Any method that accepts a Boolean must do two things, or leave the Boolean parameter untouched. Not to mention, this method also leaves the logic to determine whether it’s a new month or not to the method caller.</p><p><strong>(2) </strong>BudgetPlan stores a reference to the user’s savings goal, which is inputted at the beginning of each month, and the user’s budget.</p><p><strong>(3) </strong>BudgetPlan can estimate the user’s monthly earnings.</p><p><strong>(4) </strong>BudgetPlan can post itself to the back-end via the DatabaseManager singleton.</p><p>Next, let’s look at Transaction.</p><p><strong>(1) </strong>It’s been designed to easily be posted to a database.</p><p><strong>(2) </strong>BudgetPlan holds an array of Codable transactions so Eli can parse through them to easily extract insights like the user’s estimated monthly income.</p><p><strong>(3) </strong>Transaction also has a utility function to easily format the amount into a dollar format (e.g “$15.20”) to display on the front end.</p><p>That’s waaay too much responsibility for 41 lines of code in just 2 source files.</p><h3>Refactor Time!</h3><h4>Business Logic</h4><p>Let’s start with <strong>Eli’s </strong>responsibilities. You may have already realized, Eli’s estimateMonthlyIncome method doesn’t actually need to know much about the Transaction objects at all. For now, the business logic can be separated from the Transaction objects, which are tied heavily to the UI. Most methods Eli might be interested depend only on dollar amounts; net incomes, credits, debits, etc. Eli cares little about the vendor or date.</p><p>With this premise, let’s make a separate class just for Eli and place it in a new source file. By doing this not only do we make the app more testable, we pave the road to incorporate more complex back-end functions without touching other areas of the app.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7d49913247cc7737f873456979d5a72/href">https://medium.com/media/c7d49913247cc7737f873456979d5a72/href</a></iframe><p>We’ve decoupled Eli’s methods into a new struct called BudgetPlanner (better name left as exercise to reader). I’ve also added a new method which determines whether or not the user is on the right trajectory to meet their monthly savings goal.</p><h4>Presentation &amp; UI</h4><p>Let’s create an object with <strong>Terry</strong> and “the presenter” in mind. Let’s call it BudgetPresenter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8631e71c9a9bbcd35a704ba452f8a3f3/href">https://medium.com/media/8631e71c9a9bbcd35a704ba452f8a3f3/href</a></iframe><p>Now we’ve made another simple model for all our UI presentation needs. If we want to release our app in China, all we need to do is make those Strings NSLocalizedString objects.</p><p>We can also create colour assets in our assets file to easily change the styling of our app, like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S6xRnFpast36aYyWRQ3SyA.png" /></figure><p>By doing this we’ve also removed a lot of the heavy lifting from our BudgetDashboardController. As a side note, with <a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a> coming soon, it’ll be a lot easier to decouple UI logic from the business logic of your app.</p><h4>Back-end Interface</h4><p>Finally, let’s talk about <strong>Matt</strong>. We originally designed BudgetPlan to be easily transferable to a back-end, but what if we deem there’s certain properties we don’t want to persist, namely netToDate since it’s so easily calculable? We could implement Codable‘s protocol methods to tell it which fields to serialize, but that would be too much responsibility.</p><p>Let’s create a data transfer object (DTO) designed solely to be sent to our back-end so we no longer have to worry about adding properties to BudgetPlan.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d38b1ea2f7d348428493812c6f979dc/href">https://medium.com/media/8d38b1ea2f7d348428493812c6f979dc/href</a></iframe><p>Voilá. It holds only properties with no methods, and can be initialized from a BudgetPlan object. Now BudgetPlan no longer has to know anything about back-end interfaces.</p><p>What about Transaction? Should we create a TransactionDetail DTO as well? Maybe, especially if we add a new view controller just to sift through transactions. In the current state, however, I think it would look too much like a restatement of the Transaction implementation.</p><p>For the communication between the BackendManager and BudgetDetails many options exist. You could create a BudgetCommunicator struct in the same source file as BudgetDetails with all the REST methods you might use, or design a general Communicator struct using generics, protocols and a factory get method with NotificationCenter notifications.</p><h3>The Result…</h3><p>Let’s revisit BudgetPlan. I’ve replaced that multi-tasking setter with a simple property observer on the transactions model which resets the array if it’s a new month (although the method’s logic might be lacking). Now our model is just a container object with some utility methods a properties to make our lives easier.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5a27d53975bb53eea8de3bf7aa3280d/href">https://medium.com/media/a5a27d53975bb53eea8de3bf7aa3280d/href</a></iframe><p>Here’s our new Transaction struct. Without the misplaced formatting utility method, it’s a lot more concise.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e9645af28423947a7afb4a52d9d8761/href">https://medium.com/media/3e9645af28423947a7afb4a52d9d8761/href</a></iframe><p>To summarize, we turned two structs, each with a lot of responsibility into:</p><ul><li>BudgetPlan and Transaction which are now simple business objects,</li><li>BudgetPlanner which encapsulates all the business logic,</li><li>BudgetPresenter which turns logic into meaningful data,</li><li>and BudgetDetails which is a DTO responsible for encapsulating the properties we want to persist.</li></ul><h3>Conclusion</h3><p>Using the single responsibility principle is not easy. It requires scrupulous thought and consideration, and even then you may have to refactor more than you’d like. Or, after working so hard to decouple your app you may find out pieces are truly more dependent on each-other than you anticipated, and the root of the problem is actually very deep.</p><p>The single responsibility principle is also divisive, and the age old “should business objects contain business logic” question doesn’t always have a great answer, except for “it depends.”</p><p>With that being said, using the single responsibility principle properly can, and should make your code easier to update to fit the changing needs of stakeholders in turn giving your software more value.</p><h4>Citations</h4><p><em>Wikipedia Article on SOLID </em>— <a href="https://en.wikipedia.org/wiki/SOLID">https://en.wikipedia.org/wiki/SOLID</a><br><em>Bob Martin’s Clean Code</em> — <a href="https://amzn.to/2JtSDPx">https://amzn.to/2JtSDPx</a><br><em>Swift Docs</em> — <a href="https://docs.swift.org/swift-book/">https://docs.swift.org/swift-book/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=33d1c1659f3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to backup your music collection in 8.8MB]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@jacobgoldfarb/how-to-backup-your-music-collection-in-8-8mb-ed3e80d671ea?source=rss-89fe17d71eed------2"><img src="https://cdn-images-1.medium.com/max/1764/1*uzUopzHsfpdiRXxvPRmqiA.png" width="1764"></a></p><p class="medium-feed-snippet">How to backup your music collection in 20KB; or, How I lost and recovered my Apple Music library</p><p class="medium-feed-link"><a href="https://medium.com/@jacobgoldfarb/how-to-backup-your-music-collection-in-8-8mb-ed3e80d671ea?source=rss-89fe17d71eed------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@jacobgoldfarb/how-to-backup-your-music-collection-in-8-8mb-ed3e80d671ea?source=rss-89fe17d71eed------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed3e80d671ea</guid>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[apple-music]]></category>
            <category><![CDATA[itunes]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Jacob Goldfarb]]></dc:creator>
            <pubDate>Mon, 11 Feb 2019 22:32:55 GMT</pubDate>
            <atom:updated>2019-02-12T03:49:34.027Z</atom:updated>
        </item>
    </channel>
</rss>