Clean Code. Robert C Martin Flashcards
(26 cards)
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)
This is a triadic function (three arguments). How can we reduce the number of arguments?
Circle makeCircle(double x, double y, double radius);
Circle makeCircle(Coordinates center, double radius)
Wrap the a and y in an object of type coordinates
How should monads be named?
A verb noun pair that complement each other e.g.
print(name)
When talking about functions, what is a side effect?
It is a behaviour of the function, additional to the main function (this also breaks the idea of each function doing just one thing).
E.g. a function that checks a password and user name but also initialises a session.
What is Command Query Separation?
It’s the idea that a function either answers a query or does something, not both
What should the content of a try catch bloch be - how long should it be?
1 or 2 lines of code, ideally
The content should be brief, containing a call to another well-named function to deal with the thing and another function dealing with the error
What should you do before putting a comment in the code?
Ask yourself, whether you can change the code to make it clearer regarding its intent and purpose.
Comments are nearly always a failure to write clear code
What are the 4 circumstances where comments are good?
Legal information
Explaining the reason behind a decision
To serve as a warning
A comment may be used to amplify the importance of something that may otherwise seem inconsequential.
Where should variables be declared?
At the top of our short function
Where should instance variables be declared?
At the top of the Class