<?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:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[J7mbo | James Mallison]]></title><description><![CDATA[J7mbo - James Mallison]]></description><link>https://blog.j7mbo.com/</link><image><url>https://blog.j7mbo.com/favicon.png</url><title>J7mbo | James Mallison</title><link>https://blog.j7mbo.com/</link></image><generator>Ghost 3.42</generator><lastBuildDate>Mon, 13 Apr 2026 15:39:29 GMT</lastBuildDate><atom:link href="https://blog.j7mbo.com/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[On becoming an Engineering Manager - Part 1]]></title><description><![CDATA[Some thoughts on my journey into the role of an Engineering Manager at work, what the role typically entails, how you might become one and some general thoughts on leadership that you may find interesting.]]></description><link>https://blog.j7mbo.com/becoming-an-engineering-manager-part-1/</link><guid isPermaLink="false">608c3dc0de4cb303a109566e</guid><category><![CDATA[Leadership]]></category><dc:creator><![CDATA[James Mallison]]></dc:creator><pubDate>Sat, 01 May 2021 20:36:39 GMT</pubDate><media:content url="https://blog.j7mbo.com/content/images/2021/04/Effective-leadership.png" medium="image"/><content:encoded><![CDATA[<img src="https://blog.j7mbo.com/content/images/2021/04/Effective-leadership.png" alt="On becoming an Engineering Manager - Part 1"><p>This is intended to be a multi-part series because I have a lot to write about here. This one is about my background in leadership and where my interest in moving into an EM role came from.</p><p>As an Engineering Manager I've been asked a lot by colleagues who also want to take a similar path - how did you do it? What does an EM even do? How can I do something like that? Why did you go down that path?</p><p>I thought I'd write some thoughts on my journey into the role of an Engineering Manager at work, what the role typically entails, how you might become one and some general thoughts on leadership that you may find interesting.</p><h2 id="that-one-time-i-lived-in-mallorca-">That one time I lived in Mallorca...</h2><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://blog.j7mbo.com/content/images/2021/04/bike_mallorca.jpeg" class="kg-image" alt="On becoming an Engineering Manager - Part 1" srcset="https://blog.j7mbo.com/content/images/size/w600/2021/04/bike_mallorca.jpeg 600w, https://blog.j7mbo.com/content/images/size/w1000/2021/04/bike_mallorca.jpeg 1000w, https://blog.j7mbo.com/content/images/size/w1600/2021/04/bike_mallorca.jpeg 1600w, https://blog.j7mbo.com/content/images/size/w2400/2021/04/bike_mallorca.jpeg 2400w" sizes="(min-width: 720px) 720px"><figcaption><em>My salary mostly went on the Ducati...</em></figcaption></figure><hr><p>When I worked at <a href="https://www.linkedin.com/in/james-mallison-a647694b/">trivago</a> as a <strong>Señor Software Engineer</strong> in Mallorca - which helped fund a replacement Ducati after writing off the one I'd crashed back in England* (*it wasn't my fault!!) -  after six months~ish I was asked by the department lead to take on "one of the most important projects for the company" (brought in several hundred million euros per year, exciting). I would be placed in a <strong>Technical Lead</strong> role and given the ability to build a team and the software as well. I jumped at the chance - it sounded really interesting. Given that I had no experience in any sort of lead role, I knew this was going to be a real challenge for me. I had no idea what I was in for...</p><figure class="kg-card kg-image-card"><img src="https://blog.j7mbo.com/content/images/2021/04/a335b9275180e7f594ce9db5441cc1f4.jpg" class="kg-image" alt="On becoming an Engineering Manager - Part 1"></figure><p>Two years later I was almost burned out. Burned out as in, having lost all enthusiasm for coding, creativity, working or anything in general (which is stress), but also more physically ill than I'd ever been before in my life! I'd hired a team of developers having done the recruiting, interviewing, onboarding and upskilling myself - let me tell you, the recruiting took absolutely ages on it's own - one of the first more junior guys to join the team was about to be fired by the department lead because of ineffective behaviour over a long period of time that I had no idea how to handle because I'd never experienced that before, and the project had been grossly over-engineered. I remember buying books on how to better communicate with different personality types, attending training courses to try and get better at the communication side of things, and trying everything I could to learn what I needed to improve on.</p><p>Another one of the first people to join the team barely spoke a word of English and didn't have any experience with distributed systems, so there was a huge communication barrier and learning curve for them. I remember them getting a little frustrated and almost giving up sometimes, just nodding and saying "yes" when I tried to explain something because the language barrier was difficult and playing charades and joking can only get you so far when you don't speak the same language. But by the end of two years together, and it did take a lot of effort from both of us, not only had their English vastly improved so they could communicate with everyone better but they had also become a key member of the team with very valuable contributions. My whole journey there I believe aged me about 5 years.</p><p>By the end I had two teams: one team of developers and the other devops. I'd learned an absolute tonne of different technologies and their positives and drawbacks, from distributed systems, docker swarm, real fully automated CI/CD - I implemented <a href="https://www.jetbrains.com/upsource/">upsource</a> and automated our production deployments when code reviews were approved and closed by the code owner - to communication with colleagues and stakeholders and agile processes. Each new member of the team had their own specialisation that they brought in with them and I am immensly grateful to them for having taught me so much in such a short period of time. But the 'startup culture', extreme pressure, zero support and lack of leadership above me had taken it's toll on my health - I was in hospital for random things more times when I was in Mallorca over a sixt month period than I have been over here in Bavaria in two years - even more than ever in my life actually - and it was simply because of this stress.</p><h2 id="so-it-was-kinda-good-but-kinda-bad-too-">So it was kinda good, but kinda bad too...</h2><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://blog.j7mbo.com/content/images/2021/05/trv.JPG" class="kg-image" alt="On becoming an Engineering Manager - Part 1" srcset="https://blog.j7mbo.com/content/images/size/w600/2021/05/trv.JPG 600w, https://blog.j7mbo.com/content/images/size/w1000/2021/05/trv.JPG 1000w, https://blog.j7mbo.com/content/images/size/w1600/2021/05/trv.JPG 1600w, https://blog.j7mbo.com/content/images/size/w2400/2021/05/trv.JPG 2400w" sizes="(min-width: 720px) 720px"><figcaption><em>I met some awesome people at trivago</em></figcaption></figure><hr><p>My time at trivago was bittersweet. I met some awesome people there. But looking back it was also stressful to the extreme and definitely affected my health both physically and mentally. You know all those people on twitter that like to say "mental health is important!!" and go on about it forever - they're absolutely not bloody wrong!! At the end when the company was going further downhill and a while before they closed their offices in Mallorca, I left and joined Sixt in Bavaria (which is an absolutely amazing place to live) with the intention of learning Go and working more with distributed systems.</p><h2 id="sixt">Sixt</h2><p>The move was pretty simple: Sixt flew me to Bavaria in October, interviewed me, I went to Oktoberfest, flew back to Mallorca and got the job offer from them all within five days. At Sixt I did exactly what I planned on doing - I learned Go (and I'm not just talking about the syntax here; I'm talking best practices, edge cases to be concerned about, the purism from inexperienced developers in the Go community specifically etc.) and then <a href="https://www.youtube.com/watch?v=EhAlKVUuw-w&amp;list=PLUXT7uzX81byVuvjGmF-OopGKZo_wxcmq">I gave a conference talk at GoDays in Berlin</a> on some of the things I'd learned and some crazy automatic reflection-based dependency injection implementation I'd tried in that time all within two months.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://blog.j7mbo.com/content/images/2021/05/IMG_3289.jpeg" class="kg-image" alt="On becoming an Engineering Manager - Part 1" srcset="https://blog.j7mbo.com/content/images/size/w600/2021/05/IMG_3289.jpeg 600w, https://blog.j7mbo.com/content/images/size/w1000/2021/05/IMG_3289.jpeg 1000w, https://blog.j7mbo.com/content/images/size/w1600/2021/05/IMG_3289.jpeg 1600w, https://blog.j7mbo.com/content/images/2021/05/IMG_3289.jpeg 1930w" sizes="(min-width: 720px) 720px"><figcaption><em>Sixt peoples :)</em></figcaption></figure><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://blog.j7mbo.com/content/images/2021/05/IMG_2821.JPG" class="kg-image" alt="On becoming an Engineering Manager - Part 1" srcset="https://blog.j7mbo.com/content/images/size/w600/2021/05/IMG_2821.JPG 600w, https://blog.j7mbo.com/content/images/size/w1000/2021/05/IMG_2821.JPG 1000w, https://blog.j7mbo.com/content/images/size/w1600/2021/05/IMG_2821.JPG 1600w, https://blog.j7mbo.com/content/images/size/w2400/2021/05/IMG_2821.JPG 2400w" sizes="(min-width: 720px) 720px"><figcaption><em>Here's a picture of me drinking a cup of tea whilst giving my first Go talk at a conference</em></figcaption></figure><hr><p>At Sixt I was tasked yet again with leading a team to rewrite another over-engineered piece of software, but this time I focussed on ensuring the team did so with a really decent dose of pragmatism. It was a resounding success! After doing this I said to myself hey, I've done this with Swift before, now I've done it with Go, I could do it with Rust next... and then I realised that actually what I was doing wasn't really a challenge for me any more. The only way up was for me to learn completely unecessary architectures and technologies for 99% of use-cases and then sell them to inexperienced developers as a salesman on twitter! I'm only 32, I don't have to be a sellout just yet...</p><p>I'm lucky to be a very fast learner though so picking up a language, learning the frameworks and best practices and then building something production-ready and scalable was just a bit easy for me now. I wanted something else: to have more impact. And so my enthusiasm for work and drive to learn and progress returned. That took a while... burning out is no laughing matter folks.</p><p>So why after all the stress at trivago would I want to be in any position of leadership again? I was given some parting words by a colleague of mine there that really had an impact on me:</p><blockquote>James, you might not know it, but your team loves you.</blockquote><p>I still keep in touch with a lot of these people by the way - before the pandemic we met up for drinks, visited eachother's countries and I went with a bunch of them abroad for a two week bachelor party in Vietnam... but that is an entirely different story that is absolutely not in the scope of this blog post!</p><p>But most importantly for me: to know that I had such an impact on the people themselves, their careers and the team that I built and led is still something I think about regularly. Messaging some of them on telegram and telling them that I was considering an Engineering Manager role quickly filled my screen with words of encouragement and praise in response. I want to replicate that success again, this time not in a startup culture, this time with support from those who have done this successfully themselves, and this time with a healthy dose of pragmatism, the knowledge of how to communicate expectations properly to all parties and the maturity, growth and experience behind me needed to be successful. </p><p>Figuring out just exactly how to move into another leadership role was to be my next challenge.</p><h2 id="onto-what-an-em-actually-does-">Onto what an EM actually does...</h2><p>So what is an EM? What do they actually do? Here I provide the favourite words of consultants everywhere: "It depends".</p><p>Saving this one for Part II. Coming soon...</p>]]></content:encoded></item><item><title><![CDATA[Constructors in Go]]></title><description><![CDATA[One of Go's weaknesses is the ability to initialise a struct in an 'invalid state', leading to the potential of accidental nil pointer dereferencing further down the line. Learn how we solved this pragmatically.]]></description><link>https://blog.j7mbo.com/constructors-in-go/</link><guid isPermaLink="false">5ff7813a275cf4039015326f</guid><category><![CDATA[Golang]]></category><dc:creator><![CDATA[James Mallison]]></dc:creator><pubDate>Sun, 29 Dec 2019 13:04:14 GMT</pubDate><media:content url="https://blog.j7mbo.com/content/images/2021/01/Screenshot-2019-02-21-at-15.43.19-1-1.png" medium="image"/><content:encoded><![CDATA[<!--kg-card-begin: markdown--><img src="https://blog.j7mbo.com/content/images/2021/01/Screenshot-2019-02-21-at-15.43.19-1-1.png" alt="Constructors in Go"><p>When I first started working with Go, I wrote a blog post on <a href="https://blog.j7mbo.com/bypassing-golangs-lack-of-constructors/">&quot;bypassing Go's lack of constructors&quot;</a>, or initialisers as they're known in Go.</p>
<p>Go doesn't support traditional <em>default constructors</em>, but it does provide constructor-like factory functions useful for initialising types.</p>
<p>This post is effectively a part II where I basically do a 180 on my original idea in the post linked above. But first, why the original post?</p>
<h2 id="avoidingnilpointers">Avoiding nil pointers</h2>
<p>Go has a lot of strengths as a language, from a simple userland concurrency API to being very quick to set up a microservice communicating over HTTP with only the built-in webserver, which is production ready, or very easily over RPC with other microservices with something like protobuf.</p>
<p>For me, the fact that Go is a strictly typed language is also a strength - you have to be explicit in what you are building and this actually makes your programs easier to read. I'm also starting to see the benefits of only needing small, implicit interfaces defined next to the consumers. This is a big part of SOLID - <a href="https://en.wikipedia.org/wiki/Interface_segregation_principle">interface segregation</a>. In fact, my team and I are planning on removing all explicit interfaces from the codebase.</p>
<p>On the flipside, as all languages have, Go has some weaknesses too. One of these is the ability to initialise a struct in an 'invalid state', leading to the potential of accidental nil pointer dereferencing further down the line.</p>
<script src="https://gist.github.com/J7mbo/8d7b5d5255f50da3655f6c3f64a7f162.js"></script>
<p>Note how in the above example, the use of Service{} allows the Service to be initialised without dependencies. And there's no valid reason these dependencies should not exist.</p>
<p>This can be mitigated in part through a technique called &quot;making the zero value useful&quot; and involves actually allowing your code to call methods on nil pointers and returning an empty string, or struct, or whatever your return type is. So now if, for some reason, your Service is nil or the repository within it is nil, you just return a default value instead.</p>
<script src="https://gist.github.com/J7mbo/eba5646124a376dc9649272f85f373bb.js"></script>
<p>So instead of finding out what exactly is wrong with your code at compile-time, a real strength of using a compiled language like Go, you find out not only that <em>something</em> is wrong somewhere in the application, but also that <em>you're not quite sure where or why</em> it is and so debugging throughout the whole stack ensues.</p>
<blockquote>
<p>&quot;Useful&quot;, it seems, is either in the eye of the beholder or the evangelist; not necessarily in the eye of the software engineer.</p>
</blockquote>
<h2 id="enforcingvalidstate">Enforcing valid state</h2>
<p>The ideal scenario would be to never even have the option of nil pointers for dependencies in the first place, so you never need to write pointless filler code to check if anything that is supposed to exist anyway <em>might</em> be nil. A runtime dependency injector could enforce this, but again this comes with the drawback of losing all those valuable compile-time guarantees. There are no compile-time dependency injectors in Go, only <a href="https://github.com/google/wire">pre-compile-time object graph generators</a>.</p>
<p>Initially in my experiments with Go, I realised that initialisation of structs in a valid state could be enforced at the language level through elaborate use of factory methods, private structs and interfaces, with the factory method returning the interface. Returning an interface is shown in the manual as okay given the case that both the interface and the implementing struct have the exact same methods defined.</p>
<script src="https://gist.github.com/J7mbo/788c4071e4c9dc1fa3c55a5d6854e31b.js"></script>
<p>With the above, you can no longer write s := Service{} or r := Repository{} because <strong>these structs are not exported</strong>. The only way to access them is with a call to New(), which returns only the exported interface containing the exact same methods the struct has. An additional point here is that the Service cannot exist without it's non-nillable dependency (we're not allowing a pointer to be passed in here).</p>
<p>This strategy effectively enforced constructor-like semantics one can see in other languages - a single, tightly controlled entrypoint for intialisation that cannot be bypassed (except via the reflect and unsafe packages, which is beyond the scope of this article and likely wouldn't easily sneak past code review anyway).</p>
<p>There were a few drawbacks with this approach. One of them was the introduction of duplicate code because we were creating an explicit interface next to every single struct in the codebase, just to enforce something that should have been in the language by default and may not even happen that regularly. Another drawback was godoc didn't work properly with the methods documented on the interface.</p>
<p>In fact, when using this on a team, it ended up being more trouble than it was worth. Everyone agreed that we had no use for &quot;useful zero values&quot;, as the business had no concept for them, and so the only reason we would use &quot;zero values&quot; would be to satisfy the compiler and drawbacks of the language, and would simply consist of 'filler code' - so we opted to leave them out in many cases. This was actually the best outcome for us as we didn't need to care about nils any more unless they were absolutely required and intentional.</p>
<h2 id="whatworkedbestforus">What worked best for us</h2>
<p>The end result agreed on was: always use the New convention for every single struct. Unfortunately this could not be enforced at the language level, but simple static analysis on CI and a linter could check for initialisation of structs that didn't use a factory method in any file that was not a test, and this ended up providing the best balance for us (tests can use shortcuts when needed).</p>
<p>This approach had four benefits:</p>
<ul>
<li>Every single struct had one point of initialisation and validation, so would always be valid once initialised, simply via focussing on New().</li>
<li>Chances of nil pointers found only at runtime were significantly reduced.</li>
<li>The &quot;default value&quot; approach would never need to be considered throughout the codebase which removed a lot of the associated cognitive overhead required to work with something that is effectively proprietary in Go.</li>
<li>New Go developers were able to on-board and work with the codebase faster because they didn't need to understand any unnecessary intricacies.</li>
</ul>
<p>At the end of the day you should be using a language to bring value and further something. Perfectionism for the sake of it only hinders this progress and being pragmatic when working with programming languages and their drawbacks only gets you where you want to be faster. The extra effort required to try and force the language to do something it couldn't easily handle at the language was an interesting experiment, but that was about it. We solved this with process, agreement and a tiny bit of automation at the CI level to provide us that extra guarantee and that was enough for us.</p>
<p>It has been suggested that I should take a look at Rust next. This language will also have it's drawbacks I'm sure, but perhaps it might align more with the philosophy of 'safety by default' which I'm starting to see real value in, and is something I definitely think should be a goal for many languages to strive towards.</p>
<p>If you're interested in my first blog post where I argue for using the private struct, public interface technique, you can read it <a href="https://blog.j7mbo.com/bypassing-golangs-lack-of-constructors/">here</a>. Otherwise, going forward, using the New convention wherever possible I think is a wiser and more pragmatic decision.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[The law of obviousness]]></title><description><![CDATA[Simple code extremely obvious in intention over unecessary abstraction and indirection.]]></description><link>https://blog.j7mbo.com/the-law-of-obviousness/</link><guid isPermaLink="false">5ff7813a275cf4039015326e</guid><category><![CDATA[Architecture]]></category><dc:creator><![CDATA[James Mallison]]></dc:creator><pubDate>Thu, 19 Sep 2019 16:11:02 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><blockquote>
<h1 id="therearebenefitsinsoftwaredevelopmentfromcodeextremelyobviousinitsintentionratherthanunnecessaryabstractionandindirection">There are benefits in software development from code extremely obvious in its intention rather than unnecessary abstraction and indirection.</h1>
</blockquote>
<p><em>Short:</em></p>
<blockquote>
<h1 id="simplecodeextremelyobviousinintentionoverunnecessaryabstractionandindirection">Simple code extremely obvious in intention over unnecessary abstraction and indirection.</h1>
</blockquote>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Bypassing Golang's lack of constructors]]></title><description><![CDATA[Golang doesn't provide constructors but instead advocates for a package-level factory method with the New convention. If only this were then the singular way of initialising an object; unfortunately this is not always necessarily the case..]]></description><link>https://blog.j7mbo.com/bypassing-golangs-lack-of-constructors/</link><guid isPermaLink="false">5ff7813a275cf4039015326a</guid><category><![CDATA[Golang]]></category><dc:creator><![CDATA[James Mallison]]></dc:creator><pubDate>Fri, 22 Feb 2019 11:41:34 GMT</pubDate><media:content url="https://blog.j7mbo.com/content/images/2021/01/Screenshot-2019-02-21-at-15.43.19-1-1-1.png" medium="image"/><content:encoded><![CDATA[<!--kg-card-begin: markdown--><img src="https://blog.j7mbo.com/content/images/2021/01/Screenshot-2019-02-21-at-15.43.19-1-1-1.png" alt="Bypassing Golang's lack of constructors"><p>I've been writing Go for around a month now as for me it was a requirement that I get to learn it as a part of my <a href="https://www.linkedin.com/in/james-mallison-a647694b/">new role at Sixt</a>. One of the more interesting or frustrating aspects of the language, coming from a PHP background, is the ability to initialise objects in an invalid state.</p>
<p>Coming from an OO language, I am used to the ability to define constructors on objects - static methods that are an entrypoint into building an object. Without calling this, and without messing around with reflection, there is no other way to create one of these objects.</p>
<p>Golang doesn't provide constructors but instead advocates for a package-level factory method with the <code>New</code> <a href="https://golang.org/doc/effective_go.html#composite_literals">convention</a>. If only this were then the singular way of initialising an object; unfortuntely this is not always necessarily the case.. and so embarks my internal struggle on 'the right way' of doing things in this programming language.</p>
<p>How can I emulate what I gained from constructors whilst adhering more towards &quot;idiomatic go&quot;?</p>
<h2 id="constructors">&quot;Constructors&quot;</h2>
<p>Firstly, let's define the goal. I want to provide a very simple object API to only allow third-party consumers to create an object in a <em>valid state</em>. Take a look at the following example that follows Golang's conventions:</p>
<pre><code class="language-go">type MyStruct struct { }

func NewMyStruct() MyStruct {
    return MyStruct{dep: ADependency{}}
}

type ADependency struct { }

myStruct := NewMyStruct()  // We get back a valid struct
</code></pre>
<p>This is great. We have a factory function by convention to create an instance of MyStruct. However, we can also do the following:</p>
<pre><code class="language-go">myStruct := MyStruct{}
</code></pre>
<p>Boom - we now have an instance of <code>MyStruct</code> in an invalid state. Why does this matter?</p>
<pre><code class="language-go">type MyStruct struct {
	dep *ADependency
}

func (m *MyStruct) DoSomethingWithDep() {
    m.dep.DoSomething()
}

type ADependency struct { 
    num int
}

func (a *ADependency) DoSomething() {
    fmt.Println(a.num)
}

(&amp;MyStruct{}).DoSomethingWithDep()
</code></pre>
<p>The above gives us:</p>
<blockquote>
<p>panic: runtime error: invalid memory address or nil pointer dereference</p>
</blockquote>
<p>This is simply because <code>ADependency</code> was not initialised. It's a nil. This problem exists in part because we are able to initialise this object in an invalid state, and in more complex codebases this can go unnoticed and blow up at runtime further down the line.</p>
<p>What can we do about this to ensure that it doesn't happen accidentally but in a consistent way?</p>
<h2 id="makingthingsprivate">Making things private</h2>
<p>The reason we can call <code>MyStruct{}</code> is because the type is exported from the package - meaning it's publically available for third parties (other packages) to use. To fix this, we can make it private:</p>
<pre><code class="language-go">/* Yep, now it's private because of the lower-cased 'm' */
type myStruct struct {
    num int
}

func NewMyStruct() *myStruct {
    return &amp;myStruct{num: 42}
}
</code></pre>
<p>Now, from <em>another package</em>, we cannot call <code>myStruct{}</code>. This struct's initialisation is only available via calling <code>PackageName.NewMyStruct()</code> <em>from other packages</em>. This is great! However, this limitation does not exist from within the same package so you just need to make sure that you enforce (again, not by language design but by your own implementation) that you create this object correctly behind the veil of your package.</p>
<h2 id="butwhatabouttypedeclarations">But what about type declarations?</h2>
<p>So let's presume we have <code>myStruct</code> in <code>Package1</code>, and then a struct within <code>Package2</code> wants an instance of <code>myStruct</code> passed in via a type declaration on a method signature:</p>
<pre><code class="language-go">package Package2

type SomeService struct { }

func (*SomeService) DoSomething(obj myStruct) { /* WONT WORK!!*/ }
</code></pre>
<p>Oops! Because <code>myStruct</code> is from <code>Package1</code> and is not an exported type any more, <code>Package2</code> can't access it. We can't even ask for an <code>interface{}</code> and type assert because the type is not exported; it simply doesn't exist in this context.</p>
<h2 id="interfaces">Interfaces</h2>
<p>One way to maintain a private struct but be able to typehint for it in another package would be via a public interface for <code>MyStruct</code>. You cannot initialise an interface, but we can use the type in other packages:</p>
<pre><code class="language-go">package Package1

/* The interface is exported, so publically available */
type MyStructsInterface interface {
    SomeInterfaceMethod() int
}

/* Private struct */
type myStruct struct {
    num int
}

func NewMyStruct() myStruct {
    return MyStruct{num: 22}
}

/* With this, myStruct now implicitly implements MyStructsInterface */
func (*myStruct) SomeInterfaceMethod() int { return 42 }
</code></pre>
<p>Now in other packages' function declarations we can typehint for an instance of <code>MyStructsInterface</code> and use it's public API, and we're also only able to construct the object in a valid state with the <code>NewMyStruct</code> function. GREAT!</p>
<p>So this just means we need to...</p>
<h2 id="interfaceeverything">Interface Everything</h2>
<p>This means that we now have to fully <em>interface everything</em> that we want to be able to use in another package, if we want to protect from the object being built in a way we do not intend. In some cases this is fine, however interfacing everything is overkill and absolutely unnecessary. What about objects that don't actually have any public methods, just a bunch of properties? We can't interface those with because of Go's implicit interfaces (there's no method to add to an interface to say &quot;our object implements this interface now&quot;) and this would be a code smell anyway.</p>
<h2 id="nilpointers">Nil Pointers</h2>
<p>Let's say we decide that interfacing everything is a stupid idea - it is not pragmatic nor necessary and we would be doing it because of the lack of a language feature, not because we actually need them, which points to this being a code smell. Instead, let's publically expose that struct so we can typehint for it and see what can go wrong.</p>
<pre><code class="language-go">type MyStruct struct { 
    child *ChildStruct
}

func (s MyStruct) StructFunc() { 
    /* Here's where things go wrong */
    fmt.Println(s.child.num)
}

type ChildStruct struct {
    num int
}

func DoSomething(obj MyStruct) {
    obj.StructFunc()
}

DoSomething(MyStruct{})
</code></pre>
<p>We can pass in a <code>MyStruct</code> initialised only with <code>MyStruct{}</code>. The function it is passed into calls <code>s.child</code>, which will be <code>nil</code>, and when we try and call <code>.num</code> on this nil we will get:</p>
<blockquote>
<p>panic: runtime error: invalid memory address or nil pointer dereference</p>
</blockquote>
<p>So we would have to guard against this:</p>
<pre><code class="language-go">type MyStruct struct { 
    child *ChildStruct
}

func (s *MyStruct) StructFunc() { 
    if s.child != nil {
        fmt.Println(s.child.num)
    }
}
</code></pre>
<p>Above we declare a function on <code>*MyStruct</code>. What about if <code>s.child</code> isn't a pointer but instead is a value (struct)?</p>
<h2 id="emptystructs">Empty Structs</h2>
<p>Consider the following:</p>
<pre><code class="language-go">type MyStruct struct { 
    /* Note how s.child is not a pointer any more */
    child ChildStruct
}

func (s *MyStruct) StructFunc() {
    if s.child != nil {
        fmt.Println(s.child.num)
    }
}
</code></pre>
<p>The <code>!= nil</code> check will no longer work. We will get the following panic:</p>
<blockquote>
<p>prog.go:22:16: cannot convert nil to type ChildStruct</p>
</blockquote>
<p>So instead we have to check for an empty struct:</p>
<pre><code class="language-go">func (s MyStruct) StructFunc() {
    if s.child == (ChildStruct{}) {
    	return
    }

    fmt.Println(s.child.num)
}
</code></pre>
<p>This is just another way through which we have to defend code against nils if we allow the type to be exported.</p>
<h2 id="wherewearenow">Where we are now</h2>
<p>We have the following options:</p>
<ul>
<li>Check for a <code>nil</code> for every pointer we have a type declaration for, in every function in the codebase</li>
<li>Check for an empty struct for every concrete type we have a type declaration for, in every function in the codebase</li>
<li>Interface everything so we can only build objects in a valid state, and then there is no easy way to create an invalid <code>MyStruct</code> - we can't use <code>package1.myStruct{}</code> and we can't use <code>new(package1.myStruct)</code> - only <code>package1.NewMyStruct()</code>.</li>
</ul>
<p>But I don't want to interface everything!!</p>
<h2 id="encapsulation">Encapsulation</h2>
<p><img class="center" src="https://blog.j7mbo.com/content/images/2021/01/IPC-Dresden_Icon_encapsulation.gif" alt="Bypassing Golang's lack of constructors"></p>
<blockquote>
<p>A language mechanism for restricting direct access to some of the object's components.</p>
</blockquote>
<p>I require the ability to restrict any object's initialisation to a single entrypoint which guarantees that it cannot be initialised in an invalid state and rightly so, in whatever programming language I am using that has some object oriented capabilites. But in doing so, in Golang, I require an interface to typehint for it anywhere else because of package visibility and a lack of real constructors.</p>
<p>In PHP for example, we have: <code>__construct()</code> which can be the only way to create an instance of a class, and then via that, and our implementation, we guarantee the object exists in a valid state and we can typehint for that as a dependency elsewhere ensuring we only get that valid object to work on. It doesn't seem like I can do anything this with Golang. Is there anything better?</p>
<h2 id="implicitinterfaces">Implicit Interfaces</h2>
<p>Go is special in the way that it's not like Java, PHP or other OO languages with explicit interfaces. For example in PHP, our object would implement an interface only when we explicitly type that it does:</p>
<pre><code class="language-php">interface MyInterface { }

class MyClass implements MyInterface { } // Explicit
</code></pre>
<p>In Go on the other hand, we have implicit interfaces:</p>
<pre><code class="language-go">type MyInterface interface { 
    MethodMustBeImplemented()
}

type MyStruct struct { }

func (*MyStruct) MethodMustBeImplemented() { } // Implicit
</code></pre>
<p>Idiomatic Go suggests that sometimes you should define interfaces not next to or near the class that implements them, as you would do with traditional OO languages, but rather <em>alongside the type that will use it</em>. This is an immensely powerful feature once you get to grips with what it means for your code.</p>
<p>Imagine the following struct again with our <code>New</code> best practice used. Note that we are not exporting the type, so we can avoid the problems mentioned earlier:</p>
<pre><code class="language-go">package Package1

type myStruct struct { }
func (*myStruct) SomeFunc() int { return 42 }
func (*myStruct) SomeOtherFunc() string { return &quot;42&quot; }

/* Factory */
func NewMyStruct() *myStruct {
    return &amp;myStruct{}
}
</code></pre>
<p>Now a struct in <code>Package2</code> requires the ability to use this struct - but let's think about this for a moment. <strong>What exactly does Package2 want?</strong> - The answer is not the struct, but the ability to call an API method on the struct.</p>
<p>We could go about declaring a globally available interface, which I've already elaborated wasn't the best way of doing things. We don't want to interface everything.</p>
<p>But let's say that our consumer actually only wants to call one method on this struct. We can define an implicit interface, next to the consumer, only containing the one method!</p>
<pre><code class="language-go">package Package2

/* Implicit interface time! */
type interfaceToConsume interface {
    SomeFunc() int
}
</code></pre>
<p>Immediately on creating this interface in <code>Package2</code>, the struct from <code>Package1</code> can actually fulfil this requirement and fulfil this interface. Now if we add the following code to <code>Package2</code>, we can take in an instance of <code>MyStruct</code> without explicitly declaring that we need to do so with the struct that is not publically available.</p>
<pre><code class="language-go">type consumer struct { }

func NewConsumer() *consumer {
    return &amp;consumer{}
}

func (*consumer) DoSomething(obj interfaceToConsume) { 
    /* We can actually pass in a Package1.myStruct here! */
}
</code></pre>
<p>How can we pass in a <code>Package1.myStruct</code> when it is private?</p>
<pre><code class="language-go">package Package3

myStruct := Package1.NewMyStruct()
consumer := Package2.NewConsumer()

consumer.DoSomething(myStruct)
</code></pre>
<p>From this we have gained the following:</p>
<ul>
<li>Types are private so they cannot be initialised in an invalid state</li>
<li>We utilise Go's equivalent of constructors: factory functions for initialisation</li>
<li>Idiomatic Go usage in defining the interface next to the consumer</li>
<li>The implicit interface defined next to the consumer is also <em>Private</em>, which means we're not going to pollute the global namespace with random interfaces everywhere</li>
<li>Smaller interfaces for simpler test mocks - we don't need every method, only the ones we are implementing</li>
<li>Also adhereing more towards the <a href="https://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a> of SOLID (smaller, more specific interfaces AKA role interfaces)</li>
<li>You can remove all of the nil and empty struct checks that you repeat throughout the application because you have <em>guaranteed</em> that you can no longer have any of your objects in an invalid state</li>
<li>In the case that you do want to define a public interface to use everywhere, that's still perfectly fine as well, whether or not it is next to the consumer or traditionally next to the implementation</li>
</ul>
<p>Understanding this required a bit of a mindset change for me but it helped remove a lot of boilerplate and trust your code more. But there are always tradeoffs. Here however, I think there a less chances of error.</p>
<h2 id="conclusion">Conclusion</h2>
<p><strong>But you said you don't like having to declare an interface for every object</strong>! Well, we're not creating one huge interface for each struct and that technicality makes the difference. We are defining specific subsets of functionality that other structs can fulfil next to the consumer!</p>
<p>Usually there are tradeoffs to make in any language. For me, this is just a more fundamental one that I have to consider when designing the code. I would very much like to be able to request a valid type and not have to check that it has been initialised correctly, as there should only be a specific method of initialisation, and I would like to not have to create one huge interface for every object in the application.</p>
<p>It turns out that Go's implicit interfaces can provide the safety needed.</p>
<p>I'm really enjoying learning Go and having my mindset challenged and changed as I learn more. Golang definitely has an interesting way of doing things.</p>
<p>Is this also how you write defensively in Go, or have you found a better way of doing things? Let me know what you think in the comments.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[From callbacks to async / await in Swift]]></title><description><![CDATA[Most of the code you write in Swift has to at least consider asynchronicity when building an iOS app, apart from updating the ui, and even then you have to think about where the correct place to do this is.]]></description><link>https://blog.j7mbo.com/using-async-await-in-swift-with-awaitkit/</link><guid isPermaLink="false">5ff7813a275cf40390153266</guid><category><![CDATA[Swift]]></category><category><![CDATA[Async]]></category><dc:creator><![CDATA[James Mallison]]></dc:creator><pubDate>Wed, 08 Aug 2018 14:18:32 GMT</pubDate><media:content url="https://blog.j7mbo.com/content/images/2021/01/687474703a2f2f79616e6e69636b6c6f72696f742e636f6d2f7265736f75726365732f41776169744b69742d4172697374612d42616e6e65722e706e67-3.png" medium="image"/><content:encoded><![CDATA[<!--kg-card-begin: markdown--><h1 id="introduction">Introduction</h1>
<img src="https://blog.j7mbo.com/content/images/2021/01/687474703a2f2f79616e6e69636b6c6f72696f742e636f6d2f7265736f75726365732f41776169744b69742d4172697374612d42616e6e65722e706e67-3.png" alt="From callbacks to async / await in Swift"><p>It's been a while since I wrote a blog post, (actually, this is my first, I have no excuses!), but I really wanted to touch upon asynchronous programming in Swift. Most of the code you write in Swift must at least take asynchronicity into consideration at some level. When <a href="https://developer.apple.com/documentation/code_diagnostics/main_thread_checker">updating the ui</a> after some computation that typically does not belong in the controller layer, even then you have to think about where the correct place for this piece of code should be.</p>
<blockquote>
<p>Updating UI on a thread other than the main thread is a common mistake that can result in missed UI updates, visual defects, data corruptions, and crashes.</p>
</blockquote>
<p>When beginners start out with iOS, they use callbacks to execute certain functionality when another process has finished. Usually the final part of the chain of functionality which is executed upon operation completion, if required, would be to update the UI. Assuming this were the case, using callbacks we would have to pass an 'Update the UI' callback all the way through the chain of callbacks repeatedly until we get to the end of the chain, finally calling that callback to update the UI, assuming we don't want to pollute our domain / infrastructure logic with a call to updating the UI because we value <a href="https://en.wikipedia.org/wiki/Separation_of_concerns">Separation of Concerns</a>.</p>
<p>Let's take a look at how a piece of code can evolve from simple callbacks, through to utilising async / await, allowing you to write completely blocking code in a background thread with only a few extra lines of boilerplate each time. This is very close to the mayhem that I wrote when I first started out working with Swift.</p>
<h1 id="callbacks">Callbacks</h1>
<p><img src="https://blog.j7mbo.com/content/images/2021/01/run_from_callbacks-1.jpeg" alt="From callbacks to async / await in Swift"></p>
<p>Our use case: we're building an application that first will make a HTTP request to retrieve some json. Within this json is the URL of an image that we want to download. Finally, once we have downloaded that image, we should present it to the user and display it in the UI. Let's build this with callbacks first.</p>
<h2 id="step1theinitialhttprequest">Step 1 - The initial HTTP Request</h2>
<p>The first step would be to download some json using <code>Data(contentsOf:)</code>, which just performs a GET request to an endpoint without any headers or additional information. In fact, this is the worst way to perform a download; <code>Data(contentsOf:)</code> should be used for local files only, but for the purposes of this blog post, assume no error checking is absolutely deliberate.</p>
<pre><code class="language-swift">class DefaultViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        let response = try! Data(contentsOf: URL(string: &quot;http://our-data-endpoint.com&quot;)!)
        
        let jsonData = try! JSONSerialization.jsonObject(with: response, options: []) as! [String: Any]
        
        let imageUrl = jsonData[&quot;image_url&quot;]
    }
}
</code></pre>
<p>In this intentionally simplistic example I've removed libraries that we might use, we're using <code>Data(contentsOf:)</code> which you wouldn't do except with anything locally, no error checking is present, <a href="https://docs.swift.org/swift-book/ReferenceManual/Statements.html#//appleref/swift/grammar/if-statement">guard statements</a> aren't used etc. Force unwrap all the things!!</p>
<p>The first thing we do is synchronously <em>(note: we'll make it async next)</em> download a json string into the <code>response</code> variable, convert that json into an object over which we can iterate in <code>jsonData</code>, and then grab the <code>image_url</code> key from <code>jsonData</code>; so <code>imageUrl</code> now contains the url of the image we want to download.</p>
<p>The problem with this right now is that we're blocking the main thread (and run loop) while the json is downloading. The UI is in the main thread, so if the user is trying to do anything else like dragging or navigating somewhere else at the same time, well it won't work - these actions will be queued to be executed after the download has finished.</p>
<p>What can we do about this? We can use a background thread!</p>
<h2 id="step2multithreading">Step 2 - Multi-threading</h2>
<p><img src="https://blog.j7mbo.com/content/images/2021/01/multithreading-1.jpg" alt="From callbacks to async / await in Swift"></p>
<p>Grand Central Dispatch, or <a href="https://developer.apple.com/documentation/DISPATCH">GCD</a> for short, provides a simple and powerful API through which we can perform 'background' operations in a separate thread.</p>
<pre><code class="language-swift">DispatchQueue.global(qos: .background).async {
    let response = try! Data(contentsOf: URL(string: &quot;http://our-data-endpoint.com&quot;)!)
}
</code></pre>
<p>The above extra line of code (the first line), assuming we don't count the closing parenthesis at the end, places any calls within those parenthesis onto a background thread. Now we're not blocking the main thread so any UI changes like animations or user-invoked events like button presses can continue to be handled.</p>
<p>The problem is soon that we're going to be violating the principle of single responsibility and this class is quickly going to increase in size. If we place the image download code in here too, then this not only becomes a nightmare to test in the future but harder to refactor too.</p>
<p>Let's write code more towards the 'right way' and split things up a bit.</p>
<h2 id="step3srp">Step 3 - SRP</h2>
<p>Let's place our <code>JsonDownloader</code> into a struct and add an <code>onComplete</code> handler (closure) as a second parameter to be executed when the work has been completed asynchronously.</p>
<p>Why do we have to do this? Well, if we're using <code>.async { }</code> within a synchronous function, we can't just do a <code>return</code> from the async call because the async call <em>returns immediately</em> allowing the runloop to continue handling other things. One cannot simply return a variable from an asynchronous context to a synchronous one, because the asynchronous one returns immediately and the schronous code just continues like nothing happened. So we have to execute the callback function that we can pass in at the end instead.</p>
<p><em>Note we could still just hardcode the contents of each callback together but then we would end up with callback hell as well, and that's without the cleaner separation being shown here</em></p>
<pre><code class="language-swift">struct JsonDownloader
{
    public func downloadContentsOfUrl(url: URL, onComplete: @escaping (_ url: URL) -&gt; UIImage) -&gt; Void {
        // This async call will be thrown onto a thread and then this function will return immediately
        DispatchQueue.global(qos: .background).async {
            let json = try! Data(contentsOf: url)
            
            let jsonData = try! JSONSerialization.jsonObject(with: json, options: []) as! [String: Any]

            onComplete(URL(string: jsonData[&quot;image_url&quot;] as! String)!)
        }
    }
}
</code></pre>
<p>Notice that we've defined the <code>onComplete</code> callback to be a closure that takes a url and must return a <code>UIImage</code>. This is the next step in our process. We need to create this closure in our <code>UIViewController</code> and then pass it into our <code>JsonDownloader</code> to be executed upon completion of the json download at a later time.</p>
<h2 id="step4closuresclosuresclosures">Step 4 - Closures, Closures, Closures</h2>
<p><img class="center" src="https://blog.j7mbo.com/content/images/2021/01/closures.jpg" alt="From callbacks to async / await in Swift"></p>
<p>First, let's create our closure that takes a <code>String</code> and returns a <code>UIImage</code>. Then we can pass it into our call to <code>JsonDownloader</code>.</p>
<pre><code class="language-swift">class ViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        let imageDownloadCallback: (URL) -&gt; UIImage = { url in
            return UIImage(data: Data(contentsOf: url))!
        }
        
        let jsonDownloader: JsonDownloader = JsonDownloader()
        
        jsonDownloader.downloadContentsOfUrl(url: URL(string: &quot;ourUrl.com&quot;)!, onComplete: imageDownloadCallback)
        
        print(&quot;This line will likely be printed before the asynchronous call in JsonDownloader finishes&quot;)
    }
}
</code></pre>
<p>Remember the call to <code>downloadContentsOfUrl(url:)</code> has an asynchronous call in a background thread being executed inside of it, so the <code>print()</code> at the bottom will execute first and then later on our callback will be executed to retrieve the image.</p>
<p>Finally, we need to display this image in the UI. We should absolutely not be updating our UI from anywhere within the business logic of the application, never mind the infrastructure area that makes API calls. It's going to become an unorganised mess in a larger codebase if we do this - UI changes should be made in the view controller, which facilitiates messages between the view and the model.</p>
<p>If we want to try writing our UI updating code in our view controller, but it must be executed at the end of an asynchronous call in some other file in the model layer somewhere else, then what can we do about that? Enter... you guessed it... another callback. This time it must take the instance of <code>UIImage</code> that we have downloaded and display this in the UI.</p>
<p>So now we're passing callbacks within callbacks, and we have to update the <code>JsonDownloader</code> to take both callbacks as well. Take a look at the following awful (but working) code.</p>
<pre><code class="language-swift">struct JsonDownloader
{
    public func downloadContentsOfUrl(
        url: URL,
        onUrlRetrieved: @escaping (_ url: URL, (UIImage) -&gt; Void) -&gt; Void,
        onImageDownloaded: @escaping (_ image: UIImage) -&gt; Void
    ) -&gt; Void {
        // This async call will be thrown onto a thread and then this function will return immediately
        DispatchQueue.global(qos: .background).async {
            let json = try! Data(contentsOf: url)
            
            let jsonData = try! JSONSerialization.jsonObject(with: json, options: []) as! [String: Any]
            
            // Calling our first callback, and passing in the second, violating LoD
            onUrlRetrieved(URL(string: jsonData[&quot;image_url&quot;] as! String)!, onImageDownloaded)
        }
    }
}
</code></pre>
<p>The first thing to notice here is that <code>downloadContentsOfUrl</code> now takes two callback arguments, one to be executed once the url has been retrieved, and the second one to be executed once the image has been downloaded. This will require passing that <code>uiUpdatingCallback</code> through two functions, just so the second one can utilise it, which violates the <a href="https://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a>.</p>
<pre><code class="language-swift">class ViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        let uiUpdatingCallback: (UIImage) -&gt; Void = { image in
            // Don't forget, we must do any UI stuff on the main thread
            DispatchQueue.main.async {
                let imageView = UIImageView(image: image)

                imageView.frame = CGRect(x: 0, y: 0, width: 300, height: 300)

                self.view.addSubview(imageView)
            }
        }
        
        let imageDownloadCallback: (URL, (UIImage) -&gt; Void) -&gt; Void = { url, onImageDownloaded in
            let image = UIImage(data: try! Data(contentsOf: url))!

            onImageDownloaded(image)
        }
        
        let jsonDownloader: JsonDownloader = JsonDownloader()

        jsonDownloader.downloadContentsOfUrl(
            url: URL(string: &quot;ourUrl.com&quot;)!,
            onUrlRetrieved: imageDownloadCallback,
            onImageDownloaded: uiUpdatingCallback
        )

        print(&quot;This line will likely be printed before the asynchronous call in JsonDownloader finishes&quot;)
    }
}
</code></pre>
<p>Here we are defining our <code>uiUpdatingCallback</code> in the view controller. This will be executed at the end of the process by the <code>imageDownloadCallback</code> which we define next. The <code>imageDownloadCallback</code> will be executed when the <code>JsonDownloader</code> has finished downloading the json data containing the image url asynchronously.</p>
<p>We then pass two callbacks to the <code>downloadContentsOfUrl()</code> function call and this uses the first callback within it, but passes the second callback (the ui updating one) through to be used later once the image has been downloaded asynchronusly.</p>
<p>Can you see how this is starting to become a bit of a nightmare? We just want to be able to define what will happen next in the correct place, <em>synchronously</em>, without having to pass callbacks within callbacks.</p>
<p>Let's rewrite this with async / await.</p>
<h1 id="asyncawait">Async / Await</h1>
<p><img class="center" src="https://blog.j7mbo.com/content/images/2021/01/687474703a2f2f79616e6e69636b6c6f72696f742e636f6d2f7265736f75726365732f41776169744b69742d4172697374612d42616e6e65722e706e67-3.png" alt="From callbacks to async / await in Swift"></p>
<p>Let's take a look at the entry point first, our <code>ViewController</code>. There are other things we need to do to make this work involving promises, but for now, this is the top level code.</p>
<pre><code class="language-swift">class ViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        async {
            let jsonDownloader: JsonDownloader = JsonDownloader()
            let imageDownloader: ImageDownloader = ImageDownloader()
        
            let json: [String: AnyObject] = try! await(jsonDownloader.downloadContentsOfUrl(&quot;ourUrl.com&quot;))
            
            let image: UIImage = try! await(imageDownloader.downloadImage(json[&quot;image_url&quot;]))
            
            DispatchQueue.main.async { 
                let imageView = UIImageView(image: image)
                
                imageView.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
                
                view.addSubview(imageView)
            }
        }
    }
}
</code></pre>
<p>We've placed the image downloader callable into a class, but that's not the main change here. Firstly we have <code>async { }</code> which is the syntactically sugared equivalent of dispatching into a background thread. Secondly, we have <code>await()</code>, which will wait for the promise within it to resolve asynchronously. These can be <em>two, separate, asynchronous-using structs</em> and as long as we use promises, they will wait. No callbacks, no passing functions around.</p>
<p>This means that an asynchronous piece of code can return a value, via it's Promise, and the <code>await()</code> call handles the waiting for that value to be returned. We're writing code that looks synchronous!</p>
<p>You may be a little confused about why we don't just do the following directly in the view controller itself:</p>
<pre><code class="language-swift">DispatchQueue.global(qos: .background).async {
    let jsonDownloader: JsonDownloader = JsonDownloader()
    let imageDownloader: ImageDownloader = ImageDownloader()
    
    let json: [String: AnyObject] = jsonDownloader.downloadContentsOfUrl(&quot;ourUrl.com&quot;)
    
    let image: UIImage = imageDownloader.downloadImage(URL(string: json[&quot;image_url&quot;]))
    
    DispatchQueue.main.async {
        // -- SNIP -- Add image to UIImageView and add a subview in UIView
    }
}
</code></pre>
<p>Looks great, right? But what if you have the background threading within the <code>JsonDownloader</code> class instead, as many other classes do, both core and third-party, and not within your view controller?</p>
<ul>
<li>The call to <code>JsonDownloader</code> will return immediately</li>
<li>The call to <code>ImageDownloader</code> will try and use a url that will actually be <code>nil</code> because <code>downloadContentsOfUrl</code> has not finished yet</li>
<li>The application will crash</li>
</ul>
<p>If you restrict yourself to only using background queues from the view controller, and in many cases you need more than one thread to perform some functionality, you'll be coordinating all this logic from the controller and it's going to become a bit of a nightmare. So much for &quot;fat models and skinny controllers&quot; (or rather &quot;skinny models and skinny controllers&quot; as is the current collective thought process at the moment)!</p>
<p>We need to change our <code>JsonDownloader</code> slightly to work with this though, instead returning Promises from <a href="https://github.com/mxcl/PromiseKit">PromiseKit</a>.</p>
<pre><code class="language-swift">struct JsonDownloader
{
    public func downloadContentsOfUrl(_ url: URL) -&gt; Promise&lt;String&gt; {
        return Promise { resolve, reject in
            let jsonData = try! Data(contentsOf: url)

            let jsonData = try! JSONSerialization.jsonObject(with: response, options: []) as! [String: Any]

            return resolve(url: jsonData['image_url'])
        }
    }
}
</code></pre>
<p>That's it! Our <code>Promise</code> object now returns a call to <code>resolve</code> which internally is only executed once the download has completed. This enables us to use <code>await()</code> and write more synchronous-looking code without any callbacks.</p>
<h1 id="conclusion">Conclusion</h1>
<p><img class="center" src="https://blog.j7mbo.com/content/images/2021/01/async.jpg" alt="From callbacks to async / await in Swift"></p>
<p>I've been using both PromiseKit and AwaitKit for several months now and the changes from writing asynchronous code with callbacks to this have been staggering - they've made my development so much easier and more fun!</p>
<p>I hope this post convinces some to take a look at async / await and change from callback hell to something that looks a lot more clean and easy to work with.</p>
<p>Let me know in the comments below if you have callback hell in your current codebase, if you are considering checking this out or if you have any alternatives that work just as well in more complex codebases!</p>
<!--kg-card-end: markdown-->]]></content:encoded></item></channel></rss>