jgs SER 516 Software Agility Lecture 14: Clean Design II Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
jgs 0516 0000 1110 Agenda § Sample Exam is available. Test your system ASAP § Read the Online Proctored Exam Guidelines to know what to do in case of a problem during the exam § Midterm Exam available March 11 only during lecture time from 4:30 pm to 5:45 pm. § No lecture that day. Go directly to your exam § It is a CLOSED BOOK exam. Do not use any material § Scratch paper is allowed. Empty White Paper. Show it on camera when you show your environment!
jgs 0516 0000 1110 1. Write tests § Writing test cases drive to Single Responsibility (SRP), dependency injection (DI), and interfaces. § Writing test cases minimize coupling because tight coupling makes it difficult to write tests. § Refactoring: increase cohesion, decrease coupling, separate concerns, modularize, shrink functions and classes, choose better names, and so on. § Having tests eliminates the fear that cleaning up the code will break it.
jgs 0516 0000 1110 2. Eliminate duplication § Duplication (LOC or methods) represents additional work, risk, and unnecessary complexity. § Use Inheritance à overloading § Common mistake: get our code working and then move on to the next problem. Take care of what you have created!
jgs 0516 0000 1110 3. Ensure Expressiveness § The majority of the cost is in long-term maintenance. § It is critical to understand what the system does. § Select good names, keep items small, use patterns (observer, visitor, command, interpreter, mediator, decorator, adapter, singleton, factory, etc.). § Tests should also be expressive.
jgs 0516 0000 1110 4. Minimize the number of items § Do not end with a program that have too many tiny classes and methods. Keep classes and methods counts low. § Dogmatism vs Pragmatism. § This rule is lowest priority compared with the other principles and rules. But it is a rule. Do not ignore it.
jgs 0516 0000 1110 Assignment Grading Clean Code, Clean Design, etc. Is it Readable (guidelines, patterns, …)? Is it Understandable (short, expressiveness, …)? § Ask yourselves: Are the graders going to be able to understand this?
jgs 0516 0000 1110 2. Use Patterns § Observer (behavior) § Delegate (behavior) § Factory (creation) § Singleton (centralize) § MVC (Model-View-Controller). No, it is not about having classes named, View, Controller, and Model
jgs 0516 0000 1110 2.1. Observer § Maybe, you have a Console, a PlotPanel, a FacePanel, and all of them need access to the data collected from the server. § Queries vs Notifications § What about this?
jgs 0516 0000 1110 2.2 Delegation public interface Behavior { public void makeSomething(); } } public class B1 implements Behavior { public void makeSomething() { // ... } } public class B2 implements Behavior { public void makeSomething() { // ... } } public class User { private Behavior b; public void action() { b.makeSomething(); } public User(Behavior b) { this.b = b; } }
jgs 0516 0000 1110 Dependency Injection (DI) § A Java class has a dependency on another class, if it uses an instance of this class. § A class should not configure its dependencies statically (new) but should be configured from the outside (using vs having). § hard dependency – a class creates an instance of another class via the new operator § dependency injection – passing of a dependency to a dependent object (as a parameter)
jgs 0516 0000 1110 Dependency Injection (DI) public class ClientFrame { ... public ClientFrame() { ... = new PlotPanel(); ... = new FacePanel(); ... = new ConsolePanel(); ... = new JButton(); ... = new JLabel(); } } public class ClientFrame { public ClientFrame(Jpanel a, Jpanel b) { } }
jgs 0516 0000 1110 Definition § Low coupling. Coupling refers to the degree to which the different classes depend on each other. All classes should be independent as far as possible. § Create Mock objects (dummy implementations that emulate real code). § Create Interfaces
jgs SER 516 Software Agility Javier Gonzalez-Sanchez [email protected] Spring 2021 Disclaimer. These slides can only be used as study material for the class SER516 at ASU. They cannot be distributed or used for another purpose.