Some programming teams fail to write high-quality code while trying to meet the software development deadline. This is counterproductive. It doesn’t matter how visually appealing a new application may appear; if the underlying code is hard to understand, it will be challenging to maintain, update and scale it.
It is overly simplistic to judge code as ‘good’ or ‘bad.’ Code should be rated relative to its context. Moreover, almost all code can be improved over time, particularly as programming languages evolve and new solutions emerge.
We’re sure you would have many questions about how you can write clean and effective code for test automation.
But worry not! Our special guests Sai Krishna, Lead Consultant, ThoughtWorks, and Srinivasan Sekar, Lead Consultant, ThoughtWorks, have teamed up with Manoj Kumar, VP Developer Relations at LambdaTest to share their tips and to answer your questions on how to build a clean and effective test automation framework! They have also discussed implementing clean code principles like SOLID and DRY and seeing how test automation can improve.
In case you missed the power-packed webinar, let us look at the major highlights of the event.
About the Webinar:
The webinar starts with Manoj asking our speakers a question: “What do they think about 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 ask for their take on this as consultants.
Here is the video recording of our webinar to familiarize yourself with the powerful knowledge bombs it contains!
Srinivasan agrees with Manoj on how QAs and tester’s 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 the code, whether production or development, is not just the QA’s job. As per him, this gives the team a sense of why testing code is crucial.
Sai adds to this that even time to market plays a significant role. He explains that if we are looking for a shorter time to market, our automated tests should have the right principles and practices to enhance the application code. He adds that the automation testing framework should be robust, and the production and test code should be equally treated.
Sai then takes the lead of the webinar by explaining what they will address 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 the best practices for writing clean and effective test code.
Moving forward, Srinivasan explains what a clean code looks like. He adds that a clean code should eliminate code smells such as dispensable, code comments, duplicated code, and more. Srinivasan tells the audience they will look at bloaters such as large class, long method, and too many parameters.
Moving on, he informs the audience about the principles they will explain – The Single Responsibility Principle and the Open-closed Principle.
Sai then showcases the live code refactoring demo of a sample WebDriver script.
Following the Clean Code Principles:
Srinivasan goes on to explain the Single Responsibility Principle. As per him:
- Single Responsibility Principle states that every class, module, or function should 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, “every class should have only one reason to change.” Sai then explains the concept of SRP with the help of code.
Srinivasan also explains the open-closed principle. He says:
- Open-closed Principle is a central concept in object-oriented programming. He states that software entities should be open for extension but closed for modification or that such an entity can allow its behavior to be extended without modification of source code.
Best Practices for Writing Clean Code:
Moving forward, Sai highlights the best clean code practices. He highlights the following practices:
- Keep it Simple: Sai says that the best code is easy to read and understand. He suggests the audience write the least amount of code possible to solve any given problem and avoid duplicating code to make it easier to navigate and maintain. It’s also good manners to use reader-friendly formatting for your code so that future users can quickly understand how it works.
- Use Meaningful Names: Sai suggests that because your code will contain many variables and functions, you should choose clear, descriptive names for them. He adds that the names should reveal intent and be a verb or noun to be easy to pronounce and search for. He recommends not using any slang or offensive words and using one word per concept. Whenever possible, stick to conventional standards to make your code easier to read.
- Respect the Single Responsibility Principle: Sai suggests that to help you identify problems in your code, it is important that you know what every class, function, or method does. This will help you keep your functions small and ensure they are easy to test for errors. 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 confuse and make maintaining the code harder. Remember, the best code doesn’t need to be explained. He thinks that if you find yourself writing many comments, it’s better to rewrite the code.
- Striving for Improvement: As per 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 with time, you’ll start finding ways to optimize your code that even experts don’t know about, giving you more personal satisfaction and making you a valuable member of any software development team.
Before wrapping up, Sai and Srinivasan answered several questions the viewers raised. Here are some of the insightful questions from the session:
How do QAs contribute to the code architecture?
Srinivasan answers the question by raising an initial point in the discussion that the code’s architecture is not just a QAs responsibility. He suggests that people can invite developers and get their help in designing the architecture. He adds that pairing with the developers would help understand the design patterns and solve problems.
How often should code refactoring be considered?
To answer this, Srinivasan says you should consider refactoring the code if it is not readable. If you want to improve the design, add a new feature, or fix bugs, then refactoring should be done. Srinivasan says one should prefer code refactoring for code reviews and even for eliminating code smells.
To add to this, Sai recommends following the “boy’s scout rule,” which implies reading the code better than how you found it. He says while doing this; if you come across any code smell, you can refactor it then and there.
Hope You Enjoyed The Webinar!
We hope you liked the webinar. In case you missed it, please find the webinar recording above. Make 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 newsletter Coding Jag to stay on top of everything testing and more!
That’s all for now, happy testing!
Published on Java Code Geeks with permission by Devansh Bhardwaj, partner at our JCG program. See the original article here: Webinar: Clean Coding Practices for Test Automation
Opinions expressed by Java Code Geeks contributors are their own.