Article

Take More Shortcuts

Collin Flynn

June 22, 2020

I’d like to propose a distinction between a shortcut and a hack.

A Shortcut is:

  • Quick, functional progress in the direction of a longer-term, robust solution.
  • Imposes a functionality ceiling. A shortcut will eventually have to be revised before further progress is made.
  • Not ideal, but there are reasons to do it.

A Hack is:

  • Quick, functional progress in the direction of a brittle, short-term solution.
  • It’s a tripwire. A developer is likely to break it by touching it.
  • Not ideal, and not even advisable.

A shortcut is a compromise that gains immediate partial functionality by deferring a costly (but “correct”) choice until the shortcut can be revised, transitioned, or rewritten. It also comes with a known functionality ceiling. A day will come when the shortcut is inadequate.

A hack appears like quick progress, but in the wrong direction. For partial functionality, it trades away stability, comprehensibility, maintainability, or circumvents safety measures.

A Hack

Here’s one I’ve seen too often.

Suppose your application wants to disk-cache the details of the currently logged-in user.

val user: User

You’ve got various local storage mechanisms at your disposal, but serializing things takes a lot of keystrokes. How about this?

val userAsJsonString = user.toJson()
localStorage.putString(key = “USER”, value = userAsJsonString)
(Here, localStorage could be android shared preferences or whatever caching mechanism exists on your platform.)

Looks like a hack

❌ Brittle. Will crash on any modification to the User type.

❌ Circumvents existing safety measures. Other storage solutions such as SQLite bend over backwards to help you migrate on schema changes.

❌ Other developers could break this without knowing it.

A Shortcut

What about this?

val userId: String = currentUser.id
localStorage.putString(key = “USER_ID”, value = userId)

Instead of the full user object details, all we have is an id. That’s less than we wanted, but also matches a square peg with a square hole. We’re not trying to hammer the full User into a brittle string representation.

Looks like a shortcut.

⚠️ Not ideal, but also not broken.

⚠️ Known limitation: you’ll have to fetch full user details using the id.

✅ Partial functionality towards a more robust local storage method.

✅ Won’t break when another developer touches it.

Take more shortcuts, write fewer hacks.



Collin writes software and his personal dictionary of words at Livefront