Live data from Hacker News

Named Parameters in Java

java.dzone.com

1–10 of 62 posts

Re: Named Parameters in Java

#3

It might be crazy but I like this kind of DSLization of Java a lot

I think it would really shine with IDE support. It probably wouldn't even be that hard to do (famous last words). I think I'll take a stab at it.

I like it as well, and have done similar things by creating input/output classes to use as "structs" when the list of parameters gets unweildy or when I want multiple returns.

Re: Named Parameters in Java

#5

It might be crazy but I like this kind of DSLization of Java a lot

I think it would really shine with IDE support. It probably wouldn't even be that hard to do (famous last words). I think I'll take a stab at it. I like it as well, and have done similar things by creating input/output classes to use as "structs" when the list of parameters gets unweildy or when I want multiple returns.

When "the list of parameters gets unweildy" and "I want multiple returns" are to major indicators that you got some anti-patterns going on. I think the refactoring that you need might a new class with a single responsibility.

Re: Named Parameters in Java

#6
Whoever wrote the original code is missing a major point in OOP: you should (almost) never be passing in simple types. Create an object that holds the data (class IDObject, for instance), and pass an instance of this class in for the parameter. Then, the parameters are de facto named in the data object.

This importance of this method is that it allows the you to update the information passed in via the data object (when ID needs change, and they will over the course of a project) without breaking the interface. And that's the point of it all.

Re: Named Parameters in Java

#7
Wow this would make for some crazy code to try to read thru. I would be über-pissed to trace thru code like this only to find all of these wrapper classes. This is what Javadocs are for, it's much easier and cleaner just to document your code as you write it.

Re: Named Parameters in Java

#8
Interesting. 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 like these SAME values being used all over the place" then use DEFAULTS.

    o.doSomething1(); //defaults to "Alfred","c:\\temp",42,"shadurhaft.de",23
That or pass in an object. o.doSomething1(mySomethingObject);

If the values are genuinely different then the only way to solve this problem is to validate the options (paths should be a valid file location and not a website, websites should be a valid website etc) and then (possibly) pass in an object instead of primitives.

I don't see encapsulation doing anything really, aside from increasing the number of "junk" classed by about a hundred fold.

Why throw standard convention out the window?

Re: Named Parameters in Java

#10
post #6

Whoever wrote the original code is missing a major point in OOP: you should (almost) never be passing in simple types. Create an object that holds the data (class IDObject, for instance), and pass an instance of this class in for the parameter. Then, the parameters are de facto named in the data object. This importance of this method is that it allows the you to update the information passed in via the data object (w…

Never passing in simple types?

I don't know about you, but one of the reasons I like languages such as Python and JavaScript is I don't have to create a wrapper object for every type I'm using. The fact I had to, for instance, create URL and IP Address and such objects in C# drove me crazy.

Post reply on HN