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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Classes & Objects Computer Science I Last updated 9/30/10.
Software Construction and Evolution - CSSE 375 Bad Smells in Code Shawn Bohner & Steve Chenoweth.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Imagine Cup Team Formation XNA Input January 14, 2010 Arnav Jhala Adapted from.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Code Organization UML Patterns Code Smells Feb Arnav Jhala.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Object-Oriented PHP (1)
1 CS1001 Lecture Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.
Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0 Key Abstractions in Game Maker Foundations of Interactive Game Design Prof. Jim Whitehead.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
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.
OBJECT ORIENTED ANALYSIS & DESIGN Vassilka Kirova Department of Computer & Information Science NJIT.
OBJECT ORIENTED ANALYSIS & DESIGN Vassilka Kirova Department of Computer & Information Science NJIT.
HST 952 Computing for Biomedical Scientists Lecture 2.
CS 330 Programming Languages 09 / 06 / 2007 Instructor: Michael Eckmann.
Exam Questions Chain of Responsibility & Singleton Patterns Game Design Experience Professor Jim Whitehead February 4, 2009 Creative Commons Attribution.
1 CS1001 Lecture Overview Object Oriented Design Object Oriented Design.
8/28/97Information Organization and Retrieval Files and Databases University of California, Berkeley School of Information Management and Systems SIMS.
Backgrounds, Inheritance in GameMaker (BrickMania 1 of 2) Foundations of Interactive Game Design Professor Jim Whitehead January 28, 2008 Creative Commons.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Object Oriented Programming
Python quick start guide
Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0 Key Abstractions in Game Maker Foundations of Interactive Game Design Prof. Jim Whitehead.
Programming Languages and Paradigms Object-Oriented Programming.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
11 1 Object oriented DB (not in book) Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Learning objectives: What.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Backgrounds, Inheritance in GameMaker (BrickMania 1 of 2) Foundations of Interactive Game Design Professor Jim Whitehead January 28, 2008 Creative Commons.
Object Oriented Programming (OOP) Design Lecture 1 : Course Overview Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang.
CS 330 Programming Languages 09 / 04 / 2008 Instructor: Michael Eckmann.
Object Oriented Programming (OOP) Design Lecture 1 : Course Overview Bong-Soo Sohn Associate Professor School of Computer Science and Engineering Chung-Ang.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
LECTURE 38: REFACTORING CSC 395 – Software Engineering.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computer Concepts 2014 Chapter 12 Computer Programming.
1 21 COP 3540 Data Structures with OOP Overview: Chapter 1.
Refactoring1 Improving the structure of existing code.
Chapter 12 Computer Programming. Chapter Contents Chapter 12: Computer Programming 2  Section A: Programming Basics  Section B: Procedural Programming.
1 Design and Integration: Part 2. 2 Plus Delta Feedback Reading and lecture repeat Ambiguous questions on quizzes Attendance quizzes Boring white lecture.
ITEC 3220A Using and Designing Database Systems Instructor: Prof Z. Yang Course Website: 3220a.htm
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
XP Tutorial 8 Adding Interactivity with ActionScript.
Learners Support Publications Object Oriented Programming.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Diagnosing Design Problems in Object Oriented Systems Adrian Trifu, Radu Marinescu Proceedings of the 12th IEEE Working Conference on Reverse Engineering.
Refactoring1 Improving the structure of existing code.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
Written by: Dr. JJ Shepherd
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.
Object-Oriented Design Concepts University of Sunderland.
Object-Oriented Software Engineering Practical Software Development using UML and Java Modelling with Classes.
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.
CSCE 240 – Intro to Software Engineering Lecture 3.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
To Get Started Paper sheet
Computer Programming.
Introduction to Data Structures
Code Smells 1.
Improving the structure of existing code
ITEC 3220A Using and Designing Database Systems
Refactoring.
Presentation transcript:

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

Announcements You are expected to be in a project team by now ► Please see me after class if you are not Craig Reynolds talk ► Crowds and Emergent Teamwork ► Today, 11am ► Engineering 2, room 180 (Plaza level, “Simularium”) ► You are welcome to attend

Upcoming Assignments Monday: Game Concept Document ► A compelling document that sells your game concept ► Title page Title of game, name of group, name of team members, sample artwork ► Overview page Table at top: game genre, platform (PC/XBox), team size Key points section –Bulleted list of important elements of gameplay –Goal of game, what makes game unique, main characters, main fictional elements –Sample artwork image to give feel of the game ► Biographies True, pocket biographies of each team member (1-2 paragraphs each) stressing experience that makes you a strong game designer ► 1-3 pages giving a textual description of the game Fictional background, brief description of characters, goal of player in game, how does player interact with the game, brief description of levels, game audience, other important elements as needed. ► 1-2 pages of sample conceptual artwork Hand-drawn sketches are fine See template and evaluation criteria on course website

Homework #1 Many of the submissions for homework #1 are ► Functionally correct ► Do not exhibit object-oriented design In many cases, the design is procedural ► Even though implemented in C#, and OO language We were looking for an object-oriented design Even the object-oriented designs can have their design improved.

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 have an object-oriented design, must improve its structure via code refactoring Will add this assignment to website and syllabus over the weekend Will give some in-class instruction and readings on code refactoring

Procedural programming paradigm Programs are composed of a set of procedures ► Also known as functions, methods, etc. ► Specify a sequence of statements Each procedure specifies a set of steps in the overall computation ► Few firm criteria for how to decompose a problem into procedures Focus is on determining the steps in the computation, and the sequence of those steps Some data is passed as parameters, much data is left as global variables ► Data, and the procedures that operate on them, may be in different text files

Object-oriented programming paradigm Programs are composed of objects An object is composed of ► Data ► Methods that act on that data An object is expected to correspond to a significant abstraction in the application It is possible to specialize objects, via inheritance ► E.g., square can inherit from the more generic shape Class/object distinction ► Specification of a class, vs. instances of a class

Ways Procedural Designs are Expressed in C# Pure procedural ► All methods are static, and found in the same class as main(). There is only one class. ► Class variables act as global variables. Pure procedural with data abstraction ► All methods are static, and found in the same class as main(). There are multiple classes, but other classes only have member variables, no methods (except a constructor). ► Object-orientation only used to cluster related global variables together Procedural, with minor object-orientation ► Design is strongly procedural, but classes have a small number of methods defined on them In the vast majority of cases (and until you have substantial experience as a software designer), you should avoid use of procedural design in object-oriented software. Demonstration of some of these from homework submissions

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

Homework Read Chapter 1 (Refactoring, a First Example) in Martin Fowler, Refactoring book ► Will post link on forum, and put link in syllabus on website Examine your Wumpus source code for code smells