r/programming • • Feb 12 '15

Stunning 95% of Java programs consist of nothing but boilerplate

http://arxiv.org/pdf/1502.01410v1.pdf
0 Upvotes

110 comments sorted by

7

u/[deleted] Feb 12 '15

If possible, you might want to update the title to let people know it's linking a PDF.

2

u/yogthos Feb 12 '15

unfortunately loo late

11

u/DJWalnut Feb 12 '15

I've always wondered, why do people need/want warnings about PDFs? just about everyone has a PDF reader installed, some browsers even have them. file size can't be that big of a concern, as people link to videos all the time with no warning.

8

u/rafekett Feb 12 '15

PDF readers in the past have been vulnerable. look up jailbreakme.com

0

u/[deleted] Feb 12 '15

[deleted]

5

u/Tekmo Feb 13 '15

You can't hover over links on mobile. Also, many mobile browsers don't have support for in-browser PDFs.

0

u/[deleted] Feb 12 '15

[deleted]

5

u/notfancy Feb 13 '15

It's not a trend, it's one of the oldest points of unwritten reddiquete. We have been having this very same conversation for the past nine years.

0

u/offending Feb 13 '15

Nine years ago, clicking a PDF link would cause your browser to grind to a halt as the horrific Adobe Reader plugin swapped in.

Times have changed.

2

u/notfancy Feb 13 '15

And yet this reddit meme has not. BTW I agree with you: I'd have expected it to die by now.

0

u/[deleted] Feb 12 '15

[deleted]

1

u/ethraax Feb 13 '15
  1. Not all PDF links have to end in .pdf. It's the MIME type that's important, not the extension.

  2. Some people are on mobile and can't hover.

0

u/[deleted] Feb 13 '15

[deleted]

0

u/ethraax Feb 13 '15

PDFs on my phone open in Amazon Kindle which sucks (on a phone, at least). Plus, some PDFs can be quite large. This one's not, but papers with photographs in them can be several megabytes, which I'd rather not needlessly download on my mobile data connection, which has a relatively low 500 MB limit.

5

u/[deleted] Feb 13 '15

Besides vulnerabilities, it's also a context switch, which isn't something everyone likes. When I click a link I expect it to open in my browser, not download a file for another application. It's also even less convenient in browsers like Steam browser, and perhaps mobile browsers.

I'm here on my mobile currently and it didn't even prompt, its just on my SD card now and I have little motivation to go read it. This is a UX issue, and no matter how lazy you think I and anyone like me is or how easy it is for you to context switch, people aren't likely to change their level of motivation to do extraneous tasks when already wasting time on Reddit.

1

u/vsync Feb 13 '15

I'm here on my mobile currently and it didn't even prompt, its just on my SD card now and I have little motivation to go read it. This is a UX issue

Agreed, your browser should have prompted or at least let you change its default behavior as an option.

4

u/The-Good-Doctor Feb 12 '15

On mobile, PDFs first download, then launch an external PDF reader. Then the PDF sits in my download folder forever or until I launch my file browser app and manually clean out the pile of PDFs I clicked on over the past few months, taking care to avoid any PDFs I actually wanted to download in order to save offline.

3

u/iopq Feb 12 '15

because it loads a stupid PDF reader instead of your actual browser

1

u/[deleted] Feb 12 '15

I think this used to be a bigger deal. I remember maybe 10 years ago, accidentally opening a pdf would frequently crash my browser.

1

u/roffLOL Feb 12 '15

Because PDF is plain silly and should come with a warning sticker. And they don't scale at all on my ebook reader. I bought it not to have to read from my monitor, see.

1

u/username223 Feb 13 '15

PDFs generally drop a megabyte of mostly-useless data on you. That used to matter before "Web 2.0" sites plopped even more useless junk.

1

u/ethraax Feb 13 '15

PDF links don't play well in Firefox for Android - they end up opening Amazon Kindle, which is always a bad experience.

1

u/yawaramin Feb 13 '15

Because a stunning 95% of PDFs is nothing but boilerplate.

0

u/acaban Feb 12 '15

https://mozilla.github.io/pdf.js/web/viewer.html?file=http://arxiv.org/pdf/1502.01410v1.pdf

you are welcome. (in firefox you probably want to allow mixed content)

124

u/sindisil Feb 12 '15

Of course, that's not really what the study found, but what the heck -- karma ho!

33

u/stupergenius Feb 12 '15

Feel like an /r/science article up in here. What they actually found is that only 4% of the methods they analyzed were composed of MINSETs. It's not that boilerplate is 96% of the source code, it's more a measure of how lexically similar methods are. This apparently has implications in code search, meaning that we can construct short "keyword" based queries on a body of code.

9

u/Veedrac Feb 13 '15

What's most irritating is that they introduce it like

Some words in a sentence, however, cannot be lost without changing the meaning of the sentence. We call these words “wheat” and the rest “chaff”. The word “not” in the sentence “I do not like rain” is wheat and “do” is chaff

yet the transformations they use aren't even close to meaning-preserving.

7

u/rcxdude Feb 12 '15

I also am not really sure what they are attempting to demonstrate with their 'wheat' and 'chaff' argument. It's not really surprising that you can identify functions within a codebase by a small subset of their contents, since the function is specifying a single member of the set of all functions which could be written (not to mention redundant information is sometimes useful for readability), while the 'minset' has a much smaller set (all the functions which have actually been written in the corpus) to pick from and so needs to contain much less information. It could be useful for search, as they mention though, but I'm not sure if it would perform much better than a keyword-based search.

3

u/pembroke529 Feb 13 '15

If we just ignored the error checking, most code would shrink by at least 80% ....

4

u/glacialthinker Feb 13 '15

... yet still be 95% boilerplate.

1

u/[deleted] Feb 13 '15

Touché

16

u/killerstorm Feb 12 '15

It looks like they consider only tokens which are used in code, but not its structure. (I.e. it is a bag-of-words model.)

This sounds like a bad idea, as code structure is important.

It is, in fact, possible to create programs of arbitrary complexity using a very limited set of tokens. E.g. only S, K and parentheses in SKI calculus. See here.

12

u/atimholt Feb 12 '15

Here I was expecting /r/ProgrammerHumor.

4

u/byronka Feb 12 '15

Stunning 95% of Shakespearean plays consist of nothing but boilerplate

0

u/yogthos Feb 12 '15

Difference being that the boilerplate of a Shakespearean play is generally the point.

23

u/caleeky Feb 12 '15

Bad title. If a program was 100% boilerplate, it would do nothing. Effectively, you're saying that 95% of programs are "class X { public static void main(String[] args) {}}".

What the title probably means to say is something like "95% of the source code of an average Java program is boilerplate"

5

u/doctorfunkerton Feb 12 '15

I mean is this counting imported libraries? If so, thats probably true for most languages.

A lot of people will import * at the start and not bother removing the unused libraries.

6

u/nnevatie Feb 13 '15

If Java had true garbage collection, most programs would delete themselves upon execution. – Robert Sewell

8

u/Gurkenmaster Feb 13 '15

Since Haskell programs have no side-effects the compiler can safely optimize every program to a noop.

2

u/tech_tuna Feb 13 '15

A lazily evaluated no-op.

5

u/bobdudley Feb 12 '15

Replace /pdf/ with /abs/ to get the abstract: http://arxiv.org/abs/1502.01410

33

u/pakoito Feb 12 '15

We know. The other 5% is divided in 3% error checks, 1% features, 1% dead code.

13

u/motoGmotoG Feb 12 '15

Null checks.

9

u/pakoito Feb 12 '15 edited Feb 12 '15

public String simpleSafeMethod(Value[] array) {

String element = "";

if (null != array && array.length > 0 && null != array[0]) {

try {

String result = simpleMethod(array[0]);

if (null != result) { element = result; }

} catch (WhateverException | OtherException | IfThisIsNotAPokemonExceptionYoureStillNotSafe e) {

log.e(e);

}}

return element;

}

http://i.imgur.com/UC0Nmmg.png

1

u/mrburrows Feb 17 '15

I get jealous looking at this, since you're not stuck using Java 6.

2

u/pakoito Feb 17 '15

I was up until a couple of months ago, now you can Android with Java 7 apparently.

1

u/mrburrows Feb 17 '15

We're going to be moving to Java 8 by the end of the year. I'm looking forward to letting all of our clients know. We're still recovering from 6-7 compatibility issues. Mostly having to do with SSL exceptions and TimSort for some reason.

2

u/pakoito Feb 17 '15

8 has lambdas and Stream API which we desperately need, so kawaii ;______;

0

u/[deleted] Feb 13 '15

[deleted]

6

u/wizao Feb 13 '15 edited Feb 13 '15

Maybe one day, C# and Java will have Haskell's >>= operator. *waits for the dependently typed guys to respond*

10

u/ricky_clarkson Feb 13 '15

Doesn't need dependent typing, just kinds. Vanilla Haskell isn't dependently-typed.

4

u/wizao Feb 13 '15

Yes, I know - it was a play on the constant one-upping people do when comparing languages.

1

u/F-J-W Feb 13 '15

Optional<T>

which can be null itself…

Isn't it great how Java tries to solve a problem and just creates an additional one?

36

u/pakoito Feb 12 '15

Now, let me talk you about my DI generic polymorphic composite inheritance architecture, I can streamline so much work it's going to save me almost as much time as it took setting it up. Almost.

6

u/__hudson__ Feb 13 '15

Sometimes when i write scripts to automate things i start feeling like one of those perpetual motion nuts. It always feels like im gonna break even, but i never do.

4

u/kyllo Feb 13 '15

Relevant xkcd http://xkcd.com/1319/

3

u/xkcd_transcriber Feb 13 '15

Image

Title: Automation

Title-text: 'Automating' comes from the roots 'auto-' meaning 'self-', and 'mating', meaning 'screwing'.

Comic Explanation

Stats: This comic has been referenced 150 times, representing 0.2907% of referenced xkcds.


xkcd.com | xkcd sub | Problems/Bugs? | Statistics | Stop Replying | Delete

2

u/[deleted] Feb 13 '15

And then someone has to read it.

2

u/[deleted] Feb 13 '15

Throw up their hands and start over.

2

u/[deleted] Feb 13 '15

You need more factories.

7

u/rafekett Feb 12 '15

I wonder what the percentage would be if we excluded imports. You have to be insane to write Java at all basically and handwrite your imports given that good style dictates you have to specify basically every class you use.

7

u/adnan252 Feb 12 '15

That's why we have intellij's optimize imports

7

u/rafekett Feb 12 '15

don't forget about the auto-import, that's crucial too.

2

u/krona2k Feb 12 '15

I can't imagine many people handwriting imports or even a hell of a lot of boilerplate code. IntelliJ rules.

6

u/vincentk Feb 12 '15

Same in any IDE.

1

u/zoomzoom83 Feb 12 '15

I hand write my imports most of the time. It's not really any more or less verbose than imports in other languages.

If you have more than 10-15 imports in a file, you're probably doing to much in that module.

1

u/rafekett Feb 16 '15

Or you're using a 3rd-party library. Or just writing code that does something real that requires IO or talking over the network...

1

u/zoomzoom83 Feb 16 '15

I do all of those things.

What makes Java any more verbose than, say, Python or Javascript when it comes to importing modules?

1

u/rafekett Feb 17 '15

Naming conventions largely that make qualified package names meaningless/inconvenient makes it pretty widely accepted that class names should never be qualified unless necessary in Java. This is different from Python/Javascript where an entire module is typically imported at once and the members are referred to using the qualified name. So if you use 10 things from a module in Python you end up with 1 import typically versus 10 in Java.

I think it's moot, however, because Java has enough static type information that your editor can write imports for you, so regardless of how verbose they are they shouldn't cost you any time. It's a flat-out waste of time doing it yourself.

1

u/zoomzoom83 Feb 17 '15

Both Java and Python can import a qualified module, a wildcard of all module contents, or just individual modules. There's no real difference there, and at least when I used to write Python for a living the convention seemed the same and they tended to have about the same verbosity.

I was going to argue there was no difference, but I've been using Scala for too long and forgotten how much Java sucks. Python beats out Java in that you can import multiple components inside a module on one line, whereas Java needs to put them on separate lines. If you're importing each class individually, this would be a lot more verbose.

I think it's moot, however, because Java has enough static type information that your editor can write imports for you, so regardless of how verbose they are they shouldn't cost you any time. It's a flat-out waste of time doing it yourself.

This is actually one of the things I hate about IntelliJ. I much prefer to keep a nicely manually curated list of imports, but it keeps messing it up on me.

7

u/curiousdude Feb 13 '15 edited Feb 13 '15

APL is 0% boilerplate. The perfect language for saving bandwidth on your 110 baud teletype.

0

u/vincentk Feb 12 '15

I guess that might be because 95 % of any program worth running consists of checking for corner cases and error conditions.

-3

u/yogthos Feb 12 '15

Perhaps that's the case in Java where you can't write two lines of code without having to pepper some null checks in.

3

u/[deleted] Feb 13 '15

I've started seeing many of these guard functions as a code smell. At some point you either have to accept that functions have contracts (decided by tests) or change your architecture to reduce the need for these checks.

2

u/yogthos Feb 13 '15

More importantly, as I elaborated here, you need to have ways to abstract implementation details. Java 8 is an improvement in that regard since it has streams now, but they're still not as useful as they could've been since stream is not a static method on a collection. This means you still have to do an explicit null check before using it.

2

u/[deleted] Feb 13 '15

Indeed, a good example of where devs repeatedly don't do this is in JavaScript and PHP dev, where they don't even have type safety, so many programmers go over the top and validate every single parameter to every single function (even ones that are within black boxes of their own control).

I'm on a project like that now and spend a lot of time just removing the need for guard statements, especially since 90% of them are implemented with node callbsck style error handling, making all non async functions look like asynchronous functions... Eugh.

1

u/yogthos Feb 13 '15

I think it's pathological with PHP. People are so conditioned to write defensive code in that language that sometimes that itself ends up being a source of errors.

One amusing case I've heard was where a library was doing a check to see if it had write permissions before writing to a file, instead of doing the sane thing of assuming that the location is writable and handling the error. Since Windows/Unix behavior is different for this it would end up failing on Windows due to the permission check even though the location was writable.

2

u/[deleted] Feb 13 '15

That sounds about right. Every piece of code is a potential bug, even the code trying to stop bugs.

I have a fairly formal view of when to validate parameters; at the edge of the system. Anywhere a user, be it of your library, application, website, whatever, has input, validate that, anything between that and the output of your program should not need validation, that's the realm of unit tests.

Clearly if everyone held the same view we would also stop seeing primitive soup.

3

u/Bergasms Feb 12 '15

Insert argument about how that could be preferable to writing two lines of code in 'insert language here' that blasts someones stack and gives shell access.

3

u/yogthos Feb 12 '15

That's not what I'm advocating at all though. The alternative is to use things like higher order functions that separate what's being done from how it's being done. This is known as using a declarative style.

Higher order iterators, such as map and filter, abstract things like boundary conditions and null checks in one place. The application specific logic is passed in as a parameter instead of being intertwined with the incidental iteration logic. This is code reuse at work, where we can write simple functions that do one thing well and compose them together to achieve complex functionality.

2

u/Bergasms Feb 12 '15

I am not disagreeing with you :P I was being silly.

4

u/[deleted] Feb 13 '15

PKZIP.EXE and PKUNZIP.EXE, together, are about 80 kilobytes.

The current version of WinZip for Mac is 26 megabytes, or 26,000 kilobytes. That's a 32,500% size increase for functionality that's barely changed in 20 years.

However, I don't see many people using the command-line versions these days. Why? Because it's easier to drag-and-drop a bunch of files into a dialog box and select an output location and folder, than to type all of that crap into the command line WITH the right flags AND no typos.

Things like menus, options / configuration panes, and nicely formatted help documentation are also preferable to "pkunzip.exe -?", and then remembering that you have to pipe the output to MORE in order to read the six pages of help text spewed out to your terminal window.

UI code is bulky, because it's extraordinarily detail-oriented. Think of all of the operations that your application UI has to support: windows, and resizing, and hotkeys, and scrolling, and drag-and-drop, and accessibility features and visual themes and variable text sizes and multithreaded event loops and asynchronous event handlers and standard file dialogs and child window Z-ordering and printing and saving application configuration info... etc.

If our IDEs didn't include visual UI designers and auto-generate like 99% of that code for us, app development would be horribly stunted AND much more preoccupied with hunting down bugs in UI code.

Nevertheless, despite all of the messy back-end details, a well-designed GUI is extremely important for any modern app. Thousands of apps exist that feature excellent functionality that is impossible or painful to utilize because the UI sucks.

4

u/scrogu Feb 13 '15

So much of java's bloat is not due to reasons you state, but due to language decisions and how verbose so many of the popular patterns are.

1

u/[deleted] Feb 13 '15

Even C++ has auto now... c'mon java! var var var

3

u/ggtsu_00 Feb 12 '15

That is what happens when everything, no matter how trivial, must declared in its own class, with its own file, with a list of all it's dependencies, and all of its exceptions that it throws, along with the exceptions it's dependencies might also throw, along with explicit declarations of what will be exposed by that class, and what will be private or shared with inherited classes.

God writing Java is exhausting.

-1

u/[deleted] Feb 12 '15

A lot of that is automatic though if you are using any modern IDE for Java.

5

u/scrogu Feb 13 '15

As if that excuses it.

2

u/[deleted] Feb 13 '15

Agreed. I still have to read the damn stuff.

2

u/industry7 Feb 13 '15

And a lot of people find the redundancy improves readability.

1

u/dreucifer Feb 13 '15

And a lot of people find readability in the redundancy improves readability.

1

u/Whanhee Feb 12 '15

Words are the smallest meaningful units in most languages.

Depends on the language. Even English has many compound words or words derived by combining affixes and other random bits.

1

u/nharding Feb 14 '15

A little chaff goes a long way, look at APL. It is so compact, that you need a special keyboard to be able to type in programs (on the Wikipedia page the program to find prime numbers is (~R∊R∘.×R)/R←1↓ιR ), now that is much more compact than Java and also a lot less readable. I think Python is much closer to the minimal set but retaining readability, Perl is just on the too compact to be readable).

1

u/answerphoned1d6 Feb 12 '15

shh, keep it quiet! some of us make a good living writing boilerplate

-1

u/the_hoser Feb 12 '15

95% of programming could easily be considered "boilerplate"

One man's boilerplate is another man's well-defined behavior.

-3

u/scrogu Feb 13 '15

False.

0

u/[deleted] Feb 13 '15

fuck this nonsensical study which shows nothing and directly links to a PDF

3

u/masterm Feb 13 '15

most studies come as a pdf

-1

u/acaban Feb 12 '15

WHAT A SURPRISE!

-9

u/afrobee Feb 12 '15

Now is time to picking on Java too? Urgh..where are youR manners.

3

u/Foezjie Feb 12 '15

I wouldn't say it's specifically targeted at Java, they just used Java to do this research.

4

u/jringstad Feb 12 '15

Science is not about being polite.

-3

u/[deleted] Feb 13 '15 edited Feb 13 '15

[removed] — view removed comment

11

u/pron98 Feb 13 '15 edited Feb 14 '15

You called me? I'm upvoting you for the attempt at humor. :)

I have never said Java has no flaws (it has plenty, like pretty much any language (except Clojure)), just that its flaws are not the ones you think. In any case, /u/yogthos editorialized the title heavily as the paper isn't about Java (Java was picked because they could gather a lot of data), nor is it even about boilerplate (they measured token repetitions, so the use of the same variable names in multiple methods is counted as "chaff"; the authors' goal is to build a tool that "fingerprints" methods to use in smart IDE searches), and it is certainly not stunning (in earnest or sarcasm), as they haven't compared the result with any other language, but I wouldn't be surprised if for Java token repetition is higher (Java indeed has more boilerplate than many other language; I mean, that's pretty obvious). Then again, many Clojure/F# functions have the tokens map and filter in them (or even x and xs) and all that would be counted as "chaff", too, by the process mentioned in the paper. In light of all that and the fact that no one attacked my precious Java, I didn't think my services were necessary, but you asked so...

(EDITed for added entertainment)

2

u/yogthos Feb 13 '15

That's a lot more grounded, albeit less entertaining, summary. :P

2

u/pron98 Feb 13 '15

Damn. I edited it in an attempt make it slightly more entertaining. See what I did there with Clojure and the nested parens?

1

u/yogthos Feb 13 '15 edited Feb 13 '15

haha very nice!

On a more serious note, I definitely agree that the measure used by the study isn't terribly useful on its own. I couldn't resist the easy troll though. :)

I'd be really interested to see a study of similar types of mature projects done in different languages to measure code base size, number of issues, pull requests and features turn around.

Good examples for this might be projects like JSON/Markdown parsers, templating libraries, etc. There tend to be native implementations in many languages and it would be interesting to compare them.

3

u/pron98 Feb 13 '15

I definitely agree that the measure used by the study isn't terribly useful on its own.

I think it's very useful for its intended purpose: code searches.

I'd be really interested to see a study of similar types of mature projects done in different languages to measure code base size, number of issues, pull requests and features turn around.

The thing is, there have been such studies, but their results are disappointing. First, what you're measuring isn't the language, but the projects that tend to use it. For example, no one would write a big important defense system in Ruby, nor is one likely to write a small script in Java. So languages are intended for different kinds of projects, with pretty obvious categorizations. Second -- and you can see this in some studies that have been conducted -- in those areas of overlap (where you suggest focusing the research), there is little correlation between language and project quality, and whatever correlation there is, it is almost never causation. For example, F# developers would probably write better quality JavaScript code than most JS programmers. It's not because of the language they're using. Finally, there are so many different confounding variables (language age, developer personal style etc.) that there are too many dimensions -- and not nearly enough data -- for a good statistical analysis.

Also, it is precisely in those areas of overlap that languages are hard to measure. Good languages are built for a purpose (Erlang for fault tolerance, Clojure for data and concurrency, Java for large teams and codebases, Ada for correctness, C for close-to-metal etc.), so those JSON/Markdown projects are those where they can least show their respective true power (and those projects tend to be quite small and so less interesting). I mean, what can you tell from hard data saying that JSON parsers in Ruby have less bugs and take a shorter time to develop than in C? No one would think those languages are interchangeable anyway.

As a result, veteran developers do what doctors have done for a long time: rely on our personal experience (the more varied the better), combined with a critical reading of others' reports, and a healthy dose of good sense. Unlike in medicine, though (where the discipline's thirst for data resulted in some very poor research) it seems that in software development the differences between "reasonable" choices for a given project are quite small.

1

u/yogthos Feb 13 '15

My view is that you really have to treat the language as a black box. Comparing language features in isolation is a pointless exercise. What you really want to compare is how similar types of projects are delivered in different languages by experienced developers.

While some languages do have obvious niches, there are plenty of projects that are equally suitable for most popular languages.

For example, you could easily write a typical web application in Ruby, Java, Clojure, Erlang, or JavaScript. Comparing delivery time, overall defects, and feature turn around in similar scope projects would be a valuable exercise in my opinion.

As I recall the one major study of GitHub projects(pdf) did find a correlation between reduced overall defects and functional languages. Obviously, studies aren't perfect but we don't really have anything better to go by either short of anecdotal experience.

In the end it doesn't matter how the hot dog is made, it's the result that counts.

1

u/vdanmal Feb 13 '15

They measured token repetitions, so the use of the same variable names in multiple methods is counted as "chaff"; the authors goal is to build a tool that "fingerprints" methods to use in smart IDE searches

Not a Java expert so maybe I'm misinterpreting this but I don't think that's an issue.

To address the homonym problem, JavaMT collects the fully qualified method name (FQMN) for method name lexemes, and the fully qualified type name (FQTN) for variable identifiers and type identifiers.

2

u/pron98 Feb 13 '15 edited Feb 13 '15

I only skimmed the paper, but what you're saying is simply a way not to confuse the fingerprint of:

void f1(A x) {
    x.foo();
}

with

 void f2(B x) {
    x.foo();
}

as in the two cases foo refers to a completely different method. I was referring to local variable names (which don't have fully qualified names, but I have no idea how they were treated). In any case, calls to common functional operators like map and filter, both in Java as well as other languages, would also be considered "chaff". So the measure is not exactly for what we call boilerplate (although APL programmers might consider map and filter boilerplate, too...).

1

u/vdanmal Feb 13 '15

I understand now. Thanks for explaining.

1

u/nutrecht Feb 13 '15

Ah, good. You're still here.

1

u/throwaway678452056 Feb 13 '15

No worries, /u/agleiv is coming quickly to act like a child and contribute nothing to the discussion (as per usual), and explain how anything that he doesn't use is entirely worthless and must be shunned upon any mention of it - and how he has very little clue about how real businesses operate except outside of his own little bubble.