Er zijn tenminste twee antwoorden. Een daarvan is om een return-statement onder aan de eerste if
-instructie te zetten. Ik vind dat niet leuk omdat het misschien geen goede algemene oplossing is.
Een ander antwoord is om je code een beetje te herstructureren ...
First, since the case is where maxValX is >= 3 doesn't seem to be where the problem lies, I'd get that out of the way by reversing the first if
conditional:
if (maxValY >= 3) {
p.setColor(Color.black);
//display the value of graph width and graph height
aw = String.valueOf(x1);
p.drawString("Graph Width = ", 50, 90);
p.drawString(aw, 150, 90);
p.drawString("Graph Height = ", 50, 110);
String ah = String.valueOf(y1);
p.drawString(ah, 156, 110);
} else if (maxValY < 3) {
p.setColor(Color.black);
//display the value of graph width and graph height
aw = String.valueOf(x1);
p.drawString("Graph Width = ", 740, 490);
p.drawString(aw, 840, 490);
p.drawString("Graph Height = ", 740, 510);
String ah = String.valueOf(y1);
p.drawString(ah, 846, 510);
}
if (minValx == -1 || minValx == -2 || minValx == -3) {
p.setColor(Color.black);
//display the value of graph width and graph height
aw = String.valueOf(x1);
p.drawString("Graph Width = ", 740, 90);
p.drawString(aw, 840, 90);
p.drawString("Graph Height = ", 740, 110);
String ah = String.valueOf(y1);
p.drawString(ah, 846, 110);
}
Dus het enige dat gebeurde was dat ik de 'non duplicate if statement issue' bovenaan plaatste.
Nu hebben we twee voorwaarden, een die zich bezighoudt met minValX
, en een die zich bezighoudt met maxValY
. Maar we merken nu dat de inhoud van elk bijna identiek is. We lossen dit op door de code een beetje te refactoren (en sluipen in een andere ...):
if (maxValY >= 3) {
p.setColor(Color.black);
//display the value of graph width and graph height
aw = String.valueOf(x1);
p.drawString("Graph Width = ", 50, 90);
p.drawString(aw, 150, 90);
p.drawString("Graph Height = ", 50, 110);
String ah = String.valueOf(y1);
p.drawString(ah, 156, 110);
} else if (maxValY < 3) {
q(491, 510);
} else if (minValx == -1 || minValx == -2 || minValx == -3) {
q(90, 110);
} else {
throw new RuntimeException("This cannot possibly happen ;-)");
}
}
private void q(int loc1, int loc2) {
p.setColor(Color.black);
//display the value of graph width and graph height
aw = String.valueOf(x1);
p.drawString("Graph Width = ", 740, loc1);
p.drawString(aw, 840, loc1);
p.drawString("Graph Height = ", 740, loc2);
String ah = String.valueOf(y1);
p.drawString(ah, 846, loc2);
}
En dan merken we dat de codeblokken nog steeds bijna identiek zijn. Refactor ...
private void someName() {
//...
if (maxValY >= 3) {
q(50, 90, 510);
} else if (maxValY < 3) {
q(740, 490, 510);
} else if (minValx == -1 || minValx == -2 || minValx == -3) {
q(740, 90, 110);
} else {
throw new RuntimeException("This cannot possibly happen ;-)");
}
//...
}
private void q(int base, int graphWidth, int graphHeight) {
p.setColor(Color.black);
//display the value of graph width and graph height
aw = String.valueOf(x1);
p.drawString("Graph Width = ", base, graphWidth);
p.drawString(aw, base+100, graphWidth);
p.drawString("Graph Height = ", base, graphHeight);
String ah = String.valueOf(y1);
p.drawString(ah, base+100+6, graphHeight);
}
Dus we zien dat alle drie secties van de code in principe hetzelfde waren, met uitzondering van enkele constanten. Door ze opnieuw in te stellen, verminderen we de hoeveelheid code die moet worden gedebugd. Bovendien zien we de structuur van de if
-instructie duidelijk en hebben we de logica gescheiden van het daadwerkelijke werk, dat vaak extra dividenden oplevert.