Skip to content

Switch Statements Removed in CraftArt, CraftPotionEffectType, and CraftWorld#6

Open
PhouLee wants to merge 9 commits intoNicMcPhee:masterfrom
PhouLee:refactoring_two_4/17
Open

Switch Statements Removed in CraftArt, CraftPotionEffectType, and CraftWorld#6
PhouLee wants to merge 9 commits intoNicMcPhee:masterfrom
PhouLee:refactoring_two_4/17

Conversation

@PhouLee
Copy link
Copy Markdown

@PhouLee PhouLee commented Apr 30, 2014

Note 1: In total we changed three switch statements into maps. However we do have not three different branches therefore we only have one pull request (Which may count as three different pull requests).

Note 2: We also made a branch out of our iteration one branch by mistake. This means we have the recent commits in addition to the previous commits from iteration.
Here is the URL to the changes that matter in this pull request!

The Issue:
There were two methods in CraftArt.java that were using switch statements to generate MineCraft objects and CraftBukkit objects. These statements were long and repetitive.

There was one method in CraftPotionEffectType.java that had a switch statement. There was also another method in CraftWorld that also had a switch statement.

Justification:
This changes that we made improved readability and decreased the number of lines in the classes.

BreakDown:
Our main focus was on Switch Statements and we used switch-statements to maps refactoring. We were able to remove both switch statements in NotchToBukkit() and BukkitToNotch(). We did by constructing a EnumMap Object to hold the Art objects as keys and the EnumArt objects as the values. NotchToBukkit uses the Map to get the keys while BukkitToNotch uses the Map to grab the values. This is faster than a switch statement when we grab the values in BukkitToNotch. However, this is about as fast as a switch statement when grabbing the keys.

We also removed switch statements in CraftPotionEffectType.getName() and CraftWorld.generateTree(). Was also used Map data structures here to remove the switch statement. In these two cases we only uses the Maps to get the values. Thus, it is faster than a switch statement. This made the methods not only faster but also improved readability.

We checked and there were tests for the switch statements (we checked by commenting the code to see if any tests broke). After refactoring each switch statement we ran the tests and they were all green.

psycowithespn and others added 9 commits April 9, 2014 18:55
this chunk of code is big, we decided to extract it into the abstract
class. Now both classes use the method from the abstract class.
smaller methods from it called setTicks() and shootFireballs(). This was
done to make the code more readable.
were found within EntityBlaze and EntitySpider. We were able to extract
the duplicated method and put it in EntityMonster. Tests were green.
out how to do this so instead we moved on to other refactorings. Next,
we applied polymorphism to getEntity() in CraftEntity. We got about 1/5
of it done. All the tests seem to pass. Ran into issues with classes
with no source code.  We worked around this by just throwing the logic
into the appropriate super class.
idea that we could use it on the CraftArt switch statements. After some
time fiddling with it (and some help from Nic) we were able to construct
a Hashmap that both switch statements can access.  Hashmap is static so
we had to use a static enclosement to populate the KV pairs.
the values instead. As a result code looks clearer.
with the ids as the keys and the effect types as the values. Makes the
code a bit easier to read.
@MaxMagnuson
Copy link
Copy Markdown

Code Review:
I really like the solution of using a map to replace long switch statements. It is a really elegant solution to a common issue in this code base. It is a good thing that you populate the map outside of the methods it is used it to avoid repopulating it each time the method is called. Overall I think the solution is solid the only real complaint I would have is the for each loop used in NotchtoBukkit with the enumMap. It seems like you should be able to just use a single if statement like in your other refactorings. Also these refactorings seem to a be a bit haphazard in which you chose them. The two in the art class make sense, but the other two seemed tacked on. Not that they are bad refactorings, its just that they don't really go together.

Non-code Review:
The formatting of your pull request fits the guidelines layed out by craftbukkit. There are a few typos here and there, but otherwise it reads well. I think you explain what the changes you made do and they accurately reflect the actual changes to the code. It is very clear what changes you made, how you made them, and why you made them. Although, I think you probably could have utilized the language of refactoring a bit more. The descriptions of your testing seems sufficient enough to support the changes made.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why in the world do you look over all the entries in the map instead of just using get like you do in the other map you set up? This makes the lookup linear (admittedly over a very short list) instead of (nearly) constant time, which seems weird.

@NicMcPhee
Copy link
Copy Markdown
Owner

I quite like this refactoring – nice job. It would be nice if your handling of the lookup was more consistent in each of the cases (especially with the odd looping over the key set in the Art case).

There's obviously some similarity here between the changes you've made. Is there a way to pull those together in some fashion, or do the Map classes already capture all the real similarity in these settings?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants