SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Destaque

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
 

Destaque (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
 

Mais de 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
 

Mais de 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
 

Último

Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 

Último (20)

Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 

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

Notas do Editor

  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