Skip to content

Code Style

williamlixu edited this page Aug 11, 2019 · 5 revisions

General

In general, we follow Google's Java Style Guide. This should basically be the same as the coding style used in previous projects and courses like SOFTENG 206, 251 and 254. It matches standard Java conventions. Also, we use Project Lombok which reduces boilerplate code like the need to write getters and setters. Writing standard Java without Lombok is also fine!! Code should be consistent in our project regardless of who is writing it. Important things to take note of and differences are highlighted below.

Field Names

Field names should NOT have underscores. Use this when the field being used belongs to the class and there is room of uncertainty. For example, a constructor for MyClass with fields a and b would look like this:

public MyClass(int a, int b) {
    this.a = a;
    this.b = b;
}

Project Lombok

Project Lombok has annotations and keywords to reduce the need to write verbose and tedious code. To get it to work, you must have the Project Lombok extension installed on your IDE (I think everyone uses IntelliJ and the extension works fine). You can "lombokify" and "delomokify" the code with the extension to make it look like normal Java to help understand it if it becomes confusing. Writing code without Lombok is also fine, it's just to make some things easier if you want!! The main features used in this project that everyone should be familiar with are:

  • var and val are used to derive the type of local variables from the initialize expression. val also sets the final modifier on the variable.

  • @data is used for plain old Java objects and includes the features of @ToString, @EqualsAndHashCode, @Getter, @Setter and @RequiredArgsConstructor. It basically makes the fields of the class private and creates getters and setters (called getFieldName() (or isFieldName() for booleans) and setFieldName() and an straightforward implementation of the toString(), equals() and hashCode() functions. It also creates a constructor which takes in all fields as arguments and sets them.

  • @value is like @data but makes the class immutable. The fields are declared private and final. The class is also declared as final so it cannot be extended.

For more information about Project Lombok, see this list of all Project Lombok features.

Clone this wiki locally