Refactoring for C# developers Flashcards

1
Q

What is refactoring?

A

Changes to the internal structure of a software to make it easy to understand and modify.

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

What does refactoring should NOT affect?

A

The observable behavior of the software.

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

What happens if we don’t refactor often?

A

The software might get too complex to change.

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

When to refactor?

A

After making the unit test pass (part of tdd);
As part of fixing a bug;
As part of creating a new feature;

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

When not to refactor?

A

When current code does not work;
Massive technical debt (not developer’s call);
Imminent deadline;

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

What does martin fowler says about refactoring?

A

Othen than when you’re very close to a deadline… you should not put off refactoring because you haven’t time.

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

Does the boy scout rule apply in refactorings?

A

yes.

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

What are the four steps of refactoring?

A

Commit (or restore) current working code;
Verify existing behavior (ideally with automated tests);
Apply a refactoring;
Confirm original behavior has been preserved.

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

What is characterization tests and when they are most useful?

A

Characterization tests asserts the current behavior of the software (even without knowing if it is correct). Most useful for legacy software which does not have unit tests.

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

What are the four steps of characterization tests?

A

Write tests you know will fail;
Get the output of the test;
Update the test to assert the current behavior;
Run the test again; should pass;

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

What to refactor to achieve a cleaner code base?

A
remove duplication;
improve naming;
break up large code elements;
reduce coupling;
reduce  complexity;
split responsability (SRP);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is a code smell?

A

surface indication that usually correspond to a deeper problem in the system.

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

What is the “Principle of Least Astonishment”?

A

Do what users expect;
Be simple;
Be clear;
!!!Be consistent!!!;

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

What are the five classifications of code smells?

A

Bloaters, object-orientation abusers, change preventers, dispensables and couplers.

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

What is a Bloater?

A

More code than it is actually necessary.

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

What is a object-oriented abuser?

A

Disrupt the value provided by the object-oriented languages.

17
Q

What is a change preventer?

A

Any change requires touching many parts of the system.

18
Q

What is a dispensable?

A

Provide little or no value.

19
Q

What is a coupler?

A

Excessive coupling… preventing isolate changes to the system.

20
Q

What is a obfuscator?

A

Impede clear communication. Make harder to understand the code.

21
Q

What is non-nullable reference type? When was it introduced?

A

Forces an object to always have a reference, so not null. Introduced on c# 8.

22
Q

Whic .net version has C# 8 by default?

A

.net core 3

23
Q

how to enable nullable via compiler directive?

A
#nullable enable
#nullable disable
24
Q

How to enable nullable via csproj?

A

tag nullable enable nullable

25
Q

How to enable treat warning as errors for nullable errors?

A

adding CS8600 to project config.

26
Q

How to change the design intent to allow a nullable for a specific var (even when the whole project is not allowing it)?

A

via ?

string? message = null;

27
Q

What is the null-forgiving operator? When use it?

A

Is an operator that says that the programmer know that the given message!.Length will never be null. Use it when the compiler gives false positives (e.g when using reflection or during unit tests).

28
Q

In which situations the nullable compiler checking won’t be applied (even when it is enabled in the project)?

A
  • when reflection is used;
  • Calling code that has this feature disabled;
  • When creating libs you should consider null checking stuff.