AlgoViz

YAGNI

easy 40 min

You aren't gonna need it: build a feature when somebody needs it, and not when you imagine that somebody will.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:35

See it run

Objects at run time: who really calls what, what a guess costs, and how a shape changes later.

Operations

Six methods, one caller

Follow the calls. Five of six methods wait for nobody.

Step 1 of 6

The need

sign-up pageSignUpPageneedssavenew colleaguePersonreads6 methodsmembersMemberServicesaveusedexport xmlno callerexport csvno callerexport jsonno callerpdf reportno callerset storeno callerbuildJobcalls seen0tests run0save(noor)ok

A page wants to save a member. One method answers.

Step 1: The need

class MemberService:    def save(self, m): self.db.save(m)    def export_xml(self, m): ...    def export_csv(self, m): ...    def export_json(self, m): ...    def pdf_report(self, m): ...    def set_strategy(self, s): self.db = s
Good for
Finding guesses
Costs
Reading and testing all six
Built on a guess
5 of 6

Ask of each method: who calls it, and which person needs that?

In the real world

The same yagni doing real jobs. Pick one and step through it.

A developer looks into the future. A user asks for a file.

Step 1 of 5

Tenants

EffortWho uses it
tenantsweeksnobody
plugins
blockchain
assistant
CSV

I see many companies, says the ball.

Step 1: Tenants

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Parameters for nobody

    minor

    Each default that nobody changes must be read, and tested in combination with the others.

    Wrong
    def save(m, mode=None, retries=0, hook=None): ...
    Right
    def save(m): ...
  • YAGNI as an excuse

    critical

    Errors and attacks are not guesses.

    Wrong
    member = json.loads(body)   # checks later
    Right
    member = parse_member(body)   # raises a clear error
  • Losing what cannot be rebuilt

    critical

    A feature can be added later. A moment cannot.

    Wrong
    self.members.append(name)
    Right
    self.members.append(Member(name, joined=now()))
  • An interface for a database that may come

    major

    It copies the methods of the only one, so it will not fit another.

    Wrong
    class DbStrategy(ABC): ...   # one subclass
    Right
    class MemberStore: ...   # extract when the second is real
  • Publishing too early

    major

    A private shape changes in minutes, a public one in years.

    Wrong
    GET /v1/members   # all 14 fields
    Right
    GET /v1/members   # the 3 fields that are used

Interview tips

  • Ask three things

    • Who needs it today?
    • May I see an example?
    • What would later cost?
  • Make later cheap

    • Tests for the core
    • Small parts, clear names
    • One place for money, time, ids
  • Never leave out

    • Safety and errors
    • Data that would be lost
    • Versions on shared messages
  • In the interview

    • Agree on the scope
    • List what you left out
    • Show where it would go