Java Coding Standards

Example

Note: WE NEED A GOOD EXAMPLE for the main method! This one breaks a lot of the rules… please feel free to insert a good example.

// start out with your package name
package com.example;

// next import things you need from the Selenium test framework
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
import org.testng.TestNG;

// import extra things you want to use from Java
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        try {
            driver.get("https://google.com/ncr");
            driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);
            WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("h3>div")));
            System.out.println(firstResult.getAttribute("textContent"));
        } finally {
            driver.quit();
        }
    }
}

Principles

  • *Declare variables at the top of the file* and declare methods below the last variable. This makes it easy to add new ones and keep them organized, which can be helpful in seeing the file’s functionality at a glance. (KyleRoth)
    • List public variables and methods above private or protected ones. When developers look through your class, there’s a good chance they’re most interested in what functions are available for use from another class. Making this the first information they see helps with that. (KyleRoth)
    • Consider using protected variables and methods over private ones. Private restricts permission only to that class, while protected allows classes which inherit from it to use those variables and functions. This can help to make your code future proof in ways you didn’t anticipate. (KyleRoth)
    • No hard-coded values inside the methods! (StanVogel)
      • Examples:
        private final double Pi = 3.1415926536;
        private final double Pi = 3.1415926536;
  • Use meaningful and distinct names for methods and variables.
    • An example of not doing this would be a method I came across called "formReeqQeury", whose function was not obvious and name provided little insight. For variables, on web apps sometimes you may have two pages with submit buttons, but if the names are not distinct you may not be able to tell which variable corresponds to which page. (LeslieReis)
  • If you find yourself writing the same exact lines of code or performing the same set of actions in more than one place within your code, extract the common code into a well-named method, e.g.
    enterCredentialsAndLogin()
    and call that method when performing the common action or set of actions. (StanVogel)
  • Use comments to help people understand how scenario steps connect to GlueCode. (LeslieReis, StanVogel)
    • This can especially be helpful when coding a workflow that executes a sequence of steps, as it can help you quickly distinguish what parts of the code correspond to each step. (LeslieReis)
    • Given the proper choice of variable names, method names, and general structuring of code, it should be fairly obvious what any given block of code is doing. You can test this out by asking someone to read your code and explain what it does to you. If they can't easily do that, you need to improve your comments. (StanVogel)
    • Comments are useful for explaining business reasons or non-obvious technical reasons for making assumptions, doing something that would appear arbitrary, or otherwise performing non-obvious things. (StanVogel)
  • Use whitespace and indentation to make code readable. (LeslieReis)

Naming Conventions

Entity Format Example
Constant UPPERCASE PI
Package name lowercase mypackage
Class name CamelCase BasePage
Method name mixedCase enterCredentials()

-- GenericUltranaut - 14 Apr 2020
Topic revision: r3 - 16 Nov 2022, NicoleRadziwill
© 2020 Ultranauts - 75 Broad Street, 2nd Floor, Suite 206, New York, NY 10004 - info@ultranauts.co