SlideShare uma empresa Scribd logo
1 de 47
Why your code sucks and how
                to fix it
                                      Marjan Nikolovski
                            Senior Software Engineer, Seavus
                                Co-Owner, EmitKnowledge


emitknowledge.com/@mnikolovski
mk.linkedin.com/in/marjannikolovski
Agenda
Refactoring: Why, When
Why your code sucks
How to fix it
Refactoring cheat list
Refactoring
A change made to the internal structure of
 software to make it easier to understand and
 cheaper to modify without changing its
 observable behavior
Why refactoring – Technical factors
Make your code easier to understand
Make it easier to add new code
Improve the design of the codebase
Remove unused code
Better understanding of your codebase
Evolve your architecture instead of over
 engineering it
Why refactoring – Psychological factors
Learn to embrace the change
Refactors are result of code reviews. Accept
 that sometimes your code sucks !
Loose your fear to change existing codebase
When to refactor
Add new functionality
  – Refactor existing code to make the change with
    ease
Find bugs
  – Refactor to understand the code
Code reviews
  – Immediate results of the code review
End of a development iteration
  – Fix what is shitty
Why your code sucks
   Your code sucks if it doesn't work
   Your code sucks if it isn't testable
   Your code sucks if it's hard to read
   Your code sucks if it's not understandable
   Your code sucks if overuse a trendy framework at the cost of following good
    design/implementation practices
   Your code sucks if it has duplication
   Your code sucks if your function names suck
   Your code sucks if your variable names suck
   Your code sucks if your class names suck
   Your code sucks if your abstractions suck
   Your code sucks if a method is larger than the screen
   Your code sucks if you are reusing variables
   Your code sucks if you need to use comments to explain the code
   Your code sucks if an exception's stack trace doesn't return the original problem
Your code sucks if it doesn't work
Your code sucks if it isn't testable
Your code sucks if it's hard to read
Your code sucks if it's not
    understandable
Your code sucks if overuse a trendy
  framework at the cost of following
good design/implementation practices
Your code sucks if it has duplication
Your code sucks if your function names
                 suck
Your code sucks if your variable names
                 suck
Your code sucks if your class names
               suck
Your code sucks if your abstractions
               suck
Your code sucks if a method is larger
          than the screen
Your code sucks if you are reusing
            variables
Your code sucks if you need to use
  comments to explain the code
Your code sucks if an exception's stack
   trace doesn't return the original
              problem
How to fix it
Duplicated code
Long method
Primitive obsession
Switch statements
Long Parameter List
Duplicate code
Create a new common method
Use inheritance
Use template methods
Create a new common method
Use inheritance
Use template methods
Long method
Use extract to method
Minimize code nesting to maximum 1 level
 with method extraction
Extract each iteration as a method
Extract multiple conditions as method
Long method
Primitive obsession
Overuse of primitive types in classes instead
 of creating domain entities
Replace primitives with classes
Primitive obsession
Switch statements
Use command pattern to replace switch logic
Long Parameter List
Replace long param list with a class
Refactoring cheat list
 Replace Conditional with Polymorphism
 Remove Double Negative
 Replace Nested Conditional with Guard Clauses
 Reverse Conditional
 Replace Magic Number with Symbolic Constant
 Replace Exception with Test
 Encapsulate Downcast
 Consolidate Duplicate Conditional Fragments
 Consolidate Conditional Expression
Replace Conditional with
Polymorphism
Remove Double Negative
Reverse Conditional
Replace Nested Conditional with
        Guard Clauses
Replace Magic Number with Symbolic
             Constant
Replace Exception with Test
Encapsulate Downcast
Consolidate Duplicate Conditional
           Fragments
Consolidate Conditional Expression
Questions?
• Complete electronic evaluation
  forms on the computers in the
  hall and enter to win!
  – Telerik Ultimate Collection
  – CodeSmith Tools Generator Pro
    Personal
  – JetBrains ReSharper
  – Seavus EDC training vouchers
  – Pluralsight subscriptions
  – and many more…
Why your code sucks and how to fix it
Why your code sucks and how to fix it

Mais conteúdo relacionado

Destaque

Destaque (9)

Hidrolik Yük Asansörü Tasarımı ve Maliyet Analizi
Hidrolik Yük Asansörü Tasarımı ve Maliyet AnaliziHidrolik Yük Asansörü Tasarımı ve Maliyet Analizi
Hidrolik Yük Asansörü Tasarımı ve Maliyet Analizi
 
Ois lessons4
Ois lessons4Ois lessons4
Ois lessons4
 
How the fusion of time sensitive networking, time-triggered ethernet and data...
How the fusion of time sensitive networking, time-triggered ethernet and data...How the fusion of time sensitive networking, time-triggered ethernet and data...
How the fusion of time sensitive networking, time-triggered ethernet and data...
 
5 Signs you have an Insider Threat
5 Signs you have an Insider Threat5 Signs you have an Insider Threat
5 Signs you have an Insider Threat
 
IS test
IS testIS test
IS test
 
Intro, personality, attitude edited
Intro, personality, attitude editedIntro, personality, attitude edited
Intro, personality, attitude edited
 
Natural Language Classifier - Handbook (IBM)
Natural Language Classifier - Handbook (IBM)Natural Language Classifier - Handbook (IBM)
Natural Language Classifier - Handbook (IBM)
 
web basic 1
web basic 1web basic 1
web basic 1
 
Graphic Designer, Web Content Management
Graphic Designer, Web Content ManagementGraphic Designer, Web Content Management
Graphic Designer, Web Content Management
 

Mais de Marjan Nikolovski

Skyrocketing to the cloud with Windows Azure
Skyrocketing to the cloud with Windows AzureSkyrocketing to the cloud with Windows Azure
Skyrocketing to the cloud with Windows Azure
Marjan Nikolovski
 

Mais de Marjan Nikolovski (11)

Marjan.nikolovski down the rabbit hole error handling examined-v01
Marjan.nikolovski down the rabbit hole   error handling examined-v01Marjan.nikolovski down the rabbit hole   error handling examined-v01
Marjan.nikolovski down the rabbit hole error handling examined-v01
 
Down the rabbit hole - Error handling examined
Down the rabbit hole - Error handling examinedDown the rabbit hole - Error handling examined
Down the rabbit hole - Error handling examined
 
Built to last javascript for enterprise
Built to last   javascript for enterpriseBuilt to last   javascript for enterprise
Built to last javascript for enterprise
 
Enterprise js pratices
Enterprise js praticesEnterprise js pratices
Enterprise js pratices
 
Skyrocketing to the cloud with Windows Azure
Skyrocketing to the cloud with Windows AzureSkyrocketing to the cloud with Windows Azure
Skyrocketing to the cloud with Windows Azure
 
Band of brothers, building scalable social web apps on windows azure with asp...
Band of brothers, building scalable social web apps on windows azure with asp...Band of brothers, building scalable social web apps on windows azure with asp...
Band of brothers, building scalable social web apps on windows azure with asp...
 
Yellow.4 marjan nikolovski hunting rabbits and event-driven programming
Yellow.4 marjan nikolovski hunting rabbits and event-driven programmingYellow.4 marjan nikolovski hunting rabbits and event-driven programming
Yellow.4 marjan nikolovski hunting rabbits and event-driven programming
 
No sql - { If and Else }
No sql - { If and Else }No sql - { If and Else }
No sql - { If and Else }
 
Anti Corruption Layers
Anti Corruption LayersAnti Corruption Layers
Anti Corruption Layers
 
High Performance Computing
High Performance ComputingHigh Performance Computing
High Performance Computing
 
Entity Framework 4
Entity Framework 4Entity Framework 4
Entity Framework 4
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

Why your code sucks and how to fix it