• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is this coding style common for intermediate Java developers?

 
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading the new book about JavaFX called "Pro JavaFX 2," by Weaver et al. (No, this is not a question about JavaFX) The very first code sample includes statements such as the following:



Now, I know enough about Java to know that the API must have been designed such that create() returns a datatype which has a layoutY() method, and layoutY() returns a datatype that has a textOrigin() method, and so on until build() returns an object of type Text. But, looking at the code it is impossible to tell which datatypes are returned at each stage.

I have dug around in the JavaDocs (right-click easy, thanks to a properly set up NetBeans) and my first instinct is correct, though the generics are still tripping me up.

Is this style of writing code actually common? Is it considered "intermediate" level stuff? Is it something I should just simply plan to get used to before trying to learn anything about JavaFX? Or did the authors of this book go way overboard on the "elegance factor" to show off what can be done, at the expense of creating way too much cognitive overload for what is supposed to be a hello world program?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Grant Robertson wrote:(No, this is not a question about JavaFX)
...

Now, I know enough about Java to know that the API must have been designed such that create() returns a datatype which has a layoutY() method, and layoutY() returns a datatype that has a textOrigin() method, and so on until build() returns an object of type Text. But, looking at the code it is impossible to tell which datatypes are returned at each stage.

I have dug around in the JavaDocs


First, this *is* a JavaFX question inasmuch as the sample code is for a specific JavaFX class.

Secondly, you don't determine a return type by looking at the code. You discover it from the API.

You say you have 'dug around' in the documentation for TextBuilder. What's the return type for those methods? What specific reference do they return?

Oh, and nothing about this 'coding style' is specific to beginner, intermediate or advanced Java developers.
 
Master Rancher
Posts: 4796
72
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's also not a JavaFX question, inasmuch as this style is used in other Java classes (mostly newer APIs) and the original poster's comment seems to indicate the intent of the question, not the JavaFX-specific nature of this particular example.

Grant: it's not exactly common yet in general, but it's becoming increasingly popular. It's increasingly common in newer libraries, but in older libraries you may not see it at all. So it really depends which particular libraries you're spending time with. It's not really a big deal; you just have to get used to the Builder pattern. It commonly looks something like this:

The thing to understand is that the first creat() method creates a FooBuilder object, which is a mutable objet used to accumulate the various settings you want before the Foo is finally created. And all the other method calls except the last one return the same FooBuilder object, letting you keep working with that same object until you're finally ready to create a Foo. When you're done with all the other settings, the final build() takes all those settings and uses them to create the Foo object.

If you think it's cognitive overload, well, you don't need to learn all the different setOption() methods at once. That's what documentation is for. You just need to know that there are options available, and if you find you need to change something about the Foo, the API is the place to look.

Note that the specific method names here may vary a bit from API to API; I think I've sometimes seen build() as the first method, to create the Builder, and create() or make() to create the final Foo object. But the pattern is the same, even if the method names change.
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Grant Robertson wrote:Is this style of writing code actually common?



It was fairly unusual back when Java was new, say last century. Its become much more popular in recent years.

Google Guava package, for example, uses it frequently.
 
Mike Simmons
Master Rancher
Posts: 4796
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, anyone not using Google Guava is strongly encouraged to check it out. Great library.

Grant, this style of programming is also called a FluentInterface, and it's being used here as part of the Builder pattern. Those two links can tell you more. I suppose they might be considered intermediate rather than beginner. But they give useful background.
 
Grant Robertson
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike and Pat,

Thank you so much. You answered my question perfectly.

I do have a couple of followup questions:

Can I expect that most API designers won't lead me through a treasure-hunt game where each method returns yet some other, different object of a different class, bouncing all through the JavaDocs? Do they pretty much stick to the FooBuilder, FooBuilder, FooBuilder, Foo pattern?

Is Foo usually immutable? Or does it just depend?

Thanks
 
Mike Simmons
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Grant Robertson wrote:Can I expect that most API designers won't lead me through a treasure-hunt game where each method returns yet some other, different object of a different class, bouncing all through the JavaDocs? Do they pretty much stick to the FooBuilder, FooBuilder, FooBuilder, Foo pattern?


Well, a method that returns itself is increasingly common (see Fluent Interface above), but you may also encounter code that returns different objects. Naming is helpful - if a class calls itself XxxBuilder, or has methods named like withXXX() or useXXX() (rather than setXXX()), there's a good chance the methods return the original object.

Grant Robertson wrote:Is Foo usually immutable? Or does it just depend?


Often but not always. I would usually expect it to be immutable, but verify in the API.
 
Grant Robertson
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again.
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Guava library's Javadocs have a short code snippet that shows how to use the FooBuilder.

Guava loves immutable collections, it has tons of types and factories and examples. Obviously, if you have, say, an ImmutableList, you can then safely send it to lots of computer systems to work in parallel. Google, of course, has zillions of systems that run in parallel.

No one knows, at least no one outside Google, how many they have. But I've heard reliable sources say that 100,000 servers is nothing to them. These days, they may be counting servers in the millions.
 
Grant Robertson
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:The Guava library's Javadocs have a short code snippet that shows how to use the FooBuilder.

Guava loves immutable collections, it has tons of types and factories and examples. Obviously, if you have, say, an ImmutableList, you can then safely send it to lots of computer systems to work in parallel. Google, of course, has zillions of systems that run in parallel.



Awesome, I will check it out when I am ready for a bit more cognitive loading. Gotta go back and review generics first. I've got the O'Reily book, I just gotta dig it out and read it.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Grant Robertson wrote:Awesome, I will check it out when I am ready for a bit more cognitive loading.


Just to go back to the Builder pattern for a sec: In Java (not sure about FX) it's generally used in cases where a class has several attributes that need to be initialized, particularly when 1 or more of them has a reasonable default value.

The Builder is initialized with the defaults, and the methods are used only to change whatever values you need to change prior to building. Personally, I love 'em, and use 'em all the time.

Gotta go back and review generics first...


Erm, OK. That came out of left-field; but generics is great too.

Good hunting.

Winston
 
Marshal
Posts: 79151
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is quite easy to implement your own methods which permit this style of coding. One of the earliest examples is probably StringBuffer, many of whose methods have StringBuffer as a return type. That is repeated in StringBuilder. You can see how it is implemented by looking at their code, or Cai Horstmann’s GBC class, which is a much easier way to handle GridBagLayout than what is described in this well‑known tutorial.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:...or Cai Horstmann’s GBC class, which is a much easier way to handle GridBagLayout than what is described in this well‑known tutorial.


See? Now if somebody had shown me THAT, I'd still have some hair left...

Bookmarked.

Winston
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . See? Now if somebody had shown me THAT, I'd still have some hair left...

Bookmarked.

Winston

Shown you WHAT? The GBC class or the tutorial?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Shown you WHAT? The GBC class or the tutorial?


GBC. Mind you, if I'd spent more time thinking about it rather than tearing my hair out, I might have come up with the solution myself...

Winston
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I first found it in the 2005 edition of Horstmann’s book. There is much more explanation there.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
on the humorous side, 3 times now i have written
BigInteger x;
.
.
x.add(y);
instead of
x = x.add(y);
i catch it pretty fast now
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: or Cai Horstmann’s GBC class, which is a much easier way to handle GridBagLayout


wow...lovely idea...actually a very simple idea but never occured to me even though I read about this in Joshua's Effective Java...
 
reply
    Bookmark Topic Watch Topic
  • New Topic