Test-Driven Design - ¿Porqué?

Post on 06-Jul-2015

511 views 2 download

Tags:

description

Test-Driven Design (TDD) es una idea aparentemente simple: Escriba las pruebas para su código antes de escribir el código. Es "aparentemente simple" por que transforma el rol que testing juega en el proceso de desarrollo y cuestiona nuestros supuestos en la industria sobre el objetivo de testing. Testing ya no es solo acerca de evitar que los defectos lleguen al usuarios finales. Testing consiste en ayudar al equipo a entender las funcionalidad que los usuarios necesitan y hacer entrega de esas funcionalidad de forma confiable y productiva. Cuando TDD se sigue hasta sus últimas consecuencias, ocurren cambios radicales en la forma que desarrollamos software, la calidad de los sistemas que construimos mejora dramáticamente en términos de fiabilidad y flexibilidad en respuesta a nuevos requerimientos.En esta charla:• Los Administradores y Directores recibirán una justificación estratégica a nivel de negocios sobre TDD.

Transcript of Test-Driven Design - ¿Porqué?

Test-driven Design/Development

TDD• First agile practice to become mainstream

• Chrysler Comprehensive Compensation system

• They needed something different

• BUFP wasn’t working

• How to deal with change instead of fighting it?

Lean - Dealing with Change• Planning

• Incremental

• frequent

• Small

• features

• releases

Agile - Dealing with Change

• Plan driven vs Planning Driven

• Revise the plan weekly

• reflect reality

• make appropriate decisions

Lean Manufacturing

• View the problem as a Software Delivery System

• Programmers, testers, designers, managers, BBAA.

• Input: Feature request

• Output: Let’s decide...

Lean: “The Goal”- Eliyahu Goldratt

• Alex run a manufacturing plant soon to be closed

• He knows a consultant (Jonah)!!

• He learns a systems approach to manufacturing and its evaluation

• This new system is counterintuitive: Not seeking 100% machine utilization

Lean Basics: The Goal

• Increase the throughput

• Decrease operating expenses

• Decrease inventory

Lean: Throughput

• How much potential revenue the system can create

• Emphasis in deliver real software to real customers. It is only paid if it is used.

• Running Tested Features per time unit

• RFT/month

Lean: Operating expenses

• Computers

• Electricity

• Rent

• Insurance

• Wages ...

Lean:Inventory

• Grocery Store

• What is in the shelve

• I bet I can sell it on time

• It is not providing value (value = money)

Inventory in Software?

• Unused Features: Building a feature nobody is using [no payments]

• Unreleased features in source control

• Unnecessary complexity:

• A developer adds complexity in anticipation of a future feature

Inventory & Machine Utilization

• Machine spits out parts at 100% utilization

• Only 25% ended up ordered

• 8 hour shift: 200 sold and 600 stacking up on the floor

• Walk around parts, new warehouse!

• Operating expenses increased! Don’t use it at 100%

Inventory: Unnecessary Complexity

• Developer adds complexity in anticipation

• Developer will have to walk around these parts from now on [we have spoiled customers!!]

• Developers are slowed down

• More complexity

• Probably adding duplication

Inventory: Unnecessary Complexity

• The more unnecessary complexity

• The more difficult to change

• The harder to maintain

• Marginal costs of features goes up

• The harder to maintain a system

• The more expensive the feature

TDD’s goal is to reduce Inventory

Keeping the cost of adding a feature as low as we can by keeping the design simple

Lean Basics: The Goal

• Increase the throughput (RTF/month)

• Decrease operating expenses, - precedence

3.Decrease inventory

• Reduce unnecessary complexity

• Reduce unused features

• Reduce unreleased features

YAGNI - Decrease Unneeded Complexity

• Design only what I need to solve the problem I have now :-(

• I design what I need now in such a way that is ready for any change :-)

• Example: Personal defense failure

YAGNI - Decrease Unneeded Complexity• Design in a way that prevents attacks from

any direction

• Follow few principles to keep the design simple so the cost of implementing any new feature is reasonably low

• Example: Personal defense Principles

TDD Principles• Design is simple if it

• passes the test

• minimizes duplication

• maximizes clarity

• is small

• 1. is assumed and 4. is a by-product

TDD Principle: Reduce Duplication

“I’m doing the same thing in five places, I have to change all five of them [Put rationalized justification] so I copied and pasted in four places, I forgot the fifth one” bug ⇒

TDD Principle: Fix Bad Names

• By naming accurately

• It is easier to understand how the code works

• we may detect smells (name too weird?)

TDD Principle: Maximize Clarity

• Fix bad names

• name variables after what they represent

• name methods after what they do

• name classes what they are

• name interfaces after what the role they play

TDD Principle: Fix Bad Names

• Smell: too long with connectors like: “and”, “then”, “but”. That block of code

• is doing too much work

• too many responsibilities

• it duplicates responsibilities with another part of the system

TDD Principles• Design is simple if it

• minimizes duplication

• maximizes clarity

• Claim: IF WE FOLLOW THESE PRINCIPLES WE REDUCE INVENTORY CREATED BY UNNECESSARY COMPLEXITY

TDD: Recfatoring

• Improving the design of existing code [book]

• Systematic approach to improve the design without throwing a piece a way while rewritten it.

Refactoring: Systematic Approach

• Work in very small steps

• steps are reversible

• I can stop if I want to

• Keep the code working at every step

• I have tests (behavioral spec) that tells me if a structural change breaks behavior

Traditional Design

• We design something that meets a large number of requirements at once [cascade] Done by architects, handed off to developers

• Caveat with high level design ideas:

• high levels ideas right most of the time

• low level detail wrong most of the time

Evolutionary Design

• The act of writing the code give us information wether the design works or not, without that information, we can’t design effectively.

• Write code in a way that allows us to start with the low level detail and use some basic principles to make sure the high level ideal designs ideas work well

Evolutionary Design by TDD

• Test drives the design.

• A test articulates a goal so I know when to stop

Evolutionary Design: TDD cycle

• Write a failing test

• Articulates a goal: “I need a little piece of software to does this”

• Make the test pass:

• Don’t write any production code if there isn’t a failing test that requires it [no inventory]. Failing test evidence of need

Evolutionary Design: TDD cycle

• A new intellectual challenge

• write it simple

• articulate the goal with less code

• pass the test with less code

• remove duplication, make it clearer