jawbreaker.io

Simplifying Java: The Pitfalls of Premature Optimization


Listen Later

Hello and welcome to today’s one-minute dive into a classic Java head-scratch. On The Daily WTF, we meet Alice’s co-worker, who runs a query, pulls every row into a List by doing “result.add(new Integer(rs.getInt(1)))” and then immediately copies it into an int[] array. As the author points out, since Java 5 you get automatic boxing and unboxing—so you don’t need “new Integer” or “((Integer) result.get(h)).intValue()”. Even more puzzling is creating that fixed-size array just after populating a dynamically resizable ArrayList. The article calls it “probably a premature optimization,” and suggests our coder might just be stuck in a C mindset: “comfortable with an array of integers, which covers 90% of the data types you use in C.” In the end, a few extra lines of code add zero benefit and cost precious readability. That’s your minute of WTF—keep your Java simple, and let the JVM handle your boxing.
Link to Article

...more
View all episodesView all episodes
Download on the App Store

jawbreaker.ioBy jawbreaker.io