Bad code smells Refactoring Example

Similar documents
CSC408H Lecture Notes

CMSC Fundamentals of Computer Programming II (C++)

Agile Software Development

KS3 Computing Group 1 Programme of Study hours per week

Waterfall to Agile. DFI Case Study By Nick Van, PMP

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Quick Start Guide. 1. What is Piazza...pg What makes Piazza different...pg 1

Chapter 24 - Quality Management. Lecture 1. Chapter 24 Quality management

Chapter 9 Software Evolution

Code Kingdoms Learning a Language

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Ten Strategies to Encourage Academic Integrity in Large Lecture Classes

Java: Learning to Program with Robots. Chapter 11: Building Quality Software

So we thought we knew money

CSCI 1100 Computer Science 1 Homework 1 Calculations and Functions

User Starter Guide. Webtop Set up

Semantic Errors in SQL Queries: A Quite Complete List

CS 1340 Sec. A Time: 8:00AM, Location: Nevins Instructor: Dr. R. Paul Mihail, 2119 Nevins Hall, rpmihail@valdosta.

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

Agile Software Development

What Does Large Mean? Copyright 2003 by N. Josuttis and J. Eckstein 3. Why is Large an Issue?

SYLLABUS CCE CONSTRUCTION PLANNING & SCHEDULING FALL 2012

Foundations of Programming

Syllabus for CS 134 Java Programming

CSE 4415 / SWE 5415 Software Testing 2 Fall 2004 Olin Engineering Building, Room 128 Credits: 3.00

Agile Software Development in the Large

Review from last time. CS 537 Lecture 3 OS Structure. OS structure. What you should learn from this lecture

Game Programming & Game Design

Adopting Agile Testing

Software Engineering. So(ware Evolu1on

Computer Science CS 2334: Programming structures and abstractions

6.S096 Lecture 1 Introduction to C

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

New York City College of Technology Computer Systems Technology Department

Button, Button, Whose got the Button?

Software Service Engineering Architect s Dream or Developer s Nightmare?

Contents. 1 Introduction. CS 15 Collaboration Policy Fall Introduction Motivation Enforcement... 2

How To Write A Design Document For Anorexic Css

Test Driven Development Part III: Continuous Integration Venkat Subramaniam

New York University Stern School of Business Undergraduate College

What Is the Olweus Bullying Prevention Program?

Managing Student Portfolios

Building Strong Families

Software Engineering (CS550)

Easy Casino Profits. Congratulations!!

Five Tips for Presenting Data Analyses: Telling a Good Story with Data

Sample Syllabus (C++) CSCI 1301 Introduction to Programming Principles

How To Design Your Code In Php (Php)

CS 1301 Homework 6 Scribbler Line Sensing Due: Friday March 13th, before 11:55pm PM EST. Out of 130 points Files to submit:

Xtreme RUP. Ne t BJECTIVES. Lightening Up the Rational Unified Process. 2/9/2001 Copyright 2001 Net Objectives 1. Agenda

Department of Computer & Information Sciences. INFO-450: Information Systems Security Syllabus

The Rules 1. One level of indentation per method 2. Don t use the ELSE keyword 3. Wrap all primitives and Strings

Test-Driven Development

Parsons The New School for Design Communication Design. Interaction: Core Lab PUCD 2126 A / CRN: 6125 Fall 2015

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Corporate Valuation and Financial Strategy. Writing a Case Study

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

User experience storyboards: Building better UIs with RUP, UML, and use cases

Automated Inventory System

Corporate and Brand Identity on the Web: VIC5315 University of Florida Summer 2013

Chapter 1 Programming by Intention

Why Your Business Needs a Website: Ten Reasons. Contact Us: Info@intensiveonlinemarketers.com

5 Reasons Learning and Adoption Programs Fail And What To Do About Them

OO Design Quality Metrics

UML for the C programming language.

Computer and Network Security

Development Techniques. CSE301 University of Sunderland Harry R. Erwin, PhD

Welcome to Introduction to Computers and Programming Course using Python

AGILE SOFTWARE DEVELOPMENT. BY Sysop Technology Aurangabad

Special Report: 5 Mistakes Homeowners Make When Selling A House. And The Simple Tricks To Avoid Them!

Art of Code Front-end Web Development Training Program

ITS New Staff Induction 2014

Role of Functional Clones in Software Development

Deep Agile Blending Scrum and Extreme Programming. Jeff Sutherland Ron Jeffries

So You d Like a Sport Psychology Consultant to Work With Your Team? Three Key Lessons Learned from Olympic Teams

UNIVERSITY OF MICHIGAN SCHOOL OF INFORMATION SI301: Models of Social Information Processing Syllabus

A First Set of Design Patterns for Algorithm Animation

THE FLATMATES Language point: Adverbs of manner

Agile QA s Revolutionary Impact on Project Management

Narrative in Games. Foundations of Interactive Game Design Professor Jim Whitehead February 26, 2007

Software Configuration Management

Quotes from Object-Oriented Software Construction

xxx Lesson Comprehend the writing process 2. Respond positively to the writing process

UCC Writing Survey of Students

20-30 minutes, can be used within a longer activity

WHY DO WE GET ANGRY? EVERYONE FEELS ANGRY SOMETIMES

How to pick ediscovery software

Testing Rails. by Josh Steiner. thoughtbot

How to Have a Pee-Free Home

Neil Murray University of South Australia April 2011

Transcription:

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 Fix: Or, some methods belong on other classes Associated with duplicated code 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 Fix The same switch/case structure appears in many places 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 Fix 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 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