Rewrite Vs Refactor
@BagmarAnand
Anand Bagmar
Software Quality Evangelist, Essence Of Testing
About Me
Anand Bagmar
EssenceOfTesting.com
@BagmarAnand
Rewrite Vs Refactor
@BagmarAnand
We will focus on code for
Test Automation
@BagmarAnand
Applicable for any type of
code you write
@BagmarAnand
@BagmarAnand
https://www.liberaldictionary.com/wp-content/uploads/2018/11/begin.jpg
Case Study #1
@BagmarAnand
• Revamp Testing Practices
• Automate everything!
@BagmarAnand
Charter
https://i.pinimg.com/474x/ec/8a/17/ec8a176036490a3338f806172a23e4b3.jpg
@BagmarAnand
Assessment
https://media.istockphoto.com/vectors/assessment-infographic-with-8-steps-parts-options-vector-id640310008
• Validate the charter!
• Understand by investigation:
• What is working well,
• The challenges, and,
• The (real & false) constraints
@BagmarAnand
Assessment: Investigation
https://media.istockphoto.com/vectors/assessment-infographic-with-8-steps-parts-options-vector-id640310008
• Speak with the team members
• Code reviews
• Run the tests yourself!
• Validate / Challenge Assumptions
• Challenges
• Setup / Infra issues
@BagmarAnand
Assessment: Finding
https://media.istockphoto.com/vectors/assessment-infographic-with-8-steps-parts-options-vector-id640310008
• No CI
• Automation working with lot of
manual interventions
• Setup / execution all over the
place
• Large & separate codebase
• Revamp Testing Quality Practices
• Automate everything that makes sense to automate!
@BagmarAnand
Evolved Charter
https://storage.needpix.com/rsynced_images/smile-2352472_1280.png
Case Study #2
@BagmarAnand
• Fast feedback (from 1000s of automated UI tests)
• Clarity of intent – know what is being tested
• Maintainable & Scalable Automation
@BagmarAnand
Charter
https://i.pinimg.com/474x/ec/8a/17/ec8a176036490a3338f806172a23e4b3.jpg
@BagmarAnand
Assessment: Investigation
https://media.istockphoto.com/vectors/assessment-infographic-with-8-steps-parts-options-vector-id640310008
• Speak with the team members
• Code reviews
• Run the tests yourself!
• Validate / Challenge Assumptions
• Challenges
• Setup / Infra issues
@BagmarAnand
Assessment: Finding
• Automation code quality is sub-par
• Excessive duplication, massive files, no encapsulation
• Excessive use of “sleep”
• Extremely complex code
• No test execution trend analysis
• Too much focus on UI / end-2-end automation
• No idea what is being tested (without reading a lot of code)
• Fast feedback (from 1000s of automated UI tests)
• Clarity of intent – know what is being tested
• Maintainable & Scalable Automation
• Focus on automating tests at lower level
• Unit Test Automation
• API / Component Test Automation
• API Workflow Automation
@BagmarAnand
Evolved Charter
https://storage.needpix.com/rsynced_images/smile-2352472_1280.png
Details of the Analysis
@BagmarAnand
Evolution of the code-base
@BagmarAnand
package HelloWorld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
@BagmarAnand
Hello World
@BagmarAnand
https://cdn.makeuseof.com/wp-content/uploads/2016/07/cleaner-better-code-670x335.jpg
Actual, complex code-base
http://2.bp.blogspot.com/-ntk4QrnZT48/UoYoBG6XNlI/AAAAAAAANx0/Rtjng1VZFQY/s1600/LabVIEW_spaghetti_code.png
@BagmarAnand
In few cases ….
@BagmarAnand
@BagmarAnand
Start afresh
package HelloWorld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
http://coastalbend.edu/uploadedImages/CBC/Content/Instructional_Services/Dual_Credit/hard_working_on_computer_anim_500_clr_7364.gif
In most cases ….
@BagmarAnand
https://149351115.v2.pressablecdn.com/wp-content/uploads/2018/08/codebase_project_management-1200x675.png
@BagmarAnand
Work on existing code-base
http://2.bp.blogspot.com/-ntk4QrnZT48/UoYoBG6XNlI/AAAAAAAANx0/Rtjng1VZFQY/s1600/LabVIEW_spaghetti_code.png
Where you will ….
@BagmarAnand
https://149351115.v2.pressablecdn.com/wp-content/uploads/2018/08/codebase_project_management-1200x675.png
@BagmarAnand
Extend & evolve such code
http://2.bp.blogspot.com/-ntk4QrnZT48/UoYoBG6XNlI/AAAAAAAANx0/Rtjng1VZFQY/s1600/LabVIEW_spaghetti_code.png
https://cdnb.artstation.com/p/assets/images/images/020/039/203/large/wesam-ab-img-20190814-163328-163.jpg?1566108077
@BagmarAnand
On closer look
https://149351115.v2.pressablecdn.com/wp-content/uploads/2018/08/codebase_project_management-1200x675.png
@BagmarAnand
Multiple copies of such code
http://2.bp.blogspot.com/-ntk4QrnZT48/UoYoBG6XNlI/AAAAAAAANx0/Rtjng1VZFQY/s1600/LabVIEW_spaghetti_code.png
• Multiple repos
• Multiple and long living branches
So ….
@BagmarAnand
What is the typical
reaction when you see
existing code?
@BagmarAnand
http://cdn.tinybuddha.com/wp-content/uploads/2015/11/Exploding-Head.jpg
@BagmarAnand
Why this reaction?
@BagmarAnand
Complexity
@BagmarAnand
https://lifeboat.com/blog.images/overlooked-brain-region-key-to-complex-thought2.jpg
Complexity of thought process
@BagmarAnand
https://cdn.makeuseof.com/wp-content/uploads/2016/07/cleaner-better-code-670x335.jpg
Complexity in code
@BagmarAnand
http://www.upcreative.net/wp-content/uploads/2014/04/javascript-spaghetti-code.jpg
@BagmarAnand
Unstructured (Spaghetti) Code
https://40rik02ft2xye26xv2i0y0yc-wpengine.netdna-ssl.com/wp-content/uploads/2016/04/AdobeStock_99227409-1110x699.jpeg
Documentation – sparse / outdated
@BagmarAnand
• It is important to have lean and meaningful
documentation
• Do not just add comments in code for the sake of
“documenting the implementation logic” – your code
should be readable and understandable
Perspection
@BagmarAnand
https://i.ytimg.com/vi/9n2tmNWn1ec/maxresdefault.jpg
@BagmarAnand
Perception – State of Mind
https://pngimage.net/wp-content/uploads/2018/05/emotions-facebook-png-1.png
@BagmarAnand
Perception – Emotional state
https://i.ytimg.com/vi/L6Edb1-l5x0/maxresdefault.jpg
@BagmarAnand
Perception – Point of view
https://worrylessjourney.files.wordpress.com/2018/07/perspective.jpg
@BagmarAnand
Perception – Experience
https://webstockreview.net/images/diversity-clipart-business-group-10.png
@BagmarAnand
Perception – Experience from Diversity
We Learn Continuously
@BagmarAnand
What was good yesterday,
may not necessarily be
good today as well!
@BagmarAnand
http://www.hptx.org/images/InfoAdvanced/70/brain%20gears.png
https://spotifyhrblog.files.wordpress.com/2017/01/people-growth-company-growth.png
@BagmarAnand
Experiences change us
https://orlandoespinosa.files.wordpress.com/2018/11/learn-from-mistakes-orlando-espinosa.png
@BagmarAnand
Learn from past experiences
https://empxtrack.com/wp-content/uploads/2016/05/come_join_the_team_500_wht_10876.gif
@BagmarAnand
@BagmarAnand
Changes in Team Dynamics
• People leave
• New people join
Typical challenges /
constraints
@BagmarAnand
http://2.bp.blogspot.com/-ntk4QrnZT48/UoYoBG6XNlI/AAAAAAAANx0/Rtjng1VZFQY/s1600/LabVIEW_spaghetti_code.png
If you need to work on
such code ….
@BagmarAnand
https://marquardtlawfirm.files.wordpress.com/2016/03/istock_where-do-i-start.jpg
@BagmarAnand
When you find where
change needs to happen,
then…
@BagmarAnand
@BagmarAnand
What is the impact of change going to be?
http://textbook.stpauls.br/Business_Organization/images/pic311.jpg
Pressure of getting it done
on Time!
https://newsatjama.files.wordpress.com/2018/07/hourglass-clock-on-light-blue-background-thinkstockphotos-479876312.jpg?w=1200
https://miro.medium.com/max/5600/1*frbsX4eY4Sj2mla2vszKPA.jpeg
@BagmarAnand
Common anti-patterns
@BagmarAnand
@BagmarAnand
Anti-pattern: Forget past lessons learnt
https://storage.needpix.com/rsynced_images/reminder-23771_1280.png
@BagmarAnand
Anti-pattern: Taking shortcuts
https://grahamchastney.files.wordpress.com/2018/06/20180612_111442516_ios.jpg
@BagmarAnand
Anti-pattern: Reinvent the wheel
https://external-preview.redd.it/ZVPjiFo_Ubl4JeiU63SaTjdIoq5zveSnNZimKpgn2I8.png?auto=webp&s=bf32c94b630befa121075c1ae99b2599af6dedc5
• Don’t want to modify existing code
@BagmarAnand
Anti-pattern: Reinvent the wheel
https://storage.needpix.com/rsynced_images/traveller-3213992_1280.jpg
• Unable to find what you were looking for
@BagmarAnand
Anti-pattern: Copy-Paste
https://tr1.cbsistatic.com/hub/i/r/2019/02/12/ac377858-9c14-44f0-9560-
c9c737b60248/resize/770x/2a82e9d0d67fe2091050585de6c0006a/istock-122227621copypaste.jpg
https://upload.wikimedia.org/wikipedia/commons/thumb/7/76/Evil_red.svg/1200px-Evil_red.svg.png
@BagmarAnand
Anti-pattern: Sleep to reduce flakiness
https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Zzz_sleep.svg/1024px-Zzz_sleep.svg.png
https://upload.wikimedia.org/wikipedia/commons/thumb/7/76/Evil_red.svg/1200px-Evil_red.svg.png
https://storage.needpix.com/rsynced_images/snail-2383080_1280.jpg
Why do we end up using
these anti-patters?
@BagmarAnand
Because …. It (still) works!
https://www.linkedin.com/posts/bhagyaitrecruiteratgmail_activity-6642620864221274112-NnWI
@BagmarAnand
@BagmarAnand
Let someone else figure out a better way… later
https://lh3.googleusercontent.com/proxy/MuzrRuna5ts_O-FX_KPUcsC-2SA_blkGTlwPUBfLS9uWo11NN5RLhI8dUBucVrGUtTvQs5FP-AGlBrLTXI0VnZzBT0C30E7HvWtOHyqd8TP3QIeJCg
Refactoring
@BagmarAnand
https://en.m.wikipedia.org/wiki/Code_refactoring
@BagmarAnand
Refactoring is an essential
practice when evolving
existing code
@BagmarAnand
But … we may end up
creating another mess IF
@BagmarAnand
@BagmarAnand
Refactoring without clear objective
https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRPV-74mTtTdR8VcenNpVhYM5AQq7PWuEkr2hW5jTlrqnaFfBrQ
As a result
@BagmarAnand
@BagmarAnand
Tech Debt
https://caylent.com/wp-content/uploads/2018/02/Technical-debt-featured-image2.png
@BagmarAnand
Automation does not provide enough value!
https://4.bp.blogspot.com/-dgoCWivfsb8/XDcqaFvQC2I/AAAAAAAAF2o/qz5zcKCf9lcD6ExCo3p16YsFCwkQsCxjgCLcBGAs/s1600/DRC-falling-robot.png
• Difficult to add new features / tests
• Difficult to scale
• Supposedly simple changes take long time, and have
unexpected behavior
@BagmarAnand
Manual, repeated effort keeps increasing
http://logico4576.blogspot.com/2012/03/bertrand-russell-para-que-repetir-los.html
Report the facts!
@BagmarAnand
https://i.kinja-img.com/gawker-media/image/upload/s--JRWxEP3V--/c_scale,f_auto,fl_progressive,q_80,w_800/18h5yv82d3yxzjpg.jpg
@BagmarAnand
Assessment – The Summary!
• Testing was QA team responsibility
• Automation not adding any value
@BagmarAnand
Assessment – The Report!
@BagmarAnand
Assessment – The Report!
@BagmarAnand
Overall challenges from Testing perspective
• Process / Way of Working & Ownership of Testing
• Understanding of Automation
• Experience & Skillset
• Coding skills
• Time pressure
• CI
• Device & Driver management
• Run on demand, on QA machine
• Slow (100s of sleeps)
• Static methods, no-OOPS
• Sequential
• Branching nightmare!
@BagmarAnand
Reality of existing Automated Tests
https://i.stack.imgur.com/SDmtn.png
Code Analysis & Execution
@BagmarAnand
@BagmarAnand
Test Code Snippets
• Methods returning default value
• Extremely complex method (deep nesting of loops)
• Lack of naming conventions
• Swallowing of exceptions, returning Null
• Sleeps, static members / methods
@BagmarAnand
Test Code Snippets
@BagmarAnand
Excessive cyclomatic
complexity in method:
46 warnings
Java analysis:
10 errors, 6212
warnings
Test Code Analysis
@BagmarAnand
Excessive cyclomatic
complexity in method:
126 warnings
Java analysis:
20 errors, 10355
warnings
Test Code Analysis
@BagmarAnand
CI status
How to turn the ship
around in such cases?
@BagmarAnand
https://upload.wikimedia.org/wikipedia/commons/7/72/Container_ship_Reecon_Whale_on_Black_Sea_near_Constan%C8%9Ba_Romania.jpg
Provide solutions
@BagmarAnand
https://i.kinja-img.com/gawker-media/image/upload/s--JRWxEP3V--/c_scale,f_auto,fl_progressive,q_80,w_800/18h5yv82d3yxzjpg.jpg
@BagmarAnand
How do we achieve our objectives?
https://www.expandgh.com/wp-content/uploads/2018/04/Set-Objectives-for-Each-Person.jpg
@BagmarAnand
How do we achieve our objectives?
https://www.expandgh.com/wp-content/uploads/2018/04/Set-Objectives-for-Each-Person.jpg
Know your objectives first!
• Fast in execution and feedback
• Independent tests
• Executed on every change
• Know what is being tested, as the customer / consumer
• Able to release with confidence!
@BagmarAnand
Expectations from Automated Tests
@BagmarAnand
Approach
• Increase awareness
• Upskill (pairing, training)
• Evolved way-of-working
• In initial stages, you may want to consider Automation
rewrite as a separate, independent project
@BagmarAnand
Approach - Automation
• Identify Design Patterns that will help
• Setup Coding guidelines and coding styles
• Use powerful IDEs
• Automate Infrastructure setup for test execution
• Keep Tests Independent
95
Criteria for automating
tests scenarios
@BagmarAnand
https://chiefexecutive.net/wp-content/uploads/2019/02/AdobeStock_82846171.jpeg
@BagmarAnand
What are you automating?
• Know your users
• Simulate user behavior
@BagmarAnand
Criteria for Implementing Automation
• Clear & visible intent of the automated test
• Multi-browser & multi-device support
• Run on every (local & deployed) change (local, CI & cloud)
• Fast feedback (parallel execution)
• Rich execution reports & Trend Analysis
• https://www.infoq.com/articles/test-automation-ai-ml/
Rewrite or Refactor?
@BagmarAnand
https://lh3.googleusercontent.com/proxy/3JFWJGTlmTym_g9xCZpHIFNLN3rQZu3js8f7iJDHFc6CCeTDb4hpLJaaQm1YCe2BfxyuA_7Lv3B4Clt7MpICbe_Ay09TY8k
Will team be able to refactor,
while other projects are going on,
and get value from automation,
the way it is supposed to be?
@BagmarAnand
https://lh3.googleusercontent.com/proxy/3JFWJGTlmTym_g9xCZpHIFNLN3rQZu3js8f7iJDHFc6CCeTDb4hpLJaaQm1YCe2BfxyuA_7Lv3B4Clt7MpICbe_Ay09TY8k
Get buy-in!
@BagmarAnand
https://i.kinja-img.com/gawker-media/image/upload/s--JRWxEP3V--/c_scale,f_auto,fl_progressive,q_80,w_800/18h5yv82d3yxzjpg.jpg
https://empresas.blogthinkbig.com/wp-content/uploads/2019/11/Tecnicas-negociaci%C3%B3n.jpg
@BagmarAnand
My approach
“Rewrite with Selective Reuse”
@BagmarAnand
Lead by Example!
https://miro.medium.com/max/900/1*eFnlzb6ZEWsehg1i3RTA6g.png
@BagmarAnand
Approach for Rewrite with Reuse …
• Formed team of 2 people initially
• Automated test execution environment setup script
• Created new repository
• Implemented 2 tests
• Parallel execution
• Triggered automatically via CI
• No manual setup / intervention
@BagmarAnand
Approach for Rewrite with Reuse
• Used tools / frameworks / libraries that can achieve the
objectives of the test
• Build Vs Buy / Reuse
• Hard discussions around duplication Vs complexity of
specific modules – ex: test data setup for product
• Rewrite everything, or reuse some existing
implementation?
@BagmarAnand
Framework Architecture
• IDEs provide you lot of information about the code
automatically
• Additional plugins can be installed / used
• Free / Open-Source plugins may also be available
• Static Code Analysis can be setup in VCS / CI pipelines
@BagmarAnand
Maintain Code Quality
@BagmarAnand
Remember, and watch out for …
https://i.pinimg.com/originals/0e/0b/1f/0e0b1f8c2b36404e4740b6f733097f19.jpg
Thank you!
Anand Bagmar
EssenceOfTesting.com
@BagmarAnand

Rewrite vs Refactor (AgileIndia 2021)