All posts by Jon Reid

Single Responsibility Principle: Is It a Fundamental Mistake?

The “Single Responsibility Principle” (SRP) sounds so noble. But I’m afraid it’s misunderstood and misapplied. Ask your teammates: “What is the Single Responsibility Principle?” Go ahead, ask them. Then ask if the SRP is a good thing or a bad thing. I’d bet many of them will say something like this: “In principle, it’s a good idea. But in practice, it’s overkill.”

Single Responsibility Principle poster: monster Swiss Army knife

SOLID Motivational Posters by Derick Bailey, used under CC BY-SA 3.0 US

On Twitter, Chris Eidhof pointed to an example of taking the Single Responsibility Principle too far. Specifically, Chris was unhappy with the argument that Singletons violate the SRP because, besides their main responsibility, they also manage their own life cycle:

This led to a lively discussion. Many reacted against “over-architecture.” No doubt they experienced fragmented code that grew from over-zealous attempts at SRP.

I think that SRP isn’t just over-applied. It’s fundamentally misunderstood, even misquoted. The repeated misquotes perpetuate that misunderstanding.

Let’s see if we can clear things up, and point to a better way.

Continue reading

Please Take My 2017 Reader Survey

Shares

I want to ensure my platform does the best possible job of answering your needs and interests. And that means I need to know more about you. To do that, I’ve created my 2017 Reader Survey.

feedback

Would you please take a few minutes to fill out the survey? By doing so, you will ultimately be helping yourself. Why? Because you will be helping me create content even more interesting and relevant to you.

Your input is important to me. The survey is easy to fill out, and the results are completely anonymous. I can’t tell who said what. And you can finish in five minutes.

Yes, I’m Happy to Help. Take Me to the Survey!

Thanks in advance for your help.

Refactoring: Is There More To It than Just Changing Stuff?

Shares

Refactoring. It’s a word I hear quite a bit. Usually, in the context of conversations with management, it means, “Rewriting that thing. Hopefully without introducing bugs.” Often, among developers, it means, “One of the options in the Refactoring menu in my IDE.”

Inigo Montoya meme: "Refactoring, you keep using that word. I do not think it means what you think it means."
Continue reading

Revealing Hidden Objects: Can DDD Improve Your Code?

Shares

Code that’s easier to understand, maintain, and extend — that’s the promise of Object-Oriented Programming. But the reality for many iOS developers is that our objects are bloated. They know too much, and do too much. What if our code has hidden objects, waiting to be found?

Each hidden object could provide a new abstraction, a new tool. They could make the code more manageable. Is there a way to discover these hidden objects? Domain Driven Design (DDD) provides a way.

Continue reading

How to Make Swift Mock Objects More Useful

Shares

I’ve written about my experience of going to try! Swift Tokyo 2017. Now thanks to the video and transcript provided by Realm, I can also share the talk I gave: “Making Mock Objects More Useful”.

Speaking at try! Swift Tokyo on Swift Mock Objects

I start by showing the basics of how to make a mock object by hand. But this easily leads to fragile tests because the assertions are overspecified. We need ways to make tests more malleable, with mocks that are more flexible.

Continue reading

How to Safely Parse JSON into Immutable Models, All with TDD

Shares

How can we unit test JSON parsing, handling every possible error? Can we generate immutable models? And for Swift, how can we keep our Response Models free of optionals?

Of course, there areb many JSON parsing libraries out there. Plug one in, define all fields as non-optional, and you’re good to go! …Until your app crashes, because something was different in the actual JSON data.

Unlikely? “The backend team would never do that to us”? I’ve had a released app crash because the backend folks changed one field from a string to an integer. I’ve seen app development and QA forced to pause because a commit assumed all fields were non-optional. (It crashed on the missing field, because Swift.)

So let’s look at a pattern that will help us

  • Handle required types
  • Avoid optionals
  • Deliver immutable models

Even if you never plan to do your own parsing, we’ll learn things along the way about design and testing.

Continue reading

Love Swift? Want to Visit Charming / Quirky Japan?

Shares

If you’re interested in Swift development, and want to visit Japan, start making plans to go to the next try! Swift conference in Tokyo. It was my privilege to be a speaker earlier this month.

Let me share my impressions of the conference, and why you might consider making the trip there.

(But first, a side-note about this blog: Sorry I’ve been so quiet lately! I was spending my time preparing for try! Swift Tokyo 2017. Next up is CocoaConf Chicago, where I’ll be leading a TDD workshop in addition to giving a talk. But I’ve also continued to TDD a JSON parsing example, in both Objective-C and Swift versions, so I have plenty to blog about. To make sure you don’t miss any new articles, you can sign up to get updates via email.)

Logo

Riko

“Riko”, the official logo of try! Swift Tokyo

First, let me point out the conference’s logo/mascot. The Swift bird has never been so adorable! Without a doubt, this is the cutest tech conference logo I’ve ever seen.

Continue reading

Let’s Stop Overusing Swift Equatables in Unit Tests

Shares

Enumerations with associated values are my favorite feature of Swift. But how can we write unit tests against them? “Make them Equatable and use XCTAssertEqual” is common advice.

I’m here to argue otherwise. In fact, let’s use this as a jumping-off point to discuss Swift Equatables in unit tests.

Apples and oranges: Swift equatables and testing

Continue reading

In Defense of Uncle Bob’s Dark Path: Swift and Testability

Uncle Bob set off another firestorm with his blog post The Dark Path. Condemnation from the Swift programming community was, well, swift. How dare he insult our wonderful new language? Clearly he’s a n00b who hasn’t done enough Swift programming.

Emperor Palpatine: "Good, good, let the hate flow through you"

Except that he’s no n00b — not even close. As Mark Seemann said,

I’m here to defend Uncle Bob’s post, as it relates to unit testing and TDD.

Continue reading

JSON Parsing: One of the Easiest Places for TDD beginners

Shares

How can we use Test-Driven Development for JSON parsing? Most developers are concerned with ways to implement the production code. There are various approaches, various libraries… But what about the unit test side of things?

If we write effective unit tests, the design can appear incrementally. And with a strong suite of tests, you’re free to change the implementation — even radically. When the results are guaranteed by tests, whatever approach you take or library you use becomes an implementation detail!

Last time, we looked at design principles, such as sticking to the Single Responsibility Principle and returning a Response Model. This time, let’s look at:

  • Test principles
  • A TDD demo in Objective-C

Continue reading

1 2 3 11