Function Structure [Clean Code] Flashcards

1
Q

What is Partitioning Code

A

System composed of independent modules

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

What is Polymorphic Dispatch

A

Objects that share a method

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

What is temporal coupling

A

A method needs to be invoked before other

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

What is a example of temporal coupling

A
File.read(); File.process(); File.close();
var b = new EndpointAddressBuilder();
b.Uri = new UriBuilder().Uri;
var e = b.ToEndpointAddress();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to solve temporal coupling

A

Wrap flux inside block

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

What is CQS good for

A

Handle side effects

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

What a function that changes state should do

A

Changes state, returns NOTHING, maybe throw an exception

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

What a function that queries the state should do

A

Changes no state, Return the state, maybe throw an exception

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

What can you do about side effects

A

Use CQRS

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

What is fail codes

A

Is return a value to represent error

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

This function should return a fail message

A

The function should throw a exception instead

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

What is Tell, don’t ask

A

Is to only send commands to the object, don’t ask the object about it’s current state

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

What is a example of tell don’t ask 1

A
if(user.isLoggeIn())
    user.execute(command);
else
    annunciator.promptLogin();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a example of tell don’t ask 2

A
try
    user.execute(command);
catch(user.notLoggeIn e)
    annuncaitor.promptLoggin();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a counter alternative of tell don’t ask

A
user.execute(command, annunciator);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why not to pipe the invocation of the return values of a function like this:

o.getX().getY().getZ().doStuff();
A

Your function should not know this much about your code structure, this examples knows that getX() returns getY(),
but it actually doesn’t matter to it, what it want to do is to change a state of get a payload

17
Q

What is the law of demeter

A

The law of demeter OR principle of least knowledge OR Law of Demeter for Functions/Methods states:

Each unit should have only limited knowledge about other units: only units “Closely” related to the current unit.

Each unit should only talk to its friends; don’t talk to strangers.

The fundamental notion is that a given object should assume as little as possible about the structure or properties of anything else, in accordance with the principle of “Information hiding”.

In order to achieve a loose coupled code.

18
Q

What the law of demeter states, informally

A
  • Your method can call other methods in its class directly.
  • Your method can call methods on its own fields directly (but not on the fields’ fields).
  • When your method takes parameters, your method can call methods on those parameters directly.
  • When your method creates local objects, that method can call methods on the local objects.
19
Q

What the law of demeter states, mega informally

A
  • You can play with yourself.
  • You can play with your own toys (but you can’t take them apart),
  • You can play with toys that were given to you.
  • You can play with toys you’ve made yourself.
20
Q

What the law of demeter states, formally

A

the Law of Demeter for functions requires that a method Foo of an object Bar may only invoke the methods of the following kinds of objects:

Bar itself
Foo’s parameters
Any objects created/instantiated within Foo
Bar’s direct component objects
A global variable, accessible by Bar, in the scope of Foo

21
Q

What is a nice thing to have in mind about: Tell don’t ask and propagation of command/data

A

We tell or objects what they need to do, And the propagate to the responsible

22
Q

What is a nice thing to have in mind about: code responsability

A

Who is the responsible for that value/action?

23
Q

What shouldn’t I break a loop

A

You should never do things to diverge the reader from it’s normal flux, break in loops are hard to reason about

24
Q

What should we use exceptions for

A

Things that are not expected

25
A function that does one thing
Does not handle errors
26
What is a good place to expose my errors, and why
In my class, a error that is escaped in it's origin class, is much easier to find