We don't need them after class declaration or method signature.
- Bad example:
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
- Improved example:
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
- Bad example:
public User createUser(String name, int age) {
User user = new User(name, age);
return user;
}
- Improved example:
public User createUser(String name, int age) {
return new User(name, age);
}
Static methods are in general a bad practice. Let's better create an instance of a class which method you want to call.
- Bad example:
public class UserService {
public static User findByEmail(String email) {
//some implementation
}
}
public class Main {
public static void main(String[] args) {
User user = UserService.findByEmail("[email protected]");
}
}
- Improved example:
public class UserService {
public User findByEmail(String email) {
//some implementation
}
}
public class Main {
public static void main(String[] args) {
UserService userService = new UserService();
User user = userService.findByEmail("[email protected]");
}
}
- Bad example:
public class Main {
public static void main(String[] args) {
String[] emails = {"[email protected]", "not@[email protected]"};
for(String email : emails) {
if (new EmailValidator().isValid(email)) {
System.out.println("Email " + email + " is valid");
}
}
}
}
- Improved example:
public class Main {
public static void main(String[] args) {
String[] emails = {"[email protected]", "not@[email protected]"};
EmailValidator emailValidator = new EmailValidator();
for(String email : emails) {
if (emailValidator.isValid(email)) {
System.out.println("Email " + email + " is valid");
}
}
}
}
- Bad example:
public class AccauntService {
public int calculateTax(int income) {
TaxService taxService = new TaxService();
int tax = taxService.getTax();
return income * tax / 100;
}
}
- Improved example:
public class AccauntService {
private TaxService taxService = new TaxService();
public int calculateTax(int income) {
int tax = taxService.getTax();
return income * tax / 100;
}
}
https://mate-academy.github.io/style-guides/java/java.html#s5.2.4-constant-names
Classes, fields, constructors, methods must have access modifiers otherwise default will be used that isn't a good practice.
For example, you need to create several users and write them in an array. In our case let's say number of users is 3.
- Bad example:
public class Main {
private static final int NUMBER_OF_USERS = 3;
public static void main(String[] args) {
User[] users = new User[NUMBER_OF_USERS];
User firstUser = new User();
User secondUser = new User();
User thirdUser = new User();
users[0] = firstUser;
users[1] = secondUser;
users[2] = thirdUser;
}
}
- Improved example:
public class Main {
private static final int NUMBER_OF_USERS = 3;
public static void main(String[] args) {
User[] users = new User[NUMBER_OF_USERS];
for(int i = 0; i < users.length; i++) {
users[i] = new User();
}
}
}
Please see this article to learn about constant fields and their naming requirements.
- Bad example:
public class FigureSupplier {
private Random random = new Random();
public Figure getRandomFigure() {
int `figureNumber` = random.nextInt(5);
// generate a specific figure based on the `figureNumber` value
}
}
- Improved example:
public class FigureSupplier {
private static final int FIGURE_COUNT = 5;
private Random random = new Random();
public Figure getRandomFigure() {
int figureNumber = random.nextInt(FIGURE_COUNT);
// generate a specific figure based on the `figureNumber` value
}
}
Don't use toString()
or String.valueOf()
(it will call toString()
under the hood) for getting String
representation of enum constants.
toString()
is common for all enum constants. If you override this method like below:
@Override
public String() toString() {
return "default";
}
then for every constant toString()
will be returning default
, that's not ok. So it's better to use standard method of enum name()
that will be returning always String
representation of concrete enum constant.
Bad example of commit/PR message: done
/fixed
/commit
/solution
/added homework
/my solution
and other one-word, abstract or random messages.