Webinar: Clean Coding Practices for Test Automation – Java Code Geeks5 min read
Some programming teams fail to write high-quality code while trying to meet a software development deadline. This is counterproductive. It doesn’t matter how attractive a new app may look; if the underlying code is difficult to understand, it will be a challenge to maintain, update, and scale it.
It is too simplistic to judge code as “good” or “bad”. Code should be evaluated against its context. Also, almost all code can be improved over time, especially as programming languages evolve and new solutions emerge.
We’re sure you’ll have a lot of questions about how you can write clean and efficient test automation code.
But don’t worry! Our special guests Sai Krishna, Lead Consultant, ThoughtWorks, and Srinivasan Sekar, Lead Consultant, ThoughtWorks, teamed up with Manoj Kumar, VP of Developer Relations at LambdaTest to share their tips and answer your questions on how to build a clean and an effective test automation framework! They have also discussed the application of clean code principles such as HARD and DRYING and see how test automation can be improved.
In case you missed the powerful webinar, let’s take a look at the main highlights of the event.
About the webinar:
The webinar begins with Manoj posing a question to our speakers: “What do they think of the test automation code? Is it at the same level as development code?” Manoj shares how testers and QA tasks are not prioritized among companies and asks them for their opinion on this as consultants.
Here is the video of our webinar so you can familiarize yourself with the power bombs of knowledge it contains!
Srinivasan agrees with Manoj about how QA and tester tasks are considered unimportant and points out that while consulting, they always emphasize that quality is everyone’s responsibility. He adds that ensuring the quality of code, whether production or development, is not just a QA job. According to him, this gives the team an idea of why code testing is critical.
Sai adds that even time-to-market plays an important role. He explains that if we are looking for faster time to market, our automated tests must have the right principles and practices to improve the application code. He adds that the framework for automated testing should be robust, and production and test code should be treated equally.
Sai then takes over the webinar, explaining what they will cover in the webinar. He adds that for this session, they will explain code refactoring with a live WebDriver script, show how to eliminate code smell, and discuss best practices for writing clean and efficient test code.
Moving on, Srinivasan explains what clean code looks like. He adds that clean code should eliminate unpleasant code smells such as irreplaceables, code comments, duplicate code, and others. Srinivasan tells the audience that they will see bloat like a large class, a long method, and too many parameters.
Moving on, he informs the audience of the principles that will explain – The principle of single responsibility and on Open-closed principle.
Sai then demonstrated the live code refactoring demo on a WebDriver sample script.
Following the principles of clean code:
Srinivasan goes on to explain the principle of single responsibility. According to him:
- Principle of single responsibility states that each class, module, or function must have one and only one responsibility. He adds that a class should not be responsible for more than one task. As a commonly used definition, “each class should have only one reason to change”. Sai then explains the concept of SRP using code.
Srinivasan also explains the open-closed principle. He says:
- Open-closed principle is a central concept in object-oriented programming. It states that software objects must be open to extension but closed to modification, or that such an object can allow its behavior to be extended without source code modification.
Best practices for writing clean code:
Moving forward, Sai highlights best practices for clean code. It highlights the following practices:
- Don’t make it complicated: Sai says the best code is easy to read and understand. It suggests to the audience to write the least amount of code possible to solve a given problem and avoid code duplication to make it easier to navigate and maintain. It’s also good manners to use readable formatting for your code so that future users can quickly understand how it works.
- Use meaningful names: Sai suggests that since your code will contain many variables and functions, you should choose clear, descriptive names for them. He adds that names should convey intent and be a verb or noun to be easy to pronounce and search. He recommends not using slang or offensive words and using one word per concept. Whenever possible, stick to conventional standards to make your code easier to read.
- Follow the principle of single responsibility: Sai suggests that to help you identify problems in your code, it’s important to know what each class, function, or method does. This will help you keep your features small and ensure they are easy to test for bugs. It will also make them easier for other developers to read and use.
- Use comments: Comments are valuable for providing support and information to other developers who may use your code. But too many comments can get confusing and make code more difficult to maintain. Remember, the best code doesn’t need to be explained. He thinks that if you find yourself writing a lot of comments, you’re better off rewriting the code.
- Pursuit of improvement: According to Sai, the best way to improve the quality of your code is to practice writing it and learn from more experienced developers. He adds that over time you’ll start to find ways to optimize your code that even the experts don’t know about, which will give you more personal satisfaction and make you a valuable member of any software development team.
Question and Answer Session:
Before wrapping up, Sai and Srinivasan answered a few questions from the audience. Here are some of the insightful questions from the session:
How do QAs contribute to code architecture?
Srinivasan answers the question by raising a starting point in the discussion that code architecture is not just a QA responsibility. It suggests that people can invite developers and get their help in designing the architecture. He adds that pairing with developers will help understand design patterns and solve problems.
How often should code refactoring be considered?
To answer this question, Srinivasan says you should consider refactoring the code if it’s not readable. If you want to improve the design, add a new feature or fix bugs, then refactoring should be done. Srinivasan says that one should prefer code refactoring to code reviews and even to eliminating code smells.
To add to that, Cy recommends following the “Boy Scout Rule,” which involves reading the code better than the way you found it. He says as he does this; if you come across any code smell, you can refactor it then and there.
We hope you enjoyed the webinar!
We hope you enjoyed the webinar. In case you missed it, please find the webinar recording above. Be sure to share this webinar with anyone who wants to learn more about clean coding practices for test automation. Stay tuned for more exciting LambdaTest webinars. You can also subscribe to our Coding Jag newsletter to keep up with all the testing and more!
That’s all for now, happy testing!