Code Review

Code review is examination (often known as peer review) of computer source code to check whether the written code follows the standards.

Code review is intended to find and fix mistakes overlooked in the initial development phase, improving both the overall quality of software and the developers' skills.

Code review is one of the important part in selenium webdriver code. Below is sample example of mistake normally we make, most of us donot know that this is a bug.

										boolean flag = true;
										if(flag == true)
											System.out.println("flag is true");

You should never compare the boolean values because the result also will be boolean only, Let's take a real example of selenium webdriver

For Example : You are using .isDisplayed() method on a element which returns a boolean value (true/false).

Now if you compare the returned boolean value with true or false, the result will be either true or false.

										boolean display = driver.findElement("123")).isDisplayed();
										if(display == true){
											// you could have assert statement instead of prints
											System.out.println("Element is displayed");
											System.out.println("Element is ot present");

When you get result again as true or false, then there is no point to compare. Because conditional statements always works based on true or false.

So instead of comparing the boolean values, you can use them directly on the conditional statements.

We can re-write above example as below, and you will get the same result as above

										boolean display = driver.findElement("123")).isDisplayed();
											// you could have assert statement instead of prints
											System.out.println("Element is displayed");
											System.out.println("Element is ot present");

Code review items on Java and selenium Webdriver

Below are few thing we need to consider in code review taks.

1. Class and method size: Big classes and long methods tend to be more brittle.

2. In general, look for aggregation over implementing interfaces, and interfaces over inheritance

3. Especially if you're doing TDD or disciplined unit testing, look for interfaces for most major classes; this makes it easier to build mock objects and to change implementations later.

4. Every class and method should have Javadoc, Javadocs should meet standards.

5. Avoid complicated compound statements, and conditional statements

6. "Fluent" method names, that is, you want to be able to read the methods as sentences. "if object is initialized" should read if(obj.isInitialized())".

7. Comments reflect what the code does.

8. Code does what's expected functionally

9. Adherence to standards and guidelines (This means you should have coding standards set for your project)

10. You should avoid writing repetetive code.

11. Variable names should give the intention of the variable

12. Exception handling. I prefer to use the Logger to log instead of ex.printStackTrace()

13. Remove unused methods/variables

14. Look for potential cause of NullPointerException.

15. Should avoic compring boolean values

16. You should create a method whenever you face repeating steps/code. Somtimes you may have 6 steps in a testcase but 5 steps out of 5 would be matching with steps in other testcase, In such cases write a method for the 5 steps and form the testcase by combining the method and the test step

17. Try to use the library code as much as possible

18. Use finally block whenever required.

19. Do foloow the case convention according to your project

20. Comment about the code not only what the code does but also why the code is like that.

21. Use String Buffer or Builder instead of String.

22. Use explicit wait instead of Implicit wait

23. user navigate().to() rathar than driver.get() method.

24. Try to avoid nested loops

25. Have separate files for different functionalities like move handling excel sheet to different file.

Tips to code reviewer

1. Critique code instead of people – be kind to the coder, not to the code.

2. Treat people who know less than you with respect, deference, and patience

3. The only true authority stems from knowledge, not from position.

4. Please note that Review meetings are NOT problem solving meetings.

5. Ask questions rather than make statements.

6. Avoid the “Why�? questions.

7. Remember to praise, if somebody writes good code

8. Make sure you have good coding standards to reference

9. Remember that there is often more than one way to approach a solution

10. You shouldn’t rush through a code review - but also, you need to do it promptly.

11. Review fewer than 200-400 lines of code at a time.

12. If somebody comes new to team, teach him instead of expectig him to do all the stuff.

Tools to Improve Java Code Quality

Every developer makes mistake and it is common. Usually, the compiler catches the syntactic(syntax related) and arithmetic issues and lists out a stack trace.

But there still might be some issues that compiler does not catch. These could be inappropriately implemented requirements, incorrect algorithm, bad code structure or some sort of potential issues that community knows from experience.

The only way to catch such mistakes is to have some senior developer to review your code

With each new developer in the team, you should have an extra pair of eyes which will look at his/her code.

But luckily there are many tools which can help you control the code quality including SonarLint, FindBugs, SonarQube etc.

All of them are usually used to analyze the quality and build some useful reports. Very often those reports are published by continuous integration servers, like Jenkins.

In this tutorial we would be learning how to install sonarlint, sonarQube.

SonarLint with Selenium Webdriver

SonarLint extension in eclipse is used by coders(dev / qa) to get instantaneous feedback for proactive code quality while coding.

SonarLint lives only in the IDE (IntelliJ, Eclipse and Visual Studio) and it concentrates on what code you are adding or updating.

SonarLint feature is available for Java, Javascript, and PHP developers to try for themselves. SonarLint offers a fully-integrated user experience in Eclipse-based IDEs.

After installing the plugin issues will be reported as Eclipse markers. SonarLint is free, open source, and available in the Eclipse Marketplace.

SonarLint shows you issues as you code. It subtly points out new issues so that you can still focus on coding.

SonarLint lists issues found in all the files that you added and updated.

SonarLint provides explanations to help understand issues found and why it is a problem.

Install SonarLint in Eclipse for Selenium Webdriver

We can install SonarLint to eclipse for selenium webdriver in many ways. After installing the plugin, it will enable common rule set defined by the SonarLint standalone, Note that, SonarLint will require Java 8 to run.

1. Open Eclipse and Navigate to Help > Eclipse Market Place

2. Search for SonarLint and click install
3. Confirm the installation of sonarlint into eclipse
4. Accept the licence (opensource), and eclipse will as you to restart the Eclipse

5. Now you will be able to see the errors and suggestion by the sonarlint about the code. sonarlint-suggestion-selenium-webdriver
6. You can right click on any File/Project you want to scan, and you can use SonarLint option either to scan or to exclude the scanning. analyze-sonarlint-selenium-webdriver
7. Sonarlint give detailed report about the error and details about which are the lines causing the error.
8. We can also view the Rule description on the Description tab.
Developer can improve the code quality by fixing those issues mentioned with SonarLint on time.

Join My Facebook Group
Join Group

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Selenium-Webdriver.Com [Group]
Facebook Group · 801 members
Join Group
Learn Selenium Webdriver Concepts and Interview Questions Learn and code
Copyright © CherCher Tech