The copied-code-with-only-one-thing-changed stuff is bad enough, but what's worse is the hardcoded reflection when from the looks of it you could just reference the fields directly
Unless the fields are dynamic in which case may God save your soul
That's pretty good but it's missing that the "building" part is also a variable. OP code seems to suggest that there are multiple buildings, and each has DL/DR/UL/UR variants, so you'd need a list of lists.
Yup, realized after writing a bit and decided to go with it so tried to point it out here:
while ignoring the fact that you probably need a map type with dynamic member prefix
:P
Might make sense to have a nested lists or maps in that case. Depends on the other code. Would also probably add enums for the building types and rotation values and make the Inspector side easy to use as well. And pool in loading screen instead of the game loop. And, and.. :)
Is that a game or what? And if that's the case, note that reflection is pretty inefficient. Not sure what the use case of that code is, but there are probably better solutions. And I mean, that's overlooking the reason why it was posted here, which I guess is about that horrible switch statement.
well could get line shrinkage by just doing Destroy(holo) outside the switch as it always occurs, then set a string and set that string in the switch and run the instantiate with that string.
59
u/crass-sandwich Apr 14 '23
The copied-code-with-only-one-thing-changed stuff is bad enough, but what's worse is the hardcoded reflection when from the looks of it you could just reference the fields directly
Unless the fields are dynamic in which case may God save your soul
49
u/NUTTA_BUSTAH Apr 14 '23
Step 1 in refactoring (remove reflection and duplication while ignoring the fact that you probably need a map type with dynamic member prefix):
Step 2 in refactoring (remove switch and duplication):
Step 3 in refactoring (pooling/performance):
---'
And so on.. :) Pardon my Unity / C#. Been some years..
8
u/matthkamis Apr 15 '23
Remove if statement and replace with this
rotation = (rotation + 1) % 4
6
u/-_one_-1 Apr 14 '23 edited Apr 14 '23
Is that a game or what? And if that's the case, note that reflection is pretty inefficient. Not sure what the use case of that code is, but there are probably better solutions. And I mean, that's overlooking the reason why it was posted here, which I guess is about that horrible switch statement.
4
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 14 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
2
u/[deleted] Apr 15 '23 edited Apr 15 '23
If quick prototyping, id even say its fine if you were tired.
If thats the code that was left in the game, god have mercy on your soul thats terrible in so many ways.
1
u/ILikeCodeBrackets Apr 14 '23
How would we improve this code?
1
u/iNeedOneMoreAquarium Apr 15 '23
Who's Dr Holo?
1
u/No-Adeptness5810 Apr 23 '23
well could get line shrinkage by just doing Destroy(holo) outside the switch as it always occurs, then set a string and set that string in the switch and run the instantiate with that string.