code structural analysis

54
Code Structural Analysis

Upload: eduards-sizovs

Post on 08-May-2015

1.189 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Code Structural Analysis

Code Structural Analysis

Page 2: Code Structural Analysis

Eduards [email protected]

www.linkedin.com/in/eduardsi

@eduardsi on Twitter

Who is broadcasting?

Page 3: Code Structural Analysis

Agenda

• Introduction to structural analysis• Logical Design principles• Q&A

Page 4: Code Structural Analysis

Introduction to structural analysis

Page 5: Code Structural Analysis

Most apps begin life small and neat.

Page 6: Code Structural Analysis

Time goes by…

Page 7: Code Structural Analysis

Hello. I am your rotting enterprise app.

Page 8: Code Structural Analysis

How do we feel design rot?

Page 9: Code Structural Analysis

Rigidity

Fragility

Immobility

Viscosity

Opacity

Needless Complexity

Needless Repetition

There are different symptoms that smell…

Page 10: Code Structural Analysis

What structural analysis shows…

Page 11: Code Structural Analysis

Structure of Hibernate Core 3.6.6

Page 12: Code Structural Analysis

Artwork by Jackson Pollock

Page 13: Code Structural Analysis

However, it could be better…

Page 14: Code Structural Analysis

Structure of Spring Framework 3.2.0

Page 15: Code Structural Analysis

Structural analysis allows you to keep app’s structure in shape and catch

design flaws early.

Page 16: Code Structural Analysis

Logical Design principles

Page 17: Code Structural Analysis

Package is the first-class citizen and key element of logical design.

Page 18: Code Structural Analysis

Treat packages as hierarchical even if they’re represented flat.

Page 19: Code Structural Analysis

Given packagescom.myproject.clientcom.myproject.client.personalcom.myproject.client.profilecom.myproject.api

Personal and Profile packages are part of Client.

Page 20: Code Structural Analysis

Use packages to group functionally-related artifacts. Do not group artifacts

that do the same thing, but are different by nature.

Page 21: Code Structural Analysis

Avoid

com.myproject.enumscom.myproject.modelcom.myproject.servicescom.myproject.repositorycom.myproject.factorycom.myproject.helpers…

Prefer

com.myproject.clientcom.myproject.lendingcom.myproject.loyality…

Page 22: Code Structural Analysis

The Common Closure Principle

Group tightly coupled classes together. If classes that change together are in the same package, then the impact of

change is localized.

Page 23: Code Structural Analysis

Make sure artifacts are not floating away.

Page 24: Code Structural Analysis

Make packages highly cohesive by following Single Responsibility

Principle.

Page 25: Code Structural Analysis

Keep packages loosely coupled, ideally – completely independent. Reflection

doesn’t count.

Page 26: Code Structural Analysis

Avoid dependency magnets. Sometimes duplication is not that evil.

Page 27: Code Structural Analysis

Aggregate root relationship

com.app.loan

class Loan {

}

com.app.client

class Client {// a bunch of stuff

}@ManyToOneClient client

Page 28: Code Structural Analysis

Aggregate root relationship

com.app.loan

class Loan {

}

com.app.client

class Client {// a bunch of stuff

}@EmbeddedClientId clientId

com.app.client

@Embeddableclass ClientId {

Long value

}

Page 29: Code Structural Analysis

Provide slim package interface and hide implementation details.

Page 30: Code Structural Analysis

Strive for weak package connascence. Keep locality in mind.

Page 31: Code Structural Analysis

Manage relationships. Every dependency arrow has a reason. Burn

bi-directional dependences in fire.

Page 32: Code Structural Analysis

Otherwise

Page 33: Code Structural Analysis

Version 1.0 – A great start

Page 34: Code Structural Analysis

Version 1.1 – Imperfection creeps in

Page 35: Code Structural Analysis

Version 1.2 – Imperfection takes hold

Page 36: Code Structural Analysis

Version 1.3 – Chaos begins

Page 37: Code Structural Analysis

Version 1.4 – Explosion

Page 38: Code Structural Analysis

The Acyclic Dependencies Principle

The dependencies between packages must not form cycles.

Page 39: Code Structural Analysis

Can be solved by:- Merging

- Dependency Inversion

- Escalation

- Demotion

Page 40: Code Structural Analysis

Client.Beans

Merging

Client

Client

Page 41: Code Structural Analysis

Tracking

Dependency Inversion

Guest

TrackingGuest

Page 42: Code Structural Analysis

Billing

Escalation

Customer

class Customer { Collection<Bill> bills def calculateDiscount() { } }

class Bill { def pay(Customer customer) { customer.calculateDiscount() … }}

Page 43: Code Structural Analysis

Billing

Escalation

Customer

class Customer { Collection<Bill> bills}class Bill { def pay(BigDecimal discount) { }}

class DiscountCalculator { def calculate(Customer, Bills)}

Discount

Page 44: Code Structural Analysis

Billing

Demotion

Customer

class Customer { Collection<Bill> bills def getDiscountCalculator()}

class Bill { def pay(DiscountCalculator) { }}

class DiscountCalculator { def calculate()}

Discount

Page 45: Code Structural Analysis

Class design principles (SOLID)

Page 46: Code Structural Analysis

Single Responsibility PrincipleOpen-Closed PrincipleLiskov Substitution PrincipleInterface Segregation PrincipleDependency Inversion Principle

Page 47: Code Structural Analysis

Class should have only one responsibility and

only one reason to change.

The Single Responsibility Principle

Page 48: Code Structural Analysis

Depend on abstractions, as anything concrete is

volatile

The Dependency Inversion Principle

High Level Policy

Abstraction

Abstraction

Impl. Impl.

Package Package

X Y

X Yinterfac

e

X

Package Package

Page 49: Code Structural Analysis

Client B

Service

«Client A method»«Client B

method» «Client C method»

Client C

Client A

Many client-specific interfaces are better than

one general-purpose interface

The Interface Segregation Principle

Interface A

«Client A method»

Client B

Client C

Client A

Interface B

«Client B method»

Interface C

«Client C method»

Service

«Client A method»«Client B

method» «Client C method»

Page 50: Code Structural Analysis

Tools

Macker

JDepend

Page 51: Code Structural Analysis

OO Design Principles & Metrics, Jason Gorman http://goo.gl/RTW9GTThe Economics of Software Design, J.B. Rainsberger http://goo.gl/ra7Q8Q SOLID Principles, Eduards Sizovs http://goo.gl/RpxavdDesigning Object-Oriented Software, Jouni Smed http://goo.gl/iyE1R2Grand Unified Theory Of Software Design, Jim Weirich http://goo.gl/ASqyAsFun With Modules, Kirk Knoernschild http://goo.gl/i8jx8Y

More

Page 52: Code Structural Analysis

Conclusion

Every application requires structure.

Structure must be carefully managed.

Integrate Code Structural Analysis into

development:• Install CSA tool

• Look at the «big picture»

• Manage complexity

• Prevent entropy

Page 53: Code Structural Analysis

Wish you beautiful architectures.

Page 54: Code Structural Analysis

Thank you!