antipatterns - lpu guide · antipatterns negative solutions that present more problems than they...

65
AntiPatterns

Upload: others

Post on 11-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

AntiPatterns

Page 2: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

AntiPatterns

Negative solutions that present more problems than they

address

Natural extension to design patterns

Bridge the gap between architectural concepts and real-world

implementations

Provide the knowledge to prevent or recover from them

2004/05/21 Patterns and AntiPatterns 2

Page 3: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

AntiPattern Research

“The presence of ‘good’ patterns in a successful system is not enough; you

also must show that those patterns are absent in unsuccessful systems.

Likewise, it is useful to show the presence of certain patterns(AntiPatterns) in

unsuccessful systems, and their absence in successful systems.

- James O. Coplien

2004/05/21 Patterns and AntiPatterns 3

Page 4: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Anti Pattern Research

Principal viewpoints

Development Anti Patterns

Technical problems and solutions encountered by programmers

Architectural Anti Patterns

Identify and resolve common problems in how systems are structured.

Managerial Anti Patterns

Address common problems in software processes and development

organizations.

2004/05/21 Patterns and AntiPatterns 4

Page 5: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

AntiPattern Research

2004/05/21 Patterns and AntiPatterns 5

Page 6: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Design Pattern and AntiPattern

2004/05/21 Patterns and AntiPatterns 6

Page 7: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Software Refactoring

A form of code modification, used to improve the software

structure in such a way that it does not alter the external

behavior of the code and long-term maintenance.

AntiPatterns

Define a migration (or refactoring) from negative solutions to positive

solutions.

Not only do they point out trouble, but they also tell you how to

get out it.

2004/05/21 Patterns and AntiPatterns 7

Page 8: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

The Blob

General Form

One class monopolizes the processing and other classes primarily

encapsulate data.

Key Problem

The majority of the responsibility are allocated to a single class.

Procedural-style rather than object-oriented architectures

2004/05/21 Patterns and AntiPatterns 8

Page 9: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

2004/05/21 Patterns and AntiPatterns 9

Page 10: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

2004/05/21 Patterns and AntiPatterns 10

Page 11: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

2004/05/21 Patterns and AntiPatterns 11

Page 12: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

2004/05/21 Patterns and AntiPatterns 12

Page 13: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

2004/05/21 Patterns and AntiPatterns 13

Page 14: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

2004/05/21 Patterns and AntiPatterns 14

Page 15: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Summary

AntiPatterns

not against using patterns.

a more compelling form of patterns.

Each AnitPattern includes a solution + solution pair.

AntiPattern Solution Generates mostly negative consequences.

Refactored Solution Generates mostly positive benefits.

2004/05/21 Patterns and AntiPatterns 15

Page 16: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Anti Patterns and Refactoring

Page 17: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Some Pattern Related Concepts

Pattern: good ideas

AntiPatterns: bad ideas

Refactoring: better ideas

Page 18: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Software Development

Anti-Patterns

Control Freak

Old Baggage

Functional Fixation

Gadget Clutter

Golden Hammer

Clipboard Coding

Copyright W. Howden 18

Page 19: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Control Freak

Description

large object with all the control, which accesses simple data classes

Blob, God Class

essentially a procedural design

Consequences

difficult to modify functionality

not based on domain model

difficult to test and reuse

Copyright W. Howden 19

Page 20: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Old Baggage

Description

system contains many classes whose purpose is not known

Lava Flow OR Dead Code

much of the code is left over from previous ideas and no longer has

a purpose

was once fluid and useful, now is solid lava that you are afraid to remove

Consequences

difficult to maintain, just gets worse

Copyright W. Howden 20

Page 21: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Functional Fixation

Description

main top level routine

main calls other routines that have been made into objects/classes

top down development

Functional Decomposition

Consequences

class models make no sense

no O/O benefits such as inheritance and polymorphism

Copyright W. Howden 21

Page 22: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Gadget Clutter

Description

transient, ghost-like (no state) classes whose objects are created to

perform some temporary responsibility

popups

unnecessary abstractions that clutter the design

Consequences

clutter up the design

non-object oriented classes with names like “start up counter”,

“initialize data base”

Copyright W. Howden 22

Page 23: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Golden Hammer

Description

a particular technology or product is used for everything because

a development team has gained proficiency in it.

Consequences

may leave out awkward but necessary features

system architecture fits the preordained solution rather than the

problem domain

Copyright W. Howden 23

Page 24: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Spaghetti (tangled code)

Description

well known bad detailed design feature for functional code – rats nest of control flows

O/O: objects are basically functions and there is little interaction. Single process control weaving its way in and out of objects

Consequences

difficult to maintain and understand, lack of code re-use opportunities, does not have an underlying domain model

Copyright W. Howden 24

Page 25: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Clipboard Coding

Description

re-use of code within a system

re-use of code from other systems

generally a good idea but can be over-used, and without due caution

Consequences

bug duplication

incomplete required modification of re-used code

failure to identify and create re-usable classes, etc.

Copyright W. Howden 25

Page 26: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Thank You

Page 27: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Software Architecture

Anti-Patterns

Coincidental Architecture

Cover Your Assets

Bondage and Submission

Assumed Architecture

Platypus

I Did it My Way

Copyright W. Howden 27

Page 28: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Coincidental Architecture

Terminology and variations

combining things in a way that is driven by their properties rather

than by a comprehensive idea for an architecture

needs constant repairs, patches and adhoc pieces

Stovepipe

Possible contexts leading to this:

Migration: moving to distributed system

System: subsystems integration

Enterprise: multiple non-matching, layered systems

Copyright W. Howden 28

Page 29: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Coincidental Architecture

and Integration

Description

lack of common subsystem abstractions

hodgepodge of pairs of subsystem dependencies; complete graph

pair-wise integration-driven implementation (as opposed to documented) architecture

Legacy, Uncle Sam special, Ad Hoc integration

Consequences

very difficult to understand and maintain

Copyright W. Howden 29

Page 30: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Coincidental Architecture and

Enterprise Systems

Description

Enterprise: multiple layered systems

Independently designed subsystems at all levels

lack of standard reference model across lower layers

Islands of automation

Consequences:

brittle, picky integration of subsystems

Architecture driven by interfacing heterogeneous system standards

and infrastructure

Copyright W. Howden 30

Page 31: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Enterprise Coincidental Illustration

Enterprise System – Version 1 Enterprise System- Version 2

Sub A Sub B Sub C Sub A Sub B Sub C

Application

specific

services

Value added

functional

services

Basic services

inrfastructure

Standards and

conventions

Copyright W. Howden 31

Page 32: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Cover Your Assets

Description

avoid making decisions

list all the alternatives

paralysis by analysis

course of action not clear

Consequences

not clear what the present and future architecture will be

documents will not be used

Copyright W. Howden 32

Page 33: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Bondage and Submission

Descriptions

Architecture based on a product line such as Visual Studio

Vendor lock-in

Consequences

lack of control over future product upgrades

mixed product versions

commercial product updates drive maintenance

Copyright W. Howden 33

Page 34: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Bondage and Submission - Avoidance

Isolation

isolation layer

isolation wrapper for preserving standard interface

Copyright W. Howden 34

Page 35: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Assumed Architecture

Description

no specified architecture

wherefore art thou architecture?

usually similar to experience on previous systems

turns out to be too big/complex to do informally

Consequences

hidden risks, misunderstood requirements, problems with backup

and contingency plans

Copyright W. Howden 35

Page 36: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Platypus

Description

designed by a committee

design by committee, political party, standards disease

everyone had to be accommodated

poorly run meetings

Consequences

overly verbose, incoherent, no prioritization

Copyright W. Howden 36

Page 37: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

I Did it My Way

Description

Lack of design re-use, i.e. not the same as not re-using the wheel

re-inventing the wheel, design in a vacuum

Failure to carry out domain analysis to find essential features

Lack of legacy design documents

Consequences

replication of commercial software

Immature and unstable architectures

Copyright W. Howden 37

Page 38: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Refactoring

Periodically need to clean up the design and the code

Refactoring goals

improve cohesion, reduce coupling

Copyright W. Howden 38

Page 39: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Refactoring Processes

Strategic

general design

Tactical

detailed design

Transformations

“mechanical” steps

Copyright W. Howden 39

Page 40: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Strategic and Tactical Refactoring

Examples

Strategic

Convert Procedural Design to Objects

Separate Domain Layer From Presentation

Tactical

Tease Apart Inheritance

Extract Hierarchy

Copyright W. Howden 40

Page 41: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Strategic Refactoring: Convert

Procedural Design to Objects

Functional/Procedural Design data is passed from one function to the next, which operates on it and

returns data

Approach convert data entities into objects

decompose behavior from functions

move behavior to objects

Copyright W. Howden 41

Page 42: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Structured Design

Module Notation

Boxes are functional modules

Edges are data flow

small arrows with circles are called “data couples”

hollow: computational data, used in expressions to compute a result

solid: control data (e.g. flags, etc. that tell receiving module which subcases

to compute)

Page 43: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS Procedural Example

Copyright W. Howden 43

Page 44: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS Procedural Data (Top Down)

GetDate and SetData requests

Add and Delete member requests

Name and Prefs for get date requests

Name, Props for setting data

MemberRecord

File Manager control input data

Add,Delete,First,Next request flags

Copyright W. Howden 44

Page 45: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Moving Behavior to Objects

Requests

e.g. (GetDate + Name +PrefsProps) data becomes an object.

Animate object with execute() method that finds dates

Data Base/File Manager data

e.g. Member Record and Operation selection flags (add, delete, first,

next) become DataBase with associated methods

Copyright W. Howden 45

Page 46: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Introduce Architecture

Identify general design metaphor (e.g. 3 tier/layer)

identify possible layers from procedural model

Create interfaces

Distribute objects/classes

Edit and revise as necessary

Copyright W. Howden 46

Page 47: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Strategic Refactoring: Separate Domain

From Presentation

Going from a 2-tier to a 3 tier system: need to separate

domain logic from presentation logic, i.e. factor out the DL

from the GUI

Starting point

create domain objects for each GUI window

move or duplicate domain oriented data in/to new domain objects

move domain oriented logic to new domain objects

Copyright W. Howden 47

Page 48: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Domain/Presentation Separation and

System Architecture

GUI will make calls to logic moved to domain object

Domain interface needs to be determined

calls may be made to facade for whole domain or subset of domain

objects that support domain interfaces

Copyright W. Howden 48

Page 49: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS Domain Separation Example

Assume 2 tier system

all domain logic in GUI

GUI interacts with Data Base subsystem

Copyright W. Howden 49

Page 50: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS – Initial Domain

Separation Objects

GUI/Presentation windows

Domain Logic Objects:

GUI, MemberCommandsDialog, AdminCommandsDialog, SelectDaterPreferencesDialog, EnterMemberDataDialog, SelectedDateeDialog, Messages, LogOnDialog,

Architecture

Domain Logic facade may contain logic also

Copyright W. Howden 50

Page 51: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS GUI Domain Logic Domain Logic

Layer

“Check against name and user data base to determine user type”

LogOnDialog domain object

“Search through data base to find a match against specified datee

properties” MemberCommandsDialog domain object

“Add Member with given name to Data Base”

AdminCommandsDialog domain object

Copyright W. Howden 51

Page 52: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS Domain Logic Cleanup

Initial Domain object creation guidelines are rough names need to be changed, logic associated with

more meaningful object from GUI, logic transferred to Domain facade interface. e.g.

(i) LogOnDialog LogOn

(ii) logic for finding date transferred from MemberCommandsDialog to DateRequest (renamed SelectDaterPreferencesDialog)

(iii) logic for adding a member transferred to DomainLogic subsystem facade from AdminCommandsDialog

Copyright W. Howden 52

Page 53: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Tactical Refactoring:

Tease Apart Inheritance

2 or more inheritance hierarchies involved

merged hierarchies have different purposes

goal: clean up by removing the less important

approach

identify major hierarchy

extract subordinate hierarchy

create instance variable in common superclass

point to subordinate hierarchy

Copyright W. Howden 53

Page 54: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS Tease Apart Example

Superclass: DateSearchResult

Hierarchy subtypes

date found or not found

dater discovered to be frequent dater or not

Cohesion problems: lowest level classes mix two kinds of behavior/information

Main hierarchy

date found or not found

Copyright W. Howden 54

Page 55: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS – Initial Hierarchy

DateSearchResult

NoDateFound DateeProps

NoDate-Frequent NoDate-NonFrequent DateeProps-Frequent DateeProps-NonFrequent

Copyright W. Howden 55

Page 56: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS – Subordinate Extraction

-daterFrequency

DateSearchResult

NoDateFound DateeProps

FrequentDaterResultNon-FrequentDaterResult

DaterFrequency

Copyright W. Howden 56

Page 57: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS - Simplification

-daterFrequency

DateSearchResult

NoDateFound DateeProps

DaterFrequency

Copyright W. Howden 57

Page 58: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Tactical Refactoring:

Extract Hierarchy

Class grows in size and complexity as new responsibilities

are added

Starts to look like a representative of the Swiss Army Knife

Anti-Pattern

Cohesion is bad

Copyright W. Howden 58

Page 59: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Extract Hierarchy Steps

Create subclasses of the original complex class for each of the different kinds of behavior

Create a factory method for the superclass that returns an instance of the appropriate subclass. Note: superclass cannot be abstract

Change calls to the superclass constructor to the superclass factory method

Copyright W. Howden 59

Page 60: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS E.g. – Extract Hierarchy 1

Suppose DomainLogic has a Command class with getDate(),

setMemberData(), addMember(), and deleteMember()

methods. Suppose Command() constructor takes user-type

flag which it sets and which is then referenced by the

methods in their conditional logic

Suppose DomainLogic facade or other client creates an

instance of Command and invokes the appropriate method.

Copyright W. Howden 60

Page 61: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS E.g. – Extract Hierarchy 2

Alternative: Add new Command subclasses for

MemberCommands and AdminCommands

Create a factory method (e.g. called “create()”) in Command

that references a method argument to create and return the

appropriate subclass instance

Change DomainLogic facade or other clients to call the

factory method instead of the constructor. Subclasses all

have matching methods.

Copyright W. Howden 61

Page 62: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

DS Example Before and After

Page 63: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Transformational Refactoring Methods

Composing Methods

Moving Features Between Objects

Organizing Data

Simplifying Conditional Expressions

Making Method Calls Simpler

Dealing with Generalization

Copyright W. Howden 63

Page 64: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Moving Features

Between Objects - Examples

Move Method

Move Attribute

Extract Class

InLine Class

etc.

Copyright W. Howden 64

Page 65: AntiPatterns - LPU GUIDE · AntiPatterns Negative solutions that present more problems than they address Natural extension to design patterns Bridge the gap between architectural

Refactoring Tools

Simple edit-like tools

e.g. changing method names find all possible references

change names

E.g. IntelliJ IDE

Copyright W. Howden 65