Click here to load reader

Those Smells Code Smells & Refactoring · PDF file Code Smells Deeper problems Not bugs, nor errors Possibly Weakness Defects Inefficiency Design principles violation Ignore? Sure

  • View
    7

  • Download
    0

Embed Size (px)

Text of Those Smells Code Smells & Refactoring · PDF file Code Smells Deeper problems Not bugs,...

  • TechDiscuss.net Shaunak Sontakke

    Code Smells & Refactoring Those Smells

  • TechDiscuss.net Shaunak Sontakke

    Shaunak Sontakke

    ● Bachelor of Engineering, MBA in Systems ● Software Engineer @ Entrata ● Full Stack Developer for 13 years, HusBand for 6 years and Dad for 4 years ● Passionate about clean architecture, clean code, pragmatic development ● Love reading/listening Self-Help Books, Developer Podcasts ● Blog: TechDiscuss.net. Email: [email protected]

  • TechDiscuss.net Shaunak Sontakke

    Hello Everyones,

    Todays are a great day. I is happy because we are learning new thing’s.

  • TechDiscuss.net Shaunak Sontakke

    Code Smells

    ● Deeper problems ● Not bugs, nor errors ● Possibly

    ○ Weakness ○ Defects ○ Inefficiency ○ Design principles violation

    ● Ignore? Sure for cascading failures ● Agile software development term ● Code Smell Catalog gives you vocabulary in code review discussions ● Code Smells vs Anti-Pattern

  • TechDiscuss.net Shaunak Sontakke

    Code Smells Catalog

    1. Alternative classes w/ different interfaces

    2. Comments

    3. Data class

    4. Data clumps

    5. Dead code

    6. Divergent change

    7. Duplicated code

    8. Feature envy

    9. Inappropriate intimacy

    10. Incomplete library client

    11. Large class

    12. Lazy class

    13. Long method

    14. Long parameter list

    15. Message chains

    16. Middle man

    17. Parallel inheritance hierarchies

    18. Primitive obsession

    19. Refused bequest

    20. Shotgun surgery

    21. Speculative generality

    22. Switch statements

    23. Temporary field

  • TechDiscuss.net Shaunak Sontakke

    Code Smells Catalog

    1. Alternative classes w/ different interfaces

    2. Comments

    3. Data class

    4. Data clumps

    5. Dead code

    6. Divergent change

    7. Duplicated code

    8. Feature envy

    9. Inappropriate intimacy

    10. Incomplete library client

    11. Large class

    12. Lazy class

    13. Long method

    14. Long parameter list

    15. Message chains

    16. Middle man

    17. Parallel inheritance hierarchies

    18. Primitive obsession

    19. Refused bequest

    20. Shotgun surgery

    21. Speculative generality

    22. Switch statements

    23. Temporary field

  • TechDiscuss.net Shaunak Sontakke

    Code Smells Catalog

    1. Long method

    2. Large class

    3. Data clumps

    4. Long parameter list

    5. Primitive obsession

    1. Alternative classes w/ different interfaces

    2. Refused bequest

    3. Switch statements

    4. Temporary field

    1. Divergent change

    2. Shotgun surgery

    3. Parallel inheritance hierarchies

    1. Feature envy

    2. Inappropriate intimacy

    3. Message chains

    4. Middle man

    1. Comments

    2. Data class

    3. Dead code

    4. Duplicated code

    5. Lazy class

    6. Speculative generality

    B lo

    aters

    D isp

    en sab

    les C

    o u

    p lers

    C h

    an ge

    P reven

    ters

    O b

    ject- O

    rien tatio

    n

    A b

    u sers

  • TechDiscuss.net Shaunak Sontakke

    Code Smell Groups

    Change Preventers

    If you want to change something in one place you have to

    make many changes in other places too

    Excessive coupling between classes

    Couplers Object-Orientation Abusers

    Incomplete or incorrect

    implementation of OOP

    Bloaters

    Grown to a large size

    Dispensables

    Absence of these would make the code

    clean and improve code readability

  • TechDiscuss.net Shaunak Sontakke

    Long method

    ● Ask questions if the method is more than 10 lines ● Probably violating design principles like SRP, KISS

    Bloaters

    Grown to a large size

  • TechDiscuss.net Shaunak Sontakke

    Long method

    Large class

    ● Class doing too many things ● Too many member variables ● Breeding ground for duplicate code

    Bloaters

    Grown to a large size

  • TechDiscuss.net Shaunak Sontakke

    Data clumps

    Long method

    Large class

    ● Like children - enjoy hanging around in groups ● Same set of variables seen together ● name_first-name_last, start_date-end_date

    Bloaters

    Grown to a large size

  • TechDiscuss.net Shaunak Sontakke

    Long method

    Long parameter list

    Large class

    Data clumps

    ● More parameters ~ doing more things ● Another object hiding there ● SonarQube defaults: 4 is good ● Uncle Bob’s Clean Code: More than 3 requires

    special justification

    function doSomething($param1, $param2, $param3, $param4, $param5) { ... }

    Bloaters

    Grown to a large size

    https://rules.sonarsource.com/php/RSPEC-107?search=parameter

  • TechDiscuss.net Shaunak Sontakke

    Long method

    Primitive obsession

    Long parameter list

    Large class

    Data clumps

    ● Developers prefer primitive types like floats for money / currency classes, 2 string dates vs

    DateRange object

    ● Developers reluctant to use small objects for small tasks

    Bloaters

    Grown to a large size

  • TechDiscuss.net Shaunak Sontakke

    Long method

    Primitive obsession

    Long parameter list

    Large class

    Data clumps

    Bloaters

    Grown to a large size

  • TechDiscuss.net Shaunak Sontakke

    Divergent change

    ● Requires you to change multiple unrelated methods when you want to do a small change in

    class

    ● Adding a new coupon code functionality? Change shipping function, printing function, order function,

    etc.

    ● Applicable to changes in one class

    Change Preventers

    If you want to change something in one place you have to

    make many changes in other places too

  • TechDiscuss.net Shaunak Sontakke

    Divergent change

    Shotgun surgery

    ● One reason for changing multiple classes ● Similar to Divergent Change ● Divergent change is many changes to one class,

    while shotgun surgery is single change modifying

    multiple classes

    Change Preventers

    If you want to change something in one place you have to

    make many changes in other places too

  • TechDiscuss.net Shaunak Sontakke

    Divergent change

    Shotgun surgery

    Parallel inheritance hierarchies

    Departments

    HR Marketing

    Privilege

    HRPrivileges MarketingPrivileges

    Sales SalesPrivele ges

    Change Preventers

    If you want to change something in one place you have to

    make many changes in other places too

  • TechDiscuss.net Shaunak Sontakke

    Divergent change

    Shotgun surgery

    Parallel inheritance hierarchies

    Change Preventers

    If you want to change something in one place you have to

    make many changes in other places too

  • TechDiscuss.net Shaunak Sontakke

    Feature envy

    ● Method is more interested in a another class ● Accesses more public features of other class than

    its own

    class SalaryCalculator {

    public function calculateTotal() { $tax = $this->calculateTax(); }

    public function calculateTax() { $employee = new Employee(); $grossTotal = $employee->getTotalPay()

    + $employee->getTotalBonus();

    // ... return $tax; } }

    Excessive coupling between classes

    Couplers

  • TechDiscuss.net Shaunak Sontakke

    Feature envy

    Inappropriate intimacy

    ● Class touches internal fields and methods of another class

    ● Compromises other class's encapsulation

    class Customer { public function sendSms( $name, Contact $contact ) { $message = 'Hello '. $name; $message .= 'Contact:' . $contact->getPhoneNumber() . PHP_EOL; $message .= 'Email:' . $contact->getEmail() . PHP_EOL; } }Excessive coupling

    between classes

    Couplers

  • TechDiscuss.net Shaunak Sontakke

    ● Class that uses another class’s method, which in turn uses another class’s and so on

    ● Chain: Employee->EmployeeConfig->Config Feature envy

    Inappropriate intimacy

    Message chains

    class Employee { public function getConfiguration() { $this->employeeConfig->getConfiguration(); } } class EmployeeConfig { public function getConfiguration() { $this->config->getConfiguration(); } } class Config { public function getConfiguration() { $this->loadConfiguration(); } }

    Excessive coupling between classes

    Couplers

  • TechDiscuss.net Shaunak Sontakke

    ● Class exists just to delegate to another ● Is there a real purpose of this class?Feature envy

    Middle man

    Inappropriate intimacy

    Message chains

    class Customer {

    /** @var Person */ private $person;

    public function getNameFir

Search related