Introduction to Testing and Maintainable code

Similar documents
Design Patterns. An introduction

Topics in Object-Oriented Design Patterns

Using Design Patterns in Java Application Development

Writing High Performance Delphi Applications. Primož Gabrijelčič

Design Patterns. Gunnar Gotshalks A4-1

SOLID DESIGN PATTERNS FOR MERE MORTALS

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Design Patterns. CSC207 Fall 2017

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

CPSC 310 Software Engineering. Lecture 11. Design Patterns

Design Patterns. CSC207 Winter 2017

The following topics will be covered in this course (not necessarily in this order).

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Software Engineering I (02161)

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Design Patterns. CSC207 Fall 2017

Summary of the course lectures

Build Testable Client and Service Applications

An Introduction to Patterns

SDC Design patterns GoF

SOLID Principles. Equuleus Technologies. Optional Subheading October 19, 2016

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

DESIGN PATTERNS FOR MERE MORTALS

Object-Oriented Design

What is Design Patterns?

1 Software Architecture

WRITING HIGH PERFORMANCE DELPHI APPLICATION. Primož Gabrijelčič

Applying the Observer Design Pattern

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Cocoa Design Patterns. Erik M. Buck October 17, 2009

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Lecture 13: Design Patterns

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Ingegneria del Software Corso di Laurea in Informatica per il Management. Software quality and Object Oriented Principles

Application Architectures, Design Patterns

DESIGN PATTERN - INTERVIEW QUESTIONS

Software Engineering I (02161)

COURSE 2 DESIGN PATTERNS

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Software Engineering

The Design Patterns Matrix From Analysis to Implementation

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

The GoF Design Patterns Reference

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Applying Some Gang of Four Design Patterns CSSE 574: Session 5, Part 3

SOFTWARE PATTERNS. Joseph Bonello

Applying the Factory Method Design Pattern

Index. Dmitri Nesteruk 2018 D. Nesteruk, Design Patterns in Modern C++,

Applying the Decorator Design Pattern

Abstraction. Design fundamentals in OO Systems. Fundamental Software Development Principles

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Object-Oriented Design

Advanced WCF 4.0 .NET. Web Services. Contents for.net Professionals. Learn new and stay updated. Design Patterns, OOPS Principles, WCF, WPF, MVC &LINQ

Design patterns. Jef De Smedt Beta VZW

Software Development

Crash course on design patterns

Agile Architecture. The Why, the What and the How

Object-Oriented Design

From Design Patterns: Elements of Reusable Object Oriented Software. Read the sections corresponding to patterns covered in the following slides.

Mock Objects and Distributed Testing

Software Development Project. Kazi Masudul Alam

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Software Engineering I (02161)

Tuesday, October 4. Announcements

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

SOLID: Principles of OOD

As a programmer, you know how easy it can be to get lost in the details

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

Lessons Learned. Johnny Bigert, Ph.D., Skype/Microsoft October 26, 2011

Single Responsibility Principle (SRP)

CS251 Software Engineering Lectures 18: Intro to DP

Recitation 13. Software Engineering Practices and Introduction to Design Patterns

An Introduction to Patterns

The Software Design Process. CSCE 315 Programming Studio, Fall 2017 Tanzir Ahmed

CSC7203 : Advanced Object Oriented Development. J Paul Gibson, D311. Design Patterns

Object-oriented Software Design Patterns

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

What is Design Patterns?

CSE 70 Final Exam Fall 2009

A Reconnaissance on Design Patterns

Improve Your SystemVerilog OOP Skills

SOFTWARE ENGINEERING SOFTWARE DESIGN. Saulius Ragaišis.

Design Pattern- Creational pattern 2015

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

Practical Objects: Test Driven Software Development using JUnit

Eliminate enterprise software design instability - protect variations! Nickolay Kofanov

Dependency Inversion, Dependency Injection and Inversion of Control. Dependency Inversion Principle by Robert Martin of Agile Fame

WS01/02 - Design Pattern and Software Architecture

Object-Oriented Design I

CS342: Software Design. November 21, 2017

CS/CE 2336 Computer Science II

administrivia today UML start design patterns Tuesday, September 28, 2010

Transcription:

Introduction to Testing and Maintainable code

Reasons not to write unit tests 1. I don't know how to write tests. 2. Writing tests is too hard. 3. I don't have enough time to write tests. 4. Testing is not my job. 5. My code is too simple for tests. 6. My code is too compex for tests.

Reasons to write unit tests 1. Tests reduce bugs in new and existing features 2. Tests are good documentation of requirements 3. Tests reduce the cost of change 4. Tests improve design 5. Tests allow fearless refactoring

What should be tested? Unit tests are cheap and fast and there should be many Integration tests might require extensive setup of external systems and thus are not as fast as pure unit tests GUI tests are complex to be maintained automatically

What is TDD? Test Driven Development A continuous process Write tests early Run tests often Get prompt feedback

But where do I start? Using the right tools Unit testing framework (DUnit, DUnit2, DUnitX) IDE Integration (TestInsight) Continuous integration (FinalBuilder, Continua, ) FastMM, CodeCoverage, LeakCheck,... Making code testable Follow design patterns and coding principles Clean code, SOLID,

What is testability?

What is testable code? Seams to separate different code pieces from each other No hidden input in methods Avoid singletons, other global state and hardwired external systems (database, filesystem, ) Use principles like SOLID

Constructor does real work How to fix: use dependency injection

Service object digging in value object How to fix: directly pass what is needed

Service object digging in value object

Service directly violating law of demeter How to fix: again pass only what is directly needed

Service directly violating law of demeter

Global state and singleton Dirties the design If A and B are not passed to each other they should not be able to interact Enables spooky action at a distance Interaction with unknown collaborators Makes for brittle applications (and tests) Changes in implementation may break them Turns APIs into liars How to fix?

Class does too much Why: Hard to debug Hard to test Not or very hard to extend Difficult to understand How to detect: Try to sum up what a class does in a single phrase and, or are signs that it may do too much

Roads to clean code 1. Trial and error Takes long time May take you in completely wrong direction 2. Follow recommended practices Many human-years went into them Years of practical use removed the bad ideas 3. Write comments

Roads to clean code 1. Trial and error Takes long time May take you in completely wrong direction 2. Follow recommended practices Many human-years went into them Years of practical use removed the bad ideas 3. Write comments

Commenting Writing good comments is incredibly hard Comments are not for you, they are for unknown reader! If a code needs to be commented, it is too complicated Exception: Document road not to be taken in the future!

Commenting

Commenting

Commenting Writing good comments is incredibly hard Comments are not for you, they are for unknown reader! If a code needs to be commented, it is too complicated Exception: Document road not to be taken in the future! Document APIs, not the code

Different levels of pre-packaged knowledge Design principles High-level recommendations, not problem-specific Architectural patterns Program organization at large Design patterns Ways to concrete solutions, are problem-specific Idioms Language-specific patterns that work at code level

Idioms Object creation for..in enumeration Fluent interfaces Helpers

Good Software Architecture

Clean Code What is clean code? complex!= complicated Principles that help design code that is easy to Test Maintain Extend

Principles KISS keep it simple, stupid DRY don t repeat yourself SoC deparation of concerns SLA single level of abstraction SOLID SRP single responsible principle OCP open closed principle LSP Liskov supsitution principle ISP interface segregation principle DIP dependency inversion principle

Don t repeat yourself (DRY) Reusable pieces - no copy and paste structure code into classes, methods, functions Imposes a level of abstraction Sometimes it is necessary to duplicate exact same or slightly modified code Depending on programming language choice this might be more or less.

Keep it simple, stupid (KISS) Everything should be made as simple as possible, Albert Einstein Solutions should be simple and easy to understand even tomorrow, next year or by the new developer in the team but not simpler sometimes Software developers tend to be clever Don t be clever

Single responsibility principle A class should only have one responsibility Modifications should have as little impact as possible to other parts higher complexity makes it harder to understand code

Separation of concerns (SoC) Releated to the SRP but expands beyond a class Concerns are for example: Logging, persistence, caching Depending on the programming language it might be easier AOP makes separation and integration very easy

Principle of least astonishment Software and source code behavior should not surprise you

Code quality code review

Algorithms Different algorithms scale differently What works in testing may fail in production

Algorithms complexity Tells us how algorithm slows down if data size is increased by a factor of n O() O(n), O(n 2 ), O(n log n) Time and space complexity

Typical complexities O(1) accessing array elements O(log n) searching in ordered list O(n) linear search O(n log n) quick sort (average) O(n 2 ) quick sort (worst), naive sort (bubblesort, insertion, selection) O(c n ) recursive Fibonacci, travelling salesman

Comparing complexities Data size O(1) O(log n) O(n) O(n log n) O(n 2 ) O(c n ) 1 1 1 1 1 1 1 10 1 4 10 43 100 512 100 1 8 100 764 10.000 10 29 300 1 9 300 2.769 90.000 10 90

Optimization Jackson s First Rule of Program Optimization: Don t do it. Jackson s Second Rule of Program Optimization (for experts only!): Don t do it yet. - Michael A. Jackson

What to optimize? Is slow too slow? Slower code may be easier to maintain It is hard to optimize extremely fast code Measure first! TStopwatch Profilers Measure on real data Now executes empty loop 2.5 times faster! It works fine on this table with 10 entries!

A task for you! If you choose to accept it

Task 2_3\Task23 Use RTL function with better O complexity to make the program run faster.

Design patterns

Design patterns Distillation of multiple solutions Language-agnostic (partially) Template for problem-solving Custom vocabulary

Design patterns are NOT A way to architect the program A silver bullet A detailed recipe A goal

Gang of Four Design Patterns: Elements of Reusable Object-Oriented Software, 1994 Erich Gamma, Richard Helm, Ralph Johson, John Vlissides 23 patterns In parts very OO-specific Composition over inheritance

Classification 1. Creational patterns Creating new objects and groups of objects 2. Structural patterns Compose objects to create new functionality 3. Behavioral patterns Object cooperation 4. Concurrency patterns Parallel programming

Use design patterns! Well, use all of them except one! Pop quiz which one? A. Facade B. Singleton C. Observer D. Thread pool

Examples: Singleton A class with only one instance Hard to implement correctly in Delphi Hard to mock Hard to configure Don t be an idiot works just as good

Examples: Factory method Defer creation of internal data to a subclass Better ways to do it than with subclassing

Examples: Iterator Traverse a container and access the container s elements Enumeration for..in

Examples: Observer Notify dependent objects on state changes Key part of the Model-View-Controller architectural pattern

A task for you! If you choose to accept it

Task 2_4\Task24 Introduce a pattern to make the code easier to maintain.

Parallel Programming

Parallel programming Don t do it. Don t do it yet.

Unit testing Very hard to do! Hard to test interactions between threads Hard to test edge cases Hardest of all: anticipate problems Also very important!

Clean code Immutable data structures Pure methods (no side effects)

Architecture Messaging instead of locking Not always possible! Proper locking! Deadlocks

Architecture Don t access GUI from a background thread!

No globals! Shared state introduces problems Not always possible to remove => locking

Patterns Locking Lock & test Test & lost & test again Atomic modifications Messaging Pipelining

A task for you! If you choose to accept it

Task 2_5\Task25 Fix the program so that it will work correctly.