AlgoViz

Abstraction

easy 40 min

Show what a thing does, hide how it does it.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:38

See it run

Watch what the caller has to know, and what it may forget.

Operations

A caller with all the steps

The café heats, grinds and presses by itself.

Step 1 of 5

Heat

cafecallermust recall3 stepsmachineCoffeePartshotyesgroundnocupemptyheat()

Step one is in the caller's head.

Step 1: Heat

m.heat()m.grind()m.press()m.grind()      # second cupm.press()      # forgot to heat
Good for
One caller, one day
Costs
Every caller owns the recipe

If there is one useful order, why can the caller choose?

In the real world

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

Pedals in front, an engine behind, then a motor.

Step 1 of 4

Press go

drivercallersees30 km/hpedalsinterfacewordsgo, brakeinsidehiddenhaspetrol enginego

The driver presses one pedal.

Step 1: Press go

Common mistakes

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

  • Steps as the interface

    critical

    Every caller must know the only useful order.

    Wrong
    m.heat(); m.grind(); m.press()
    Right
    m.make_beverage()
  • Vendor words

    major

    The second inside cannot keep them.

    Wrong
    store.put_object(bucket, key, acl, data)
    Right
    photos.save(photo_id, data)
  • An interface per class

    minor

    Two files, the same methods, nothing hidden.

    Wrong
    class IUserService: ...   # one implementation, no fake
    Right
    class UserService: ...
  • A method some refuse

    major

    Callers cannot trust the type.

    Wrong
    class Penguin(Bird):    def fly(self): raise NotImplementedError
    Right
    class Penguin:    def swim(self): ...
  • Flags

    major

    The caller chooses code inside again, and combinations explode.

    Wrong
    make("tea", True, False, True)
    Right
    children_menu.tea()
  • No virtual destructor in C++

    critical

    Deleting through the base pointer is undefined behaviour.

    Wrong
    class Base { virtual void f() = 0; };
    Right
    virtual ~Base() = default;

Interview tips

  • Find the word

    • What does the caller want?
    • One verb, caller's language
    • Then the steps go inside
  • Write the card

    • Meaning and duties
    • Errors and limits
    • What is not promised
  • Prove it

    • A second inside, even a fake
    • One contract test
    • Delay and failure injected
  • Know when not

    • One class, no second in sight
    • Only two examples
    • A layer that only forwards