Clean Code. Robert C Martin Flashcards

1
Q

What things make good, clean code?

What MUST you ask as you plan and have written code?

A
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

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

What are the benefits of clean code?

A

Increased:
Speed of changes and additions
Productivity

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

What does it mean when code is expressive?

A

Names are expressive: purpose and meaning

Objects and methods are concerned with just one thing

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

What are the 2 rules for names generally?

A

The name reveals purpose / intention

They are distinguishable from other names

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

What is the rule for naming a class?

A

It must be named with a noun

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

What is the rule for naming a function/method

A

It must be a verb or verb phrase

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

Accessors and Mutators should be named how?

A

With a prefix get, set, and is followed by the name of the value

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

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;
}
}

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How large should a function be?

A

Very small - aim for 20 lines or less

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

How long should blocks within if, else and while be? What is the best practice here?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many topics should a function call address?

A

Just 1 - or should do just one this and do it well

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

What is the step down rule?

A

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

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

When passing arguments to s function how can we try and ensure we limit the number being passed in?

A

We could wrap the arguments into a class and pass the class in as one argument

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

What is a flag argument? When should it be passed to a function?

A

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

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

What is a dyadic function? What should the relationship of the passed arguments be?

A

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)

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

This is a triadic function (three arguments). How can we reduce the number of arguments?

Circle makeCircle(double x, double y, double radius);

A

Circle makeCircle(Coordinates center, double radius)

Wrap the a and y in an object of type coordinates

17
Q

How should monads be named?

A

A verb noun pair that complement each other e.g.

print(name)

18
Q

When talking about functions, what is a side effect?

A

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.

19
Q

What is Command Query Separation?

A

It’s the idea that a function either answers a query or does something, not both

20
Q

What should the content of a try catch bloch be - how long should it be?

A

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

21
Q

What should you do before putting a comment in the code?

A

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

22
Q

What are the 4 circumstances where comments are good?

A

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.

23
Q

Where should variables be declared?

A

At the top of our short function

24
Q

Where should instance variables be declared?

A

At the top of the Class

25
Q

What are the 4 rules of the Law of Demeter

A

The Law of Demeter

A method f of class C should only call the methods of these:
• C
• An object created by f
• An object passed as an argument to f
• An object held in an instance variable of C

26
Q

What does self-discipline involve in a coding sense - rules that you MUST follow?

A

follow the good, clean code rules
ask how it could be done better
be willing to change