James Course - The Representable/Valid Principle Flashcards

1
Q

Building something that does the right thing is easy. What is the challenge?

A

The challenge is to build something that can’t do the wrong thing.

Maintain a one-to-one correspondence between representable and valid states of your program.

One thing is what your program can do, another is what your program should do.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can we eliminate an entire class of bugs?

A

By making sure that our program state space can’t represent invalid states and invalid transitions through states can’t happen.
Remember about the Thread.start vs Thread.run example.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How the Representable/Valid Principle changes our view of software?

A

The principle makes us understand how software is a collection of states, and transitions between theses states.

Thought this lenses of transitions and states we can constrain the state space to make bugs impossible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Imagine a specific method can’t be called before calling another one. How can you make sure they will never be called in the wrong order?

A

You can move the second method to another object and make the first method return this object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

task.await(true)

true stands for interruptible tasks, how to prevent users of this API to make a mistake?

A

Use an enum

task.await(INTERRUPTIBLE)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Rectangle(int x, int y, int width, int height)

How to prevent programmers from passing parameters in the wrong order?

A

Use keyword arguments, or ask for a point and a dimension object instead of integers. Avoid primitive obsession.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the external view?

A

The external view looks at state transitions and help us to eliminate bad transitions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the internal view?

A

The internal view look at what possible states could exist given the variables we have and help us to eliminate bad states.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When we create a object usually it has a representation invariant that described what must be true to be valid state representation. What invariant should we strive for?

A

We should strive for not having a representation invariant, that is, concrete state representations that always satisfies the invariant, that is, all concrete values are valid.

Remember the Oven example:

class Oven
  bool isOn, isBroil, isBake
  int bakeTemp

Representation invariant
¬(isBroil & isBake) &
((isBroil | isBake) => isOn) &
(isBake <=> bakeTemp != null)

The mapping between concrete states and abstract states is not 1 to 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What tools can we use to remove invalid state representations?

A

We can use algebraic data type like a sum or a product.

Use precise types to represent your data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the power of design?

A

Is not in what it can do, but in what it can’t do.

Our goal is to design programs where is easy to do the right thing and impossible to do the wrong thing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Should we have the possibility of having two concretes states representing the same abstract state?

A

No, the Representable/Valid Principle says that we should have a 1 to 1 mapping between concrete and abstract representations, that is, an injective function between concrete and abstract representations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which techniques were used in the JMock library that prevented library misuse?

A

The use of interfaces to define the syntax of JMock. This technique restricted the order that methods were allowed to be called.

Implement the syntax interfaces in Builder objects: These builder objects allowed the program to be built in two layers, one for the syntax and another one for the interpreter. The syntax layer was focused on providing methods to describe what we wanted the program to do in a declarative manner. When called these methods would build the object graph (interpretation layer) that executed the computation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the fundamental idea behind returning one token object that is needed to call other methods (that is, enforcing ordering)?

A

The token represents a proof of work that should happen before calling some other method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the abstract function do?

A

It removes the details of the implementation and only keeps what is essential to represent the abstract concept. Remember about the ring buffered queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is another way of describing the good taste of Linus about removing edge cases?

A

Edge cases probably mean your representation has invalid states, so reducing the number of edge cases you will probably reduce the number of invalid states, thus, following the representable valid principle

17
Q

What the bugs and battleships article talked about?

A

It uses an analogy with the game Battleship to talk about different strategies we can use to test our software. The sea would be all possibles tests we could write, and the missiles would be the ones we have chosen.

The greatest conclusion was that in order to use equivalent partitioning in your software you have to find tests that generalize. Chunks of the test input space which are said to be equivalent to each other correspond to a single case, part of a larger mathematical proof.

The ability to generalize behavior of specific tests to the behavior of the program is precisely what distinguishes a good program from a bad one. So… good programs are easy to prove correct.

18
Q

What was the problem in libcurl that caused a huge number of vulnerabilities?

A

There were a bunch of inconsistency uses of the API. There was this specific function to set up which validations the library should do and it took two constants as input, the first one was a true/false value, and the second one was supposed to be 1 or 2. The default value was 2, that meant to perform security validations, but people misused the library and set the value to true, and true is converted to 1, disabling the security check.