StackOverflow’s Most Copied Java Code Snippet Has A ‘Bug’


I am sure every developer has taken some sort of help from StackOverflow, and there is no doubt that the Q&A platform for coding is a lifesaver. But not every piece of code found on the platform is safe as many of them contain bugs, and interestingly, the most copied code snippet on StackOverflow also has a bug in it!

The most copied snippet on StackOverflow is a piece of Java code that was authored by Andreas Lundblad. He is a Java developer at Palantir and one of the highest-ranked contributors to StackOverflow for programming-related topics.

In 2018, an academic paper identified the code snippet by Lundblad as the most copied Java code on StackOverflow and also one of the highly re-used ones in open source projects.

Posted in 2010, the code snippet printed byte counts (123,456,789 bytes) in a human-readable format, that is, 123.5 MB.

most copied code snippet stackoverflow

Nine years later, Lundblad found the flaw on his code and admitted that it incorrectly converted byte counts into human-readable formats. He acknowledged the flaw in a blog post and published a corrected version on it.

Apparently this code had been copied and included in more than 6,000 GitHub Java projects — which is more than any other StackOverflow Java snippet.

Github projects

StackOverflow Code Often Has Security Flaws

Lundblad’s code contained a minor conversion bug that produced a slightly inaccurate file size, but things could become much worse if it were a major bug.

But while Lundblad’s code snippet contained a trivial conversion bug that only resulted in slightly inaccurate file size estimations, things could have been much worse.

It is now a well-known fact that copy-pasting code from StackOverflow is not the best practice, however, developers still do it.

Also Read: Microsoft Is Working On A Rust-Based ‘Memory Safe’ Programming Language
Manisha Priyadarshini

Manisha Priyadarshini

An Editor and a Tech Journalist with a software development background. I am a big fan of technology and memes. At Fossbytes, I cover all aspects of tech but my specific area of interest is Programming and Development.
More From Fossbytes

Latest On Fossbytes

Find your dream job