Topics

Important Points

Java Dynamic Binding

Review Dynamic Binding from CS2030S

Java Enumeration

An Enumeration is a fixed set of values that can be considered as a data type. An enumeration is often useful when using a regular data type such as int or String would allow invalid values to be assigned to a variable.

For example, you want to define an enumeration to represent days of a week. In your Day.java, you can define the following,

Day.java
public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY
}

Java enumerations are objects of their own enum type. So, in this case, each constant in the enum (like SUNDAY, MONDAY, etc.) is actually an instance of the enum type (Day in our case). Under the hood, the Java compiler creates something like a class with fixed static final objects.

So, in our Main.java, we can use the enumerations as follows,

Main.java
Day today = Day.MONDAY;
Day[] holidays = new Day[]{Day.SATURDAY, Day.SUNDAY};

switch (today) {
case SATURDAY:
case SUNDAY:
    System.out.println("It's the weekend");
    break;
default:
    System.out.println("It's a week day");
}

SWE Regression

When you modify a system, the modification may result in some unintended and undesirable effects on the system. Such an effect is called a regression.

Here are some tips for this week's code quality task:

  1. Avoid long methods: A method is long if it is more than 30 LoC.

  2. Avoid deep nesting: An easier way to figure nesting is to see the indentation.

  3. Avoid complicated expressions: This often applies in the boolean expressions, e.g., try to give reasonable names for the boolean expression!

  4. Avoid magic numbers: Give the magic number a meaningful name.

  5. Avoid premature optimization: Make it work, make it right, make it fast.

  6. Avoid having multiple levels of abstraction within a code fragment: This is guided by the rule called SLAP (Single Level of Abstraction Principle)

SWE Code Quality: Refactoring

The process of restructuring code in small steps without modifying its external behavior is called refactoring.

Classic Questions

1

Regression Testing

Regression testing is the automated re-testing of a software after it has been modified.


It is "Partially true" because regression testing need not be automated but automation is highly recommended.

2

Refactoring and Regresstion Testing

Do you agree with the following statement? Justify your answer.

Whenever you refactor code to fix bugs, you need not do regression testing if the bug fix was minor.


Disagree. Because,

  1. Even a minor change can have major repercussions on the system. You MUST do regression testing after each change, no matter how minor it is.

  2. Fixing bugs is technically not refactoring.

Last updated