Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to loop setters and getters?

I'm fairly confident that there's no way this could work, but I wanted to ask anyway just in case I'm wrong:

I've heard many times that whenever you have a certain number of lines of very similar code in one batch, you should always loop through them.

So say I have something like the following.

setPos1(getCard1());
setPos2(getCard2());
setPos3(getCard3());
setPos4(getCard4());
setPos5(getCard5());
setPos6(getCard6());
setPos7(getCard7());
setPos8(getCard8());
setPos9(getCard9());
setPos10(getCard10());
setPos11(getCard11());
setPos12(getCard12());

There is no way to cut down on lines of code as, e.g., below, right?

for (i = 0; i < 12; i++) {
setPos + i(getCard + i)());
}

I'm sure this will have been asked before somewhere, but neither Google nor SO Search turned up with a negative proof.

Thanks for quickly confirming this!

like image 639
s.d Avatar asked Dec 02 '22 00:12

s.d


1 Answers

No way to do that specifically in Java without reflection, and I don't think it would be worth it. This looks more like a cue that you should refactor your getcard function to take an integer argument. Then you could loop.

like image 121
Joe Avatar answered Dec 15 '22 00:12

Joe