Coming back to Java after learning Objective-C made realize how named parameters are awesome
Objective-C doesn't have named parameters (that is, Lisp-like "keyword" parameters). It has a hack which allows a method to be so named that it has to be used in a named way. Named parameters can be in any order and may or may not exist in the function call. Obj-C can't do this because its "names" are actually just part of the method signature. It's fake.
Named Parameters in Java
31–40 of 62 posts
Re: Named Parameters in Java
#32Interesting. But it looks like it is solving a problem with another problem. The receiving class would then look like this: public void doSomething(Name name, Link link, UltimateAnswer ultimateAnswer, TempFile tempFile, Zip zip) { String name = name.name; String link = link.link; int ultimateAnswer = ultimateAnswer.ultimateAnswer; String tempFile = tempFile.tempFile; int zip = zip.zip; } If the problem is "I don't li…
Also, I don't think the original problem was the same values being used all over the place. The problem is how to be clear about which parameters you're providing, and accepting static types and giving the type a simple way of constructing it solves that.
Re: Named Parameters in Java
#33Earlier quoted context omitted.
On the other hand, the Law of Demeter says that you should almost always be passing in and fetching primitive types. At least in an API. Does it not? Though I'm guessing that "primitive types" would include algebraic data types, which serve the same purpose here as little objects like IDObject. And since Java doesn't have algebraic data types.... All the more reason to switch to Scala. Edit : I should have written "b…
Hmm, I've never seen Demeter interpreted to mean that you should favour primitives over rich types. Can you explain your interpretation?
The Law of Demeter is also sometimes summarized by "One dot: good. Two dots: bad."
Or from Wikipidia:
In particular, an object should avoid invoking methods of a member object returned by another method. For many modern object oriented languages that use a dot as field identifier, the law can be stated simply as "use only one dot". That is, the code a.b.Method() breaks the law where a.Method() does not.
Edit: I should have written "built-in types" above, rather than "primitive types".
Re: Named Parameters in Java
#34It might be crazy but I like this kind of DSLization of Java a lot
Examples like Guava:
Cache userCache = CacheBuilder.newBuilder()
.initialCapacity(7)
.expireAfterWrite(20, TimeUnit.MINUTES)
.build();
Or Hamcrest: assertThat(result, containsString("OK"));
Or Rest-Assured: expect().statusCode(200).when().get("/user/1");Re: Named Parameters in Java
#35Wouldn't the Builder pattern address this issue? So you can do: object.setName("Alfred E. Neumann") .setLink(" http://blog.schauderhaft.de ) .setUltimateAnswer(42) .setTempFile("c:\\temp\\x.txt") .setZip(23);
It's not addressing named parameters issue, but I find it better like this.
Re: Named Parameters in Java
#36or you know, use an IDE!
This is a good observation, people who seem to like named parameters strike me as people who might code in plain text editors a lot. Nothing wrong with that but an IDE makes things so much easier and faster.
Re: Named Parameters in Java
#37Since long parameter list is considered a code smell, people should be discouraged from writing this kind of code anyways. Namely, this example from the article is in fact a bad practice: > o.doSomething1("Alfred E. Neumann", " http://blog.schauderhaft.de , 42, "c:\\temp\\x.txt", 23); As other commenters have pointed out, I think the builder pattern is one of the two solutions I would use. The other solution is to si…
But yes, long parameter lists (whether represented as argument structs or not) generally indicate a problem.
Re: Named Parameters in Java
#38Earlier quoted context omitted.
Hmm, I've never seen Demeter interpreted to mean that you should favour primitives over rich types. Can you explain your interpretation?
I've heard the Law of Demeter at times stated explicitly as that you should usually return only primitive data types from an API. Rich Hickey certainly claims this too, although he doesn't refer to it as the Law of Demeter. The Law of Demeter is also sometimes summarized by "One dot: good. Two dots: bad." Or from Wikipidia: In particular, an object should avoid invoking methods of a member object returned by another…
Re: Named Parameters in Java
#39Since long parameter list is considered a code smell, people should be discouraged from writing this kind of code anyways. Namely, this example from the article is in fact a bad practice: > o.doSomething1("Alfred E. Neumann", " http://blog.schauderhaft.de , 42, "c:\\temp\\x.txt", 23); As other commenters have pointed out, I think the builder pattern is one of the two solutions I would use. The other solution is to si…
Re: Named Parameters in Java
#40Earlier quoted context omitted.
"when ID needs change, and they will over the course of a project" That sounds awfull like YAGNI would apply: http://en.wikipedia.org/wiki/You_ain%27t_gonna_need_it
There is a small but meaningful distinction: Encapsulating the data in OOP fashion allows you to respond when the need arises . You don't build it until you need it, but when/if you do ever need it you won't have to refactor the interface (and the interfaces of all the classes that use these data). This minimizes changes down the road and leads to maintainability.
If you do need to pass in additional information, refactor and change the primitive to an object when you really need to, not because you might need it at some point in the future (and the chances are you won't).