So I have just gotten a bunch of old code where I should improve the performance of. After looking for a while I came accross this piece of code:
//Reference on how the global Variables look like:
private static final int PLS_4_SYSTEM_MALFUNCTION_FL = 4;
private static final int SPS1_20_PUFFER_EMPTY = 20;
private static final int SPS1_30_STOPPERFL_LOCKED = 30;
//...
int disturbance1 = bMessage.getDisturbanceSps1().intValue();
int disturbance2 = bMessage.getDisturbanceSps1().intValue();
if (disturbance1 == SPS1_20_PUFFER_EMPTY && disturbance2 == 0) {
checkWarnings(bMessage, PLS_5_PUFFER_EMPTY);
}
if (disturbance1 == SPS1_30_STOPPERFL_LOCKED && disturbance2 == 0) {
checkWarnings(bMessage, PLS_6_STOPPERFL_LOCKED);
}
if (disturbance1 == SPS1_40_DISTURBANCEFL && disturbance2 == 0) {
if (bMessage.getDisturbanceType().intValue() == DISTURBANCETYPE_SYSTEMDISTURBANCE_SHORT) {
checkWarnings(bMessage, PLS_3_SHORTDISTURBANCE_FL);
}
else if (bMeldung.getStoerungsartMde().intValue() == DISTURBANCETYPE_SYSTEMDISTURBANCE_LONG) {
checkWarnings(bMessage, PLS_4_SYSTEMMALFUNCTION_FL);
}
}
//...
This is just a small example of the code. There are like 300 more lines that goes on like this.
Now I know that there are many answers on the web already, and I have looked at some of them, but I am unsure which answer would help me the best in this case.
CodePudding user response:
You could change that to something like enums, new-style switches etc. This can certainly make the code more readable and maintainable.
However, you were talking about enhancing performance. Changing this code most likely will have hardly any impact on performance, at least not in a positive way.
If you want to improve performance, measure and find the bottlenecks in the code and focus on those.
CodePudding user response:
this might help !!
if(disturbance2 == 0) {
switch (disturbance1){
case SPS1_20_PUFFER_EMPTY:
checkWarnings(bMessage, PLS_5_PUFFER_EMPTY);
break;
case SPS1_30_STOPPERFL_LOCKED:
checkWarnings(bMessage, PLS_5_PUFFER_EMPTY);
break;
case SPS1_40_DISTURBANCEFL:
if (bMessage.getDisturbanceType().intValue() == DISTURBANCETYPE_SYSTEMDISTURBANCE_SHORT) {
checkWarnings(bMessage, PLS_3_SHORTDISTURBANCE_FL);
}
else if (bMeldung.getStoerungsartMde().intValue() == DISTURBANCETYPE_SYSTEMDISTURBANCE_LONG) {
checkWarnings(bMessage, PLS_4_SYSTEMMALFUNCTION_FL);
}
break;
}
}
also you can write
if(disturbance2 != 0){
// return or exit if requires
}
// then start switch() , you can remove one branch of block
CodePudding user response:
Switch on disturbance1 can help.
Check out the new switch style from Java17. It makes code really lean.