I have code where I have defined over 350+ variables to be used in a 350+ if else statement.
My question is this: "why does my code take over 30 seconds to update what I paste in with the auto-fill manager? Is this because I have SO many if, else if statements and defined variables?"
//350+ defined initially
var A244: String = "A244" //1
var C219: String = "C219" //2
var A099: String = "A099" //3
var A169: String = "A169" //4
var A185: String = "A185" //5
// ETC ETC ETC...
//THEN THE 350+ ELSE IF STATMENTS BEGIN
else if (A985.compareTo(punitstring) == 0) {
val mtext2 = findViewById(R.id.textView4) as TextView
mtext2.setText("4-5t DBB")
val mtext3 = findViewById(R.id.textView8) as TextView
mtext3.setText("Second")
}
else if (B024.compareTo(punitstring) == 0) {
val mtext2 = findViewById(R.id.textView4) as TextView
mtext2.setText("N/A0")
val mtext3 = findViewById(R.id.textView8) as TextView
mtext3.setText("First")
}
else if (B199.compareTo(punitstring) == 0) {
val mtext2 = findViewById(R.id.textView4) as TextView
mtext2.setText("N/A1")
val mtext3 = findViewById(R.id.textView8) as TextView
mtext3.setText("First")
}
else if (B215.compareTo(punitstring) == 0) {
val mtext2 = findViewById(R.id.textView4) as TextView
mtext2.setText("N/A2")
val mtext3 = findViewById(R.id.textView8) as TextView
mtext3.setText("First")
}
else if (B218.compareTo(punitstring) == 0) {
val mtext2 = findViewById(R.id.textView4) as TextView
mtext2.setText("DC Walkie")
val mtext3 = findViewById(R.id.textView8) as TextView
mtext3.setText("First")
}
else if (B219.compareTo(punitstring) == 0) {
val mtext2 = findViewById(R.id.textView4) as TextView
mtext2.setText("AC 3 WS")
val mtext3 = findViewById(R.id.textView8) as TextView
mtext3.setText("First")
}
From my code you can see what I have pasted in (see "before" picture below), but after some thinking the IDE updates to what is shown in the after picture (see "after" picture below). I believe it may be due to soo many statements, but need some confirmation and possibly some input on how I should begin to clean this up.
Before, when the IDE initially loads up: https://i.sstatic.net/OOBqR.jpg
AFTER about 30+ seconds of waiting for the manager to update the code with auto-fill: https://i.sstatic.net/9LjwR.jpg
I have already tried allocating the maximum amount of disk and RAM space to the IDE and this did not help.
My next step is to possibly concatenate all of the var to one specific statement. I am not sure how to shorten up the if statements though.
Thanks!
After multiple tries, I ended fixing this myself.
What I did:
Each variable that was added was causing a warning which needed to be updated in the IDE everytime I updated a line of code. fixing the warning by making it a "val" instead of a "var" sped things up a little bit.
The other thing that really helped was doing a "cleanup code" alt+enter action on some of the if statements.
The final thing that helped the most was apparently if you have more than 4 if/else statements you should just use a "When" statement. This one provided the greatest processing efficiency.