Refactoring for C# developers Flashcards
What is refactoring?
Changes to the internal structure of a software to make it easy to understand and modify.
What does refactoring should NOT affect?
The observable behavior of the software.
What happens if we don’t refactor often?
The software might get too complex to change.
When to refactor?
After making the unit test pass (part of tdd);
As part of fixing a bug;
As part of creating a new feature;
When not to refactor?
When current code does not work;
Massive technical debt (not developer’s call);
Imminent deadline;
What does martin fowler says about refactoring?
Othen than when you’re very close to a deadline… you should not put off refactoring because you haven’t time.
Does the boy scout rule apply in refactorings?
yes.
What are the four steps of refactoring?
Commit (or restore) current working code;
Verify existing behavior (ideally with automated tests);
Apply a refactoring;
Confirm original behavior has been preserved.
What is characterization tests and when they are most useful?
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.
What are the four steps of characterization tests?
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;
What to refactor to achieve a cleaner code base?
remove duplication; improve naming; break up large code elements; reduce coupling; reduce complexity; split responsability (SRP);
what is a code smell?
surface indication that usually correspond to a deeper problem in the system.
What is the “Principle of Least Astonishment”?
Do what users expect;
Be simple;
Be clear;
!!!Be consistent!!!;
What are the five classifications of code smells?
Bloaters, object-orientation abusers, change preventers, dispensables and couplers.
What is a Bloater?
More code than it is actually necessary.
What is a object-oriented abuser?
Disrupt the value provided by the object-oriented languages.
What is a change preventer?
Any change requires touching many parts of the system.
What is a dispensable?
Provide little or no value.
What is a coupler?
Excessive coupling… preventing isolate changes to the system.
What is a obfuscator?
Impede clear communication. Make harder to understand the code.
What is non-nullable reference type? When was it introduced?
Forces an object to always have a reference, so not null. Introduced on c# 8.
Whic .net version has C# 8 by default?
.net core 3
how to enable nullable via compiler directive?
#nullable enable #nullable disable
How to enable nullable via csproj?
tag nullable enable nullable
How to enable treat warning as errors for nullable errors?
adding CS8600 to project config.
How to change the design intent to allow a nullable for a specific var (even when the whole project is not allowing it)?
via ?
string? message = null;
What is the null-forgiving operator? When use it?
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).
In which situations the nullable compiler checking won’t be applied (even when it is enabled in the project)?
- when reflection is used;
- Calling code that has this feature disabled;
- When creating libs you should consider null checking stuff.