Function Structure [Clean Code] Flashcards
What is Partitioning Code
System composed of independent modules
What is Polymorphic Dispatch
Objects that share a method
What is temporal coupling
A method needs to be invoked before other
What is a example of temporal coupling
File.read(); File.process(); File.close();
var b = new EndpointAddressBuilder(); b.Uri = new UriBuilder().Uri; var e = b.ToEndpointAddress();
How to solve temporal coupling
Wrap flux inside block
What is CQS good for
Handle side effects
What a function that changes state should do
Changes state, returns NOTHING, maybe throw an exception
What a function that queries the state should do
Changes no state, Return the state, maybe throw an exception
What can you do about side effects
Use CQRS
What is fail codes
Is return a value to represent error
This function should return a fail message
The function should throw a exception instead
What is Tell, don’t ask
Is to only send commands to the object, don’t ask the object about it’s current state
What is a example of tell don’t ask 1
if(user.isLoggeIn()) user.execute(command); else annunciator.promptLogin();
What is a example of tell don’t ask 2
try user.execute(command); catch(user.notLoggeIn e) annuncaitor.promptLoggin();
What is a counter alternative of tell don’t ask
user.execute(command, annunciator);
Why not to pipe the invocation of the return values of a function like this:
o.getX().getY().getZ().doStuff();
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
What is the law of demeter
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.
What the law of demeter states, informally
- 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.
What the law of demeter states, mega informally
- 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.
What the law of demeter states, formally
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
What is a nice thing to have in mind about: Tell don’t ask and propagation of command/data
We tell or objects what they need to do, And the propagate to the responsible
What is a nice thing to have in mind about: code responsability
Who is the responsible for that value/action?
What shouldn’t I break a loop
You should never do things to diverge the reader from it’s normal flux, break in loops are hard to reason about
What should we use exceptions for
Things that are not expected