this is an example of what I mean!

Perhaps you are the only author of the code for all eternity then fine.

However, if you write code in an environment where there are many
developers who may have to maintain your code then I think you are
doing them a dis-service.

Using getters/setters and making class instance variables private is
an established Java idiom. I mean its in Effective Java (Item 14).

Sure, by initilising the fields in the constructor you can't change
the fields but thats not the point. The point is encapsulation and
following established idioms so you don't give the next dev a hard
time.

Cheers

Rakesh

On Fri, Apr 30, 2010 at 11:38 AM, Kevin Wright
<[email protected]> wrote:
> My Java style definitely changed since I learned Scala, and for the
> better. I can give one clear example.
> Whereas I used to write traditional beans, I'm now tending to favour public
> final fields and constructor initialisation.
> So instead of this:
> class Foo {
>   int x;
>   String y;
>   int getX() {
>     return x;
>   }
>   void setX(int x) {
>     this.x = x;
>   }
>   String getY() {
>     return y;
>   }
>   void setX(String y) {
>     this.y = y;
>   }
> }
>
> I'm now writing this:
> class Foo {
>   public final int x;
>   public final String y;
>   public Foo(final int x, final String y) {
>     this.x = x;
>     this.y = y;
>   }
> }
> It's smaller, immutable, easier to maintain and easier to use.  After all,
> nobody could realistically argue that:
>   foo.getX()
> is nicer than
>   foo.x
> This style even works with spring by using constructor injection.  My only
> real issues are:
> - working against frameworks that really do demand full "bean" compliance
> - implementing an interface (interfaces only have methods, not fields)
> But so long as I'm not in either of those scenarios, I find this approach to
> be much more usable.
>
>
>
> On 30 April 2010 11:12, Rakesh <[email protected]> wrote:
>>
>> I can see on disadvantage to learning multiple languages - abandonment
>> of established idioms in favour of so-called "better code".
>>
>> Let me explain, in Java a lot of people complain about the amount of
>> boiler plate we have to write. However, that boiler plate is something
>> a lot experienced Java devs are used to and recognise and aid their
>> understanding of a new code base and hopefully allow them to extend it
>> further correctly.
>>
>> People I work with have come from a Ruby background predominately (and
>> some have done some Lisp-like development).
>>
>> The code written by these people is very strange to a Java-only dev
>> and I don't think for the better. Source code is for HUMANS and
>> writing code that abandons established Java idioms is doing a
>> dis-service to the next Java dev who comes along and has to maintain
>> that code.
>>
>> A small example is the use of final everywhere. Is the code really
>> better? Really? How did you cope before? Did you find lots of bugs
>> that were due to not using final?
>>
>> A bigger example is the desire to create internal frameworks/dsls. I
>> like the idea of a dsl but its very hard and almost next to impossible
>> to extend unless you are the original author.
>>
>> We have internal dsls that are completely alien to me
>> (functors/frankenstien generics) and a nightmare to understand and
>> extend - all so at some top-level, you can write a statement that
>> (almost, but not quite) reads like English. Grrr!
>>
>> So, in summary, I think learning about other languages is ultimately
>> good experience from a personal point of view but be very careful how
>> it affects the code you write in other languages.
>>
>> Cheers
>>
>> Rakesh
>>
>> PS I am off on a Scala course in June (with M. Odersky no less) so I
>> am not against other languages per se.
>>
>> On Thu, Apr 29, 2010 at 4:52 PM, Chris Adamson <[email protected]>
>> wrote:
>> > Oddly, and this was a long time ago, I found that LISP helped me
>> > understand OO when I was learning Java.  I had previously done all
>> > procedural languages -- BASIC, Pascal, C -- and the "indirectness" of
>> > LISP was helpful for me in letting go of my need to own the program
>> > counter at all times.  I had done a little AppleScript, and that was
>> > an early OO gateway for a lot of people too.
>> >
>> > One of the things that's really helpful about learning new languages
>> > is mental flexibility.  My favorite example is to ask "how do you add
>> > 1 to every item in a list?"  Most developers will think in terms of a
>> > while or for-next loop.  In LISP, the cleanest way to do it is to
>> > write a function that adds 1 to the first member and then calls itself
>> > recursively with the rest of the list.  Which would be batty in Java,
>> > of course, but after a deep dunk in LISP's recursion-land, you'll be
>> > more willing to adopt it elsewhere when it makes sense to (like
>> > parsing a tree structure, even in a procedural or OO language).
>> >
>> > The Prags put up a wiki to help pick the 7 languages.  I'm pleased as
>> > punch they picked Prolog, even if it did almost flunk me out of
>> > college.  Anyone who's been intrigued by Drools for Java should be
>> > interested too.
>> >
>> > That said, the language I voted for was assembly.  I think it's wrong
>> > that developers don't even know what a register is anymore, or have a
>> > sense of how their code is going to be compiled for and executed on
>> > the CPU.
>> >
>> > --Chris
>> >
>> > On Apr 29, 9:56 am, Wildam Martin <[email protected]> wrote:
>> >> I have learned about Lisp in school but that was one of the languages
>> >> I was sure I would never use in a real project. Maybe it is my kind of
>> >> thinking or just a personal preference but I really don't like that
>> >> functional stuff. I find it less readable and have a few other
>> >> complaints I can hardly put into words. Maybe I should give such a
>> >> language really a try. But it is not that I didn't look at them at
>> >> all.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "The Java Posse" group.
>> > To post to this group, send email to [email protected].
>> > To unsubscribe from this group, send email to
>> > [email protected].
>> > For more options, visit this group at
>> > http://groups.google.com/group/javaposse?hl=en.
>> >
>> >
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "The Java Posse" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/javaposse?hl=en.
>>
>
>
>
> --
> Kevin Wright
>
> mail/google talk: [email protected]
> wave: [email protected]
> skype: kev.lee.wright
> twitter: @thecoda
>
> --
> You received this message because you are subscribed to the Google Groups
> "The Java Posse" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/javaposse?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to