Bad code smells Refactoring Example Game Design Experience Professor Jim Whitehead January 26, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.

Slides:



Advertisements
Similar presentations
Electronic Timesheet User Manual
Advertisements

CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
An Object-Oriented Approach to Programming Logic and Design
Lecture 1: Overview CMSC 201 Computer Science 1 (Prof. Chang version)
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Software Construction and Evolution - CSSE 375 Bad Smells in Code Shawn Bohner & Steve Chenoweth.
Decorator & Chain of Responsibility Patterns Game Design Experience Professor Jim Whitehead February 2, 2009 Creative Commons Attribution 3.0 (Except for.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Code Organization UML Patterns Code Smells Feb Arnav Jhala.
Procedural vs Object Oriented Design Bad code smells Game Design Experience Professor Jim Whitehead January 23, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Career Research, Pathways& Course Selection. What is EXPOED ? An internet based program for selecting courses based on career research and pathways.
Delegates & Events Observer and Strategy Patterns Game Design Experience Professor Jim Whitehead January 30, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
Inheritance, Method Overloading, Interfaces, Abstract Classes Game Design Experience Professor Jim Whitehead January 28, 2009 Creative Commons Attribution.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
HST 952 Computing for Biomedical Scientists Lecture 2.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Object-Oriented Analysis and Design Lecture 11 Maintenance (from Schach, “O-O and Classical Software Engineering”)
Exam Questions Chain of Responsibility & Singleton Patterns Game Design Experience Professor Jim Whitehead February 4, 2009 Creative Commons Attribution.
Chapter 4 Topics –Sampling –Hard data –Workflow analysis –Archival documents.
Object-Oriented Enterprise Application Development Course Introduction.
Section 1.3 Prime numbers and fractions
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Spring 2012 MATH 250: Calculus III. Course Topics Review: Parametric Equations and Polar Coordinates Vectors and Three-Dimensional Analytic Geometry.
Object Oriented Programming
How Do I Find a Job to Apply to?
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Recitation Week #1 Chem Queens College Spring 2010.
Testing. Definition From the dictionary- the means by which the presence, quality, or genuineness of anything is determined; a means of trial. For software.
Welcome to CS 3260 Dennis A. Fairclough. Overview Course Canvas Web Site Course Materials Lab Assignments Homework Grading Exams Withdrawing from Class.
Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.
1 Project Information and Acceptance Testing Integrating Your Code Final Code Submission Acceptance Testing Other Advice and Reminders.
BMTRY 789 Introduction to SAS Programming Lecturer: Annie N. Simpson, MSc.
LECTURE 38: REFACTORING CSC 395 – Software Engineering.
Western Oregon University Service Provider Handbook Notetaker’s Edition.
Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
Welcome – You’ve found CSE120 OR Computer Science Principles OR UWIT…  Announcements are usually listed here and displayed before the start of class 
Math Open Lab: A computer lab where Developmental Math students work on lab activities in the presence of Lab Instructors.
Refactoring Improving the structure of existing code Refactoring1.
Introduction to Data Structures
CSC Programming for Science Lecture 16: Debugging.
CS 350 – Software Design The Strategy Pattern – Chapter 9 Changes to software, like other things in life, often focus on the immediate concerns and ignore.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Class 1: What this course is about. Assignment Read: Chapter 1 Read: Chapter 1 Do: Chapter 1 ‘workbook’ pages not finished in class Do: Chapter 1 ‘workbook’
Refactoring1 Improving the structure of existing code.
Rev.04/2015© 2015 PLEASE NOTE: The Application Review Module (ARM) is a system that is designed as a shared service and is maintained by the Grants Centers.
1 Design and Integration: Part 2. 2 Plus Delta Feedback Reading and lecture repeat Ambiguous questions on quizzes Attendance quizzes Boring white lecture.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
AP Calculus BC The seat you pick is your assigned seat Mr. Goodrich’s Class
COMN Lecture 1a WELCOME!WELCOME! What We are Here to Do Become aware of the limitations of our common sense understanding of how interpersonal.
COMN Organizational Communication Lecture 1a
Website for our class. The information is updated at the end of each.
Welcome! Hello and welcome to English Composition 102, Introduction to Literature. I am looking forward to exploring, writing, and discussing literature.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
 Reading Quiz  Peer Critiques  Evaluating Peer Critiques.
Refactoring1 Improving the structure of existing code.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
1 Project 3 String Methods. Project 3: String Methods Write a program to do the following string manipulations: Prompt the user to enter a phrase and.
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
5.3. S ECTION R OUNDUP Exploration of project hand-in and section roundup.
CSCE 240 – Intro to Software Engineering Lecture 3.
Overview of MyAccountingLab [MAL] as a Homework Management Tool for AMIS 3300 Spring Semester 2016.
Reviewing Code A guide to smelling another developer’s source code.
Refactoring and Code Smells
Code Smells 1.
Improving the structure of existing code
Presentation transcript:

Bad code smells Refactoring Example Game Design Experience Professor Jim Whitehead January 26, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0

Announcements Game concept document due today ► Please let us know immediately if your partner has dropped the class ► Or, did not participate in the assignment (you couldn’t reach them, for example) Bob Mitchell talk ► CTO of Sony Online ► Today, 2pm, speaking to CS 80K ► Media Theater ► You are welcome to attend

Homework #1 Revisited Improve the design of your Hunt the Wumpus ► New class assignment, due February 9 ► If you had a procedural design, must make it object-oriented ► If you had gotos, you must remove them Except for use in case statements, and even here they are strongly discouraged ► If you have an object-oriented design, must improve its structure via code refactoring At least four refactorings to fix at least two different types of code smells Must mark up original code with location of bad code smells, and indicate type of refactoring ► See lecture notes from Friday on difference between procedural and object-oriented code Ask in section for help on assessing your own design

Homework #1 Revisited Submission instructions First HW#1 is Procedural (or non-OO) design ► Submit original code printout ► Submit printout of entire new program ► Submit all new code electronically First HW#1 is Object-Oriented design ► Submit original code printout marked up with location of bad code smells, and refactorings ► Submit printout of only changed code in new version ► Submit all new code electronically Details on web site

Goto Statement We were surprised to receive a number of homework #1 submissions containing goto statements ► Possibly due to access of BASIC source code for Wumpus ► Possibly due to C# having a goto construct that can be used for case statements Use of goto statement (outside of case) is unacceptable ► Core problem: with goto, control flow within a method does not even remotely follow the linear arrangement of statements on a page ► Leads to code that is substantially more difficult to understand, debug, and get correct ► Edsger Dijkstra's letter, Go To Statement Considered Harmful, published in the March 1968 Communications of the ACM (CACM) Major milestone in move towards structured programming ► In the year 2009, use of goto in professional settings leads to immediate (and perhaps irrevocable) loss of respect from peers, and casts tremendous doubt over your ability to program. Depending on the setting, could lead to immediate termination.

Bad Code Smells Once code has an object-oriented design, can then focus on improving its design ► If the design is procedural, can’t even begin to do this Refactoring literature has notion of “code smells” ► “If it stinks, change it” (M. Fowler, Refactoring) ► A characteristic of a design that is a strong indicator it has poor structure, and should be refactored ► Code smells are rules of thumb It’s not always straightforward that a bad smell must lead to a refactoring. Have to use judgement. Still, as new designers, bad code smells likely mean you should change your code.

Code Smells: Duplicated Code Duplicated code (code clones) ► The same, or very similar code, appears in many places ► Problem A bug fix in one code clone may not be propagated to all Makes code larger that it needs to be ► Example from homework Adjacency checks to print warnings (“I smell a Wumpus”, etc.) ► Fix: extract method refactoring Create new method that encapsulates duplicated code Replace code clones with method call

Code smells: Long Method Long method ► A method that has too many lines of code How long is too long? Depends. Over 20 is usually a bad sign. Under 10 lines is typically good. Still, no hard and fast rules. ► Problem The longer a method, the harder it is to understand, change, and reuse ► Example from homework Shooting logic, Main ► Fix: extract method Take chunks of code from inside long method, and make a new method Call new method inside the now-not-so-long method.

Code smells: Feature Envy Feature Envy ► A method in one class uses primarily data and methods from another class to perform its work Seems “envious” of the capabilities of the other class ► Problem: Indicates abstraction fault. Ideally want data, and actions on that data, to live in the same class. Feature Envy indicates the method was incorrectly placed in the wrong class ► Fix: Move method –Move the method with feature envy to the class containing the most frequently used methods and data items

Code smells: Large class Large class ► A class is trying to do too much ► Many instance variables ► Many methods Problem: ► Indicates abstraction fault There is likely more than one concern embedded in the code Or, some methods belong on other classes ► Associated with duplicated code Fix: ► Extract class refactoring Take a subset of the instance variables and methods and create a new class with them This makes the initial (long) class shorter ► Move method refactoring Move one or more methods to other classes Example from homework: ► Class containing Main() tends to have too much game logic

Code smells: switch statements Switch statements ► The cases in a switch statement contain logic for different types of instances of the same class ► In object-oriented code, this indicates new subclasses should be created Problem ► The same switch/case structure appears in many places Fix ► Create new subclasses ► Extract method to move case block logic into methods on the new subclasses

Code smells: Data class Data class ► A class that has only class variables, getter/setter methods/properties, and nothing else ► Is just acting as a data holder Problem ► Typically, other classes have methods with feature envy ► That is, there are usually other methods that primarily manipulate data in the data class ► Indicates these methods should really be on the data class ► Can indicate the design is really procedural Fix ► Examine methods that use data in the data class, and use move method refactoring to shift methods

Refactoring Example Walk through refactoring example from Chapter 3 of Martin Fowler, Refactoring.

Homework Read Chapter 3 of Refactoring, by Martin Fowler ► Username: cs116 ► Password: cs116 ► Same for all chapters