+ All Categories
Home > Documents > From Stupid to Solid Code

From Stupid to Solid Code

Date post: 07-Apr-2016
Category:
Upload: andrei-olteanu
View: 17 times
Download: 1 times
Share this document with a friend
Description:
From stupid to solid code Really helpful
Popular Tags:
50
FROM STUPID TO SOLID CODE March 28, 2015 FLORIN OLARIU CENTRIC IT SOLUTION ROMANIA
Transcript
Page 1: From Stupid to Solid Code

FROM STUPID TO SOLID CODE

March 28, 2015

FLORIN OLARIU

CENTRIC IT SOLUTION ROMANIA

Page 2: From Stupid to Solid Code

ABOUT ME

• I’m working in field since 1996

• I’ve started as Java developer– I’m SCJP certified

• I’ve continued as team leader for a mixt team(Java/.NET

developers working with Waterfall methodologies)

• I’ve continued with .NET technologies since in 2003– I’m MCPD Windows Forms 2.0 certified – since 2006

– I’m MCPD Windows Forms 4.0 certified – since 2010

– I’m MCPD Web Forms 4.0 certified – since 2011

• Technical Lead/Scrum Master in Centric

March 28, 2015TITLE PRESENTATION

Page 3: From Stupid to Solid Code

WHAT ABOUT?

Don't Be Stupid, Grasp Solid

March 28, 2015TITLE PRESENTATION

Page 4: From Stupid to Solid Code

AGENDA

March 28, 2015TITLE PRESENTATION

Page 5: From Stupid to Solid Code

STUPID CODE

March 28, 2015TITLE PRESENTATION

Stupid code, seriously?

Page 6: From Stupid to Solid Code

STUPID CODE

• Singleton

• Tight coupling

• Untestability

• Premature optimization

• Indescriptive naming

• Duplication

March 28, 2015TITLE PRESENTATION

Page 7: From Stupid to Solid Code

SINGLETON

• The most well-known pattern used• Program using global state are very difficult to test

• Program that rely on global state hide their dependencies

March 28, 2015TITLE PRESENTATION

Page 8: From Stupid to Solid Code

TIGHT COUPLING

• Aka : strong coupling

• The main goal is to reduce coupling between modules• Difficult to reuse

• Hard to test

• Coupling - the degree to which each program module relies

on each one of the other modules.

March 28, 2015TITLE PRESENTATION

Page 9: From Stupid to Solid Code

UNTESTABILITY

• The unit tests should be easy

• Untestability is cause by tight coupling

March 28, 2015TITLE PRESENTATION

Page 10: From Stupid to Solid Code

PREMATURE OPTIMIZATION

• Donald Knuth said: premature optimization is the root of all

evil. There is only cost, and no benefit.

• Two rules to optimize an application:• don't do it;

• (for experts only!) don't do it yet.

March 28, 2015TITLE PRESENTATION

Page 11: From Stupid to Solid Code

INDESCRIPTIVE NAMING

• Programming languages are for humans.

• “Any fool can write code that a computer can understand.

Good programmers write code that humans can understand.”

– Martin Fowler

March 28, 2015TITLE PRESENTATION

Page 12: From Stupid to Solid Code

DUPLICATION

• DRY – don’t repeat yourself

• KISS – keep it simple(stupid)

• Be lazy in the right way – write code only once.

March 28, 2015TITLE PRESENTATION

Page 13: From Stupid to Solid Code

CODING KATA - INTRO

March 28, 2015TITLE PRESENTATION

Page 14: From Stupid to Solid Code

CODING KATA - INTRO

March 28, 2015TITLE PRESENTATION

Page 15: From Stupid to Solid Code

CODING KATA - INTRO

• KATA – came from martial arts

• Dave Thomas – has introduced Kata as learning technique

March 28, 2015TITLE PRESENTATION

Page 16: From Stupid to Solid Code

DEMO - I

• Fibonacci – Kata TDD

March 28, 2015TITLE PRESENTATION

Page 17: From Stupid to Solid Code

SOLID PRINCIPLES

• Single Responsibility Principle - SRP

• Open-Closed Principle - OCP

• Liskov Principle - LSP

• Interface Segregation Principle - ISP

• Dependency Inversion Principle - DIP

March 28, 2015TITLE PRESENTATION

Page 18: From Stupid to Solid Code

JENGA GAME

March 28, 2015TITLE PRESENTATION

Page 19: From Stupid to Solid Code

SOLID

March 28, 2015TITLE PRESENTATION

Page 20: From Stupid to Solid Code

SINGLE RESPONSIBILITY PRINCIPLE

A class should have

only one reason to

change.

March 28, 2015TITLE PRESENTATION

Page 21: From Stupid to Solid Code

SINGLE RESPONSIBILITY

March 28, 2015TITLE PRESENTATION

Page 22: From Stupid to Solid Code

SINGLE RESPONSIBILITY - TIPS

• Split big classes

• Use layers

• Avoid god classes

• Write straightforward comments

March 28, 2015TITLE PRESENTATION

Page 23: From Stupid to Solid Code

OPEN-CLOSED PRINCIPLE - OCP

Software entities

should be open for

extension and closed

for modification.

March 28, 2015TITLE PRESENTATION

Page 24: From Stupid to Solid Code

OPEN-CLOSED PRINCIPLE

March 28, 2015TITLE PRESENTATION

Page 25: From Stupid to Solid Code

OPEN-CLOSED PRINCIPLE - TIPS

• Make all members variables private

• No global variables

• Avoid setters (as much as possible)

March 28, 2015TITLE PRESENTATION

Page 26: From Stupid to Solid Code

LISKOV SUBSTITUTION PRINCIPLE

March 28, 2015TITLE PRESENTATION

Page 27: From Stupid to Solid Code

LISKOV SUBSTITUTION PRINCIPLE

• LSP states that objects in a

program should

be replaceable with

instances of their subtypes

without altering the

correctness of the program.

March 28, 2015TITLE PRESENTATION

Page 28: From Stupid to Solid Code

LISKOV SUBSTITUTION PRINCIPLE

March 28, 2015TITLE PRESENTATION

Page 29: From Stupid to Solid Code

LISKOV SUBSTITUTION PRINCIPLE

March 28, 2015TITLE PRESENTATION

Page 30: From Stupid to Solid Code

LISKOV SUBSTITUTION PRINCIPLE

March 28, 2015TITLE PRESENTATION

Page 31: From Stupid to Solid Code

INTERFACE SEGREGATION PRINCIPLE

March 28, 2015TITLE PRESENTATION

Page 32: From Stupid to Solid Code

INTERFACE SEGREGATION PRINCIPLE

•ISP states

that many client-

specific interfaces are

better than one general-

purpose interface.

March 28, 2015TITLE PRESENTATION

Page 33: From Stupid to Solid Code

INTERFACE SEGREGATION PRINCIPLE

•You should not have to

implement methods that you

don't use. Enforcing ISP

gives you low coupling,

and high cohesion.

March 28, 2015TITLE PRESENTATION

Page 34: From Stupid to Solid Code

DEPENDENCY INVERSION PRINCIPLE

March 28, 2015TITLE PRESENTATION

Page 35: From Stupid to Solid Code

DEPENDENCY INVERSION PRINCIPLE

•DIP has 2 key points:

March 28, 2015TITLE PRESENTATION

Page 36: From Stupid to Solid Code

DEPENDENCY INVERSION PRINCIPLE

•DIP has 2 key points:–Abstractions should not depend upon

details – both should depend on

abstractions

–Details should not depend upon

abstraction – details should depend

on abstractions

March 28, 2015TITLE PRESENTATION

Page 37: From Stupid to Solid Code

DEPENDENCY INVERSION PRINCIPLE

•Note•DIP is not the same like

Dependency Injection

March 28, 2015TITLE PRESENTATION

Page 38: From Stupid to Solid Code

DEPENDENCY INVERSION PRINCIPLE

•Note•Dependency Injection is about

how one object knows about

another dependent object.

March 28, 2015TITLE PRESENTATION

Page 39: From Stupid to Solid Code

DEPENDENCY INVERSION PRINCIPLE

•Note•In other words, it is about how

one object acquires a

dependency.

March 28, 2015TITLE PRESENTATION

Page 40: From Stupid to Solid Code

DEPENDENCY INVERSION PRINCIPLE

•http://stackoverflow.com/questi

ons/6766056/dip-vs-di-vs-ioc

March 28, 2015TITLE PRESENTATION

Page 41: From Stupid to Solid Code

ON MORE THING…

March 28, 2015TITLE PRESENTATION

Page 42: From Stupid to Solid Code

SMILES

March 28, 2015TITLE PRESENTATION

Page 43: From Stupid to Solid Code

SMILES

• Give a man a program, frustrate him for a day. Teach a man to

program frustrate him for a lifetime - Muhammad Waseem

March 28, 2015TITLE PRESENTATION

Page 44: From Stupid to Solid Code

SMILES

• Give a man a program, frustrate him for a day. Teach a man to

program frustrate him for a lifetime - Muhammad Waseem

• What is the object-oriented way of getting rich?

March 28, 2015TITLE PRESENTATION

Page 45: From Stupid to Solid Code

SMILES

• Give a man a program, frustrate him for a day. Teach a man to

program frustrate him for a lifetime - Muhammad Waseem

• What is the object-oriented was of getting rich? Inheritance of

course.

March 28, 2015TITLE PRESENTATION

Page 46: From Stupid to Solid Code

SMILES

• Give a man a program, frustrate him for a day. Teach a man to

program frustrate him for a lifetime - Muhammad Waseem

• What is the object-oriented was of getting rich? Inheritance of

course.

• Walking on water and developing software to specification are

easy ….

March 28, 2015TITLE PRESENTATION

Page 47: From Stupid to Solid Code

SMILES

• Give a man a program, frustrate him for a day. Teach a man to

program frustrate him for a lifetime - Muhammad Waseem

• What is the object-oriented was of getting rich? Inheritance of

course.

• Walking on water and developing software to specification are

easy as long as both are frozen.

March 28, 2015TITLE PRESENTATION

Page 48: From Stupid to Solid Code

CONCLUSIONS

• Use your brain

• Writing SOLID code is not that hard

• Avoid STUPID code

March 28, 2015TITLE PRESENTATION

Page 49: From Stupid to Solid Code

QUESTIONS?

Page 50: From Stupid to Solid Code

THANK YOU!

March 28, 2015

FLORIN OLARIU

[email protected]


Recommended