architecture vs. code

73
Simon Brown @simonbrown Software Architecture vs Code

Upload: latcraft

Post on 12-Apr-2017

331 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Architecture vs. Code

Simon Brown @simonbrown

Software Architecture

vs Code

Page 2: Architecture vs. Code

Jersey

Page 3: Architecture vs. Code

Jersey

Page 4: Architecture vs. Code

Jersey

Page 5: Architecture vs. Code
Page 6: Architecture vs. Code

I help software teams understand

software architecture,

technical leadership and

the balance with agility

Page 7: Architecture vs. Code

Software architecture needs to be more

accessible

Page 8: Architecture vs. Code

Free!

Page 9: Architecture vs. Code

Communication and

design thinking

Page 10: Architecture vs. Code

Abstraction is about reducing detail rather than creating a different representation

Page 11: Architecture vs. Code

Abstractions help us

reason about a big and/or complex

software system

Page 12: Architecture vs. Code

As an industry, we lack a common vocabulary

with which to think about, describe and communicate software architecture

Page 13: Architecture vs. Code

Does your code reflect the

abstractions that you think about?

Page 14: Architecture vs. Code

“the model-code gap”

Page 15: Architecture vs. Code
Page 16: Architecture vs. Code

We often think

in components

but write classes (usually in layers)

Page 17: Architecture vs. Code

Package by layer (horizontal slicing)

Presentation layer

Business layer

Data layer

Controller BController A

Data Access A

Data Access B

Service BService A

Page 18: Architecture vs. Code
Page 19: Architecture vs. Code
Page 20: Architecture vs. Code
Page 21: Architecture vs. Code
Page 22: Architecture vs. Code
Page 23: Architecture vs. Code

Package by layer (horizontal slicing)

Presentation layer

Business layer

Data layer

Controller BController A

Data Access A

Data Access B

Service BService A

Controller C

Service C

Data Access C

Page 24: Architecture vs. Code

Package by layer (horizontal slicing)

Presentation layer

Business layer

Data layer

Controller BController A

Data Access A

Data Access B

Service BService A

Controller C

Service C

Data Access C

Page 25: Architecture vs. Code

Hexagons and onions

Page 26: Architecture vs. Code

Should layers be

considered harmful?

Page 27: Architecture vs. Code

Is layering another cargo cult?

Page 28: Architecture vs. Code

Are layers significant

structural elements

or just an

implementation detail?

Page 29: Architecture vs. Code
Page 30: Architecture vs. Code
Page 31: Architecture vs. Code

Package by feature (vertical slicing)

Feature Set B Feature Set A

Controller BController A

Data Access A

Data Access B

Service BService A

Page 32: Architecture vs. Code

Abstractions should reflect the

code

Page 33: Architecture vs. Code

A software system is made up of one or more containers,

each of which contains one or more components,

which in turn are implemented by one or more classes.

Class Class Class

Component Component Component

Container (e.g. web application, application server, standalone application,

browser, database, file system, etc)

Container (e.g. web application

browser, database, file system, etc)

Container (e.g. web application

browser, database, file system, etc)

Software System

Page 34: Architecture vs. Code

Diagrams are maps that help a team navigate a complex codebase

Page 35: Architecture vs. Code

Componentdiagram

(level 3)

Container diagram

(level 2)

Context diagram

(level 1)

Class diagram

(level 4)

Page 36: Architecture vs. Code

Componentdiagram

(level 3)

Container diagram

(level 2)

Context diagram

(level 1)

Class diagram

(level 4)

Page 37: Architecture vs. Code

Component diagram

(level 3)

Container diagram

(level 2)

Context diagram

(level 1)

Class diagram

(level 4)

Page 38: Architecture vs. Code

Componentdiagram

(level 3)

Container diagram

(level 2)

Context diagram

(level 1)

Class diagram

(level 4)

Page 39: Architecture vs. Code

Where’s my component?

The “Tweet Component”

doesn’t exist as a single thing …

it’s a combination of interfaces and classes

across a layered architecture

Page 40: Architecture vs. Code

“architecturally-evident coding style”

Page 41: Architecture vs. Code

Architecturally-evident coding styles include:

Annotations/attributes (@Component, [Component], etc)

Naming conventions (*Service)

Namespacing/packaging (com.mycompany.system.components.*)

Maven modules, OSGi modules, Java 9 and Jigsaw, JavaScript module patterns,

ECMAScript 6 modules, microservices, etc

Page 42: Architecture vs. Code

Package by component

Component B

Feature Set BFeature Set A

Component A

Controller BController A

Data Access A

Data Access B

Service BService A

Page 43: Architecture vs. Code

An example of an architecturally-evident coding style

Refactored to…

Page 44: Architecture vs. Code
Page 45: Architecture vs. Code

Component diagram

(level 3)

Container diagram

(level 2)

Context diagram

(level 1)

Class diagram

(level 4)

Page 46: Architecture vs. Code

Componentdiagram

(level 3)

Container diagram

(level 2)

Context diagram

(level 1)

Class diagram

(level 4)

Page 47: Architecture vs. Code

Modularity as a principle

Page 48: Architecture vs. Code

Design decisions

Page 49: Architecture vs. Code

Where do you put

domain classes?

Page 50: Architecture vs. Code

Shared code,

and utilities?

Page 51: Architecture vs. Code

Shared database tables, database schemas and foreign keys?

Page 52: Architecture vs. Code

Conscious decisions

rather than cargo cult

Page 53: Architecture vs. Code

Software architecture

vs testing (a quick aside)

Page 54: Architecture vs. Code

“In the early days of computing when computers were slow, unit tests gave the developer more immediate feedback about whether a change

broke the code instead of waiting for system tests to run. Today, with cheaper and more powerful computers, that argument is less persuasive.”

Page 55: Architecture vs. Code

“do not let your tests drive your design”

Page 56: Architecture vs. Code

An example of an architecturally-evident coding style

Refactored to…

Page 57: Architecture vs. Code

Do we also cargo cult unit testing?

Page 58: Architecture vs. Code

Instead of blindly unit testing everything, what about

testing your significant structural elements

as black boxes?

Page 59: Architecture vs. Code

Do we need to rethink the testing pyramid?

UI and functional

Integration

Unit

Page 60: Architecture vs. Code

Architecturally-aligned testing (and a reshaped testing pyramid)

Class Tests Tests focused on individual classes and methods, sometimes by mocking out

dependencies (typically referred to as “unit” tests)

Component and Service Tests Tests focused on components and services

through their public interface (often referred to as “integration” tests)

System Tests UI, API, functional and

acceptance tests (“end-to-end” tests)

Page 61: Architecture vs. Code

Software Architecture

Code Tests

Monoliths or

microservices …

there should be a stronger and more explicit

relationship between software architecture, code and tests

Page 62: Architecture vs. Code

Why?

Page 63: Architecture vs. Code

Maintainability is inversely proportional

to the number of

public classes dependencies microservices[ ]

Page 64: Architecture vs. Code

A good architecture

enables agility

Page 65: Architecture vs. Code

Agility is a

quality attribute

Page 66: Architecture vs. Code

Monolithic architecture

Service-based architecture

(SOA, micro-services, etc)

Something in between (components)

Page 67: Architecture vs. Code

Well-defined, in-process components is a stepping stone to out-of-process components

(i.e. microservices)

From components to microservices

High cohesion Low coupling

Focussed on a business capability Bounded context or aggregate

Encapsulated data Substitutable Composable

<- All of that plus

Individually deployable Individually upgradeable Individually replaceable

Individually scalable Heterogeneous technology stacks

Page 68: Architecture vs. Code

Choose microservices for

the benefits, not because your monolithic

codebase is a mess :-)

Page 69: Architecture vs. Code

Think about how to align the

software architecture

and the

code

Page 70: Architecture vs. Code

Be conscious of the software

architecture model … adopt an architecturally-evident

coding style

Page 71: Architecture vs. Code

Stop making every class

public

Page 72: Architecture vs. Code

1 CHF charity donation every time you type

without thinking :-)

public class

Page 73: Architecture vs. Code

If your code is hard to work with,

change it!

[email protected]

@simonbrown on Twitter