Lies. Java is intrinsically verbose. Say I want to make a class with
a read-only attribute "Foo".
CLOS:
(defclass class () ((foo :reader get-foo :initarg :foo)))
Moose:
class Class {
has 'foo' => ( is => 'ro' );
}
Java:
class Class {
private String foo;
public Class(String foo) { // named initargs are clearly for losers
this.foo = foo;
}
};
Notice how a common high-level task requires a manual error-prone
implementation, every single time. Java provides only the concepts of
"class", "field" and "constructor", while the other languages provide
the high-level concept of an "attribute", which is what the programmer
actually wants. (The first concept is easy for the language
implementor, the second concept is easy for the practicing
programmer.)
I should also point out how the verbosity exists only for the sake of
being verbose, not for creating more reliable software. Perl and CLOS
assume I want named initargs, making use of my class self-documenting:
That's verbose, but it makes the code very easy to read and write. No
guessing about what slot is being set to what value.
With Java, though, it's anyone's guess as to what's going on:
new Class(42, 123);
The only way to know which field gets set to which value is to Read The Fine Source:
public Class(int bar, int foo){
this.foo = bar;
this.bar = foo;
System.Utils.delete_users_pr0n_stash();
}
foo does get set to 42, but only by accident... and there are unexpected side effects.
So much less typing though! cough.
Another common programming pattern is reusing code. (I hear people
like doing that.) Say you have classes representing numbers, and you
want to add a "not equals" method to each type. That's easy, you can
just write a generic role like:
Then I can easily reuse that extremely-complex "not_equal" method:
class Int with Eq {
has value => ( is => 'ro', required => 1 );
method equals($a: $b){
return $a->value == $b->value;
}
}
Int->new( value => 42 )->not_equals( Int->new( value => 123 ) ); # True
In Java, I can still maintain a similar interface:
interface Eq {
public bool equal(Eq that); // Eq is not really the type I want, but whatever.
public bool not_equal(Eq that);
}
But the implementation can't be shared:
class Int implements Eq {
private int value;
public Int(int value){ // didn't we just say "int value" like one line ago?
this.value = value;
}
public bool equal(Int that){
return this.value == that.value;
}
public bool not_equal(Int that){
return !this.equal(that);
}
}
This is both verbose and error-prone. (I will also point out that
Java calls Eq "Comparable" and uses the "implements" keyword instead
of "with". But this is petty. I'm surprised they were happy with
"implements" and didn't choose the clearer expression
"implementsTheInterfaceThatIAmAboutToTypeRightAfterIAmDoneTypingThis".
Your editor can type that for you, after all...)
But wait, we can use the strategy pattern to save us!
class NotEqualStrategy {
public bool not_equal(Eq a, Eq b){
return !a.equal(b);
}
}
class Int {
private NotEqualStrategy not_equal_strategy;
private value;
public Int(NotEqualStrategy nes, int value){
this.not_equal_strategy = new;
this.value = value;
}
public bool equal(Int that){
return this.value == that.value;
}
public bool not_equal(Int that){
return not_equal_strategy.not_equal(this, that);
}
}
At least I don't have to cut-n-paste the logic anymore, but it's still
very verbose. Imagine you had to delegate more than one method -- you
can do it, but it involves a ton of meaningless code.
In Perl, I can just say:
class NotEqualsStrategy {
method not_equal(Eq $a, Eq $b){
return !$a->equal($b);
}
}
class Int {
has 'not_equals_strategy' => (
is => 'ro',
isa => 'NotEqualStrategy',
required => 1,
handles => ['not_equal'],
);
has 'value' => ( is => 'ro' );
method equal(Int $a: Int $b){
$a->value == $b->value;
}
}
Notice how I didn't have to type the cut-n-paste delegation; the
language did it for me. I didn't have to type it, I don't have to
look at it, and I can't accidentally fuck it up. That's what
programming languages are supposed to do for you, and that's what Java
doesn't do.
In conclusion, Java is verbose, and verbosity is harmful.
I read those exactly the same. There's no difference to me apart from taste, and potential for errors.
The potential for errors however, is far higher with the Moose and CLOS versions. Which is why I wouldn't like to use either of those syntax in a large project.
The java version, you can only really typo on the variable name. And even if you do that it likely won't compile unless your variables are similarly named. But with the moose example, you're starting to have silly things like '=>' which can easily be typoed and which can still compile, but completely change the meaning of the code.
Obviously at the extreme of this is regexp. Hugely compact, but a complete bitch if you typo anything. Personally I'd hate to write complete programs in regexp type syntax.
So you can keep your non-verbosity at the expense of sanity.
>> "In conclusion, Java is verbose, and verbosity is harmful."
I'd say Java has longer tokens than some other languages. By my definition that doesn't make it any more verbose. It makes it harder to go wrong with typos etc.
If you look at the underlying programming constructs in your first 3 examples, they're the same... thus for me, those code fragments are the same. No more or less verbose.
I think some of it just comes from having experience of reading code. Once you learn to read code in terms of programming concepts, rather than actual lines of code, you see no real difference.
The big issue is having to manually feed data from the constructor into the fields. There is no high-level concept of "attribute" in Java, which is what programmers actually want.
In Perl, you say "has foo => ( init_arg => 'foo', isa => Whatever, reader => 'get_foo', writer => 'set_foo' )" and the correct code is generated. In Java, there is no concept of "attribute", so you have to build it every time by manually declaring the field, manually writing the constructor, and manually writing the getters and setters. Anything you do manually more than once has the potential to introduce a subtle bug, and more importantly, is tedious and degrading.
It'd take you 2 minutes to write a java preprocessor to build your constructors if you really care about such things. (I don't particularly. I don't spend much time at all writing constructors, and often want to do nonstandard things, validation etc rather than just passing over variables as they are given).
With due respect, surely that's what being a good programmer is about? There's no law against preprocessing java if you really dislike the defaults.
How many constructors do you write? Sounds like perhaps too many.
But almost everything in Java is more verbose, and so you'd end up writing "preprocessors" for everything (and the strong, static, explicit typing means that a lot of this preprocessing would have to be rewritten for even slightly different cases).
So why not just cut-out the middle man and work in a higher level language directly?
>> "But almost everything in Java is more verbose"
Sure, that's the common misconception. You're correct. However, if you actually look at the language and write stuff in it, you'll find it's not actually true.
The issue is when people look at some existing insane Java code and cite it as proof Java is verbose.
I'd absolutely agree - most open source / freely available Java code is verbose. Several of the libraries (enterprise etc) are verbose and insane. But that's not Java.
Higher level languages are not always better. Use what is suited to the job at hand. I'm far more comfortable working at lower level than high level.
Which is better.... talking high level and relying on a compiler/runtime to figure out what you want? or just talking a bit lower level in the first place.
I completely agree with the point you are making about accessors, but something I don't agree with is the way you made your point:
> In Perl, I can just say:
Moose is an OOP framework built on top of Perl, comparing it to out-of-the-box Java code is quite misleading and I'm not sure what point it proves? It's like saying MVC in PHP is verbose compared to Django or Rails rather than Python or Ruby. Using MooseX::Declare is even more misleading. Does that even work on Windows yet?
So you're saying that the argument would work better if the examples were in Ruby? At first glance, I'd say they would translate quite easily into idiomatic Ruby.
jrockway is a Perl programmer; he writes in the language he knows.
And if you object to comparing against "out-of-the-box" Java code, that's fine. My understanding is that there are, in fact, JVM-based tools that let you write code in the approximate style of Moose. The only stumbling block is that these tools are called "JRuby" or "Clojure", and the average Java programmer doesn't know or recognize them. [1] Certainly they make no appearance at all in the original article.
---
[1] Or maybe my impression is wrong and one or another of the alternative JVM languages really is on track for widespread recognition. I wouldn't know.
Please don't misunderstand my point - I agree with what you were saying and I think Java is far too verbose. I just think by comparing a framework to a language you are misrepresenting the capabilities of Perl and the first Java programmer who knows how much of a hack OOP is in Perl could completely tear your post apart.
I mean, in Java can you do this?
class MyClass {
private String foo;
public Class(String foo) {
this.foo = foo;
}
}
var inst = new MyClass();
inst._foo = 'I just broke your encapsulation.';
Because you can with psuedo-privacy in Perl and Moose.
The other thing is that OOP in Perl without Moose is even more verbose than Java. I will never get over having to write my own new subroutine :)
P.S. MooseX::Declare absolutely did not compile on Windows last time I tried back in August. When I asked about this on IRC, it was a "known issue."
Language vs. framework is a meaningless discussion. All languages are frameworks for getting a big chunk of silicon to do stuff. I'm not sure how Perl's OO is a "hack", in my opinion, people that say this only have a superficial understanding of what object-oriented programming is. It's a way to organize code, it's not a way to prevent people from calling methods. Perl's OO is superset of Java's OO. Hack or not, it encourages reuse and modularity, and makes this very easy for the programmer to get right.
"private" is one of the worst programming language concepts ever invented. Perl's lack of this bug is a feature. Private methods are nice for documentation purposes, but most developers misuse "protected" and "private", and it makes their libraries nearly impossible to use. In a perfect world; great idea. In the real world; a huge waste of time.
Why are private methods good for documentation? They shouldn't even be documented save for inline comments. The whole idea of privacy in my mind is that the only person or people who should know that these functions even exist are those who maintain the class/library/framework. This helps immensely because when you want to refactor you know EXACTLY which method signatures or attributes have to be supported for backward compatibility. You can really keep on top of things, and as long as you took the time to create a flexible API, you can handle change gracefully. I don't even unit test my private methods because I don't even care about them or what they do. I only care that some data is passed to a public API and returns the correct result. They are there, as you say, purely as a means to organise my code.
For example, in my last company I wrote this ORM in Moose. I released it. And the guys I was working with just didn't get the concept of Moose. They kept accessing the attributes from the underlying blessed hash like they did before rather than the accessors. It still worked because internally Moose stores stuff the way Conway recommended in OOP Perl (which was a weird decision imho). One day I renamed one of my database columns and so had to rename my accessor and created a manual function that warned of "obselete call to accessor x" whilst returning the new column name... backwards compatibility, yay! But they didn't see this because they didn't use the accessor and now all their manual hash accessing was returning undefined or null or whatever it is.
Of course it only took me going over to their table and explaining to them how Moose worked or getting together for a meeting. Fine. But that was a company with a technical team of around 10 people. Now I work for a big tech company, and I'm in charge of a framework again except now its JavaScript which has the same problems when people force this classical inheritance pattern and now when some guy doesn't get it he's in another building or another country and did so in a repository I didn't even know existed and his code that breaks my encapsulation is in a production device and when I refactor my code I have the possibility that I break live code on some project or some device I don't know about.
So yeah, privacy is an important concept and if you don't think so I would say it is you who lives in a superficial bubble where everyone who uses your API or works on your code can be trusted to have good sense.
No programming language will solve the problem of bad programmers. If you can edit the code, private methods provide no protection -- the programmer that wants to get at that functionality will just change it to public and recompile, or worse, cut-n-paste the code into his own module. The enforced privacy, therefore, adds no safety. Not enforcing privacy at least allows you to write a subclass that carefully changes the behavior of the original, at the risk of coupling the subclass to the superclass (but this is better than cut-n-pasting).
In an ideal world, you could rewrite every library that was designed incorrectly, but there is not always time for this. Intentionally limiting flexibility ("private") is worse for code reuse than allowing someone to intentionally change internal details. (Which is what privacy prevents.)
A good programmer will use his judgment to determine whether to cut-n-paste, subclass, or rewrite. The programming language should not remove options from his toolbox.
Now I work for a big tech company, and I'm in charge of a framework again except now its JavaScript which has the same problems when people force this classical inheritance pattern and now when some guy doesn't get it he's in another building or another country and did so in a repository I didn't even know existed and his code that breaks my encapsulation is in a production device and when I refactor my code I have the possibility that I break live code on some project or some device I don't know about.
Well, the tests stop passing. The tests are what ensure your code works, not "private" directives. "private" is just documentation that suggests "you probably don't want to call this directly". "Probably."
All I can say is that technical solutions to social problems never work. Rewrite your app in Haskell and I guarantee that dumbass developers will still be able to mess up your code. People are much more clever than programming languages.
private and protected are fantastic for narrowing down bugs, and controlling access.
Certainly not a waste of time. In comparison, a looser language like javascript where any code can call any code any time it likes, can be a complete pain to debug.
It can call any code any time it likes, but it shouldn't. Why do you think you know what the Future User of your class wants to do better than that Future User? In theory, a good library will have a perfect public interface that anticipates every need that the future users will have. "protected" and "private" eliminate many classes of bugs in that case. But in the real world, they just eliminate many classes of features, because they prevent people from using imperfect libraries.
I hardly ever use external libraries so luckily don't have that problem, but I don't think it's a widespread issue :/ If you really want access to a private I'm sure you could work out a way.
>> "It can call any code any time it likes, but it shouldn't"
Sure but it's nice to have a lock on the door that says "I can say with 100% certainty that this code will not be called from anywhere else". That's a pretty useful feature to have.
That's only a nice feature until you pull out the Decorator or Adapter patterns to get around some class marked `final` in a library somewhere you can't modify and for which you have to appease the type system because classes do not imply interfaces.
People like getters and setters because they provide structured and consistent access, and decouple consumers from the internal implementation of your class.
If you have a "get_foo" method in your class, you can change what "foo" is under the hood without consumers knowing. If they touch the field directly, you can never change that -- your implementation becomes the interface. And the idea of OO (and good programming in general) is to avoid that.
You keep talking about "ignore Java's syntax and focus on being a better programmer", but I think if you use fields for your public interface, you are compromising the quality of your code. You should be able to have the protection that getters and setters afford without the cost of typing them into every class that needs them. That would allow the "better programmer" to write a better program.
It's not really rocket science to do a search+replace.
Getters+Setters have their place, where you may need to change things under the hood definitely. But most of the Java I see using them is just complete overuse of them for the sake of it.
FWIW, I only use getters+setters when I need to - eg they do something other than just get/set a variable. I think it makes for clean concise readable code.
No, I disagree. If I need to add a getter/setter so that it does something other than just access a field, I'll add that code at the point I need it.
It's not efficient to always use setters/getters, even if the language provides them.
The only sorts of things I find painful in Java is writing a class to do Comparator etc, but the number of times you need to do that can be counted on one hand.
Some people who write Java astonish me with their churning out of toString, equals, hashcode, getters, setters. Checkout a few open source projects and you'll see a whole set of files defining interfaces, then a whole mirror set of source files defining the implementation of that. Which is just ridiculous when there is only 1 implementation of everything. Makes the mind boggle, but often it's an IDE churning out autogenerated code (Does that still count as Java? ;) )
This is a good technique for libraries. Decoupling interface and implementation makes flexibility the default. If components only speak an interface to each other, it is easy to substitute them in the future.
The problem with Java is that the interfaces are usually useless. Imagine you have a build backend talking to a stoplight (red/green widget). The logical interface is for the builder to emit success or failure, and for the stoplight to internally change that to red or green. But of course, many people decide that the build backend should emit red or green, and that the stoplight widget should display that color. So while the build backend isn't coupled to an implementation in theory ("it just uses the RedOrGreenShowable interface!"), it actually is.
1. The above implementation was clearly intended to include a getter, since a class with only a private field and no way to access it doesn't make sense...
2. Using getters for properties is "idiomatic" Java, or at least it was last I used Java a few years ago...
3. More importantly, here we're comparing equivalent code in different languages and the lisp code specified a reader so I think it's only fair that the Java version should have the equivalent getter.
CLOS:
Moose: Java: Notice how a common high-level task requires a manual error-prone implementation, every single time. Java provides only the concepts of "class", "field" and "constructor", while the other languages provide the high-level concept of an "attribute", which is what the programmer actually wants. (The first concept is easy for the language implementor, the second concept is easy for the practicing programmer.)I should also point out how the verbosity exists only for the sake of being verbose, not for creating more reliable software. Perl and CLOS assume I want named initargs, making use of my class self-documenting:
That's verbose, but it makes the code very easy to read and write. No guessing about what slot is being set to what value.With Java, though, it's anyone's guess as to what's going on:
The only way to know which field gets set to which value is to Read The Fine Source: foo does get set to 42, but only by accident... and there are unexpected side effects.So much less typing though! cough.
Another common programming pattern is reusing code. (I hear people like doing that.) Say you have classes representing numbers, and you want to add a "not equals" method to each type. That's easy, you can just write a generic role like:
Then I can easily reuse that extremely-complex "not_equal" method: In Java, I can still maintain a similar interface: But the implementation can't be shared: This is both verbose and error-prone. (I will also point out that Java calls Eq "Comparable" and uses the "implements" keyword instead of "with". But this is petty. I'm surprised they were happy with "implements" and didn't choose the clearer expression "implementsTheInterfaceThatIAmAboutToTypeRightAfterIAmDoneTypingThis". Your editor can type that for you, after all...)But wait, we can use the strategy pattern to save us!
At least I don't have to cut-n-paste the logic anymore, but it's still very verbose. Imagine you had to delegate more than one method -- you can do it, but it involves a ton of meaningless code.In Perl, I can just say:
Notice how I didn't have to type the cut-n-paste delegation; the language did it for me. I didn't have to type it, I don't have to look at it, and I can't accidentally fuck it up. That's what programming languages are supposed to do for you, and that's what Java doesn't do.In conclusion, Java is verbose, and verbosity is harmful.