SlideShare ist ein Scribd-Unternehmen logo
1 von 41
TOP 5 WAYS TO IMPROVE
YOUR CODE

Top 5 best ways to improve your code

Write once
read many.
we are

Expertise
Quality
Respect

/studio
Agile

Big Data custom

CQRS Desktop Event
Sourcing Microsoft .NET C#
Scrum Software
development Training
© Pyxis Technologies inc.

Web
INTRODUCTION
Legacy code is
code without tests
 Michael Feather in
Working effectively
with legacy code

Without
maintenance, code
degrade rapidly
We must detect and
get rid of code
smell
© Pyxis Technologies inc.

Top 5 best ways to improve your code

3
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

4
SIMPLIFY CONDITIONALS
WHY?

1
2
3
4
5






Reduce complexity
Improve readability
Improve maintainability
Improve Reusability

© Pyxis Technologies inc.

Top 5 best ways to improve your code

5
SIMPLIFY CONDITIONALS
WHEN?

1
2
3
4
5







There is more than one conditions (and / or)
There is too many code in the body
Condition is based on type
There are nested conditions
There are many outcome from the same information (if /
elseif / switch case)

© Pyxis Technologies inc.

Top 5 best ways to improve your code

6
SIMPLIFY CONDITIONALS
HOW?

1
2
3
4
5

 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

© Pyxis Technologies inc.

Top 5 best ways to improve your code

7
2
3
4
5

SIMPLIFY CONDITIONALS

1

DECOMPOSE CONDITIONAL

© Pyxis Technologies inc.

Top 5 best ways to improve your code

8
2
3
4
5

SIMPLIFY CONDITIONALS

1

CONSOLIDATE CONDITIONAL EXPRESSION

© Pyxis Technologies inc.

Top 5 best ways to improve your code

9
2
3
4
5

SIMPLIFY CONDITIONALS

1

CONSOLIDATE DUPLICATE CONDITIONAL
FRAGMENTS

© Pyxis Technologies inc.

Top 5 best ways to improve your code

10
2
3
4
5

SIMPLIFY CONDITIONALS

1

INTRODUCE NULL OBJECT

© Pyxis Technologies inc.

Top 5 best ways to improve your code

11
2
3
4
5

SIMPLIFY CONDITIONALS

1

FLATTEN NESTED IF

© Pyxis Technologies inc.

Top 5 best ways to improve your code

12
2
3
4
5

SIMPLIFY CONDITIONALS

1

DON'T USE NEGATIVE

© Pyxis Technologies inc.

Top 5 best ways to improve your code

13
2
3
4
5

SIMPLIFY CONDITIONALS

1

KEEP CONDITIONAL STATEMENT LEAN
 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

© Pyxis Technologies inc.

Top 5 best ways to improve your code

14
2
3
4
5

SIMPLIFY CONDITIONALS

1

REPLACE CONDITIONAL WITH
POLYMORPHISM

© Pyxis Technologies inc.

Top 5 best ways to improve your code

15
2
3
4
5

SIMPLIFY CONDITIONALS

1

REPLACE CONDITIONAL LOGIC WITH
STRATEGY

© Pyxis Technologies inc.

Top 5 best ways to improve your code

16
2
3
4
5

SIMPLIFY CONDITIONALS

1

REPLACE CONDITIONAL DISPATCHER WITH
COMMAND

© Pyxis Technologies inc.

Top 5 best ways to improve your code

17
1

REMOVE COMMENTS
WHY?

2
3
4

 Improve readability
 Improve maintainability
 Avoid obsolete comments

5

© Pyxis Technologies inc.

Top 5 best ways to improve your code

18
REMOVE COMMENTS
WHEN?

1
2
3
4
5

 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

© Pyxis Technologies inc.

Top 5 best ways to improve your code

19
REMOVE COMMENTS
HOW?

1
2
3
4
5

 Replace comment with good naming
 Extract method
 Use meaningful words

 Write useful comments
 Respect standard naming (MSDN: Guidelines for names)





Properties
Enums
Events
Methods

© Pyxis Technologies inc.

Top 5 best ways to improve your code

20
EXTRACT METHOD
2
3
4
5

REMOVE COMMENTS

1

© Pyxis Technologies inc.

Top 5 best ways to improve your code

21
USE MEANINGFUL NAMES
2
3
4
5

REMOVE COMMENTS

1

 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

© Pyxis Technologies inc.

Top 5 best ways to improve your code

22
PROPERTIES
2
3
4
5

REMOVE COMMENTS

1

 Use PascalCase naming
 Use noun or adjective
 Don't use names that might be confused with a
Get
 Prefix boolean with Can, Is, or Has

© Pyxis Technologies inc.

Top 5 best ways to improve your code

23
ENUMS
2
3
4
5

REMOVE COMMENTS

1

 First element should be
default value
 Use PascalCasing naming
 Single value enum should
be singular
 Bit field enum should be
plural and has a Flag
attribute
 Bit field enum values must
be coherent (Read & Write
== ReadWrite)

© Pyxis Technologies inc.

Top 5 best ways to improve your code

24
EVENTS
2
3
4
5

REMOVE COMMENTS

1

 Use PascalCase naming
 Use past to describe post -event and present
progressive for pre-event
 Provide a virtual method
 Provide a cancel behavior for a pre -event

© Pyxis Technologies inc.

Top 5 best ways to improve your code

25
EVENTS
2
3
4
5

REMOVE COMMENTS

1

© Pyxis Technologies inc.

Top 5 best ways to improve your code

26
METHODS
2
3
4
5

REMOVE COMMENTS

1

 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)

© Pyxis Technologies inc.

Top 5 best ways to improve your code

27
1

CLARIFY CONTRACTS
WHY?

2
3
4

 Improve performance
 Improve readability
 Improve reusability

5

© Pyxis Technologies inc.

Top 5 best ways to improve your code

28
CLARIFY CONTRACTS
QUAND?

1
2
3
4
5

 The is too many paramters (how many is too much?)
 A method has more than one responsability
 A method use out paramters
 Except for:
 TryPattern
 Service contracts

 You need a default value

© Pyxis Technologies inc.

Top 5 best ways to improve your code

29
CLARIFY CONTRACTS
HOW?

1
2
3
4
5

 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

© Pyxis Technologies inc.

Top 5 best ways to improve your code

30
INTRODUCE PARAMETER OBJECT
2
3
4
5

CLARIFY CONTRACTS

1

© Pyxis Technologies inc.

Top 5 best ways to improve your code

31
CREATE OVERLOAD WITH FEWER
PARAMETERS

2
3
4
5

CLARIFY CONTRACTS

1

© Pyxis Technologies inc.

Top 5 best ways to improve your code

32
USE DEFAULT VALUE
2
3
4
5

CLARIFY CONTRACTS

1

© Pyxis Technologies inc.

Top 5 best ways to improve your code

33
CONTROL SCOPE
WHY?

1
2
3
4

 Avoid side effects
 Improve reusability
 Improve maintainability

5

© Pyxis Technologies inc.

Top 5 best ways to improve your code

34
CONTROL SCOPE
WHEN?

1
2
3

 A field is used only n a few methods
 Internal implementation is exposed by public members

4
5

© Pyxis Technologies inc.

Top 5 best ways to improve your code

35
CONTROL SCOPE
HOW?

1
2
3
4
5

 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

© Pyxis Technologies inc.

Top 5 best ways to improve your code

36
FLUSH DEAD CODE
WHY?

1
2
3
4
5







Because we must
Improve maintainability
Improve performance
Improve readability
100% test coverage

© Pyxis Technologies inc.

Top 5 best ways to improve your code

37
1

FLUSH DEAD CODE
WHEN?

2
3
4

 You know the code is dead
 You think the code is dead
 You want the code to be dead

5

© Pyxis Technologies inc.

Top 5 best ways to improve your code

38
FLUSH DEAD CODE
HOW?

1
2
3
4
5

 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

© Pyxis Technologies inc.

Top 5 best ways to improve your code

39
REFERENCES
 Refactoring – Improving the design of existing code
 Auteur: Martin Fowler
 Edition: Addison Wesley
 ISBN: 978-0-201-48567-7

 Refactoring to patterns (Martin Fowler signature)
 Auteur: Joshua Kerievsky
 Edition: Adison Wesley
 ISBN: 978-0-321-21335-1

 Clean code – a handbook of agile software craftsmanship
 Auteur: Robert C. Martin
 Edition: Prentice Hall
 ISBN: 978-0-132-35088-4

 Working effectively with legacy code
 Auteur: Michael C. Feather
 Edition: Prentice Hall
 ISBN: 978-0-13-117705-5

© Pyxis Technologies inc.

Top 5 best ways to improve your code

40
THE END
 Questions?
 Remember

Simplify
conditionals

Remove
comments

Clarify
contracts

Control
scope

Flush dead
code

 Eric De Carufel
 eric@decarufel.net
 http://blog.decarufel.net
© Pyxis Technologies inc.

Top 5 best ways to improve your code

41

Weitere ähnliche Inhalte

Andere mochten auch

Web Components & Shadow DOM
Web Components & Shadow DOMWeb Components & Shadow DOM
Web Components & Shadow DOMThomas Bassetto
 
Industrialiser ses développements PHP - RMLL 2010
Industrialiser ses développements PHP - RMLL 2010Industrialiser ses développements PHP - RMLL 2010
Industrialiser ses développements PHP - RMLL 2010Jean-Marc Fontaine
 
La qualité au-delà du code - ConFoo 2012
La qualité au-delà du code - ConFoo 2012La qualité au-delà du code - ConFoo 2012
La qualité au-delà du code - ConFoo 2012Jean-Marc Fontaine
 
Pyxis medication administration system
Pyxis medication administration systemPyxis medication administration system
Pyxis medication administration systemawrightlewis
 
Becoming a Polyglot Programmer Through the Eyes of a Freelance Musician
Becoming a Polyglot Programmer Through the Eyes of a Freelance MusicianBecoming a Polyglot Programmer Through the Eyes of a Freelance Musician
Becoming a Polyglot Programmer Through the Eyes of a Freelance Musiciankickinbahk
 
Practical git for developers
Practical git for developersPractical git for developers
Practical git for developersWim Godden
 
Top 5 Reasons to get Country Code Top Level Domain (ccTLD) name for your blog...
Top 5 Reasons to get Country Code Top Level Domain (ccTLD) name for your blog...Top 5 Reasons to get Country Code Top Level Domain (ccTLD) name for your blog...
Top 5 Reasons to get Country Code Top Level Domain (ccTLD) name for your blog...Yogesh M. A.
 
Managing performance appriasal at QU changing culture
Managing performance appriasal at QU changing cultureManaging performance appriasal at QU changing culture
Managing performance appriasal at QU changing cultureDR Hend Al Muftah
 
Building interactivity with websockets
Building interactivity with websocketsBuilding interactivity with websockets
Building interactivity with websocketsWim Godden
 
Running .NET on Docker
Running .NET on DockerRunning .NET on Docker
Running .NET on DockerBen Hall
 

Andere mochten auch (18)

Web Components & Shadow DOM
Web Components & Shadow DOMWeb Components & Shadow DOM
Web Components & Shadow DOM
 
Leaderpalooza Feb2010
Leaderpalooza Feb2010Leaderpalooza Feb2010
Leaderpalooza Feb2010
 
Python avancé : Qualité de code et convention de codage
Python avancé : Qualité de code et convention de codagePython avancé : Qualité de code et convention de codage
Python avancé : Qualité de code et convention de codage
 
Af confef
Af confefAf confef
Af confef
 
Industrialiser ses développements PHP - RMLL 2010
Industrialiser ses développements PHP - RMLL 2010Industrialiser ses développements PHP - RMLL 2010
Industrialiser ses développements PHP - RMLL 2010
 
La qualité au-delà du code - ConFoo 2012
La qualité au-delà du code - ConFoo 2012La qualité au-delà du code - ConFoo 2012
La qualité au-delà du code - ConFoo 2012
 
Influence With Peers
Influence With PeersInfluence With Peers
Influence With Peers
 
Pyxis medication administration system
Pyxis medication administration systemPyxis medication administration system
Pyxis medication administration system
 
Becoming a Polyglot Programmer Through the Eyes of a Freelance Musician
Becoming a Polyglot Programmer Through the Eyes of a Freelance MusicianBecoming a Polyglot Programmer Through the Eyes of a Freelance Musician
Becoming a Polyglot Programmer Through the Eyes of a Freelance Musician
 
Geothermal energy
Geothermal energyGeothermal energy
Geothermal energy
 
Practical git for developers
Practical git for developersPractical git for developers
Practical git for developers
 
Top 5 Reasons to get Country Code Top Level Domain (ccTLD) name for your blog...
Top 5 Reasons to get Country Code Top Level Domain (ccTLD) name for your blog...Top 5 Reasons to get Country Code Top Level Domain (ccTLD) name for your blog...
Top 5 Reasons to get Country Code Top Level Domain (ccTLD) name for your blog...
 
Managing performance appriasal at QU changing culture
Managing performance appriasal at QU changing cultureManaging performance appriasal at QU changing culture
Managing performance appriasal at QU changing culture
 
Methods Of PM
Methods Of PMMethods Of PM
Methods Of PM
 
Pa3
Pa3Pa3
Pa3
 
Qualité de code et bonnes pratiques
Qualité de code et bonnes pratiquesQualité de code et bonnes pratiques
Qualité de code et bonnes pratiques
 
Building interactivity with websockets
Building interactivity with websocketsBuilding interactivity with websockets
Building interactivity with websockets
 
Running .NET on Docker
Running .NET on DockerRunning .NET on Docker
Running .NET on Docker
 

Mehr von Eric De Carufel

Bracket Show Episode 35 - histoire de c# de 2002 à 2019
Bracket Show Episode 35 - histoire de c# de 2002 à 2019Bracket Show Episode 35 - histoire de c# de 2002 à 2019
Bracket Show Episode 35 - histoire de c# de 2002 à 2019Eric De Carufel
 
Architecture azure performante
Architecture azure performanteArchitecture azure performante
Architecture azure performanteEric De Carufel
 
Refactoring to Design Patterns
Refactoring to Design PatternsRefactoring to Design Patterns
Refactoring to Design PatternsEric De Carufel
 
Cqrs + event sourcing pyxis v2 - en
Cqrs + event sourcing   pyxis v2 - enCqrs + event sourcing   pyxis v2 - en
Cqrs + event sourcing pyxis v2 - enEric De Carufel
 
Dvcs mercurial - pyxis - eric de carufel
Dvcs   mercurial - pyxis - eric de carufelDvcs   mercurial - pyxis - eric de carufel
Dvcs mercurial - pyxis - eric de carufelEric De Carufel
 
Top 5 des meilleures façon d'améliorer ton code
Top 5 des meilleures façon d'améliorer ton codeTop 5 des meilleures façon d'améliorer ton code
Top 5 des meilleures façon d'améliorer ton codeEric De Carufel
 

Mehr von Eric De Carufel (7)

Bracket Show Episode 35 - histoire de c# de 2002 à 2019
Bracket Show Episode 35 - histoire de c# de 2002 à 2019Bracket Show Episode 35 - histoire de c# de 2002 à 2019
Bracket Show Episode 35 - histoire de c# de 2002 à 2019
 
Architecture azure performante
Architecture azure performanteArchitecture azure performante
Architecture azure performante
 
Refactoring to Design Patterns
Refactoring to Design PatternsRefactoring to Design Patterns
Refactoring to Design Patterns
 
Cqrs + event sourcing pyxis v2 - en
Cqrs + event sourcing   pyxis v2 - enCqrs + event sourcing   pyxis v2 - en
Cqrs + event sourcing pyxis v2 - en
 
CQRS + Event Sourcing
CQRS + Event SourcingCQRS + Event Sourcing
CQRS + Event Sourcing
 
Dvcs mercurial - pyxis - eric de carufel
Dvcs   mercurial - pyxis - eric de carufelDvcs   mercurial - pyxis - eric de carufel
Dvcs mercurial - pyxis - eric de carufel
 
Top 5 des meilleures façon d'améliorer ton code
Top 5 des meilleures façon d'améliorer ton codeTop 5 des meilleures façon d'améliorer ton code
Top 5 des meilleures façon d'améliorer ton code
 

Kürzlich hochgeladen

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 

Kürzlich hochgeladen (20)

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Top 5 best ways to improve your code pyxis v2 - en

  • 1. TOP 5 WAYS TO IMPROVE YOUR CODE Top 5 best ways to improve your code Write once read many.
  • 2. we are Expertise Quality Respect /studio Agile Big Data custom CQRS Desktop Event Sourcing Microsoft .NET C# Scrum Software development Training © Pyxis Technologies inc. Web
  • 3. INTRODUCTION Legacy code is code without tests  Michael Feather in Working effectively with legacy code Without maintenance, code degrade rapidly We must detect and get rid of code smell © Pyxis Technologies inc. Top 5 best ways to improve your code 3
  • 4. 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 4
  • 5. SIMPLIFY CONDITIONALS WHY? 1 2 3 4 5     Reduce complexity Improve readability Improve maintainability Improve Reusability © Pyxis Technologies inc. Top 5 best ways to improve your code 5
  • 6. SIMPLIFY CONDITIONALS WHEN? 1 2 3 4 5      There is more than one conditions (and / or) There is too many code in the body Condition is based on type There are nested conditions There are many outcome from the same information (if / elseif / switch case) © Pyxis Technologies inc. Top 5 best ways to improve your code 6
  • 7. SIMPLIFY CONDITIONALS HOW? 1 2 3 4 5  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 © Pyxis Technologies inc. Top 5 best ways to improve your code 7
  • 8. 2 3 4 5 SIMPLIFY CONDITIONALS 1 DECOMPOSE CONDITIONAL © Pyxis Technologies inc. Top 5 best ways to improve your code 8
  • 9. 2 3 4 5 SIMPLIFY CONDITIONALS 1 CONSOLIDATE CONDITIONAL EXPRESSION © Pyxis Technologies inc. Top 5 best ways to improve your code 9
  • 10. 2 3 4 5 SIMPLIFY CONDITIONALS 1 CONSOLIDATE DUPLICATE CONDITIONAL FRAGMENTS © Pyxis Technologies inc. Top 5 best ways to improve your code 10
  • 11. 2 3 4 5 SIMPLIFY CONDITIONALS 1 INTRODUCE NULL OBJECT © Pyxis Technologies inc. Top 5 best ways to improve your code 11
  • 12. 2 3 4 5 SIMPLIFY CONDITIONALS 1 FLATTEN NESTED IF © Pyxis Technologies inc. Top 5 best ways to improve your code 12
  • 13. 2 3 4 5 SIMPLIFY CONDITIONALS 1 DON'T USE NEGATIVE © Pyxis Technologies inc. Top 5 best ways to improve your code 13
  • 14. 2 3 4 5 SIMPLIFY CONDITIONALS 1 KEEP CONDITIONAL STATEMENT LEAN  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 © Pyxis Technologies inc. Top 5 best ways to improve your code 14
  • 15. 2 3 4 5 SIMPLIFY CONDITIONALS 1 REPLACE CONDITIONAL WITH POLYMORPHISM © Pyxis Technologies inc. Top 5 best ways to improve your code 15
  • 16. 2 3 4 5 SIMPLIFY CONDITIONALS 1 REPLACE CONDITIONAL LOGIC WITH STRATEGY © Pyxis Technologies inc. Top 5 best ways to improve your code 16
  • 17. 2 3 4 5 SIMPLIFY CONDITIONALS 1 REPLACE CONDITIONAL DISPATCHER WITH COMMAND © Pyxis Technologies inc. Top 5 best ways to improve your code 17
  • 18. 1 REMOVE COMMENTS WHY? 2 3 4  Improve readability  Improve maintainability  Avoid obsolete comments 5 © Pyxis Technologies inc. Top 5 best ways to improve your code 18
  • 19. REMOVE COMMENTS WHEN? 1 2 3 4 5  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 © Pyxis Technologies inc. Top 5 best ways to improve your code 19
  • 20. REMOVE COMMENTS HOW? 1 2 3 4 5  Replace comment with good naming  Extract method  Use meaningful words  Write useful comments  Respect standard naming (MSDN: Guidelines for names)     Properties Enums Events Methods © Pyxis Technologies inc. Top 5 best ways to improve your code 20
  • 21. EXTRACT METHOD 2 3 4 5 REMOVE COMMENTS 1 © Pyxis Technologies inc. Top 5 best ways to improve your code 21
  • 22. USE MEANINGFUL NAMES 2 3 4 5 REMOVE COMMENTS 1  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 © Pyxis Technologies inc. Top 5 best ways to improve your code 22
  • 23. PROPERTIES 2 3 4 5 REMOVE COMMENTS 1  Use PascalCase naming  Use noun or adjective  Don't use names that might be confused with a Get  Prefix boolean with Can, Is, or Has © Pyxis Technologies inc. Top 5 best ways to improve your code 23
  • 24. ENUMS 2 3 4 5 REMOVE COMMENTS 1  First element should be default value  Use PascalCasing naming  Single value enum should be singular  Bit field enum should be plural and has a Flag attribute  Bit field enum values must be coherent (Read & Write == ReadWrite) © Pyxis Technologies inc. Top 5 best ways to improve your code 24
  • 25. EVENTS 2 3 4 5 REMOVE COMMENTS 1  Use PascalCase naming  Use past to describe post -event and present progressive for pre-event  Provide a virtual method  Provide a cancel behavior for a pre -event © Pyxis Technologies inc. Top 5 best ways to improve your code 25
  • 26. EVENTS 2 3 4 5 REMOVE COMMENTS 1 © Pyxis Technologies inc. Top 5 best ways to improve your code 26
  • 27. METHODS 2 3 4 5 REMOVE COMMENTS 1  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) © Pyxis Technologies inc. Top 5 best ways to improve your code 27
  • 28. 1 CLARIFY CONTRACTS WHY? 2 3 4  Improve performance  Improve readability  Improve reusability 5 © Pyxis Technologies inc. Top 5 best ways to improve your code 28
  • 29. CLARIFY CONTRACTS QUAND? 1 2 3 4 5  The is too many paramters (how many is too much?)  A method has more than one responsability  A method use out paramters  Except for:  TryPattern  Service contracts  You need a default value © Pyxis Technologies inc. Top 5 best ways to improve your code 29
  • 30. CLARIFY CONTRACTS HOW? 1 2 3 4 5  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 © Pyxis Technologies inc. Top 5 best ways to improve your code 30
  • 31. INTRODUCE PARAMETER OBJECT 2 3 4 5 CLARIFY CONTRACTS 1 © Pyxis Technologies inc. Top 5 best ways to improve your code 31
  • 32. CREATE OVERLOAD WITH FEWER PARAMETERS 2 3 4 5 CLARIFY CONTRACTS 1 © Pyxis Technologies inc. Top 5 best ways to improve your code 32
  • 33. USE DEFAULT VALUE 2 3 4 5 CLARIFY CONTRACTS 1 © Pyxis Technologies inc. Top 5 best ways to improve your code 33
  • 34. CONTROL SCOPE WHY? 1 2 3 4  Avoid side effects  Improve reusability  Improve maintainability 5 © Pyxis Technologies inc. Top 5 best ways to improve your code 34
  • 35. CONTROL SCOPE WHEN? 1 2 3  A field is used only n a few methods  Internal implementation is exposed by public members 4 5 © Pyxis Technologies inc. Top 5 best ways to improve your code 35
  • 36. CONTROL SCOPE HOW? 1 2 3 4 5  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 © Pyxis Technologies inc. Top 5 best ways to improve your code 36
  • 37. FLUSH DEAD CODE WHY? 1 2 3 4 5      Because we must Improve maintainability Improve performance Improve readability 100% test coverage © Pyxis Technologies inc. Top 5 best ways to improve your code 37
  • 38. 1 FLUSH DEAD CODE WHEN? 2 3 4  You know the code is dead  You think the code is dead  You want the code to be dead 5 © Pyxis Technologies inc. Top 5 best ways to improve your code 38
  • 39. FLUSH DEAD CODE HOW? 1 2 3 4 5  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 © Pyxis Technologies inc. Top 5 best ways to improve your code 39
  • 40. REFERENCES  Refactoring – Improving the design of existing code  Auteur: Martin Fowler  Edition: Addison Wesley  ISBN: 978-0-201-48567-7  Refactoring to patterns (Martin Fowler signature)  Auteur: Joshua Kerievsky  Edition: Adison Wesley  ISBN: 978-0-321-21335-1  Clean code – a handbook of agile software craftsmanship  Auteur: Robert C. Martin  Edition: Prentice Hall  ISBN: 978-0-132-35088-4  Working effectively with legacy code  Auteur: Michael C. Feather  Edition: Prentice Hall  ISBN: 978-0-13-117705-5 © Pyxis Technologies inc. Top 5 best ways to improve your code 40
  • 41. THE END  Questions?  Remember Simplify conditionals Remove comments Clarify contracts Control scope Flush dead code  Eric De Carufel  eric@decarufel.net  http://blog.decarufel.net © Pyxis Technologies inc. Top 5 best ways to improve your code 41

Hinweis der Redaktion

  1. Pourquoi?Quand?Comment?
  2. Only one level of abstraction per methodSwitch case? Polymorphism?
  3. The nameshouldasnwer all big questionsThe code shouldbereadablelike a storyOne word per concept (i.e. State -> AddressState != SystemState)
  4. ** Use Intention-Revealing names (d -> elapsedInDays)** Avoid disinformation (AccountList -> Accounts)** Make meaningful distinction (moneyAmountvs money vs amount -> InvoiceTotal)** Use pronounceable names (genymdhms -> generationTimestamp** Avoid encoding (phoneString -> phone)** Avoid mental mapping (a -> account)** Class names -> noun not verbs** Method names -> verbs (SaveAccount)** Don't be cute. Use standard names (Destroy, kill, obliterate -> Delete)** Solution Domain Names (Interface) vs Problem Domain Names (Implémentation)
  5. Reduce number of parameters - Clean code chapter 3 (p. 40-43)
  6. Réduisez la visibilitéUtilisez les protectedUtilisez les privateUtilisez les InternalRéduisez le scopeDéplacez un field vers une méthodeScindez une classeDéplacer une variable près de son utilisationRéduisez la durée de vieInitialisation tardiveRéduisez les référencesLibérer tôt
  7. Dead procedure / function / methodDead variableDead parameterDead return valueDead event declarationDead enumeration / constantDead typeDead class / structDead interface