-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: add inspection of reaching SomeEnum.values()[someIndex] #40
Comments
But wait, |
@Miha-x64 Not the same. What you probably wanted to compare to is
https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html So an equivalent of SomeEnum.values().forEach {
if(it.ordinal==someIndex)
return it
}
throw IndexOutOfBoundsException() It's written in more places that you shouldn't rely on index, hence you shouldn't use these. The "valueOf" is much more common than both of these, even combined. It's much more valid use than being based on index. Have the inspection turned off by default, if you wish, but I still think it's useful. |
The funny thing is that enum names, actually, are also by declaration and not explicit. Moving breaks order, renaming breaks names. Both without changing any integer/string literals. |
You have a point, but renaming an item is like removing the old one and adding a different one instead. The Enum values are the ID of them. Not their order. When you create a new Enum value, you create a new instance of it that is different than the rest. Plus, it is much more different than just changing the order, and it's much easier to break, as you can add items in between, too, and remove items in between, too. That's more cases that it can break. I mean that you are comparing 1 case (renaming) vs 3 (change order, put item, remove item). Renaming is much more obvious, too, as you have a different instance. |
This is for both Java and Kotlin.
Such a code can lead to bugs easily in case the order of the enum values change, or items get removed/replaced.
The text was updated successfully, but these errors were encountered: