COMP2511

25T2 Week 10 🥳

Friday 1-2pm (F13A)

 

MyExperience ->

Admin

  • Assignment 2 due 3pm today!
    • No extensions
    • Please let me know as soon as possible for contribution issues
  • Please check over your lab marks and let myself or Adrian know if any issues

MyExperience

Please fill it in (5 mins)

Group Task

Finding patterns

Finding Patterns

  1. Sorting collections of records in different orders
  2. Listing the contents of a file system
  3. Updating a UI component when the state of a program changes
  4. A frozen yogurt shop model which changes cost and weight of a bowl of frozen yogurt based on the toppings that customers choose
  5. You are making a stealth game and want the enemy guards to be alerted when the player produces a loud noise.
  6. Creating a cross-platform application that needs to generate different types of UI components (buttons, menus, dialogs) that match the look and feel of each operating system (Windows, macOS, Linux).
  7. A text editor application where you can apply multiple formatting options (bold, italic, underline, highlight, different fonts) to text, with the ability to combine these effects and add or remove them dynamically.

Strategy

Composite

Observer

Decorator

Observer

Abstract Factory

Decorator

Group Task

Code and Design Smells

Code and Design Smells

Mark, Bill and Jeff are working on a PetShop application. The PetShop has functionality to feed, clean and exercise different types of animals.

Mark notices that each time he adds a new species of animal to his system, he also has to rewrite all the methods in the PetShop so it can take care of the new animal.

What code or design smell is present here?

  • Code smell - Divergent change
  • Design problem - Open Closed Principle, high coupling

Code and Design Smells

public class Person {
    private String firstName;
    private String lastName;
    private int age;
    private int birthDay;
    private int birthMonth;
    private int birthYear;
    private String streetAddress;
    private String suburb;
    private String city;
    private String country;
    private int postcode;
    public Person(String firstName, String lastName, 
    			  int age, int birthDay, int birthMonth,
                  int birthYear, String streetAddress,
                  String suburb, String city, String country,
                  int postcode) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.birthDay = birthDay;
        this.birthMonth = birthMonth;
        this.birthYear = birthYear;
        this.streetAddress = streetAddress;
        this.suburb = suburb;
        this.city = city;
        this.country = country;
        this.postcode = postcode;
    }
    // Some various methods below...
}

What code or design smell is present here?

Data clumps & long parameter list

How would you refactor to fix this?

  • Refactor by making more classes for birthday and address (Extract Class / Introduce Parameter Object)
  • Design problem - DRY and KISS

 

If some of the input was "optional" builder pattern may be useful here

Code and Design Smells

public class MathLibrary {
    List<Book> books;

    int sumTitles {
        int total = 0
        for (Book b : books) {
            total += b.title.titleLength;
        }
        return total;
    }
}

public class Book {
    Title title; // Our system just models books
                 // as titles (content doesn't matter)
}

public class Title {
    int titleLength;

    int getTitleLength() {
        return titleLength;
    }

    void setTitleLength(int tL) {
        titleLength = tL;
    }
}

What code or design smell is present here?

  • Inappropriate intimacy (accessing public fields)
  • Message chains/Law of Demeter
  • Data classes/Lazy classes Design smell - High coupling, from encapsulation being broken

How would you refactor to fix this?

  • Make things private
  • Delete the classes and represent titles as strings

Code and Design Smells

How do these code smells cause problems when developing code?

  • Reusability
  • Maintainability
  • Extensibility

How fast does it take for a new developer to understand what is happening? Is behaviour predictable, reasonable and uniform?

Is a code smell always emblematic of a design problem?

No

  • Switch statements & comments are often listed as code smells, but are aren't always 'a problem'

Exam Environment

Exam Tips

Exam Tips

  • Practice good exam techniques
  • Learn the patterns and the differences (YouTube has some really good resources if you don't understand certain ones).
  • Know code smells and methods of refactoring
    • refactoring.guru!
  • Please ensure that your code compiles. Even if you didn't finish the question, at least get it to a compliable state (Write all the placeholder code you need to, to achieve this)
  • Please ensure that you get familiar with VSCode.
  • Open VSCode in the correct folder (for syntax support)
  • Don't waste all your time on 10% of the exam
  • If you get particularly stuck on a question, come back to it later
  • You can use dot points if you're short on time or prefer to

Kahoot

COMP2511 Week 10 25T2

By Sam Zheng

COMP2511 Week 10 25T2

  • 108