SlideShare a Scribd company logo
1 of 84
Download to read offline
Smell your Code!

  Yaser Sulaiman




    www.free-dimension.com   1
These slides are based on an
internal presentation I gave
  @ Free Dimension (FD)




          www.free-dimension.com   2
It contained examples from real
            projects



            www.free-dimension.com   3
FD agreed to publish it online under
     a CC BY-NC-SA license…



              www.free-dimension.com   4
after removing, or disguising, the
         real examples



             www.free-dimension.com   5
Disguised examples are marked
                 with



δ              www.free-dimension.com   6
Our road map




   www.free-dimension.com   7
What?     Why?                   How?




        www.free-dimension.com          8
If you can take away only 1 lesson…




              www.free-dimension.com   9
Don’t Repeat Yourself!




       www.free-dimension.com   10
This rule is so important I have to
              break it



              www.free-dimension.com   11
Don’t Repeat Yourself!




       www.free-dimension.com   12
What?     Why?                   How?




        www.free-dimension.com          13
Code smells?!




   www.free-dimension.com   14
Warning signs




   www.free-dimension.com   15
photo by pj_in_oz
www.free-dimension.com   16
Surface indications of deeper
          problems



           www.free-dimension.com   17
photo by BenChenowethWork
     www.free-dimension.com   18
Surface indications of deeper
          problems



           www.free-dimension.com   19
www.free-dimension.com   20
What?     Why?                   How?




        www.free-dimension.com          21
The bigger picture




     www.free-dimension.com   22
Software
   Quality


  Code Quality




      Code Smells




   not to scale
www.free-dimension.com   23
Writing HQ code should be the
          priority



           www.free-dimension.com   24
www.free-dimension.com   25
www.free-dimension.com   26
One path to cleaner code goes
    through code smells



           www.free-dimension.com   27
What?     Why?                   How?




        www.free-dimension.com          28
Can you smell code?!




      www.free-dimension.com   29
photo by lanceball

www.free-dimension.com   30
There’re many code smells




         www.free-dimension.com   31
The following smells are just a
            subset



            www.free-dimension.com   32
They’re some of the smells I smelled
         while working here



              www.free-dimension.com   33
Code Smell #1




   www.free-dimension.com   34
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
            www.free-dimension.com   35
The nastiest code smell




        www.free-dimension.com   36
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
DupIicated code DupIicated code
            www.free-dimension.com   37
Example #1




  www.free-dimension.com   38
BooksController
             &
    NotebooksController



δ         www.free-dimension.com   39
The diff test




   www.free-dimension.com   40
FD Employees Only!




    ignoring whitespace differences
           www.free-dimension.com     41
Example #2




  www.free-dimension.com   42
MessagesController




δ         www.free-dimension.com   43
class MessagesController extends AppController {
  function zebra_inbox() {
    // inbox code
  }

    function zebra_view($id = null) {
      // view message code
    }
}




δ                      www.free-dimension.com      44
class MessagesController extends AppController {
  function zebra_inbox() {
    // inbox code
  }

    function elephant_inbox() {
      // inbox code
    }

    function zebra_view($id = null) {
      // view message code
    }

    function elephant_view($id = null) {
      // view message code
    }
}



δ                                 www.free-dimension.com   45
But then came the penguin, the
         dolphin, the chimp,…



δ              www.free-dimension.com   46
Refactor!




 www.free-dimension.com   47
class MessagesController extends AppController {
  function zebra_inbox() {
    $this->setAction('inbox');
  }

 function elephant_inbox() {
   $this->setAction('inbox');
 }

 function inbox() {
   // inbox code
 }

 function zebra_view($id = null) {
   $this->setAction('view', $id);
 }

 function elephant_view($id = null) {
   $this->setAction('view', $id);
 }

 function view($id = null) {
    // view message code
  }
}




δ                                          www.free-dimension.com   48
Added plus: no duplicated views




            www.free-dimension.com   49
FD Employees Only!




      www.free-dimension.com   50
Code Smell #2




   www.free-dimension.com   51
Too many parameters




      www.free-dimension.com   52
How many is too many?




       www.free-dimension.com   53
www.free-dimension.com   54
www.free-dimension.com   55
Example

(brace your noses!)




     www.free-dimension.com   56
public abstract class AProvider
{
  public abstract void AddToFamily(int familyId, string firstName, string sex,
    string photo, DateTime? birthDate, string birthCountry, string birthCity,
    bool living, DateTime? deathDate, string country, string city, string email,
    string homepage, string mobilePhone, string biography, string jobTitle,
    string maritalStatus, string spouse, int order, int? fatherId);

    public abstract void UpdateInFamily(int id, int familyId, string firstName,
      string sex, string photo, DateTime? birthDate, string birthCountry,
      string birthCity, bool living, DateTime? deathDate, string country,
      string city, string email, string homepage, string mobilePhone,
      string biography, string jobTitle, string maritalStatus, string spouse,
      int order, int? fatherId);
}




δ                                  www.free-dimension.com                    57
!




www.free-dimension.com   58
I misunderstood coupling. My bad!




             www.free-dimension.com   59
public abstract class AProvider
{
  public abstract void AddToFamily(Person person);

    public abstract void UpdateInFamily(Person person);
}




δ                        www.free-dimension.com           60
The remaining smells have many
 examples; check the repository



            www.free-dimension.com   61
Code Smell #3




   www.free-dimension.com   62
Not following a coding
convention/standard/style
       consistently



         www.free-dimension.com   63
Not following a coding
convention/standard/style
       consistently



         www.free-dimension.com   64
The specifics doesn’t really matter;
 what matters most is consistency



              www.free-dimension.com   65
Code Smell #4




   www.free-dimension.com   66
// code.CommentOut();




       www.free-dimension.com   67
“Commented-out code is an
abomination.”—Uncle Bob
     photo used with permission of Uncle Bob
                www.free-dimension.com         68
Don’t commit commented-out code




            www.free-dimension.com   69
Code Smell #5




   www.free-dimension.com   70
redundant

// This is a comment




       www.free-dimension.com   71
Comments
should focus on



            What?                  Why?   How?




          www.free-dimension.com                 72
What?     Why?                   How?




        www.free-dimension.com          73
I may have come off as an a$$




           www.free-dimension.com   74
My code smells as bad as everyone
 else’s.. sometimes even worse



             www.free-dimension.com   75
But I’m aware of my smells




         www.free-dimension.com   76
I try my best to prevent them




           www.free-dimension.com   77
I don’t ignore them; I deal with
             them



            www.free-dimension.com   78
I smell my code




    www.free-dimension.com   79
You should smell your code too




           www.free-dimension.com   80
If you want to sniff more…




         www.free-dimension.com   81
more than just code smells
    www.free-dimension.com   82
…</presentation>
  <questions>…



     www.free-dimension.com   83
www.free-dimension.com   84

More Related Content

Similar to Smell your Code! @ Free Dimension

Similar to Smell your Code! @ Free Dimension (20)

Writing clean code in C# and .NET
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NET
 
Clean Code - The Next Chapter
Clean Code - The Next ChapterClean Code - The Next Chapter
Clean Code - The Next Chapter
 
Code Excellence for the Average Programmer
Code Excellence for the Average ProgrammerCode Excellence for the Average Programmer
Code Excellence for the Average Programmer
 
Smell your Code! @ IET-KFUPM PATW
Smell your Code! @ IET-KFUPM PATWSmell your Code! @ IET-KFUPM PATW
Smell your Code! @ IET-KFUPM PATW
 
Coding Dojo (November, 2017)
Coding Dojo (November, 2017)Coding Dojo (November, 2017)
Coding Dojo (November, 2017)
 
Death of a Themer - Frontend United - 14 April 2013
Death of a Themer - Frontend United - 14 April 2013Death of a Themer - Frontend United - 14 April 2013
Death of a Themer - Frontend United - 14 April 2013
 
DDD beyond the infamous repository pattern - GeeCon Prague 2018
DDD beyond the infamous repository pattern - GeeCon Prague 2018DDD beyond the infamous repository pattern - GeeCon Prague 2018
DDD beyond the infamous repository pattern - GeeCon Prague 2018
 
Monitoring a program that monitors computer networks
Monitoring a program that monitors computer networksMonitoring a program that monitors computer networks
Monitoring a program that monitors computer networks
 
Clean Code @Voxxed Days Cluj 2023 - opening Keynote
Clean Code @Voxxed Days Cluj 2023 - opening KeynoteClean Code @Voxxed Days Cluj 2023 - opening Keynote
Clean Code @Voxxed Days Cluj 2023 - opening Keynote
 
DDD for real
DDD for realDDD for real
DDD for real
 
Efficient JavaScript Development
Efficient JavaScript DevelopmentEfficient JavaScript Development
Efficient JavaScript Development
 
Wtf per lineofcode
Wtf per lineofcodeWtf per lineofcode
Wtf per lineofcode
 
Refactoring 2TheMax (con ReSharper)
Refactoring 2TheMax (con ReSharper)Refactoring 2TheMax (con ReSharper)
Refactoring 2TheMax (con ReSharper)
 
Handle your Lambdas - From event-based processing to Continuous Integration /...
Handle your Lambdas - From event-based processing to Continuous Integration /...Handle your Lambdas - From event-based processing to Continuous Integration /...
Handle your Lambdas - From event-based processing to Continuous Integration /...
 
Efficient JavaScript Development
Efficient JavaScript DevelopmentEfficient JavaScript Development
Efficient JavaScript Development
 
Roboform Basic Tutorial
Roboform Basic TutorialRoboform Basic Tutorial
Roboform Basic Tutorial
 
Effective Code Reviews
Effective Code ReviewsEffective Code Reviews
Effective Code Reviews
 
Tetuan Valley Startup School presentation
Tetuan Valley Startup School presentationTetuan Valley Startup School presentation
Tetuan Valley Startup School presentation
 
Tetuan Valley Startup School Presentation
Tetuan Valley Startup School PresentationTetuan Valley Startup School Presentation
Tetuan Valley Startup School Presentation
 
Sylius, the good choice
Sylius, the good choiceSylius, the good choice
Sylius, the good choice
 

Recently uploaded

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
panagenda
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

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
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
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
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 

Smell your Code! @ Free Dimension