-
Notifications
You must be signed in to change notification settings - Fork 4
Code Style
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 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 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:
-
varandvalare used to derive the type of local variables from the initialize expression.valalso sets thefinalmodifier on the variable. -
@datais used for plain old Java objects and includes the features of@ToString,@EqualsAndHashCode,@Getter,@Setterand@RequiredArgsConstructor. It basically makes the fields of the classprivateand creates getters and setters (calledgetFieldName()(orisFieldName()for booleans) andsetFieldName()and an straightforward implementation of thetoString(),equals()andhashCode()functions. It also creates a constructor which takes in all fields as arguments and sets them. -
@valueis like@databut makes the class immutable. The fields are declaredprivateandfinal. The class is also declared asfinalso it cannot be extended.
For more information about Project Lombok, see this list of all Project Lombok features.