Live data from Hacker News

Java 9 features announced

jaxenter.com

191–200 of 228 posts

Re: Java 9 features announced

#191

Earlier quoted context omitted.

We already have that, it's called the Builder pattern.

Up until recently, I've enjoyed the builder pattern. It becomes a pain in the ass though when your objects are deeply nested. For example, with protocol buffers you end up doing things like. AlbumCollection collection = user.getPreferences().getFavorites().getAlbums().toBuilder().addAlbum(album).build(); Favorites favorites = user.getPrefernces().getFavorties().toBuilder().setAlbumCollection(collection).build(); Pref…

> Up until recently, I've enjoyed the builder pattern. It becomes a pain in the ass though when your objects are deeply nested.

That and good luck adding a new required field in your "constructor", because you just gave up on statically checking the parameters of the "constructor".

Re: Java 9 features announced

#192

Earlier quoted context omitted.

> It is addressed by better design. This! A lot of the horrible Java you see around is nothing but an artifact of mindlessly applying patterns to constructs that don't need them. > a lot of java was created when we still hadn't figured out how to properly do Object Oriented Programming. Which is quite unforgivable, considering Smalltalk has been around since the early 80's.

My problem is I don't think there's any agreement what Object Oriented Programming is and isn't, so it's pretty hard to "properly do it". Let's see. OOP generally involves "objects", ie structs or records with visibility controls on fields, sporting "methods," ie functions with a magic/hidden this/self argument. This alone doesn't buy you much, and in fact is quite inflexible when compared to good old ADTs, so let's…

> This alone doesn't buy you much, and in fact is quite inflexible when compared to good old ADTs, so let's hope there's more to it.

It at least buys you a way of organizing your code that makes it easier to understand not just for yourself, but other programmers who read it in the future. Creating objects and using data types isn't really mutually exclusive, you use both as needed.

> OOP does not mean "all methods are defined within the class declaration/file" a la Java, plenty of OOPLs allow external method definition. So it doesn't mean "well in OOP I always know the behavior of my class" when you have friend functions etc.

This is true. The main benefit of OOP in this regard is to help see the interactions between related groups of functions as organized into objects.

> OOP might mean "abstract classes" ie virtual methods. This leads us to inheritance, which is a) not confined to OOP and b) widely criticized. Remember that inheritance != polymorphism, c.f. Visual Basic for instance (at least VB5. Showing my age...). In practice, OOP usually invokes a great-chain-of-being-like hierarchy descending from Object, but again this is not universal.

Inheritance definitely has downsides and should be considered carefully when used, but it also has valid use cases. As with any language feature, it really is more of a question of is this useful to fix the problem I'm trying to solve? Granted, a lot of programmers (at least from the code I've worked on) seem to see inheritance as a universal hammer to their current problem nail :)

> OOP generally means "polymorphism", but the mechanisms vary widely. OOP's "Classic" inheritance-driven polymorphism is probably the worst way to do it, good for quick code-reuse and that's about it. "Interfaces"/pure virtual classes are a design pattern on top of abstract classes, better expressed as mixins or type-classes.

Whether mixins, type-classes, or a polymorphic inheritance is the right choice really depends on language and problem space. At least with mixins there are definitely circumstances where a mixin could have so many methods that it is essentially a form of multiple inheritance. Again, knowing the right tool for the job and knowing what limitations your development language has is more important than the existence of any one feature or tool.

> OOP generally implies strong typing but not always (python). OOP typing is quite clunky, especially in "noun-oriented" Java. You can make an argument that the rise of DI frameworks is in response to having impoverished type systems.

I'm not sure about DI being the result of impoverished type systems. Generally DI is about insuring that function dependencies are explicitly consumed rather than implicitly existing somewhere as a more global state. DI is more about contract enforcement on functions than anything else.

> My semi-rant here is because people say things like "do it this way, it's good OOP" without there being any agreement about whether we're discussing polymorphism, inheritance, encapsulation, factoring, normalization, or design patterns that are largely a response to the confusion.

This is definitely the crux of it. "Good OOP" is a meaningless phrase for the most part. What exactly is good for a project? Given the amount of tools available in OO languages, many different approaches could be used that would function as good based on your particular problem and developers.

Re: Java 9 features announced

#193
post #115

Earlier quoted context omitted.

It can take up to 5 minutes to restart my application.

So run multiple instances of the app, and put some kind of load balancer / proxy in front of the instances. Unfortunately, this will require externalization of state data, but if you can't be down for 5 minutes, that's what you need to do. You probably only need one large server running the instances, rather than a cluster. Java apps don't seem to do well -- longer GC pauses -- when they have over 2 GB of memory, any…

I don't think he meant hotswapping in productive environments but during development. Load balancing doesn't help here.

The webapp I'm working on currently has a startup time of 50s on my macbook. If I restart the webapp 100 times a day, thats a total waiting time of 30+ minutes. Our team is 6 developers, so we loose 3h per day.

Real hotswapping may be extremely complex but IMO it would be absolutely worth the effort.

Re: Java 9 features announced

#194

Earlier quoted context omitted.

And you can replace it with a virtual property in the future (changing the underlying implementation) without breaking anything. And user code ends up simpler and more readable too: foo.baz += foo.bar; vs. foo.setBaz( foo.getBaz() + foo.getBar() );

That example reminds me of this recent post on The Old New Thing: http://blogs.msdn.com/b/oldnewthing/archive/2014/08/14/10549... Interesting behavior.

Of course, setters don't protect you from this either.

Re: Java 9 features announced

#195
post #189
post #187

Earlier quoted context omitted.

> The big issue is that a lot of java was created when we still hadn't figured out how to properly do Object Oriented Programming. Bollocks, there were already plenty of OO languages when Java appeared on the block.

Sure, but more radicial OO languages like Smalltalk were full of getters/setters too (e.g. in Smalltalk all member variables are private, so getters/setters are the only way to access those) so it makes sense to think that this was just the way to go.

Eiffel introduced properties in 1985.

The famous CLOS book was written in 1988, which makes use of slots instead.

I am at work now, otherwise I could provide more information.

Re: Java 9 features announced

#196

Earlier quoted context omitted.

So run multiple instances of the app, and put some kind of load balancer / proxy in front of the instances. Unfortunately, this will require externalization of state data, but if you can't be down for 5 minutes, that's what you need to do. You probably only need one large server running the instances, rather than a cluster. Java apps don't seem to do well -- longer GC pauses -- when they have over 2 GB of memory, any…

I don't think he meant hotswapping in productive environments but during development. Load balancing doesn't help here. The webapp I'm working on currently has a startup time of 50s on my macbook. If I restart the webapp 100 times a day, thats a total waiting time of 30+ minutes. Our team is 6 developers, so we loose 3h per day. Real hotswapping may be extremely complex but IMO it would be absolutely worth the effort…

Ah, gotcha. It's funny, I used to do very frequent tests like that when I worked in C (particularly the first few years in the language), but tend to do much fewer test runs in Java. Fewer fatalities, but longer build/start times, I guess.

Re: Java 9 features announced

#197

Earlier quoted context omitted.

I admire Java for the fact that it is significantly more painful to write mutable rather than immutable classes. I love C#, but I do wish that setters weren't so easy to define (and that classes were sealed by default, and that fields were read-only, and that all extant warnings would become errors, etc.).

I agree 100%, but the problem is the culture that has grown up around Java. The whole Bean concept encourages violation of encapsulation. IDEs provide automatic generation of accessors and mutators. Many Java programmers have been trained to expect them. At this point, encouraging good OO practice is shouting against the tide. That being said, I still make my classes immutable unless there is a damn good reason not t…

In Java culture immutable generally means private final member with only a getter and no setter. I would argue that this is good OO design as the purpose of OO design is to protect your interface from future design changes. If you need to change your accessor, you already have one. If the member needs to be mutable at a later date you need only remove the final keyword and potentially add a setter. The interface remains backwards compatible.

Re: Java 9 features announced

#198
post #11

Nothing pollutes java source more than getters and setters. I can't believe this still isn't being address. Wish they'd move in the direction Groovy has in this regard.

Nothing pollutes java source more than code which REQUIRES setter methods for functionality. If you have classes which require constant mutable access to internal state, your design is probably flawed.

>If you have classes which require constant mutable access to internal state, your design is probably flawed.

How do you account for value objects in that statement? Are you saying there should be no such thing?

Re: Java 9 features announced

#199

Earlier quoted context omitted.

I agree 100%, but the problem is the culture that has grown up around Java. The whole Bean concept encourages violation of encapsulation. IDEs provide automatic generation of accessors and mutators. Many Java programmers have been trained to expect them. At this point, encouraging good OO practice is shouting against the tide. That being said, I still make my classes immutable unless there is a damn good reason not t…

In Java culture immutable generally means private final member with only a getter and no setter. I would argue that this is good OO design as the purpose of OO design is to protect your interface from future design changes. If you need to change your accessor, you already have one. If the member needs to be mutable at a later date you need only remove the final keyword and potentially add a setter. The interface rema…

I disagree. If you require an accessor to actual internal state, you should rethink your dependencies and reassign some behaviors. Accessors should be rare, mutators almost non-existent.

Re: Java 9 features announced

#200
post #11

Nothing pollutes java source more than getters and setters. I can't believe this still isn't being address. Wish they'd move in the direction Groovy has in this regard.

Then don't use getters and setters... A lot of complaints people have about Java seem to be about the way in which they choose to write Java.

My somewhat unorthodox rules for writing Java:

1. Don't use private. No reason to restrict yourself or others, and do extra typing. Friendly is a perfectly good default.

2. Don't use reflection. Reflection is only useful for making frameworks. Frameworks don't reduce the complexity of the problem you're trying to solve. Don't use them.

3. Don't use getters/setters. Sometimes they make sense for updating a cached value, or using a different underlying representation, but in general it's unnecessary clutter. If you still want them (e.g., for code style reasons), just generate them.

4. Don't use other people's code (tongue-in-cheek). If you use third-party libraries, consider wrapping them in your own interfaces as needed. This makes it easy to change the implementation and perform tests. It's not much effort, since you'll rarely use more than a limited subset of the functionality offered by the library. If you need a large subset, then just use the library directly.

5. Use final. Immutability makes programs simpler, more efficient, and inherently thread-safe.

6. Consider writing bean classes as follows. It's simple, efficient, thread-safe, and has a clean syntax (item.name):

    public class Item {
        public final int id;
        public final String name;

        public Item(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }
7. Use an IDE, refactor, find references, generate code. It's easy to changes names and styles later, so just focus on important things like performance and reliabiltiy.

8. Keep improving the way you write your code, don't listen to the crowd.

Post reply on HN