Value Objects: AppCode’s Code Generation to the Rescue

Shares

A Value Object is a handy way to combine multiple values in a single object. It sounds simple — you just make each value a property, right? Well, there’s usually more to it than that. There’s even an entire objc.io article about Value Objects. …So how do you define a new Value Object?

I believe in laziness. Let’s do as little work as possible!

[This post is part of the series TDD Sample App: The Complete Collection …So Far]

What we need

In the Marvel Browser iOS TDD sample app, the goal is to display comic characters whose names start with a given prefix. The Marvel Comics API offers many parameters for fetching characters. We want to use just three of them:

  • nameStartsWith
  • limit
  • offset

This will allow us to fetch the results a page at a time, and request the next page.

Services are better without state

The page size will likely be a fixed quantity in the app. We could build a Service that knows this fixed quantity. It could also keep track of the offset, encapsulating “request the next page.”

Disclosure: The book links below are affiliate links. If you buy anything, I earn a commission, at no extra cost to you.

I’ve written Services like that before. But the guidance given by Domain-Driven Design is “make the Service stateless.” Why?


Let’s picture what it means to make the Service stateless. In our example, it means the Service won’t track the prefix, the page size, or the page offset. We’ll track these values elsewhere in the app. The Service only does what it’s told. What we get is a Service that is ignorant about our app.

This makes testing easier. It also opens the door to reusability. As I wrote last time: when it comes to objects, ignorance is bliss!

Generating Value Objects with AppCode

So we’ll pass in three parameters. Let’s pass them together as a Value Object. Are you ready?

Instead of assembling another set of screenshots, I made a screencast — my shortest yet. It’s just three-and-a-half minutes long.

Whether you use Xcode or AppCode, be sure to get my free set of test snippets:

Links:

What do you think about stateless Service objects? Have I convinced any of you to try AppCode yet? Leave a comment below.


[This post is part of the series TDD Sample App: The Complete Collection …So Far]

About the Author Jon Reid

When I was a kid, programming was fun. But working in Silicon Valley, I saw poor code lead to fear, with real human costs. Searching for ways to make life better, I learned about Design Patterns, Refactoring, and Test Driven Development (TDD). Programming became fun again! I've now been doing TDD in Apple environments for 17 years. I'm committed to software crafting as a discipline, with the hope of raising us all to greater effectiveness and joy.

follow me on:
Disclosure: The links below are affiliate links. If you buy anything, I earn a commission, at no extra cost to you.
  • Natalia says:

    Hi, I can’t view the video I get the “private content” error.

  • Ben Smith says:

    Hey John – FYI the YouTube vid you posted is marked as private.

    Cheers,

    Ben.

  • Jon Reid says:

    Sorry about that! It’s public now, please try again. *blush*

  • Juan says:

    Hi Jon,
    Do you know if there is a way to auto-generate with AppCode a unit test for the initialiser?
    Cheers,
    Juan

    • Jon Reid says:

      Juan, there’s really no need for such a test. Here’s why: Other tests will use the Value Object by creating and initializing it with certain values. Those tests will then confirm that the input (via the initializer) affects the output in a certain way.

      Did that make sense?

      • Juan says:

        Yes, it makes sense. But at this stage of the project there is production code that is not tested (the body of the initializer). If you comment out that code and return nil, unit tests still pass. So we broke the rule “You are not allowed to write any production code unless it is to make a failing unit test pass.”

        This is why I thought the unit test is needed now. Maybe it can be removed later if it adds no value.

        Maybe I’m too strict applying TDD.

        Thanks for your response!

  • >