• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Effective Java: includes var keyword? best practices?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Josh,

great to hear that there will be a third edition of your book! I enjoyed the last edition a lot - at this point I also want to say thanks again for the video you made for us in May 2014 (if you still remember ;-).

For my question about the new book, I'd like to know the following:

(1) On the back of the book it says: "Type inference, including the diamond operator for generic types". Does this also include the new posibility to define variables with the "var" keyword that is announced for Java 10? Because I am curious to understand whether we should really use that one. Doesn't it make the code more complicated to read?

(2) Since programming with streams really changes the way we need to read code (and code is read ten times more often then written), do you you cover some experience in the book on switching to this new way of writing code? Should these two ways be mixed inside a code base? What are the difficulties in working with this new technique if other collegues aren't very experienced with it yet?

(3) In general, is the book focusing on the technical parts only or are there also recommendations on best practices when working with teams / with old code bases / etc.?

So much for today ;-). I am very much looking forward to reading the book. I wish you best success with the book and greetings to Pittsburgh from Frankfurt,

Mathias
 
Sheriff
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mathias Bader wrote:(1) On the back of the book it says: "Type inference, including the diamond operator for generic types". Does this also include the new posibility to define variables with the "var" keyword that is announced for Java 10? Because I am curious to understand whether we should really use that one. Doesn't it make the code more complicated to read?


The book is written a few months ago, so there are no Java 10 specifics in there.
 
Author and "Sun God"
Posts: 185
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mathias.

(1) On the back of the book it says: "Type inference, including the diamond operator for generic types". Does this also include the new posibility to define variables with the "var" keyword that is announced for Java 10? Because I am curious to understand whether we should really use that one. Doesn't it make the code more complicated to read?



As Rob says, I don't discuss the var keyword in the book; it only goes through Java 9. That said, I generally agree with you. I would only use var in cases where the the type was utterly clear from context, and long. You could certainly make your program difficult to read and maintain by overusing this feature. Given that they already added the diamond operator (in Java 7), and broadened its applicability (in Java 8, and again in Java 9) the potential for code improvement with var isn't huge.

(2) Since programming with streams really changes the way we need to read code (and code is read ten times more often then written), do you you cover some experience in the book on switching to this new way of writing code? Should these two ways be mixed inside a code base? What are the difficulties in working with this new technique if other collegues aren't very experienced with it yet?



Yes, I devote a lot of space to this topic, and I strongly advocate mixing streams and iteration. This is the major topic of Item 45, "Use streams judiciously." Among other things, that item contains three versions of a program to print all of the anagram groups in a dictionary whose size is above a given threshold. One version is purely iterative (and would have been exemplary before streams were added to the platform). The second version is purely stream-based, and hard to read. The best version combines the two approaches:



(3) In general, is the book focusing on the technical parts only or are there also recommendations on best practices when working with teams / with old code bases / etc.?



It's just like the previous two editions: it is technically focused, but some of the recommended best practices do enhance the ability to work in teams, with old code bases, etc. I don't discuss things like the importance of code reviews, even though I do think they're critical. You can't cover everything in one book ;)
 
Joshua Bloch
Author and "Sun God"
Posts: 185
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should also mention that my good buddy Stuart Marks has written a best-practices article for the var keyword: http://openjdk.java.net/projects/amber/LVTIstyle.html
 
Marshal
Posts: 76095
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are people calling var a keyword? Is var a reserved word?
 
Joshua Bloch
Author and "Sun God"
Posts: 185
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell. Technically, var is not a reserved word in Java 10, though it feels like one. This was done for backward compatibility, so that existing programs that used var as a variable name are not broken. But it's not stretching the truth too far to call var a keyword in Java 10.

By the way, here's an obscure fact that you might be able to use in a bar bet some day: goto (!) and const are reserved words in Java, even though they are not used.
 
Campbell Ritchie
Marshal
Posts: 76095
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joshua Bloch wrote:Hi Campbell. . . .

Thank you

goto (!) and const are reserved words in Java, even though they are not used.

Yes, I know; it says in the JLS that it might allow you better compiler error messages. Nobody in the bars I frequent would have heard of goto and const
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic