You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue outlines several refactoring techniques applied to different methods in the code. Each technique aims to improve code readability, maintainability, and robustness.
Decompose Conditional
Problem: The method isSuitableForVm has a complex conditional expression in its return statement, making the code hard to understand and maintain.
Refactoring: The conditional expression is broken down into individual methods with descriptive names, making it easier to understand and maintain.
Original:
Replace Conditional with Polymorphism
Problem: The method processEvent uses a switch statement with multiple cases to handle different event types.
Refactoring: Subclasses are created to represent different types of events and a general interface is used. The shared method is moved to the corresponding subclasses.
Original Code:
public void processEvent(SimEvent ev) {
int srcId = -1;
switch (ev.getTag()) {
case CloudSimTags.RESOURCE_CHARACTERISTICS:
srcId = (Integer) ev.getData();
sendNow(srcId, ev.getTag(), getCharacteristics());
break;
// other cases
default:
processOtherEvent(ev);
break;
}
}
Refactored Code:
public interface EventProcessor {
void process(SimEvent ev);
}
public class ResourceCharacteristicsProcessor implements EventProcessor {
@Override
public void process(SimEvent ev) {
int srcId = (Integer) ev.getData();
sendNow(srcId, ev.getTag(), getCharacteristics());
}
}
// Other EventProcessor implementations
public void processEvent(SimEvent ev) {
EventProcessor processor = EventProcessorFactory.getProcessor(ev.getTag());
if (processor != null) {
processor.process(ev);
} else {
processOtherEvent(ev);
}
}
Extract Variable
Problem: The method getNumHop contains a complex expression that may be difficult to understand.
Refactoring: Intermediate variables are introduced to make the code clearer.
Original Code:
public int getNumHop() {
int PAIR = 2;
return ((hopsNumber - PAIR) + 1) / PAIR;
}
Refactored Code:
public int getNumHop() {
int pair = 2;
int adjustedHopsNumber = hopsNumber - pair;
return (adjustedHopsNumber + 1) / pair;
}
Replace Error Code with Exception
Problem: The method isFileValid handles invalid file cases with warnings instead of throwing exceptions.
Refactoring: Exceptions are used to handle invalid file cases, making error handling more explicit.
Hi, @dquishpe, feel free to open a PR regarding your proposal so that we may discuss it (however, make sure you rebase your changes on the new cloudsim7g; You will have to update some method names, check the paper here
This issue outlines several refactoring techniques applied to different methods in the code. Each technique aims to improve code readability, maintainability, and robustness.
Problem: The method isSuitableForVm has a complex conditional expression in its return statement, making the code hard to understand and maintain.
Refactoring: The conditional expression is broken down into individual methods with descriptive names, making it easier to understand and maintain.
Original:
Refactored Code:
Problem: The methods vmDeallocate and vmDeallocateAll perform similar actions with only differences in their internal operations.
Refactoring: The methods are combined using a parameter that specifies the operation to be performed.
Original Code:
Refactored Code
Problem: The method processEvent uses a switch statement with multiple cases to handle different event types.
Refactoring: Subclasses are created to represent different types of events and a general interface is used. The shared method is moved to the corresponding subclasses.
Original Code:
Refactored Code:
Problem: The method getNumHop contains a complex expression that may be difficult to understand.
Refactoring: Intermediate variables are introduced to make the code clearer.
Original Code:
Refactored Code:
Problem: The method isFileValid handles invalid file cases with warnings instead of throwing exceptions.
Refactoring: Exceptions are used to handle invalid file cases, making error handling more explicit.
Original Code:
Refactored Code:
The text was updated successfully, but these errors were encountered: