Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997.

Slides:



Advertisements
Similar presentations
Coding Standard: General Rules 1.Always be consistent with existing code. 2.Adopt naming conventions consistent with selected framework. 3.Use the same.
Advertisements

1 Generalizations Multiple Inheritance (finishing up Class Design) Class Design – Another Look – Part 11.
Games and Simulations O-O Programming in Java The Walker School
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Object-Oriented Analysis and Design
Stéphane Ducasse«ChapterNr».1 Selected Design Patterns Design Patterns are recurrent solutions to design problems They are pros and cons We already saw:
Stéphane Ducasse«ChapterNr».1 Abstract Classes Should not be instantiated (abstract in Java) But can defined complete methods Defines a protocol common.
Stéphane Ducasse«ChapterNr».1 Arthur Riel Design Heuristics from Object-Oriented Design Heuristics by Arthur Riel Read the Heuristics… –Find reasons why.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
Some Basic Points on Classes
7. Best Practice Patterns. © Oscar Nierstrasz ST — Best Practice Patterns 7.2 Roadmap  Naming conventions  Delegation and Double Dispatch  Conversion.
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
Squeak Collections The Squeak collection hierarchy. Some collection operators. Working with collections. For-loops.
Chapter 10 Classes Continued
Modelling classes Drawing a Class Diagram. Class diagram First pick the classes –Choose relevant nouns, which have attributes and operations. Find the.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
UML Class Diagrams: Basic Concepts. Objects –The purpose of class modeling is to describe objects. –An object is a concept, abstraction or thing that.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
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.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
Patterns. Design comes from Modeling (requirements, analysis, problem) Mending (patch, refactoring) Memory (patterns, recall)
S.Ducasse Stéphane Ducasse 1 Smalltalk Coding Idioms.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Encapsulation and Data Abstraction Testing Applications in Smalltalk.
Refactoring Improving the structure of existing code Refactoring1.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
S.Ducasse Stéphane Ducasse 1 Design Heuristics.
Object-oriented Programming and Design 1 Polymorphism Poly => many Morph => shape Variables take on many shapes, or many classes of objects.
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
Refactoring1 Improving the structure of existing code.
Centralized vs. Decentralized Interpreter Pattern Visitor Pattern.
UML Class Diagram Trisha Cummings. What we will be covering What is a Class Diagram? Essential Elements of a UML Class Diagram UML Packages Logical Distribution.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
Structural Design Patterns
Object-oriented programming and design 1 Object Identity = vs. == copying objects Value Object ValueWithHistory.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Repetition af Domæne model. Artifact influence emphasizing the Domain Model.
Stéphane Ducasse 1 Singleton.
M1G Introduction to Programming 2 5. Completing the program.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
CS212: Object Oriented Analysis and Design Lecture 33: Class and Sequence Diagram.
Chapter 16 UML Class Diagrams 1CS6359 Fall 2012 John Cole.
Domain Model A representation of real-world conceptual classes in a problem domain. The core of object-oriented analysis They are NOT software objects.
Refactoring1 Improving the structure of existing code.
Stéphane Ducasse 1 Some Points on Classes.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Fusion Design Overview Object Interaction Graph Visibility Graph Class Descriptions Inheritance Graphs Fusion: Design The overall goal of Design is to.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Stéphane Ducasse 1 Abstract Classes.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
ITEC1301 Object-Oriented Systems Construction Lecture Notes #4 1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Principles and examples
UNIT-IV Designing Classes – Access Layer ‐ Object Storage ‐ Object Interoperability.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Design Points - Law of Demeter
UML Class Diagrams: Basic Concepts
Higher-Order Procedures
The Metacircular Evaluator
CSE 3302 Programming Languages
Presentation transcript:

Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse«ChapterNr».261 Coding Standards Standards –improve communication –let code be the design –make code more habitable –change

Stéphane Ducasse«ChapterNr».262 Coding Standards for Smalltalk Variables have no types Names can be any length Operations named with keywords Pretty printer

Stéphane Ducasse«ChapterNr».263 Names Names should mean something. Standard protocols –Object (printOn:, =) –Collection (do:, add:, at:put:, size) Standard naming conventions

Stéphane Ducasse«ChapterNr».264 Intention Revealing Selector Readability of message send is more important than readability of method. Name should specify what method does, not how.

Stéphane Ducasse«ChapterNr».265 Method Names If there is already a standard name, use it instead of following these rules. Three kinds of methods –change state of receiver –change state of argument –return value from receiver

Stéphane Ducasse«ChapterNr».266 Change state of receiver method name is verb phrase –translateBy: –add:

Stéphane Ducasse«ChapterNr».267 Change state of argument Verb phrase ending with preposition like on or to. –displayOn: –addTo:

Stéphane Ducasse«ChapterNr».268 Return value from receiver method name is noun phrase or adjective, a description rather than a command –translatedBy: –size –topLeft

Stéphane Ducasse«ChapterNr».269 Method Names Specialized names for specialized purposes. –Double-dispatching methods –Accessing methods –Query methods –Boolean property setting –Converter methods

Stéphane Ducasse«ChapterNr».270 Accessing Methods Many instance variables have accessing methods, methods for reading and writing them. Same name than the instance variables Accessing methods come in pairs. name, name: width, width: x, x:

Stéphane Ducasse«ChapterNr».271 When to use Accessing Methods Two opinions: –Always, including an object’s own instance variable lazy initialization, subclassing is easier –Only when you need to use it. better information hiding With the refactoring browser it is easy to transform the class using or not accessing

Stéphane Ducasse«ChapterNr».272 Query Method Methods that return a value often describe the type of the value because they are noun phrases. Query methods are not noun phrases, but are predicates. How can we make the return type clear? Provide a method that returns a Boolean in the “testing” protocol. Name it by prefacing the property name with a form of “be” or “has”- is, was, will, has

Stéphane Ducasse«ChapterNr».273 Query Method by Example Instead of: Switch>>makeOn status := #on Switch>>makeOff status := #off Switch>>status ^status Client>>update self switch status = #on ifTrue: [self light makeOn] self switch status = #off ifTrue: [self light makeOff] It is better to define Switch>>isOn, Switch>>isOff Switch>>on is not a good name... #on: or #isOn ?

Stéphane Ducasse«ChapterNr».274 Testing Method Prefix every testing method with "is". –isNil –isControlWanted –isEmpty –hasBorder

Stéphane Ducasse«ChapterNr».275 How do you set a boolean property? Switch>>on: aBoolean isOn := aBoolean Expose the representation of the status to the clients Responsibility of who turn off/on the switch: the client and not the object itself Create two methods beginning with “be”. One has the property name, the other the negation. Add “toggle” if the client doesn’t want to know about the current state beVisible/beInvisible/toggleVisible

Stéphane Ducasse«ChapterNr».276 Boolean Property Setting Don't make accessing methods whose only argument is a boolean. Create two methods beginning with "make". Add "toggle" if necessary. – makeVisible / makeInvisible / toggleVisible – makeDirty / makeClean

Stéphane Ducasse«ChapterNr».277 Converting Method Often you want to return the receiver in a new format. Prepend "as" to the name of the class of object returned. –asSet (in Collection) –asFloat (in Number) –asComposedText (in Text)

Stéphane Ducasse«ChapterNr».278 Complete Creation Method Class methods that create instances are in category "instance creation methods". –Creation followed by initialization is the most flexible. Point new x: 0; y: 0 –Important to preserve invariants and avoid ill- formed objects.

Stéphane Ducasse«ChapterNr».279 Complete Creation Method Instance creation methods should create well-formed instances. Pass all required parameters to them. Point x: 0 y: 0 SortedCollection sortBlock: aBlock Set new

Stéphane Ducasse«ChapterNr».280 Creation Parameter Method How should a Complete Creation Method initialize new object? –Separate setters are most flexible x: aNumber y: anotherNumber ^self new x: aNumber; y: anotherNumber

Stéphane Ducasse«ChapterNr».281 Creation Parameter Method Provide a single method that sets all the variables. Preface its name with "set", then the names of the variables. Forces the client to specify all arguments Place to check semantics constraints x: aNumber y: anotherNumber ^self new setX: aNumber y: anotherNumber

Stéphane Ducasse«ChapterNr».282 Composed Method How big should a method be? Write methods that perform one identifiable task. –Few lines per method. –Consistent level of abstraction. –Minimizes number of methods you have to change in subclass. –Minimizes code copying in subclass.

Stéphane Ducasse«ChapterNr».283 Composed Method Usage Top down self input; process; output Bottom up –common expressions –long loop bodies –comments –From client - two or more messages to another object is suspicious

Stéphane Ducasse«ChapterNr».284 Methods from Comments Comments indicate "identifiable task" If you need to comment a block of code, it probably should be a separate method. Turn method comment into method name.

Stéphane Ducasse«ChapterNr».285 Simple Superclass Name What should we call the root of a hierarchy? –Complex name conveys full meaning. –Simple name is easy to say, type, extend. –But need to show that subclasses are related.

Stéphane Ducasse«ChapterNr».286 Simple Superclass Name Give superclasses simple names: two or (preferably) one word –Number –Collection –VisualComponent

Stéphane Ducasse«ChapterNr».287 Qualified Subclass Name What should you call a subclass that plays a role similar to its superclass? –Unique name conveys most information –Derived name communicates relationship to superclass

Stéphane Ducasse«ChapterNr».288 Qualified Subclass Name Use names with obvious meaning. Otherwise, prepend an adjective to most important superclass. –OrderedCollection –UndefinedObject –CloneFigureCommand, CompositeCommand, ConnectionCommand

Stéphane Ducasse«ChapterNr».289 Variables: Roles vs. Types Types are specified by classes –aRectangle –aCollection –aView Roles - how an object is used –location –employees –topView

Stéphane Ducasse«ChapterNr».290 Role Suggesting Instance Variable What should you name an instance variable? –Type is important for understanding implementation. But class comment can describe type. –Role communicates intent, and this harder to understand than type.

Stéphane Ducasse«ChapterNr».291 Role Suggesting Instance Variable Name instance variables for the role they play. Make the name plural if the variable is a collection. –Point: x, y –Interval: start, stop, step –Polyline: vertices

Stéphane Ducasse«ChapterNr».292 Type Suggesting Parameter Name Name of variable can either communicate type or role. Keywords communicate their parameter's role, so name of variable should give new information.

Stéphane Ducasse«ChapterNr».293 Type Suggesting Parameter Name Name parameters according to their most general expected class, preceded by "a" or "an". If there is more than one parameter with the same expected class, precede the class with a descriptive word.

Stéphane Ducasse«ChapterNr».294 Temporaries Name temporaries after role they play. Use temporaries to: –collect intermediate results –reuse result of an expression –name result of an expression Methods are simpler when they don't use temporaries!