SlideShare a Scribd company logo
Keeping Code Clean
Agile tools and practices for better code
Topics
● Pair Programming
● Automated Unit Testing
● Refactoring
● Test-Driven Development
● Agile Architecture
Pair Programming
Pair Programming
● Continuous Code Review
● Better Design w/ Less Code
● Faster Problem Solving
● More People Familiar w/ System
● Team Grows Closer
Pair Programming - Benefits
Pair Programming - Evidence
Pair Programming - Evidence
Williams 2000
● Efforts +15%, Schedule -43%
Baheti 2002
● Effort +2%, Schedule -49%
Ciokowlski 2002
● Effort +9%, Schedule -46%
Automated Unit Testing
What is Automated Unit Testing?
Use of special software to control the
execution of unit tests and the comparison of
actual outcomes with predicted outcomes.
To get full benefit run as part of the build
process and schedule to run automatically
once or twice a day.
Automated Unit Test Suite Benefits
● Unit tests find problems early in the development cycle
● Developers will be less afraid to change code
● Development process becomes more flexible
● Easier for a developer to take over code they are
unfamiliar with (improves “Truck Factor”)
● Reduces need for manual testing
● Software development becomes more testable and
repeatable
What is a Unit Test Case?
● A test for a single unit of functionality
● Each method tests a specific bit of
functionality
● Automated by the test-runner framework
AAA Test Structure
● Structure:
○ Arrange
○ Act
○ Assert
● Benefits:
○ Readable
○ Consistent
public class SalesTaxTest
{
private SalesTax unit;
@Test
public void TestCalculate()
{
unit = new SalesTax(); // ARRANGE
double tax = unit.Calculate(10); // ACT
Assert.assertEquals(tax, 0.9); // ASSERT
}
}
Unit Testing Best Practices
● Test simple stuff first
● Get it working - then test boundaries/exceptions
● Use assert
● Keep tests small & understandable
● Make test method names descriptive
● Keep tests independent of each other
● Avoid System.out messages
● Don’t repeat tests! Test once and trust it.
Test Doubles
● Stand in for a collaborating object e.g. database,
webservice
● Create test independence
● Make tests run faster
● Types of Doubles
○ Fakes: working implementation e.g.fake web service
○ Mocks: preprogrammed with expectations
○ Stubs: provide canned answers to the test’s calls
Refactoring
Refactoring
● What is refactoring?
● Why refactor?
● When to refactor?
What is Refactoring?
“A change to the system that leaves its behavior
unchanged, but enhances some non-functional quality -
simplicity, flexibility, understandability, performance”
-- Kent Beck, Extreme Programing Explained (p. 179)
“A change made to the internal structure of software to
make it easier to understand and cheaper to modify without
changing its observable behavior.”
-- Martin Fowler, Refactoring (p. 53)
Why Refactor the Code?
● Prevent “design decay”
● Clean up messes
● Simplify
● Increase readability / understandability
● Find bugs
● Reduce debugging time
● Build in what we learn about the application
● It’s part of the creative process
When to Refactor
● Code reviews / TDD cycle
● Rule of three
○ cut & paste. Third time copying? Now is the time to
generalize and move this to a new procedure.
● When you:
○ add functionality
○ learn something about the code
○ identify a code smell
What is a Code Smell?
● any symptom in the code that possibly
indicates a deeper problem.
● not bugs - but indicate weaknesses in design
that may be slowing down development or
increasing the risk of bugs or failures in the
future.
Code Smells (1/2)
● Duplicated code: near-identical code in multiple places
● Long method: a too large method/function/procedure
● Large class: a class that has grown too large.
● Too many parameters: decreases readability/quality.
● Identifier names are too short or excessively long
● Complex conditionals: branches that check lots of
unrelated conditions and edge cases that don't seem to
capture the meaning of a block of code.
Code Smells (2/2)
● Comments - refactor so these are no longer needed!
● Feature envy: excessive use of another class’ methods
● Inappropriate intimacy: a class that has dependencies
on implementation details of another class.
● Lazy class / Freeloader: a class that does too little.
● Contrived complexity: simpler design would suffice.
● Excessive use of literals (use named constants)
Why Developers Resist Refactoring
● Lack of understanding
● Short-term focus
● Not paid for overhead tasks like refactoring
● Fear of breaking current program
Use TDD to overcome this fear!
Test-Driven Development
TDD as a Design Approach
● Tests define up front what “works” means
● Programing by intention - write tests for what
code intends to do not what the code you just wrote
does.
● Focus on minimum set of required features
● Regular integration of small changes
● Evolutionary Design: Tests provide a safety
net for refactoring.
Red-Green-Refactor Cycle
How to Get to Green? Implement it!
Obvious
Implementation
code the real implementation, if
it's obvious and can quickly make
test pass, otherwise...
public int Sum(int x, int y)
{
return x + y;
}
How to Get to Green? Fake it
Fake it
hardcode constants to make test
pass, gradually generalize code
using variables
public int Fibonacci(int n)
{
if (n >= 1) return 1;
return 0;
}
How to Get to Green? Triangulate
Triangulate
figure out the behavior of an
algorithm by examining a couple
of test cases instead of just one.
public void TestSum()
{
Assert.AreEqual(4, Plus(3, 1));
Assert.AreEqual(7, Plus(4, 3));
}
Use TDD to Earn a Gift Card!
TDD is a great tool to use in solving the code challenges. E.g. Function takes a
Roman Numeral String and returns the Decimal value.
● Start with simple valid numbers I, III... up to more complex tests
● Incorporate tests for boundary conditions and invalid numbers
● With a set of tests you can easily refactor your solution.
VALID NUMBERS INVALID NUMBERS
I Boundary: Empty, 0, MMMM
III iii
IV IVIV, IIII, IIX, MXXXX
VI, XLIX, MMMDCCCLXXXVIII ABCDEF
A Great TDD Example!
● Bowling Kata - how to score a bowling
game.
● Eclipse and junit step-by-step presentation
on red - green - refactor to solve (butunclebob.
com/files/downloads/Bowling%20Game%20Kata.ppt)
Agile Architecture
● Collaborative Design
● Good coding Standards and Principles
Agile Architecture
Collaborative Design
● Emergent Design
○ Light Design Up Front (LDUF)
● Every team member contributes
● No command and control
Collaborative Design - payoff
http://martinfowler.com/bliki/DesignStaminaHypothesis.html
Standards and Principles
● Agree as a team
○ coding and naming conventions
○ toolset
■ Static Code Analysis, Test mocks, etc
○ best practices
● Avoid Technical Debt
● Follow well established principles and
patterns
Object Oriented Design Principles
S ingle Responsibilty
O pen/Closed
L iskov Substitution
I nterface Segregation
D ependency Inversion
The Single Responsibility Principle
● Every class should have a single responsibility, and that responsibility
should be entirely encapsulated by the class. All its services should be
narrowly aligned with that responsibility.
● Single Reason to Change
class Book {
function getTitle() {
return "A Great Book";
}
function getAuthor() {
return "John Doe";
}
function turnPage() {
// pointer to next page
}
function printCurrentPage() {
echo "current page content";
}
}
The Open/Closed Principle
● You should be able to extend a classes
behavior without modifying it
● Open for extension, but closed for
modification
public double Area(object[] shapes)
{
double area = 0;
foreach (var shape in shapes) {
if (shape is Rectangle) {
Rectangle rectangle = (Rectangle) shape;
area += rectangle.Width*rectangle.Height;
} else {
Circle circle = (Circle)shape;
area += circle.Radius * circle.Radius * Math.PI;
}
}
return area;
}
The Liskov Substitution Principle
● Derived classes must be substitutable for
their base classes
public class Rectangle {
private int width;
private int height;
// setters and getters
}
public class Square extends Rectangle {
public Square(int height, int width) {
setWidth(width);
setHeight(height);
}
}
The Interface Segregation Principle
● No client should be forced to depend on
methods it does not use
interface Worker {
void work();
void eat();
}
class HumanWorker implements Worker {
Public void work { /* do work stuff */ }
Public void eat { /* eat stuff */ }
}
class RobotWorker implements Worker {
Public void work { /* do work stuff */ }
Public void eat { /* ACK! I don't eat! */ }
}
interface Worker {
void work();
}
interface Eater {
void eat();
}
class HumanWorker implements Worker, Eater {
Public void work { /* do work stuff */ }
Public void eat { /* eat stuff */ }
}
class RobotWorker implements Worker {
Public void work { /* do work stuff */ }
}
The Dependency Inversion Principle
● High-level modules should not depend on
low-level modules. Both should depend on
abstractions.
● Abstractions should not depend on details.
Details should depend on abstractions.
class Worker {
private FileWriter fileWriter;
public Worker() {
fileWriter = new FileWriter();
}
}
class Worker {
private FileWriter fileWriter;
public Worker(FIleWriter writer) {
fileWriter = writer;
}
}
interface FileWriter {
// writer files or something
}
Object Orient Patterns
● Common Design Patterns
○ Strategy, Command, Decorator, Adapter, etc
● Enterprise Integration Patterns
Acknowledgements
Pair Programming:
● http://www.cs.pomona.edu/classes/cs121/supp/williams_prpgm.pdf
● http://collaboration.csc.ncsu.edu/laurie/Papers/ieeeSoftware.PDF
SOLID
● http://lostechies.com/derickbailey/2009/02/11/solid-development-principles-in-motivational-
pictures/
Content based on:
● SolutionsIQ CSD training slides
● http://en.wikipedia.org/wiki/Code_smell
● http://www.codeproject.com/Articles/5404/The-benefits-of-automated-unit-testing

More Related Content

What's hot

Agile Test Driven Development
Agile Test Driven DevelopmentAgile Test Driven Development
Agile Test Driven Development
Viraf Karai
 
TDD
TDDTDD
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
Paulo Clavijo
 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefIntroduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed Shreef
Ahmed Shreef
 
TDD done right - tests immutable to refactor
TDD done right - tests immutable to refactorTDD done right - tests immutable to refactor
TDD done right - tests immutable to refactor
Grzegorz Miejski
 
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)
Anatoliy Okhotnikov
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
Igor Crvenov
 
Test driven development
Test driven developmentTest driven development
Test driven development
Sharafat Ibn Mollah Mosharraf
 
Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
Omar Youssef Shiha
 
User story workflow (eng)
User story workflow (eng)User story workflow (eng)
User story workflow (eng)
Anatoliy Okhotnikov
 
Android Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit TestingAndroid Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit Testing
mahmoud ramadan
 
Tdd
TddTdd
TDD refresher
TDD refresherTDD refresher
TDD refresher
Kerry Buckley
 
TDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not ImplementationsTDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not Implementations
Harshith Shetty
 
Refactoring - An Introduction
Refactoring - An IntroductionRefactoring - An Introduction
Refactoring - An Introduction
Giorgio Vespucci
 
Making Strongly-typed NETCONF Usable
Making Strongly-typed NETCONF UsableMaking Strongly-typed NETCONF Usable
Making Strongly-typed NETCONF Usable
Open Networking Summit
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your code
Pascal Larocque
 
Tdd practices
Tdd practicesTdd practices
Tdd practices
axykim00
 
TDD with RSpec
TDD with RSpecTDD with RSpec
TDD with RSpec
Rachid Calazans
 

What's hot (20)

Agile Test Driven Development
Agile Test Driven DevelopmentAgile Test Driven Development
Agile Test Driven Development
 
TDD
TDDTDD
TDD
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefIntroduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed Shreef
 
TDD done right - tests immutable to refactor
TDD done right - tests immutable to refactorTDD done right - tests immutable to refactor
TDD done right - tests immutable to refactor
 
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
 
User story workflow (eng)
User story workflow (eng)User story workflow (eng)
User story workflow (eng)
 
Tech talks#6: Code Refactoring
Tech talks#6: Code RefactoringTech talks#6: Code Refactoring
Tech talks#6: Code Refactoring
 
Android Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit TestingAndroid Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit Testing
 
Tdd
TddTdd
Tdd
 
TDD refresher
TDD refresherTDD refresher
TDD refresher
 
TDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not ImplementationsTDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not Implementations
 
Refactoring - An Introduction
Refactoring - An IntroductionRefactoring - An Introduction
Refactoring - An Introduction
 
Making Strongly-typed NETCONF Usable
Making Strongly-typed NETCONF UsableMaking Strongly-typed NETCONF Usable
Making Strongly-typed NETCONF Usable
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your code
 
Tdd practices
Tdd practicesTdd practices
Tdd practices
 
TDD with RSpec
TDD with RSpecTDD with RSpec
TDD with RSpec
 

Similar to Keeping code clean

Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-mule
ikram_ahamed
 
Usable Software Design
Usable Software DesignUsable Software Design
Usable Software Design
Alexandru Bolboaca
 
Test-Driven Development.pptx
Test-Driven Development.pptxTest-Driven Development.pptx
Test-Driven Development.pptx
Tomas561914
 
Enter the mind of an Agile Developer
Enter the mind of an Agile DeveloperEnter the mind of an Agile Developer
Enter the mind of an Agile Developer
BSGAfrica
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
Thierry Gayet
 
Agile
AgileAgile
Agile
Komal2525
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)
Peter Kofler
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
Rob Hale
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
Andrea Polci
 
Extreme programming (xp)
Extreme programming (xp)Extreme programming (xp)
Extreme programming (xp)
Mohamed Abdelrahman
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
nikhil sreeni
 
Test Driven Development Methodology and Philosophy
Test Driven Development Methodology and Philosophy Test Driven Development Methodology and Philosophy
Test Driven Development Methodology and Philosophy
Vijay Kumbhar
 
Introduction to Unit Tests and TDD
Introduction to Unit Tests and TDDIntroduction to Unit Tests and TDD
Introduction to Unit Tests and TDD
Betclic Everest Group Tech Team
 
Prashant technical practices-tdd for xebia event
Prashant   technical practices-tdd for xebia eventPrashant   technical practices-tdd for xebia event
Prashant technical practices-tdd for xebia event
Xebia India
 
Ian Cooper webinar for DDD Iran: Kent beck style tdd seven years after
Ian Cooper webinar for DDD Iran: Kent beck style tdd   seven years afterIan Cooper webinar for DDD Iran: Kent beck style tdd   seven years after
Ian Cooper webinar for DDD Iran: Kent beck style tdd seven years after
Iranian Domain-Driven Design Community
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0
Ganesh Kondal
 
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
OdessaJS Conf
 
Mixing d ps building architecture on the cross cutting example
Mixing d ps building architecture on the cross cutting exampleMixing d ps building architecture on the cross cutting example
Mixing d ps building architecture on the cross cutting example
corehard_by
 
QA Strategies for Testing Legacy Web Apps
QA Strategies for Testing Legacy Web AppsQA Strategies for Testing Legacy Web Apps
QA Strategies for Testing Legacy Web Apps
Rainforest QA
 
Software Design Principles and Best Practices - Satyajit Dey
Software Design Principles and Best Practices - Satyajit DeySoftware Design Principles and Best Practices - Satyajit Dey
Software Design Principles and Best Practices - Satyajit Dey
Cefalo
 

Similar to Keeping code clean (20)

Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-mule
 
Usable Software Design
Usable Software DesignUsable Software Design
Usable Software Design
 
Test-Driven Development.pptx
Test-Driven Development.pptxTest-Driven Development.pptx
Test-Driven Development.pptx
 
Enter the mind of an Agile Developer
Enter the mind of an Agile DeveloperEnter the mind of an Agile Developer
Enter the mind of an Agile Developer
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
Agile
AgileAgile
Agile
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
 
Extreme programming (xp)
Extreme programming (xp)Extreme programming (xp)
Extreme programming (xp)
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test Driven Development Methodology and Philosophy
Test Driven Development Methodology and Philosophy Test Driven Development Methodology and Philosophy
Test Driven Development Methodology and Philosophy
 
Introduction to Unit Tests and TDD
Introduction to Unit Tests and TDDIntroduction to Unit Tests and TDD
Introduction to Unit Tests and TDD
 
Prashant technical practices-tdd for xebia event
Prashant   technical practices-tdd for xebia eventPrashant   technical practices-tdd for xebia event
Prashant technical practices-tdd for xebia event
 
Ian Cooper webinar for DDD Iran: Kent beck style tdd seven years after
Ian Cooper webinar for DDD Iran: Kent beck style tdd   seven years afterIan Cooper webinar for DDD Iran: Kent beck style tdd   seven years after
Ian Cooper webinar for DDD Iran: Kent beck style tdd seven years after
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0
 
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
 
Mixing d ps building architecture on the cross cutting example
Mixing d ps building architecture on the cross cutting exampleMixing d ps building architecture on the cross cutting example
Mixing d ps building architecture on the cross cutting example
 
QA Strategies for Testing Legacy Web Apps
QA Strategies for Testing Legacy Web AppsQA Strategies for Testing Legacy Web Apps
QA Strategies for Testing Legacy Web Apps
 
Software Design Principles and Best Practices - Satyajit Dey
Software Design Principles and Best Practices - Satyajit DeySoftware Design Principles and Best Practices - Satyajit Dey
Software Design Principles and Best Practices - Satyajit Dey
 

Recently uploaded

First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
XfilesPro
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 

Recently uploaded (20)

First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 

Keeping code clean

  • 1. Keeping Code Clean Agile tools and practices for better code
  • 2. Topics ● Pair Programming ● Automated Unit Testing ● Refactoring ● Test-Driven Development ● Agile Architecture
  • 5. ● Continuous Code Review ● Better Design w/ Less Code ● Faster Problem Solving ● More People Familiar w/ System ● Team Grows Closer Pair Programming - Benefits
  • 7. Pair Programming - Evidence Williams 2000 ● Efforts +15%, Schedule -43% Baheti 2002 ● Effort +2%, Schedule -49% Ciokowlski 2002 ● Effort +9%, Schedule -46%
  • 9. What is Automated Unit Testing? Use of special software to control the execution of unit tests and the comparison of actual outcomes with predicted outcomes. To get full benefit run as part of the build process and schedule to run automatically once or twice a day.
  • 10. Automated Unit Test Suite Benefits ● Unit tests find problems early in the development cycle ● Developers will be less afraid to change code ● Development process becomes more flexible ● Easier for a developer to take over code they are unfamiliar with (improves “Truck Factor”) ● Reduces need for manual testing ● Software development becomes more testable and repeatable
  • 11. What is a Unit Test Case? ● A test for a single unit of functionality ● Each method tests a specific bit of functionality ● Automated by the test-runner framework
  • 12. AAA Test Structure ● Structure: ○ Arrange ○ Act ○ Assert ● Benefits: ○ Readable ○ Consistent public class SalesTaxTest { private SalesTax unit; @Test public void TestCalculate() { unit = new SalesTax(); // ARRANGE double tax = unit.Calculate(10); // ACT Assert.assertEquals(tax, 0.9); // ASSERT } }
  • 13. Unit Testing Best Practices ● Test simple stuff first ● Get it working - then test boundaries/exceptions ● Use assert ● Keep tests small & understandable ● Make test method names descriptive ● Keep tests independent of each other ● Avoid System.out messages ● Don’t repeat tests! Test once and trust it.
  • 14. Test Doubles ● Stand in for a collaborating object e.g. database, webservice ● Create test independence ● Make tests run faster ● Types of Doubles ○ Fakes: working implementation e.g.fake web service ○ Mocks: preprogrammed with expectations ○ Stubs: provide canned answers to the test’s calls
  • 16. Refactoring ● What is refactoring? ● Why refactor? ● When to refactor?
  • 17. What is Refactoring? “A change to the system that leaves its behavior unchanged, but enhances some non-functional quality - simplicity, flexibility, understandability, performance” -- Kent Beck, Extreme Programing Explained (p. 179) “A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.” -- Martin Fowler, Refactoring (p. 53)
  • 18. Why Refactor the Code? ● Prevent “design decay” ● Clean up messes ● Simplify ● Increase readability / understandability ● Find bugs ● Reduce debugging time ● Build in what we learn about the application ● It’s part of the creative process
  • 19. When to Refactor ● Code reviews / TDD cycle ● Rule of three ○ cut & paste. Third time copying? Now is the time to generalize and move this to a new procedure. ● When you: ○ add functionality ○ learn something about the code ○ identify a code smell
  • 20. What is a Code Smell? ● any symptom in the code that possibly indicates a deeper problem. ● not bugs - but indicate weaknesses in design that may be slowing down development or increasing the risk of bugs or failures in the future.
  • 21. Code Smells (1/2) ● Duplicated code: near-identical code in multiple places ● Long method: a too large method/function/procedure ● Large class: a class that has grown too large. ● Too many parameters: decreases readability/quality. ● Identifier names are too short or excessively long ● Complex conditionals: branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code.
  • 22. Code Smells (2/2) ● Comments - refactor so these are no longer needed! ● Feature envy: excessive use of another class’ methods ● Inappropriate intimacy: a class that has dependencies on implementation details of another class. ● Lazy class / Freeloader: a class that does too little. ● Contrived complexity: simpler design would suffice. ● Excessive use of literals (use named constants)
  • 23. Why Developers Resist Refactoring ● Lack of understanding ● Short-term focus ● Not paid for overhead tasks like refactoring ● Fear of breaking current program Use TDD to overcome this fear!
  • 25. TDD as a Design Approach ● Tests define up front what “works” means ● Programing by intention - write tests for what code intends to do not what the code you just wrote does. ● Focus on minimum set of required features ● Regular integration of small changes ● Evolutionary Design: Tests provide a safety net for refactoring.
  • 27. How to Get to Green? Implement it! Obvious Implementation code the real implementation, if it's obvious and can quickly make test pass, otherwise... public int Sum(int x, int y) { return x + y; }
  • 28. How to Get to Green? Fake it Fake it hardcode constants to make test pass, gradually generalize code using variables public int Fibonacci(int n) { if (n >= 1) return 1; return 0; }
  • 29. How to Get to Green? Triangulate Triangulate figure out the behavior of an algorithm by examining a couple of test cases instead of just one. public void TestSum() { Assert.AreEqual(4, Plus(3, 1)); Assert.AreEqual(7, Plus(4, 3)); }
  • 30. Use TDD to Earn a Gift Card! TDD is a great tool to use in solving the code challenges. E.g. Function takes a Roman Numeral String and returns the Decimal value. ● Start with simple valid numbers I, III... up to more complex tests ● Incorporate tests for boundary conditions and invalid numbers ● With a set of tests you can easily refactor your solution. VALID NUMBERS INVALID NUMBERS I Boundary: Empty, 0, MMMM III iii IV IVIV, IIII, IIX, MXXXX VI, XLIX, MMMDCCCLXXXVIII ABCDEF
  • 31. A Great TDD Example! ● Bowling Kata - how to score a bowling game. ● Eclipse and junit step-by-step presentation on red - green - refactor to solve (butunclebob. com/files/downloads/Bowling%20Game%20Kata.ppt)
  • 33. ● Collaborative Design ● Good coding Standards and Principles Agile Architecture
  • 34. Collaborative Design ● Emergent Design ○ Light Design Up Front (LDUF) ● Every team member contributes ● No command and control
  • 35. Collaborative Design - payoff http://martinfowler.com/bliki/DesignStaminaHypothesis.html
  • 36. Standards and Principles ● Agree as a team ○ coding and naming conventions ○ toolset ■ Static Code Analysis, Test mocks, etc ○ best practices ● Avoid Technical Debt ● Follow well established principles and patterns
  • 37. Object Oriented Design Principles S ingle Responsibilty O pen/Closed L iskov Substitution I nterface Segregation D ependency Inversion
  • 38.
  • 39. The Single Responsibility Principle ● Every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility. ● Single Reason to Change
  • 40. class Book { function getTitle() { return "A Great Book"; } function getAuthor() { return "John Doe"; } function turnPage() { // pointer to next page } function printCurrentPage() { echo "current page content"; } }
  • 41.
  • 42. The Open/Closed Principle ● You should be able to extend a classes behavior without modifying it ● Open for extension, but closed for modification
  • 43. public double Area(object[] shapes) { double area = 0; foreach (var shape in shapes) { if (shape is Rectangle) { Rectangle rectangle = (Rectangle) shape; area += rectangle.Width*rectangle.Height; } else { Circle circle = (Circle)shape; area += circle.Radius * circle.Radius * Math.PI; } } return area; }
  • 44.
  • 45. The Liskov Substitution Principle ● Derived classes must be substitutable for their base classes
  • 46. public class Rectangle { private int width; private int height; // setters and getters } public class Square extends Rectangle { public Square(int height, int width) { setWidth(width); setHeight(height); } }
  • 47.
  • 48. The Interface Segregation Principle ● No client should be forced to depend on methods it does not use
  • 49. interface Worker { void work(); void eat(); } class HumanWorker implements Worker { Public void work { /* do work stuff */ } Public void eat { /* eat stuff */ } } class RobotWorker implements Worker { Public void work { /* do work stuff */ } Public void eat { /* ACK! I don't eat! */ } } interface Worker { void work(); } interface Eater { void eat(); } class HumanWorker implements Worker, Eater { Public void work { /* do work stuff */ } Public void eat { /* eat stuff */ } } class RobotWorker implements Worker { Public void work { /* do work stuff */ } }
  • 50.
  • 51. The Dependency Inversion Principle ● High-level modules should not depend on low-level modules. Both should depend on abstractions. ● Abstractions should not depend on details. Details should depend on abstractions.
  • 52. class Worker { private FileWriter fileWriter; public Worker() { fileWriter = new FileWriter(); } } class Worker { private FileWriter fileWriter; public Worker(FIleWriter writer) { fileWriter = writer; } } interface FileWriter { // writer files or something }
  • 53. Object Orient Patterns ● Common Design Patterns ○ Strategy, Command, Decorator, Adapter, etc ● Enterprise Integration Patterns
  • 54. Acknowledgements Pair Programming: ● http://www.cs.pomona.edu/classes/cs121/supp/williams_prpgm.pdf ● http://collaboration.csc.ncsu.edu/laurie/Papers/ieeeSoftware.PDF SOLID ● http://lostechies.com/derickbailey/2009/02/11/solid-development-principles-in-motivational- pictures/ Content based on: ● SolutionsIQ CSD training slides ● http://en.wikipedia.org/wiki/Code_smell ● http://www.codeproject.com/Articles/5404/The-benefits-of-automated-unit-testing