YAGNI
easy 40 minYou 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
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
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
| Effort | Who uses it | |
|---|---|---|
| tenants | weeks | nobody |
| 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
minorEach default that nobody changes must be read, and tested in combination with the others.
Wrongdef save(m, mode=None, retries=0, hook=None): ...Rightdef save(m): ...YAGNI as an excuse
criticalErrors and attacks are not guesses.
Wrongmember = json.loads(body) # checks laterRightmember = parse_member(body) # raises a clear errorLosing what cannot be rebuilt
criticalA feature can be added later. A moment cannot.
Wrongself.members.append(name)Rightself.members.append(Member(name, joined=now()))An interface for a database that may come
majorIt copies the methods of the only one, so it will not fit another.
Wrongclass DbStrategy(ABC): ... # one subclassRightclass MemberStore: ... # extract when the second is realPublishing too early
majorA private shape changes in minutes, a public one in years.
WrongGET /v1/members # all 14 fieldsRightGET /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