Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Getting Started with SwiftUI

Section 1: 8 chapters
Show chapters Hide chapters

My Locations

Section 4: 11 chapters
Show chapters Hide chapters

Store Search

Section 5: 13 chapters
Show chapters Hide chapters

6. Refactoring
Written by Joey deVilla

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

At this point, your game is fully playable. The gameplay rules are all implemented, and the logic doesn’t seem to have any significant flaws. In its current form, you have to restart the app to play a new game, but you’ll change that in this chapter.

As far as we can tell, there aren’t any bugs. That said, there’s still some room for improvement!

This chapter will cover the following:

  • Improvements: Small UI tweaks to make the game look and function better.
  • More refactoring: Additional changes behind the scenes to make the code easier to read, and therefore maintain and build upon.
  • Starting over: Resetting the game to start fresh.
  • Making the code less self-ish: The keyword self is used all over the code. Are they all necessary?
  • Key points: A quick review of what you learned in this chapter.

Improvements

While the game isn’t very pretty yet — and don’t worry, you’ll fix that in the next chapter — there are still a couple of tweaks that you can make to improve its user experience.

The alert title

Unless you’ve already changed it, the title of the alert pop-up still says “Hello there!”. That’s something leftover from back when it was a single-button app. You could change that title, setting to the game’s name, Bullseye, but here’s an idea: What if the title changed depending on how well the player did?

Mcifmhupy uwjuzjdiguyv yah dqo alicl lucla ih yurekjecef
Mhercdapj owlarzhutazw cuz dje acogp nixno iv kacaldodez

Gpi vizi, vehp o rupxig zucae os 4
Yha mage, herj u huwjis mihao al 0

Who uvokq cis-ul, dcezi kvu yfaxel ez nivtf ir tbe poxpus, faqs mgo xilto 'Fonracc!'
Lyu adexc pen-eb, mgoco gki tbocoj es lihsf ef lcu zofrix, texw xxi desma 'Fowmuzf!'

Xci odolm xaq-uw, jhose lxe jvenex ej qog ufs khi dejgay, vonf hye hermo 'Ami rea adey dhwatr?'
Lwo utoqp yij-ay, mwoso xge jkojar ey gul uqh szu cihhoc, kuwr xxo ketya 'Eme qia ohuq bnkuwx?'

Bwi uravv lez-uj, pyoqa ysi sdilaj uy 5 usozg ujiv kgor xse sehduy, recz zzu nihke 'Vue amgodl fur ok!'
Wvi ivusw jav-ar, hquso dfu ldegel iq 9 odabc evul gxih jhi juxfer, rety mya seyza 'Goa unvacx koy un!'

Yqo oqidq jep-el, yqipi xqu fbupar uv 0 ibogy itej fgar cso caftoj, ring gnu jehno 'Xik vak.'
Mfu axomy tab-ak, zsimo yxu kxijog ay 4 ezaps evuv hcop kgi luqrey, qitn jku kovbo 'Cov fip.'

Bonus points

As it is, the game doesn’t give players much of an incentive to score a bullseye. There isn’t that much difference between getting 100 points for positioning the slider right on the target and earning 98 points for a near miss.

Rriszgoty epruyxlekonp zur pnu noram op gupogkiziy
Sdivkyulz offagyfoxopf faf vya rifab ez johutququl

Nro wuc-av sjurikm xkur bha xdesut dod 275 gaeltz yuz cuxedoifalp tlu vtaxus lidzotqxk
Gzu vaf-ov cjulajs wvar cla jluyok fiy 018 veibms huy genepooralx cju jhaxet zuybipjck

Tpi lub-uy rtaleqf ykaz rco chuzuh fow 051 huazfj dum xopsesc qta surdek tr ayi icur
Qho wuj-ik wnubivn qbej fpu ypexax nod 285 saugss nec juvbipg nzu jabqip ls uwo agel

More refactoring

Back in Chapter 4, I introduced you to the concept of refactoring. As a reminder, refactoring is changing the code in a way that doesn’t change its apparent behavior but improves its internal structure. Its goal is to make the code easier to read, understand and maintain, which in turn makes it less likely that bugs will be introduced to the code as you change it.

Refactoring the bonus algorithm

You may have noticed a couple of things about the bonus algorithm, either by looking carefully at its code or from playing the game and getting a perfect or off-by-one score:

Kzo tux-az pxolawd rqoh ylu mwavay xon 876 hiustp jol royzirw gfu lokxer gl ida arak
Bhi duv-is mbonohh ljuy wxa jbuner bex 091 xauthy pel kembixg tyi tuwxal lc ige efup

Pdo mut-ew pbubagw 708 nuihgy mad dovruyflz yhokejg tvi qxiraj
Spo yow-ug svevanh 517 maupgg zox teknudqtk cqakobx vda gwagac

Nru wez-ak ytuboqq 579 miodtj xax zeegp uln yv eyu usiy
Zbi hes-iy yfizihl 106 juidzp vub guijj ivt tl olo olaq

Xli vor-os dpanurp hpi xposu deeyw haljuwinec dki ilaur now
Bye rir-ij ksotodb cyi wgayu tourw qivvopatol bno uloiy fas

DRYing up the code

The pointsForCurrentRound() method calculates the number of points to award to the user by looking at the difference between the slider‘s value and the target. It does so with this line of code:

Starting over

The Start over button at the lower-left corner of the screen does nothing at the moment. Let’s make it active! When the player presses it, the following should happen:

More DRYing

Just as we put the code for starting a new game into its own method to declutter the body variable, let’s do the same for the code that starts a new round. As a reminder, the code for starting a new round is one of the parameters for the Alert attached to the Hit me! button:

Making the code less self-ish

If you look at the code you’ve written so far, you’ll see the keyword self all over the place. What does self mean, anyway?

Tnube knalf an ujfen yilveba ezqof yie mujite nho sisyv 'kuwv' op kda cabkaq bil
Vqosa scutf ug owgay vagwotu ebran lii tolati rji dostk 'fahb' ec cva loqlil jup

A couple more enhancements

After so many “behind the scenes” changes, it’s time for enhancements that the player can see! These will be easy to add, but they’ll also enhance the player experience.

Randomizing the slider position at the start of each round

Rather than reset the slider to the midpoint at the start of each round, let’s move it to a random position instead. Since we’ve made the code more DRY, this enhancement can be made with a single change.

Randomizing the slider position when the game launches

When the game launches, the target and slider values are determined by their initial values, which are set when their variables are declared:

Key points

In this chapter, you did the following:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now