Clean Code. Robert C Martin Flashcards
What things make good, clean code?
What MUST you ask as you plan and have written code?
Is it: Expressive: Named to reveal the purpose Methods do one thing only In the right place Tested - unit and acceptance Performing optimally Decoupled Factored Commented appropriately
Does it handle errors consistently
What are the benefits of clean code?
Increased:
Speed of changes and additions
Productivity
What does it mean when code is expressive?
Names are expressive: purpose and meaning
Objects and methods are concerned with just one thing
What are the 2 rules for names generally?
The name reveals purpose / intention
They are distinguishable from other names
-
What is the rule for naming a class?
It must be named with a noun
What is the rule for naming a function/method
It must be a verb or verb phrase
Accessors and Mutators should be named how?
With a prefix get, set, and is followed by the name of the value
What’s wrong with the function in this code?
public class UserValidator { private Cryptographer cryptographer;
public boolean checkPassword(String userName, String password) {
User user = UserGateway.findByName(userName);
if (user != User.NULL) {
String codedPhrase = user.getPhraseEncodedByPassword();
String phrase = cryptographer.decrypt(codedPhrase, password);
if (“Valid Password”.equals(phrase)) {
Session.initialize();
return true;
}
}
return false;
}
}
the function name is check password but, it has a side effect in that it also initializes a session.
If in the future the user’s credentials need to be checked mid-session, this would have the impact of erasing the existing session.
This makes it harder to implement change in the future because of this side effect.
How large should a function be?
Very small - aim for 20 lines or less
How long should blocks within if, else and while be? What is the best practice here?
They should be 1 line long - ideally this line should be a function call. This allows you to call a very nicely named function.
How many topics should a function call address?
Just 1 - or should do just one this and do it well
What is the step down rule?
It relates to the way code is structured by level of abstraction.
A calling function should be at the top and called functions at the button
E.g
Function a
Call b
Call c
Function b
Call c
Call d
Function c
Function d
What is we call b here? Then b has to be
moved to below this line
When passing arguments to s function how can we try and ensure we limit the number being passed in?
We could wrap the arguments into a class and pass the class in as one argument
What is a flag argument? When should it be passed to a function?
It is when we pass a boolean to a function (a flag on a type of state). It should never be passed to a function
What is a dyadic function? What should the relationship of the passed arguments be?
It is a function that takes two arguments. The arguments should have some natural cohesion e.g. x and y coordinates or hours and minutes (although a separate class might do for these)