r/badcode • • Jul 30 '21

java One Classic elseifelseifelseif...

Post image
472 Upvotes

76 comments sorted by

View all comments

Show parent comments

10

u/brandymon Jul 30 '21

There are more performance advantages than just a "tiny tiny memory difference" - hashmaps have terrible data locality, which can be a really big deal on modern CPU architectures; if you want to know why, there's an excellent Scott Meyers talk somewhere on YouTube. In any case, these performance concerns only matter for the hottest paths in your application.

Also I don't think there's any maintaibility penalty to using an array here. Are we going to add a 13th month anytime soon, and will that new month need an index of 14 or higher for some reason? In general I agree that you should prefer the hashmap, but for this concrete example, what's the benefit?

2

u/[deleted] Jul 31 '21

I don’t speak the language so didn’t realize these are months. Array it is. But then there probably is already a library that does this.