Clean Coding Flashcards
How is professional code written?
Clean code:
- Maintainable
- Reveals the intent
- Reduces guesswork
- Reads like a story
What do we call poorly written code?
Smelly code
What is refactoring?
Changing the structure of code without changing its external behavior!
What is ReSharper?
It’s a plugin for visual studio that helps enormously with coding and productivity!
It takes you to the next level!
Once you use it, you’ll never go back!
Every C# developer should have!
Get a license, use it!
Doing refactoring by hand takes a lot longer and you could introduce bugs!
What topics does this course cover?
Poor Names
Poor Naming Conventions
Poor Method Signatures
Long Parameter List
Output Parameters
Variables Declarations on the top
Magic Numbers
Nested Conditionals
Switch Statements
Duplicated Code
Comments
Long Methods
What should a good name be?
clear, intention revealing name
Not mysterious
Not ambiguous
No clutter (not noisy)
Not pre-fixed by data type
Don’t have to look at implementation to understand what it does!
What is one of the most common code smells?
Poor names:
- Mysterious names - names that have secrets about them
- Meaningless names - methods with 100 lines of code and no clear, intention revealing name
- Names with Encodings - Pre-fix variables with data types (Hungarian Notation) IDE’s today no need for this!
- Ambiguous Names - have to look at implementation to understand it’s behavior : /
- Noisy Names - names with “the” or other noise in names
What are mysterious names?
identifiers that require us to look somewhere else to find the meaning of the identifier.
Secret about them
Clean code:
the reader doesn’t have to go somewhere else to understand what is happening there
Smelly code:
button1_Click () : have to look at implementation or search form button is part of and figure out it’s “submit order.”
How can we fix these mysterious names?
Clean code:
SqlDataReader dr1 : dataReader / reader
int od : overdueDays
void Button1_Click ( ) : CheckAvailability_Click ( )
class Page1 { } : ViewCustomerPage
What are meaningless names?
names that you have to view function to understand
often these functions are more than 100 lines of code, hence the meaningless name!
If the function is greater than 10 lines of code, it’s doing too many things! Hence why coming up with a clean, intention revealing name isn’t easy!
When your method is 5 lines to max 10 lines of code, it’s doing only one thing and it’s easy to come up with a meaningful name.
What are names with encodings?
Pre-fix variables with data types!
Why?
In the 1980s was hard to determine data types.
Hungarian notation
Today:
We have great IDE’s can see data types of variables in Visual studio by hovering mouse. No need for this!
Smelly code:
var m_objCollection = new StringCollection ( ) ;
Clean code:
maxRequests
var countryNames = new StringCollection ( )
What is an example of ambiguous names?
What is an example of noisy names?
What is a problem with naming conventions?
Inconsistent Naming Conventions
Developers coming from different backgrounds adopt different naming conventions!
Naming Inconsistencies:
One identifier begins with a capital letter, another lowercase
one parameter has an _ the other doesn’t
What are the two standard naming conventions?