SlideShare uma empresa Scribd logo
1 de 31
Practicing Red, Green, Refactor!
Held by, Ahmed Helmy
Ahmed Helmy
@helmy204
www.linkedin.com/in/helmy204
www.github.com/helmy204
Exercise in Self Organization
Hello everyone!
Line up and organize yourself into a single file line per
programming language from the most experience to the least
experience in coding
When you are done let me know.
Development Team
When you wake up in the morning and
you come in to work, you say, “what is
the focus – are we trying to ship or are
we trying to write code?” The answer is
we are trying to ship. You’re not trying to
write code, you’re trying not to write
code.
Former Microsoft Program Manager, Chris Peters
What is eXtreme
Programming?
XP aims to produce higher quality
software using appropriate engineering
practices
Agile Software Development Methodology
Lightweight Efficient Low-risk
XP Practices
Shared
understanding
Fine-scale
feedback
Continuous
process
Programmer
welfare
XP Practices
• Fine-scale feedback
• Pair programming
• The Planning Game
• Testing
• On-site customer
• Continuous process
• Continuous integration
• Refactoring
• Small releases
• Shared understanding
• Coding standards
• Collective ownership
• Simple design
• Metaphor
• Programmer welfare
• 40 hour week
XP Practices
Let’s Discuss
What is testing? And what are the testing types?
TDD
Red, Green, Refactor
Baby-steps Kata
•Pair up
•Design a system that generates an even number.
•Ends in 2.
•Greater than 9.
•Less than 100.
•Adding both digits should result in 6.
Pair Programming
• Two people write code together on one machine
https://martinfowler.com/articles/on-pair-programming.html
FizzBuzz Kata
BREAK!
GildedRose Kata
• Clone GildedRose Kata Repository
• C#: https://github.com/NotMyself/GildedRose
• Other: https://github.com/emilybache/GildedRose-Refactoring-Kata
• Gathering Requirements (readme file)
• Add tests based on requirements
• Add tests based on code coverage report
• Refactoring
• Adding new behavior
• Extract class • Simplify arithmetic
• Extract constant strings • Simplify Booleans
• Extract constant numbers • Group related logic
• Extract methods • Rename
GildedRose Kata
• Add tests based on requirements
Do you smell something?
<code />
[Fact]
public void Given_regular_item_SellIn_and_Quality_lower()
{
IList<Item> items =
new List<Item> { new Item { Name = "Regular Item", SellIn = 15, Quality = 25 } };
Program app = new Program() { Items = items };
app.UpdateQuality();
Assert.Equal(14, items[0].SellIn);
Assert.Equal(24, items[0].Quality);
}
[Fact]
public void Given_regular_item_past_SellIn_Then_Quality_degrades_twice_as_fast()
{
IList<Item> items =
new List<Item> { new Item { Name = "Regular Item", SellIn = 0, Quality = 25 } };
Program app = new Program() { Items = items };
app.UpdateQuality();
Assert.Equal(23, item.Quality);
}
Duplicated Code!
How can we fix this duplication code smell
GildedRose Kata
• Add tests based on requirements
Let’s Discuss
Are we done yet?
Can we now safely add new functionality?
Test Coverage
• Measure how much of your code base is exercised by your unit test suite
• A code base with less than 100% coverage does not necessarily mean that the code lacks
quality.
• A code base with 100% coverage ensures only that its quality is as good as the tests that
exercise it.
• A better measure of code coverage is whether the right code is covered by tests!
• This examination will always reveal one of two outcomes
• You are missing a test
• The code is unneeded and should be deleted (NOT COMMENTED)
GildedRose Kata
• Add tests based on requirements
• Add tests based on code coverage report
Time to Refactor!
What other code smells are there?
<code />
Large Class! More responsibilities!
Program.cs
namespace GildedRose.Console
{
public class Program
{
...
}
public class Item
{
public string Name { get; set; }
public int SellIn { get; set; }
public int Quality { get; set; }
}
}
Program.cs
namespace GildedRose.Console
{
public class Program
{
…
public void UpdateQuality()
{
…
}
}
public class Item
{
…
}
}
What other code smells are there?
<code />
Magic Strings!
public void UpdateQuality()
{
for (var i = 0; i < Items.Count; i++)
{
if (Items[i].Name != "Aged Brie" && Items[i].Name != "Backstage passes to a TAFKAL80ETC concert")
{
if (Items[i].Quality > 0)
{
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
{
Items[i].Quality = Items[i].Quality - 1;
}
}
}
else
{
…
}
}
}
What other code smells are there?
<code />
Magic Numbers!
public void UpdateQuality()
{
for (var i = 0; i < Items.Count; i++)
{
if (Items[i].Name != "Aged Brie" && Items[i].Name != "Backstage passes to a TAFKAL80ETC concert")
{
…
}
else
{
if (Items[i].Quality < 50)
{
Items[i].Quality = Items[i].Quality + 1;
…
}
}
}
What other code smells are there?
<code />
Nested Conditionals! Long Function!
public void UpdateQuality()
{
for (var i = 0; i < Items.Count; i++)
{
if (item.Name != "Aged Brie" && item.Name != "Backstage passes to a TAFKAL80ETC concert")
{
if (item.Quality > 0)
{
if (item.Name != "Sulfuras, Hand of Ragnaros")
{
item.Quality = item.Quality - 1;
}
}
}
else
{
…
}
}
}
Still Complex!
Code Smells and Anti-Code Smells
Code Smells Anti-Code Smells
Mysterious name Clear naming
Duplicated code Extract function
Long method Extract function
Nested conditionals Polymorphism
Speculative generality Change function declaration
Large class Extract class
Comments Extract function
Add New Behavior
Questions
Thank You
Ahmed Helmy
@helmy204
www.linkedin.com/in/helmy204
www.github.com/helmy204

Mais conteúdo relacionado

Semelhante a Practicing Red, Green, Refactor!

LINQ Inside
LINQ InsideLINQ Inside
LINQ Inside
jeffz
 

Semelhante a Practicing Red, Green, Refactor! (20)

To successfully deliver your IT project: build your team, build your Agile it...
To successfully deliver your IT project: build your team, build your Agile it...To successfully deliver your IT project: build your team, build your Agile it...
To successfully deliver your IT project: build your team, build your Agile it...
 
Automated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and ChallengesAutomated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and Challenges
 
Tech talks#6: Code Refactoring
Tech talks#6: Code RefactoringTech talks#6: Code Refactoring
Tech talks#6: Code Refactoring
 
If you want to automate, you learn to code
If you want to automate, you learn to codeIf you want to automate, you learn to code
If you want to automate, you learn to code
 
Write tests, please
Write tests, pleaseWrite tests, please
Write tests, please
 
GIDS13 - Building Service for Any Clients
GIDS13 - Building Service for Any ClientsGIDS13 - Building Service for Any Clients
GIDS13 - Building Service for Any Clients
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - Greach
 
Tech talk on code quality
Tech talk on code qualityTech talk on code quality
Tech talk on code quality
 
Microservices Chaos Testing at Jet
Microservices Chaos Testing at JetMicroservices Chaos Testing at Jet
Microservices Chaos Testing at Jet
 
Developing a Culture of Quality Code (Midwest PHP 2020)
Developing a Culture of Quality Code (Midwest PHP 2020)Developing a Culture of Quality Code (Midwest PHP 2020)
Developing a Culture of Quality Code (Midwest PHP 2020)
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnit
 
Useful practices of creation automatic tests by using cucumber jvm
Useful practices of creation automatic tests by using cucumber jvmUseful practices of creation automatic tests by using cucumber jvm
Useful practices of creation automatic tests by using cucumber jvm
 
LINQ Inside
LINQ InsideLINQ Inside
LINQ Inside
 
Coding Naked 2023
Coding Naked 2023Coding Naked 2023
Coding Naked 2023
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Kku2011
Kku2011Kku2011
Kku2011
 
Swift meetup22june2015
Swift meetup22june2015Swift meetup22june2015
Swift meetup22june2015
 
Anatomy of Test Driven Development
Anatomy of Test Driven DevelopmentAnatomy of Test Driven Development
Anatomy of Test Driven Development
 
A journey to_be_a_software_craftsman
A journey to_be_a_software_craftsmanA journey to_be_a_software_craftsman
A journey to_be_a_software_craftsman
 
Apex Unit Testing in the Real World
Apex Unit Testing in the Real WorldApex Unit Testing in the Real World
Apex Unit Testing in the Real World
 

Mais de XPDays

Mais de XPDays (20)

Change the Conversation! Unleash Your Potential in a Complex World.pptx
Change the Conversation! Unleash Your Potential in a Complex World.pptxChange the Conversation! Unleash Your Potential in a Complex World.pptx
Change the Conversation! Unleash Your Potential in a Complex World.pptx
 
Agile Culture Transformation
Agile Culture TransformationAgile Culture Transformation
Agile Culture Transformation
 
Re-engineering Technology to break barriers with Business
Re-engineering Technology to break barriers with BusinessRe-engineering Technology to break barriers with Business
Re-engineering Technology to break barriers with Business
 
Ready, Steady, Sprint
Ready, Steady, SprintReady, Steady, Sprint
Ready, Steady, Sprint
 
The Whole Story of The User Story
The Whole Story of The User StoryThe Whole Story of The User Story
The Whole Story of The User Story
 
Scrum Master Facilitation Techniques
Scrum Master Facilitation TechniquesScrum Master Facilitation Techniques
Scrum Master Facilitation Techniques
 
Unit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and MoqUnit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and Moq
 
An Introduction to The Cynefin Framework
An Introduction to The Cynefin FrameworkAn Introduction to The Cynefin Framework
An Introduction to The Cynefin Framework
 
Team Mental Health
Team Mental HealthTeam Mental Health
Team Mental Health
 
Business Analyst in the Agile Space
Business Analyst in the Agile SpaceBusiness Analyst in the Agile Space
Business Analyst in the Agile Space
 
DevOps in action - Azure DevOps
DevOps in action - Azure DevOpsDevOps in action - Azure DevOps
DevOps in action - Azure DevOps
 
Priotrization techniques
Priotrization techniquesPriotrization techniques
Priotrization techniques
 
Scaled Agile Framework
Scaled Agile FrameworkScaled Agile Framework
Scaled Agile Framework
 
Building Team Habits
Building Team HabitsBuilding Team Habits
Building Team Habits
 
4 Keys to Success in your Agile Journey
4 Keys to Success in your Agile Journey4 Keys to Success in your Agile Journey
4 Keys to Success in your Agile Journey
 
Coaching stances
Coaching stancesCoaching stances
Coaching stances
 
Re-focus for Agile leaders
Re-focus for Agile leadersRe-focus for Agile leaders
Re-focus for Agile leaders
 
Business Decomposition
Business DecompositionBusiness Decomposition
Business Decomposition
 
Agile projects | Prioritization
Agile projects | PrioritizationAgile projects | Prioritization
Agile projects | Prioritization
 
Scaling Agile | Spotify
Scaling Agile | SpotifyScaling Agile | Spotify
Scaling Agile | Spotify
 

Último

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Último (20)

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 

Practicing Red, Green, Refactor!

  • 1. Practicing Red, Green, Refactor! Held by, Ahmed Helmy
  • 3. Exercise in Self Organization Hello everyone! Line up and organize yourself into a single file line per programming language from the most experience to the least experience in coding When you are done let me know.
  • 4. Development Team When you wake up in the morning and you come in to work, you say, “what is the focus – are we trying to ship or are we trying to write code?” The answer is we are trying to ship. You’re not trying to write code, you’re trying not to write code. Former Microsoft Program Manager, Chris Peters
  • 5. What is eXtreme Programming? XP aims to produce higher quality software using appropriate engineering practices Agile Software Development Methodology Lightweight Efficient Low-risk
  • 7. XP Practices • Fine-scale feedback • Pair programming • The Planning Game • Testing • On-site customer • Continuous process • Continuous integration • Refactoring • Small releases • Shared understanding • Coding standards • Collective ownership • Simple design • Metaphor • Programmer welfare • 40 hour week
  • 9. Let’s Discuss What is testing? And what are the testing types?
  • 11. Baby-steps Kata •Pair up •Design a system that generates an even number. •Ends in 2. •Greater than 9. •Less than 100. •Adding both digits should result in 6.
  • 12. Pair Programming • Two people write code together on one machine https://martinfowler.com/articles/on-pair-programming.html
  • 15. GildedRose Kata • Clone GildedRose Kata Repository • C#: https://github.com/NotMyself/GildedRose • Other: https://github.com/emilybache/GildedRose-Refactoring-Kata • Gathering Requirements (readme file) • Add tests based on requirements • Add tests based on code coverage report • Refactoring • Adding new behavior • Extract class • Simplify arithmetic • Extract constant strings • Simplify Booleans • Extract constant numbers • Group related logic • Extract methods • Rename
  • 16. GildedRose Kata • Add tests based on requirements
  • 17. Do you smell something? <code /> [Fact] public void Given_regular_item_SellIn_and_Quality_lower() { IList<Item> items = new List<Item> { new Item { Name = "Regular Item", SellIn = 15, Quality = 25 } }; Program app = new Program() { Items = items }; app.UpdateQuality(); Assert.Equal(14, items[0].SellIn); Assert.Equal(24, items[0].Quality); } [Fact] public void Given_regular_item_past_SellIn_Then_Quality_degrades_twice_as_fast() { IList<Item> items = new List<Item> { new Item { Name = "Regular Item", SellIn = 0, Quality = 25 } }; Program app = new Program() { Items = items }; app.UpdateQuality(); Assert.Equal(23, item.Quality); } Duplicated Code! How can we fix this duplication code smell
  • 18. GildedRose Kata • Add tests based on requirements
  • 19. Let’s Discuss Are we done yet? Can we now safely add new functionality?
  • 20. Test Coverage • Measure how much of your code base is exercised by your unit test suite • A code base with less than 100% coverage does not necessarily mean that the code lacks quality. • A code base with 100% coverage ensures only that its quality is as good as the tests that exercise it. • A better measure of code coverage is whether the right code is covered by tests! • This examination will always reveal one of two outcomes • You are missing a test • The code is unneeded and should be deleted (NOT COMMENTED)
  • 21. GildedRose Kata • Add tests based on requirements • Add tests based on code coverage report
  • 23. What other code smells are there? <code /> Large Class! More responsibilities! Program.cs namespace GildedRose.Console { public class Program { ... } public class Item { public string Name { get; set; } public int SellIn { get; set; } public int Quality { get; set; } } } Program.cs namespace GildedRose.Console { public class Program { … public void UpdateQuality() { … } } public class Item { … } }
  • 24. What other code smells are there? <code /> Magic Strings! public void UpdateQuality() { for (var i = 0; i < Items.Count; i++) { if (Items[i].Name != "Aged Brie" && Items[i].Name != "Backstage passes to a TAFKAL80ETC concert") { if (Items[i].Quality > 0) { if (Items[i].Name != "Sulfuras, Hand of Ragnaros") { Items[i].Quality = Items[i].Quality - 1; } } } else { … } } }
  • 25. What other code smells are there? <code /> Magic Numbers! public void UpdateQuality() { for (var i = 0; i < Items.Count; i++) { if (Items[i].Name != "Aged Brie" && Items[i].Name != "Backstage passes to a TAFKAL80ETC concert") { … } else { if (Items[i].Quality < 50) { Items[i].Quality = Items[i].Quality + 1; … } } }
  • 26. What other code smells are there? <code /> Nested Conditionals! Long Function! public void UpdateQuality() { for (var i = 0; i < Items.Count; i++) { if (item.Name != "Aged Brie" && item.Name != "Backstage passes to a TAFKAL80ETC concert") { if (item.Quality > 0) { if (item.Name != "Sulfuras, Hand of Ragnaros") { item.Quality = item.Quality - 1; } } } else { … } } }
  • 28. Code Smells and Anti-Code Smells Code Smells Anti-Code Smells Mysterious name Clear naming Duplicated code Extract function Long method Extract function Nested conditionals Polymorphism Speculative generality Change function declaration Large class Extract class Comments Extract function