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.
You can define an enum type by using the enum keyword. Because they are constants, the names of an enum type's fields are in uppercase letters e.g., FLAG_SUCCESS by convention.
For example, you want to define an enumeration to represent days of a week. In your Day.java, you can define the following,
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,
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.
Regression testing is the re-testing of the software to detect regressions. The typical way to detect regressions is retesting all related components, even if they had been tested before.
Here are some tips for this week's code quality task:
Avoid long methods: A method is long if it is more than 30 LoC.
Avoid deep nesting: An easier way to figure nesting is to see the indentation.
Avoid complicated expressions: This often applies in the boolean expressions, e.g., try to give reasonable names for the boolean expression!
Avoid magic numbers: Give the magic number a meaningful name.
Avoid premature optimization: Make it work, make it right, make it fast.
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.
In short, it is just make your code work first, then improve your code quality step by step.
Classic Questions
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,
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.
Fixing bugs is technically not refactoring.
Last updated