top 5 best ways to improve your code pyxis v2 - en
DESCRIPTION
This is my top 5 of best ways to improve your code base on many code reviews and some good practicesTRANSCRIPT
![Page 1: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/1.jpg)
Top 5 best ways to improve your code
TOP 5 WAYS TO IMPROVE YOUR CODE
Write once read many.
My
![Page 2: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/2.jpg)
© 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
![Page 3: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/3.jpg)
© 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
![Page 4: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/4.jpg)
© 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
![Page 5: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/5.jpg)
© 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
![Page 6: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/6.jpg)
© 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
![Page 7: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/7.jpg)
© 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
![Page 8: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/8.jpg)
© 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
![Page 9: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/9.jpg)
© 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
![Page 10: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/10.jpg)
© 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
![Page 11: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/11.jpg)
© 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
![Page 12: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/12.jpg)
© 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
![Page 13: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/13.jpg)
© 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
![Page 14: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/14.jpg)
© 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
![Page 15: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/15.jpg)
© 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
![Page 16: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/16.jpg)
© 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
![Page 17: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/17.jpg)
© 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
![Page 18: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/18.jpg)
© 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
![Page 19: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/19.jpg)
© 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
![Page 20: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/20.jpg)
© 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
![Page 21: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/21.jpg)
© 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
![Page 22: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/22.jpg)
© 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
![Page 23: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/23.jpg)
© 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
![Page 24: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/24.jpg)
© 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
![Page 25: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/25.jpg)
© 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
![Page 26: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/26.jpg)
© 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
![Page 27: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/27.jpg)
© 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
![Page 28: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/28.jpg)
© 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
![Page 29: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/29.jpg)
© 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
![Page 30: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/30.jpg)
© 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
![Page 31: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/31.jpg)
© 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
![Page 32: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/32.jpg)
© 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
![Page 33: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/33.jpg)
© 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
![Page 34: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/34.jpg)
© 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
![Page 35: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/35.jpg)
© 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
![Page 36: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/36.jpg)
© 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
![Page 37: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/37.jpg)
© 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
![Page 38: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/38.jpg)
© 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
![Page 39: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/39.jpg)
© 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
![Page 40: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/40.jpg)
© 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
![Page 41: Top 5 best ways to improve your code pyxis v2 - en](https://reader035.vdocument.in/reader035/viewer/2022062514/558def021a28ab2b7e8b4691/html5/thumbnails/41.jpg)
© 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