Principles of Package Design FOSDEM 2015

45
Principles of Package Design How to create cohesive, stable packages Matthias Noback - Noback's Office FOSDEM - 1 Feb 2015

Transcript of Principles of Package Design FOSDEM 2015

Page 1: Principles of Package Design FOSDEM 2015

Principles of Package DesignHow to create cohesive, stable packages

Matthias Noback - Noback's OfficeFOSDEM - 1 Feb 2015

Page 2: Principles of Package Design FOSDEM 2015

Writingphp-and-symfony.matthiasnoback.nl

2/46

Page 3: Principles of Package Design FOSDEM 2015

A Year With Symfony

leanpub.com/a-year-with-symfony

3/46

Page 4: Principles of Package Design FOSDEM 2015

Tight couplingCoupling to a framework

4/46

Page 5: Principles of Package Design FOSDEM 2015

Code

5/46

Page 6: Principles of Package Design FOSDEM 2015

PackagesThere are many different kinds

6/46

Page 7: Principles of Package Design FOSDEM 2015

Class design

7/46

Page 8: Principles of Package Design FOSDEM 2015

Package designNothing?

butunclebob.com

8/46

Page 9: Principles of Package Design FOSDEM 2015

Principles of Package Design

leanpub.com/principles-of-package-design

9/46

Page 10: Principles of Package Design FOSDEM 2015

Package designCohesion

10/46

Page 11: Principles of Package Design FOSDEM 2015

Package designCoupling

11/46

Page 12: Principles of Package Design FOSDEM 2015

A - Cohesion principlesPerspective: the package in isolation

12/46

Page 13: Principles of Package Design FOSDEM 2015

1 - The Release Reuse Equivalence PrincipleThe granule of reuse is the granule of release

13/46

Page 14: Principles of Package Design FOSDEM 2015

1 - The Release Reuse Equivalence Principle

Version control and hostingComposer package definitionMeta-filesAuto-loading

····

Semantic versioning

Quality control

·BranchesTagsBackward compatibility

---

·

14/46

Page 15: Principles of Package Design FOSDEM 2015

1 - The Release Reuse Equivalence Principle

If you don't have the time to turn your reusable code into a proper package...

Don't release it.

15/46

Page 16: Principles of Package Design FOSDEM 2015

2- The Common Reuse PrincipleClasses that are used together are packaged together

If you use one class of a package,you will use all its other classes too.

16/46

Page 17: Principles of Package Design FOSDEM 2015

2- The Common Reuse PrincipleSmell: Feature strata

17/46

Page 18: Principles of Package Design FOSDEM 2015

2- The Common Reuse PrincipleExample of feature strata: the Symfony Security Component

18/46

Page 19: Principles of Package Design FOSDEM 2015

2- The Common Reuse PrincipleSmell: Classes with different dependencies

19/46

Page 20: Principles of Package Design FOSDEM 2015

2- The Common Reuse PrincipleExample of different dependencies: Gaufrette

20/46

Page 21: Principles of Package Design FOSDEM 2015

2- The Common Reuse PrincipleDifferent dependencies: Gaufrette

{

"name": "knplabs/gaufrette",

...

"suggest": {

"knplabs/knp-gaufrette-bundle": "to use with Symfony2",

"dropbox-php/dropbox-php": "to use the Dropbox adapter",

"rackspace/php-opencloud" : "to use Opencloud adapter",

"herzult/php-ssh": "to use SFtp adapter",

"phpseclib/phpseclib": "to use PhpseclibSftp adapter",

"aws/aws-sdk-php": "to use the Amazon S3 adapter",

"amazonwebservices/aws-sdk-for-php": "to use the legacy Amazon S3 adapters",

"doctrine/dbal": "to use the Doctrine DBAL adapter",

"microsoft/windowsazure": "to use Microsoft Azure Blob Storage adapter",

"ext-zip": "to use the Zip adapter",

"ext-apc": "to use the APC adapter",

...

},

...

}21/46

Page 22: Principles of Package Design FOSDEM 2015

2 - The Common Reuse PrincipleLeszek Prabucki's response

22/46

Page 23: Principles of Package Design FOSDEM 2015

3 - The Common Closure PrincipleClasses that change together are packaged together

23/46

Page 24: Principles of Package Design FOSDEM 2015

3 - The Common Closure PrincipleReasons for change

The application's features changeThe business rules changeThe web framework's best practices changeThe persistence library's configuration changes...

·····

24/46

Page 25: Principles of Package Design FOSDEM 2015

3 - The Common Closure PrincipleSmell: code for multiple application layers

Web User InterfaceCommand-line interfaceModelInfrastructure services

····

25/46

Page 26: Principles of Package Design FOSDEM 2015

B - Coupling principlesPerspective: the package in relation to other packages

26/46

Page 27: Principles of Package Design FOSDEM 2015

4 - The Acyclic Dependencies PrincipleThe dependency graph of packages must have no cycles

27/46

Page 28: Principles of Package Design FOSDEM 2015

4 - The Acyclic Dependencies Principle

28/46

Page 29: Principles of Package Design FOSDEM 2015

Stability

Something is stable if it's resistant to change.

29/46

Page 30: Principles of Package Design FOSDEM 2015

5 - The Stable Dependencies PrincipleAn irresponsible package

30/46

Page 31: Principles of Package Design FOSDEM 2015

5 - The Stable Dependencies PrincipleA dependent package

31/46

Page 32: Principles of Package Design FOSDEM 2015

5 - The Stable Dependencies PrincipleAn instable package: irresponsible and dependent

32/46

Page 33: Principles of Package Design FOSDEM 2015

5 - The Stable Dependencies PrincipleA responsible package

33/46

Page 34: Principles of Package Design FOSDEM 2015

5 - The Stable Dependencies PrincipleAn independent package

34/46

Page 35: Principles of Package Design FOSDEM 2015

5 - The Stable Dependencies PrincipleA stable package: responsible and independent

35/46

Page 36: Principles of Package Design FOSDEM 2015

5 - The Stable Dependencies PrincipleDepend in the direction of stability

36/46

Page 37: Principles of Package Design FOSDEM 2015

5 - The Stable Dependencies PrincipleCounter example

37/46

Page 38: Principles of Package Design FOSDEM 2015

6 - The Stable Abstractions PrincipleWhat is more likely to change?

Something concrete or something abstract?A class or an interface?

··

38/46

Page 39: Principles of Package Design FOSDEM 2015

6 - The Stable Abstractions PrincipleAbstractness should increase with stability

39/46

Page 40: Principles of Package Design FOSDEM 2015

SummaryReuse/release equivalence principle

Common reuse principle

Common closure principle

Acyclic dependencies principle

Stable dependencies principle

Stable abstractions principle

·Reuse only code that you can release as a product.-

·All code in a package is reused at the same time.-

·Code in a package only changes for a few reasons.-

·No cycles in the dependency graph.-

·Only depend on more stable packages.-

·More stable packages are also more abstract.-

40/46

Page 41: Principles of Package Design FOSDEM 2015

Word of advice

You can't maximize them all at the same time.

Keep them in mind while you are working on a package.

41/46

Page 42: Principles of Package Design FOSDEM 2015

Principles of Package Design

leanpub.com/principles-of-package-design

42/46

Page 43: Principles of Package Design FOSDEM 2015

Questions?

twitter @matthiasnoback

feedback joind.in/13057

leanpub leanpub.com/principles-of-package-design

43/46

Page 45: Principles of Package Design FOSDEM 2015

46/46