I was looking at code.golf the other day and I wondered which languages were the least verbose, so I did a little data gathering.
I looked at 48 different languages that had completed 79 different code challenges on code.golf. I then gathered the results for each language and challenge. If a “golfer” had more than 1 submission to a challenge, I grabbed the most recent one. I then dropped the top 5% and bottom 5% to hopefully mitigate most outliers. Then came up with an average for each language, for each challenge. I then averaged the results across each language and that is what you see here.
For another perspective, I ranked each challenge then got the average ranking across all challenges. Below is the results of that.
Disclaimer: This is in no way scientific. It’s just for fun. If you know of a better way to sort these results please let me know.
I’m really surprised to see Java ranked as less-verbose than OCaml.
Here’s an equivalent code sample in Java 17 vs OCaml:
Java:
abstract sealed class Expr permits Value, Add, Subtract, Multiply, Divide { abstract long eval(); } record Value(long value) extends Expr { @Override long eval() { return value; } } record Add(Expr left, Expr right) { @Override long eval() { return left.eval() + right.eval(); } } record Subtract(Expr left, Expr right) { @Override long eval() { return left.eval() - right.eval(); } } record Multiply(Expr left, Expr right) { @Override long eval() { return left.eval() * right.eval(); } } record Divide(Expr left, Expr right) { @Override long eval() { return left.eval() / right.eval(); } }
OCaml:
type expr = | Value of int | Add of expr * expr | Subtract of expr * expr | Multiply of expr * expr | Divide of expr * expr let rec eval = function | Value value -> value | Add (left, right) -> (eval left) + (eval right) | Subtract (left, right) -> (eval left) - (eval right) | Multiply (left, right) -> (eval left) * (eval right) | Divide (left, right) -> (eval left) / (eval right)
…Java has so much more syntactical overhead than OCaml, and that’s even with recent Java and being pretty aggressive about using boiler-plate reducing sugars like Records. And F# has even less, since it doesn’t require you to use different operators for numerics or do as much manual casting between strings/numerics
It makes more sense when you realize it’s based on code.golf submissions. No one is going to create a class like that for a code golf problem. They’re probably not going to create any classes (other than one just to hold the main function).
I’m pretty sure I can do the Fibonnaci one with less code than your simple class. Because these problems are simple enough they don’t benefit from any OOP stuff so you avoid most of the syntactic overhead.
I am surprised it’s not higher in the list since the overhead of setting up a main method is still quite significant compared to most languages. But other than that, these problems can be solved without running into any egregious examples of overhead.
Everyone’s comparing the placements of their favorite language and I’m just left wondering how GolfScript gets beaten by seven other languages. I take it out was created specifically to do well on code golf? Mission failed, I guess.
I’d love to see the same comparison with more real-world use-cases.
Code golf, is mostly pretty simple use-cases, which have been optimized many times over.
When, you build out an application with a user-interface, proper event handling, etc… c++ is MUCH more verbose then c# for example, and they are ranked pretty close together.
I think code golf is a great dataset for this kind of analysis specifically because they are artificial and people are paying attention to the number of characters used. Leetcode solutions might be a better option though.
In real world projects there are too many confounding factors. People aren’t implementing servers in brainfuck or websites in C. Even rewrites of a project into another language have more/fewer features. So it’s an apples to oranges comparison.
But a big problem with this dataset is error handling - or really the complete lack thereof. Real code needs to deal with errors and they can add a lot depending on the language.
I was very surprised to see rust and go so close as I find go vastly more verbose due to error handling and need to reimplement things like searching a list. But code golf type problems ignore these types of things that you see in real code.
So there is not really and useful conclusion that can be made except if you spend all day writing code golf problems.
That’s true, and you can also combine multiple errors to have a single catch block or handle each error separately. The perfect dataset for this comparison will need to be written. Code golf data is good enough for a non-academic fun analysis like this one.
It’s interesting, the results here are way different than the Code Golf & Coding Challenges Stack Exchange. I would never expect Haskell to be that low. But after looking at code.golf, I realize it’s because I/O on CG&CC is more relaxed. Most Haskell submissions are functions which return the solution.
Sidenote: I like the CG&CC method, it’s semi-competitive, semi-cooperative.
- all languages welcome
- almost all users post “Try it Online”/“Attempt This Online” links
- most users post explanations under their submissions
- often people will post solutions beginning with “port of user1234’s excellent Foolang answer” when there’s a clever shortcut someone finds
- or people will post their own solution with “here’s a solution which doesn’t use user1234’s algorithm”
- or people will add comments to answers with minor improvements
IMO It’s geared towards what is the best part about code golf: teaching people about algorithm design and language design.
Java placed way better than I expected
You can write concise Java. Just like you can write readable Haskell. It’s just not idiomatic to do so.
Code Golf rules allow people to submit an anonymous function instead of a full program, which eliminates a lot of the boilerplate.
It is always dismissed as too verbose, while in go’s case it is never mentioned, when in fact the latter is way more verbose… People’s bias show.
Maybe also bias by the number / experience of people using it.
1st semester students getting shocked by
public static void main(String args)
and meming it on the internet.Go on the other hand likely isn’t a common choice / option for a first language.
I will gladly complain any day about go being terribly verbose.
I wonder if it’s all those variables named with single letter and abbreviations, so annoying to code review
String IDontKnowWhatsWorseEspeciallyWhenTheTypeIsAlsoIncludedString = “I don’t know what’s worse, especially when the type is also included”;
Honestly, I prefer an overly long name over some cryptic naming scheme that looks like minified JS. At least you can be sure of the variable’s purpose and don’t have to guess, which is far better for readability.
System.out.print("I agree.");
Don’t you mean:
class AgreementManagerClass { public static void main(String[] args) { System.out.println("I agree."); } }
They weren’t writing enterprise Java or they’d need a dozen factories and a few factory factories and probably a factory factory factory just to be safe.
im confused, c and c have header files that are super verbose, not sure how its so high up that list
Header files are optional, they duplicate function declarations to share between multiple files, but otherwise you could write c/c++/c# without headers… the compiler might just run out of memory.
On another look, though, we have to keep in mind, though that this is code-golf, so in no way representative for actual code-bases.
Not expected C# to be this high. Also did not expected F# be so far away from C#.
In newer versions of C# you can use top level statements, which remove a lot of characters
Yeah that seems suspicious. F# is pretty succinct.
My guess is that this is really a measure of how much abuse the language will tolerate. C# probably lets you get away with a bunch of things (like checking for nulls) that F# requires.
Rust is so brief it’s not even listedI can’t readThis is why I only use machine code
This is why I love Ruby: Nearly as concise as Python but never complains about whitespace or indentation.
I’ve never understood the complaint about forced indentation. What kind of monster doesn’t use indentation for their code anyway?
If anything, it’s nice that the language forces it on you so that you don’t stumble on code written by one of those monsters.
For me at least, it’s less about forcing indentation as much as limiting what I can do with visual indentation.
Sometimes, it’s nice to group lines at a given indentation level for visual comprehension vs the needs of the interpreter.
And to be fair, I don’t hate Python’s indentation style. It’s usually not a major problem in practice. It’s just that without the ability to override it, I lose a tool for expressing intent.
You can get some bad bugs due to the fact that white space is significant, not because you are using it. For example how is the IDE supposed to know when you’re done writing your if statement? Or done with a loop? It’s impossible. It’s pretty telling that Python is the only language on this list that has significant white space.(somebody please check me on this, but I’m pretty sure I didn’t miss anything here).
I write pretty clean code already, and the white space errors just get in the way of getting things done.
Clean code would have indentation though, and you can use whatever space you want as indentation. Bonus points if you use tabs so that others with special needs can configure the tab length on their end.
And I don’t think I’ve encountered an indentation error since the day I learned the language. How often do you encounter that error when writing python scripts? Sounds more like a theoretical problem than something anyone used to python would encounter.
Haskell being so high really doesn’t make any sense. Experience level maybe?
It’s one of the tersest languages out there.
Bash being so high is what confuses me.
Damn near everything is an acronym
Who tf uses OCaml. It was created by my alma mater, we hated studying that shit, it was invented for crazy people.
we hated studying that shit
It’s a more than fine functional programming language if you ask me. Was that the functional aspect that you disliked? Or the syntax? Because in one case like the other I’ve got some bad news for you about what’s to come in the programming languages landscape :)
Who tf uses OCaml.
The rust compiler (initially), large financial companies, the energy sector, etc: practically anywhere functional programming shines
written by a Scala programmer
Seems quite nice compared to bloody scheme
I’m not familiar with code.golf but I wonder how whitespace is handled? I find python is very concise anyway, but I wonder how the white space is counted (single tab, four spaces for black, etc).
Surprised by C# and Java. People always moan that they have too much boilerplate code and something else about how OOP sucks and that makes these languages too verbose, yet they’re close to the top of the chart here for least characters used on average.
I think Java’s verbosity has more to do with the culture than the language itself
I suspect this is more a symptom of “enterprise” design patterns than the language itself. Though I do think the standard library in Java is a bit more verbose than necessary.