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

Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 

Kürzlich hochgeladen (20)

Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 

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