OO Overkill When Simple is Better than Not

Similar documents
OO Overkill When Simple is Better than Not

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

Binary Trees

Tree Structures. A hierarchical data structure whose point of entry is the root node

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

The Problem with Linked Lists. Topic 18. Attendance Question 1. Binary Search Trees. -Monty Python and The Holy Grail

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

TREES Lecture 12 CS2110 Fall 2016

Principles of Computer Science

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

A Case Study of Gang of Four (GoF) Patterns : Part 7

1B1b Implementing Data Structures Lists, Hash Tables and Trees

INF2220: algorithms and data structures Series 1

Programming II (CS300)

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

Review sheet for Final Exam (List of objectives for this course)

CO Java SE 8: Fundamentals

CS 206 Introduction to Computer Science II

Data Structures and Algorithms

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Concepts of Programming Languages

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Trees. CSE 373 Data Structures

CompuScholar, Inc. 9th - 12th grades

STUDENT LESSON AB30 Binary Search Trees

TREES. Trees - Introduction

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

TREES Lecture 10 CS2110 Spring2014

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

TeenCoder : Java Programming (ISBN )

Binary Search Trees 1

Red-Black trees are usually described as obeying the following rules :

Finish Lec12 TREES, PART 2. Announcements. JavaHyperText topics. Trees, re-implemented. Iterate through data structure 3/7/19

Comp Intermediate Programming EXAM #1 February 16, 2004 Rice University - Instructors: Cox & Nguyen

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

INCORPORATING ADVANCED PROGRAMMING TECHNIQUES IN THE COMPUTER INFORMATION SYSTEMS CURRICULUM

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

B-Trees. Based on materials by D. Frey and T. Anastasio

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

We have the pointers reference the next node in an inorder traversal; called threads

» Access elements of a container sequentially without exposing the underlying representation

Comp Intermediate Programming EXAM #2 March 30, 2005 Rice University - Instructors: Cox & Nguyen

CSE 12 Abstract Syntax Trees

ITEC2620 Introduction to Data Structures

Inheritance and Interfaces

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

CS171 Final Practice Exam

tree nonlinear Examples

INHERITANCE AND TYPE HIERARCHIES. Prof. Chris Jermaine

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

About this exam review

CSC 467 Lecture 13-14: Semantic Analysis

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Summer Final Exam Review Session August 5, 2009

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

CMSC 341 Lecture 10 Binary Search Trees

Topic 14. The BinaryTree ADT

Comp Intermediate Programming EXAM #2 April 02, 2003 Rice University - Instructors: Cox & Nguyen

CS24 Week 8 Lecture 1

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Course materials Reges, Stuart, and Stepp, Martin. Building Java Programs: A Back to Basics Approach. 2d ed. (Boston: Addison-Wesley, 2011).

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Topic 3 Encapsulation - Implementing Classes

Trees. (Trees) Data Structures and Programming Spring / 28

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

Programming Languages 2nd edition Tucker and Noonan"

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

CS 151. Binary Trees. Friday, October 5, 12

CMPT 225. Binary Search Trees

IMACS: AP Computer Science A

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

CSC 421: Algorithm Design & Analysis. Spring 2015

CS 206 Introduction to Computer Science II

Compsci 100. Owen Astrachan April 14, 2010

Garbage Collection: recycling unused memory

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Goals of design. satisfies problem requirements, usable error free, resistant to incorrect input. readable, not purposefully cryptic.

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

A Java Execution Simulator

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees

Tree traversals and binary trees

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas

CS 127 Fall In our last episode. Yet more fun with binary trees. Tree traversals. Breadth-first traversal

CISC370: Inheritance

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

CSE373 Fall 2013, Midterm Examination October 18, 2013

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

Binary Trees, Binary Search Trees

Chapter 20: Binary Trees

Transcription:

OO Overkill When Simple is Better than Not Owen Astrachan ola@cs.duke.edu http://www.cs.duke.edu/~ola NSF Career 9702550 FYI: Objects and Patterns 1

Fundamental Laws First do no harm Hippocratic Oath A robot may not injure a human being, or, through inaction, allowahumanbeingtocometoharm First law of Robots (Asimov) You know you have achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away. Saint-Exupery Do the simplest thing that could possibly work First Design Principle of Extreme Programming FYI: Objects and Patterns 2

Where are we going? (when do we get there?) Object oriented programming is here to stay (for N years) What aspects of OOP and OO Design belong in FYI? First Year Instruction must be about trade-offs CS2 especially should be about algorithmic and design tradeoffs How do we (should we) incorporate design patterns in FYI? Solving problems is important, patterns are intended to solve problems We shouldn t teach patterns, we should teach when they re applicable (well, we should teach them too) What s the right programming and design methodology in FYI? It s not PSP, it s XP Teaching and curriculum design can reflect XP too Be ready for change, hard to get it right from the start FYI: Objects and Patterns 3

Tension in Teaching OO Concepts Left to their own devices and designs, students cannot write correct and well-designed programs Solution: Frameworks, Apprentice-Learning, add to existing code, implement a design-provided Solution: Good design comes from experience, experience comes from bad design Students relish creating programs from scratch Is it ok to use an API from JDK 1.x, from the book, from the course, from the assignment? There s no time to create interesting programs from scratch OO design patterns and skills don t necessarily scale down FYI: Objects and Patterns 4

Relevant Tenets of Extreme Programming What parts of embracing change can we embrace in FYI? Evolutionary design, small releases, iterative enhancement Simple design, don t build for the future (will you need it?) Lots of testing, testing, testing Refactoring: change design, not functionality What may be hard to embrace in FYI? Code the test first Pair Programming Business aspects: meetings, customers, Links http://www.xprogramming.com/what_is_xp.htm http://www.extremeprogramming.org/rules.html http://c2.com/cgi/wiki?extremeprogrammingroadmap http://www.martinfowler.com/articles/designdead.html FYI: Objects and Patterns 5

Twenty-Questions meets binary trees Build a game tree, ask questions, add to knowledge, play again (later) Preliminary to RSG Nifty Assignment program Procedural version used, e.g., in book by Main and Savitch Used as an example of how to do it better in 1998 patterns paper FYI: Objects and Patterns 6

Goals of Twenty Questions assignment? Familiarity with trees Reading, writing, traversing, changing structure Preorder (read/write), postorder (cleanup) Reinforce concepts with coding practice Interesting and (somewhat) intriguing assignment Satisfaction higher when no guidance given Student satisfaction not always a valid metric, but satisfaction impacts understanding and internalizing Student constructed games shareable with classmates Provides context/hook for later refactoring Revisit this in OO design course; CS2 introduces ideas and coding framework for later courses FYI: Objects and Patterns 7

Twenty Questions: the good, bad, and ugly Factory classes Singleton? Do it right Do it simply Adding knowledge: Internal->Leaf Accessor Friend State Pattern Internalize patterns and design? Not used in subsequent programs Acknowledged in later classes as connect the dots programming FYI: Objects and Patterns 8

Current version of Twenty Questions Provide code that plays one game by reading the file No tree explicitly constructed, cannot add knowledge Recursive nature of file reading mirrors tree construction Provide no other classes/guidance: course covers trees Use plain-old-data, public data/struct approach to tree Code from book and in class constructs trees, prints them, copies, finds height, all using plain-old-data approach Revisit program in later course (ideally same course, but ) Discuss factory, inheritance hierarchy, other OO concepts Show how before/after approach and refactoring leads to more extendable program, but why do that first? FYI: Objects and Patterns 9

Trees: As simple as possible or too simple? Forces: introduce trees in a sequence of courses that rely on OO/procedural languages (OO + CS2 =???) NO: a course that follows a functional programming course YES: to be followed by OO/software design course Plain-old-data approach mirrors often-used source: this doesn t extend to abstract syntax trees, but it s simple, understandable What about do it right from the start? Is an inheritance hierarchy for two node types right? Is visitor warranted here? Distributed computing/control is hard Two cases for recursion in one function vs in two classes No study on this, but intuition and experience say harder FYI: Objects and Patterns 10

Trees: the old and new approach public class TreeFunctions { public static int count(plainnode root) { if (root == null) return 0; return 1 + count(root.myleft) + count(root.myright); public class Printer { public static void inorder(plainnode root) { if (root == null) return; inorder(root.myleft); System.out.println(root.myInfo); inorder(root.myright); FYI: Objects and Patterns 11 E I PlainNode myinfo myleft myright +PlainNode +tostring 2 L M P R S

java.util.treemap, understanding the source static class Entry { Object key; Object value; Entry left = null; Entry right = null; Entry parent; boolean color = BLACK; public boolean containsvalue(object value) { return (value==null? valuesearchnull(root) : valuesearchnonnull(root, value)); private boolean valuesearchnonnull(entry n, Object value) { if (value.equals(n.value)) return true; return (n.left!= null && valuesearchnonnull(n.left,value)) (n.right!= null && valuesearchnonnull(n.right,value)); FYI: Objects and Patterns 12

Trees + Null-object + visitor = CS2 OO Overkill A Node is either Empty or Internal Leaf (not shown here, but is in code online/handout) Empty Node Singleton Response to getleft() Exception No-op Visitor Encapsulate new operations over structures Structure built from static set of types (e.g., nodes) FYI: Objects and Patterns 13

Trees + Null-object + visitor = CS2 OO Overkill Visitor simulates doubledispatch Polymorphism on operation and element Distributed recursion Control not centralized Demonstrably difficult for students Writing copy/clone is trivial with plain-old data, harder with visitor Is difficulty relevant? Why do we study trees? FYI: Objects and Patterns 14

OO Overkill in a CS2 course public class InternalNode extends Node { public Object accept(visitor v, Object o) { v.visitinternal(this,o); public class SizeVisitor extends Visitor { public Object visitinternal(internalnode node, Object o) { Integer lcount = (Integer) node.getleft().accept(this,o); Integer rcount = (Integer) node.getright().accept(this,o); return new Integer(1 + lcount.intvalue() + rcount.intvalue()); public Object visitempty(emptynode node, Object o){ return ourzero; private static Integer ourzero = new Integer(0); System.out.println("# nodes = " + root.accept(sizevisitor.getinstance(),null)); FYI: Objects and Patterns 15

Hangman: two case studies of OO in FYI Contrasting goals and methodologies of Hangman FYI: Objects and Patterns 16

FYI: Objects and Patterns 17

FYI: Objects and Patterns 18