Patterns for naming - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Patterns for naming

Description:

Patterns for naming. 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. ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 26
Provided by: ralphj
Category:
Tags: meaning | names | naming | of | patterns

less

Transcript and Presenter's Notes

Title: Patterns for naming


1
Patterns for naming
2
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.

3
Simple Superclass Name
  • Give superclasses simple names two or
    (preferably) one word
  • - Number
  • - Collection
  • - VisualComponent

4
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

5
Qualified Subclass Name
  • Use names with obvious meaning. Otherwise,
    prepend an adjective to most important
    superclass.
  • - OrderedCollection
  • - UndefinedObject
  • - CloneFigureCommand, CompositeCommand,
    ConnectionCommand

6
Variables Roles vs. Types
  • Types are specified by classes
  • aRectangle
  • aCollection
  • aView
  • Roles - how an object is used
  • location
  • employees
  • topView

7
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.

8
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

9
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.

10
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.

11
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!

12
Class Comments
  • Every class should have a comment that describes
    the purpose of the class and the types of its
    instance variables

13
Method comments
  • Name of method should describe what it does.
  • Name of arguments should describe what they are.
  • Name of method should describe return type.
  • Method comment should describe anything else that
    isnt obvious.

14
Common State
  • Different instances of a class have different
    values for some state. How do you represent it?
  • Declare an instance variable in the class.
  • Name the variable using Role Suggesting Instance
    Variable Name. Initialize it with either Lazy
    Initialization, Explicit Initialization, or a
    Constructor Parameter Method.

15
Explicit Initialization
  • How do you initialize instance variables to their
    default value?
  • Make the code readable instead of changeable.
  • Implement a method initialize that sets all the
    values. Override the class method new to
    invoke it on new instances.

16
Lazy Initialization
  • How do you initialize instance variables to their
    default value?
  • Emphasize ease of change.
  • Force all accesses to variable to go through
    accessing method. Make Getting method check
    whether the variable has been initialized and
    initialize it if necessary.

17
Lazy Initialization
  • Timercount
  • count isNil
  • ifTrue count self defaultCount
  • count

18
Default Value Method
  • How do you represent the default value of a
    variable?
  • Create a method that returns the value. Prepend
    default to the name of the variable as the name
    of the method.

19
Constant Method
  • How do you code a constant?
  • Create a method that returns the constant.

20
  • How do you get and set an instance variables
    value?
  • Direct Variable Access Access and set the
    variable directly.
  • Indirect Variable Access Use a Getting Method
    and a Setting Method

21
Collection Accessor Method
  • How do you provide access to an instance variable
    that holds a collection?
  • Provide methods that delegate to the collection.
    To name the methods, add the name of the
    collection to the collection name.
  • addEmployee
  • includesEmployee

22
Enumeration Method
  • How do you provide access to collection elements?
  • Implement a method that executes a Block for each
    element of the collection. Name the method by
    concatenating the name of the collection and
    Do
  • employeesDo

23
Refactoring
  • You dont always get your code right the first
    time.
  • No - a first draft is never as good as a polished
    revision
  • Bad names
  • Long methods
  • Direct access instead of indirect access

24
Refactoring
  • Why not just fix it?
  • Causes errors
  • Takes time
  • If it isnt broken, dont fix it!
  • Knowing how to refactor makes it faster and less
    error-prone
  • Tools also help refactoring

25
Refactoring
  • Change in very small steps
  • A refactoring shouldnt change behavior
  • Run tests after each step.
  • Write tests in SUnit and use TestRunner to run
    the tests.
  • Write tests before refactoring!
Write a Comment
User Comments (0)
About PowerShow.com