Top 5 best ways to improve your code
TOP 5 WAYS TO IMPROVE YOUR CODE
Write once read many.
My
© Pyxis Technologies inc.
/studio
Expertise
Quality
Respect
Agile Big Data custom CQRS Desktop Event
Sourcing Microsoft .NET
C# Scrum Software development Training
Web
we a
re
© Pyxis Technologies inc. Top 5 best ways to improve your code 3
Legacy code is code without testsMichael Feather in
Working effectively with legacy code
Without maintenance, code degrade rapidly
We must detect and get rid of code smell
INTRODUCTION
© Pyxis Technologies inc. Top 5 best ways to improve your code 4
Reduce class
hierarchy
Prefer collaboration
Loose coupling
Increase cohesion
Une abstraction level per method
Layered architecture
Extract cut cittong concerns
MY 5 IMPROVEMENTS
1. Simplify conditionals
2. Remove comments
3. Clarify contracts
4. Control scope
5. Flush dead code
© Pyxis Technologies inc. Top 5 best ways to improve your code 5
Reduce complexity Improve readability Improve maintainability Improve Reusability
SIMPLIFY CONDITIONALSWHY?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 6
There is more than one conditions (and / or)There is too many code in the bodyCondition is based on typeThere are nested conditionsThere are many outcome from the same information (if /
elseif / switch case)
SIMPLIFY CONDITIONALS WHEN?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 7
Refactor conditional statement Decompose conditional Consolidate conditional expression Consolidate duplicate conditional fragments Introduce null object Flatten nested if Don't use negative Keep conditional statement lean
Avoid conditional statement Replace conditional with polymorphism Replace conditional logic with strategy Replace conditional dispatcher with command
SIMPLIFY CONDITIONALSHOW?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 8
SIM
PL
IFY
CO
ND
ITIO
NA
LS
DECOMPOSE CONDITIONAL
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 9
SIM
PL
IFY
CO
ND
ITIO
NA
LS
CONSOLIDATE CONDITIONAL EXPRESSION
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 10
SIM
PL
IFY
CO
ND
ITIO
NA
LS
CONSOLIDATE DUPLICATE CONDITIONAL FRAGMENTS
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 11
SIM
PL
IFY
CO
ND
ITIO
NA
LS
INTRODUCE NULL OBJECT
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 12
SIM
PL
IFY
CO
ND
ITIO
NA
LS
FLATTEN NESTED IF
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 13
SIM
PL
IFY
CO
ND
ITIO
NA
LS
DON'T USE NEGATIVE
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 14
As much as possible, try to use only one condition per if statement Use a method to combine many conditions
Inverse if statement if most of the code is inside the true branch Avoid double negation
SIM
PL
IFY
CO
ND
ITIO
NA
LS
KEEP CONDITIONAL STATEMENT LEAN
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 15
SIM
PL
IFY
CO
ND
ITIO
NA
LS
REPLACE CONDITIONAL WITH POLYMORPHISM
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 16
SIM
PL
IFY
CO
ND
ITIO
NA
LS
REPLACE CONDITIONAL LOGIC WITH STRATEGY
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 17
SIM
PL
IFY
CO
ND
ITIO
NA
LS
REPLACE CONDITIONAL DISPATCHER WITH COMMAND
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 18
Improve readability Improve maintainabilityAvoid obsolete comments
REMOVE COMMENTSWHY?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 19
Each time a comment add anything else than useful information, intention, clarification, warning, todo or amplification
Comment for empty block of code Empty catch
Describe code line by line Example:
// Getting connection string from configuration // Opening connection // Retrieving data // Closing connection
REMOVE COMMENTSWHEN?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 20
Replace comment with good naming Extract method Use meaningful words
Write useful commentsRespect standard naming (MSDN: Guidelines for names)
Properties Enums Events Methods
REMOVE COMMENTSHOW?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 21
RE
MO
VE
CO
MM
EN
TS
EXTRACT METHOD
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 22
Use Intention-Revealing names d elapsedInDays
Avoid disinformation AccountList Accounts
Make meaningful distinction moneyAmount vs money vs amount InvoiceTotal
Use pronounceable names genymdhms generationTimestamp
Avoid mental mapping a account
Class names noun not verbs Method names verbs Don't be cute. Use standard names
Destroy, kill, obliterate Delete Solution Domain Names vs Problem Domain Names
RE
MO
VE
CO
MM
EN
TS
USE MEANINGFUL NAMES
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 23
Use PascalCase namingUse noun or adjectiveDon't use names that might be confused with a
Get Prefix boolean with Can, Is, or Has
RE
MO
VE
CO
MM
EN
TS
PROPERTIES
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 24
First element should be default value
Use PascalCasing namingSingle value enum should
be singularBit field enum should be
plural and has a Flag attribute
Bit field enum values must be coherent (Read & Write == ReadWrite)
RE
MO
VE
CO
MM
EN
TS
ENUMS
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 25
Use PascalCase namingUse past to describe post-event and present
progressive for pre-eventProvide a virtual methodProvide a cancel behavior for a pre-event
RE
MO
VE
CO
MM
EN
TS
EVENTS
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 26
RE
MO
VE
CO
MM
EN
TS
EVENTS
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 27
Use verbs as method name ProcessPayment
Explicitly express return type in method name CreateCustomer GetInvoice
Use coherent naming (Get, Fetch or Retrieve but not all for the same usage context)
RE
MO
VE
CO
MM
EN
TS
METHODS
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 28
Improve performance Improve readability Improve reusability
CLARIFY CONTRACTSWHY?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 29
The is too many paramters (how many is too much?)A method has more than one responsabilityA method use out paramters
Except for: TryPattern Service contracts
You need a default value
CLARIFY CONTRACTSQUAND?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 30
Reduce the number of paramters Introduce parameter object Create overload with less parameters Use default value
Outputs Use complex return type Use out parameters only when necessary
Maintain parameters order for overloads
CLARIFY CONTRACTS HOW?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 31
CL
AR
IFY
CO
NT
RA
CT
SINTRODUCE PARAMETER OBJECT
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 32
CL
AR
IFY
CO
NT
RA
CT
SCREATE OVERLOAD WITH FEWER
PARAMETERS
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 33
CL
AR
IFY
CO
NT
RA
CT
SUSE DEFAULT VALUE
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 34
Avoid side effects Improve reusability Improve maintainability
CONTROL SCOPEWHY?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 35
A field is used only n a few methods Internal implementation is exposed by public members
CONTROL SCOPE WHEN?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 36
Visibility protected private internal
Responsibility Move a field inside a method Split class Move variable closer to usage
Lifetime Create instances as needed Release reference as early as possible
CONTROL SCOPEHOW?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 37
Because we must Improve maintainability Improve performance Improve readability100% test coverage
FLUSH DEAD CODEWHY?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 38
You know the code is deadYou think the code is deadYou want the code to be dead
FLUSH DEAD CODE WHEN?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 39
Identify and remove the code Delete le code Compile Run unit tests
What is dead code? Code in comments Code not covered by unit test
Tools There are tools that delete any line of not covered by at least one
unit test
FLUSH DEAD CODE HOW?
5
4
3
2
1
© Pyxis Technologies inc. Top 5 best ways to improve your code 40
Refactoring – Improving the design of exist ing code Auteur: Martin Fowler Edition: Addison Wesley ISBN: 978-0-201-48567-7
Refactoring to patterns (Mart in Fowler signature) Auteur: Joshua Kerievsky Edition: Adison Wesley ISBN: 978-0-321-21335-1
Clean code – a handbook of agi le software craftsmanship Auteur: Robert C. Martin Edition: Prentice Hall ISBN: 978-0-132-35088-4
Working effect ively with legacy code Auteur: Michael C. Feather Edition: Prentice Hall ISBN: 978-0-13-117705-5
REFERENCES
© Pyxis Technologies inc. Top 5 best ways to improve your code 41
Questions?Remember
Eric De Carufel [email protected] http://blog.decarufel.net
THE END
Simplifyconditiona
ls
Remove comments
Control scope
Clarify contracts
Flush dead code