Skip to main content

Code Smells and Refactoring


Knowing or unknowingly we all introduce code smell in our test automation code and thus I feel that after every 3-4 sprints, there should be a dedicated sprint for Refactoring for our test automation code. It's a very important part of any software development and thus we should constantly review our code for bad design and try to chuck out any kind of code smell.

Code Smells:
Code smells and anti-patterns are usually not bugs and they do not currently prevent the program from functioning. In-fact, they indicate poor design and implementation in software that may be increasing the risk of failures in the future. And thus these are technical debt.

Refactoring:
Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure — Martin Fowler


Here are the common causes of Code smells:

1) Comments
If you feel like writing comments for all the classes and methods, first try to refactor it as your code should be self-explanatory and self-documenting to the max-content. The naming convention should be strong- Names assigned to variables, methods, and classes should be clear and meaningful.

2) Large Classes
Large classes containing too many methods and properties are very hard to understand and maintain due to the complex structure.

3) Shotgun Surgery (Ripple effect) 
A change in one place requires us to alter many different classes.

4) Duplicated code
Same code structure in more than one place which in turn will also call the Ripple effect.

5) Incorrect locator strategy
Instead of the ones provided by a browser (Dev Tools) in the form of a "Copy full XPath", "Copy XPath", etc., use reliable locators such as id or name, etc. if possible otherwise write your own XPath.

6) Static waits
Hard-coded wait like thread.sleep lets the test pause for n number of seconds. It slows down your test execution time. Use smart waits like Explicit and Fluent as these wait for only the amount of time that is actually required.


Go through this insightful article by Mikhael Levkovsky to know more about Code smell:
https://hackernoon.com/5-easy-wins-to-refactor-even-the-worst-legacy-code-7vuc3069

Code climate is one such static quality analysis open-source project that analyzes your repositories from Github, BitBucket and other platforms and displays the number of Code Smells.

You can buy this book if you are interested in learning more about Refactoring:
https://www.amazon.com/Refactoring-Improving-Existing-Addison-Wesley-Signature/dp/0134757599/ref=sr_1_1?keywords=Refactoring%3A+Improving+the+Design+of+Existing+Code&qid=1579624156&s=books&sr=1-1

Comments

Popular posts from this blog

ARIA Snapshot in Playwright

  What is an ARIA Snapshot in Playwright? An  ARIA snapshot  in Playwright is a structured representation of a page’s  accessibility tree , which is used by assistive technologies (e.g., screen readers) to interpret the content of a web page. This snapshot helps verify if elements have the correct  roles, names, and properties  required for accessibility. Playwright provides the page.accessibility.snapshot() API to capture this accessibility tree at any given moment during test execution. How Does ARIA Work? ARIA ( Accessible Rich Internet Applications ) is a set of attributes that help improve accessibility by defining roles, states, and properties for elements that are not natively accessible. Example: In this case, the aria-label ensures that screen readers identify the button as “Submit Form.” How to Use ARIA Snapshots in Playwright? Playwright’s  accessibility.snapshot()   method retrieves the  accessible structure  of the page. Ex...

Bruno vs Postman: Which API Client Should You Choose?

  As API testing becomes more central to modern software development, the tools we use to test, automate, and debug APIs can make a big difference. For years, Postman has been the go-to API client for developers and testers alike. But now, Bruno , a relatively new open-source API client, is making waves in the community. Let’s break down how Bruno compares to Postman and why you might consider switching or using both depending on your use case. ✨ What is Bruno? Bruno is an open-source, Git-friendly API client built for developers and testers who prefer simplicity, speed, and local-first development. It stores your API collections as plain text in your repo, making it easy to version, review, and collaborate on API definitions. 🌟 What is Postman? Postman is a full-fledged API platform that offers everything from API testing, documentation, and automation to mock servers and monitoring. It comes with a polished UI, robust integration, and support for collaborati...

🔧 Self-Healing Selenium Automation with Java — A Smarter Way to Handle Broken Locators

  How to build smarter, more resilient automated tests? We’ve all been there — our Selenium test cases start failing because of minor UI changes like updated element IDs, renamed classes, or even reordered elements. It’s frustrating, time-consuming, and often the most dreaded part of maintaining automated tests. But what if your automation could heal itself? 💡 What is Self-Healing Automation? Self-healing automation  refers to the capability of a test automation framework to recover from minor UI changes by automatically trying alternative locators when the primary one fails. It’s like giving your test scripts a survival instinct. 🔨 🛠️ Implementation in Java + Selenium: Step by Step Step 1: Create a Self-Healing Wrapper We start by creating a custom class called SelfHealingDriver. This class wraps the standard WebDriver and handles locator failures gracefully. public   class   SelfHealingDriver { private   WebDriver driver ; public   SelfHealingDri...