I know it sounds terrible but in my java program I have about 100 if-else statements, all containing 3 variables that have to stay unique to those if-else blocks, 1 is a flag used to know when its the first time ever hitting that if-else block, and the other 2 are both strings and are temp variables holding that data that was used last time it ran through that if-else block, so it can be compared to the data running through this time around, sorry if it sounds sloppy, I hate the idea of having so many if-else blocks, but right now Im more concerned about the variables, because if I make 3 variables for each block that is an extra 300 variables. Any suggestions on something I could implement to reduce the amount of variables, one idea I have was 1 array for all the flags and then a 2d array holding the 2 strings for each if-else block. Thanks Beef.
Edited: to show sample of the first 2 if-else blocks, all the other have the same code inside just with different names for the flag and temp variables ex. ac101Flag, tempAC101Start, tempAC101End
// AC 101
if (room.equals("FEB 2009") || room.equals("FEB 2011") ||room.equals("FEB 2013") || room.equals("FEB 2015") || room.equals("FEB 2017") ||
room.equals("FEB 2021") || room.equals("FEB 2023") || room.equals("FEB 2025") || room.equals("FEB 2027") || room.equals("FEB 2029")) {
instanceNum = 4;
devID = 130200;
if (ac101Flag == false) {
Delete();
Insert();
ac101Flag = true;
tempAC101Start = Double.parseDouble(finalStart.substring(0, 5).replace(':', '.'));
tempAC101End = Double.parseDouble(finalEnd.substring(0, 5).replace(':', '.'));
}
//Insert();
else if (tempAC101Start <= (Double.parseDouble(finalStart.substring(0, 5).replace(':', '.'))) && tempAC101End >= Double.parseDouble(finalEnd.substring(0, 5).replace(':', '.'))) {
}
else
{
Insert();
tempAC101Start = Double.parseDouble(finalStart.substring(0, 5).replace(':', '.'));
tempAC101End = Double.parseDouble(finalEnd.substring(0, 5).replace(':', '.'));
}
}
// AC 102
else if(room.equals("FEB 1130")) {
instanceNum = 4;
devID = 130400;
if (ac102Flag == false) {
Delete();
Insert();
ac102Flag = true;
tempAC101Start = Double.parseDouble(finalStart.substring(0, 5).replace(':', '.'));
tempAC101End = Double.parseDouble(finalEnd.substring(0, 5).replace(':', '.'));
}
//Insert();
else if (tempAC101Start <= (Double.parseDouble(finalStart.substring(0, 5).replace(':', '.'))) && tempAC101End >= Double.parseDouble(finalEnd.substring(0, 5).replace(':', '.'))) {
}
else
{
Insert();
tempAC101Start = Double.parseDouble(finalStart.substring(0, 5).replace(':', '.'));
tempAC101End = Double.parseDouble(finalEnd.substring(0, 5).replace(':', '.'));
}
}
EDIT: Just to give a more concrete demonstration of the answer, I think you want something like:
Then in your loop, just use:
It’s hard to say for sure without seeing your code, but it sounds like you should encapsulate all of this in a data structure:
ifpart. The exact nature of this will depend on what you’ve gotYou can then run through a list of these
ConditionBlockobjects (or whatever you choose to call them) and check whether the block “matches” in the current context, updating it where appropriate.If you can give us a small example of the original code, we can probably refactor it for you fairly easily.
EDIT: As I apparently haven’t been clear about introducing the local variables for the sake of parsing in one place, I’d do something like this (as one first step):