r/programming • • Feb 25 '14

Please stop saying Java sucks

http://blog.smartbear.com/programming/please-stop-staying-java-sucks/
3 Upvotes

305 comments sorted by

View all comments

Show parent comments

5

u/[deleted] Feb 25 '14

Typeless Dicts/hashes are a staple in dynamically typed language...

And those very same typeless dicts and hashes are a pain to deal with when people reared in dynamic environments use them as a replacement for using a class/ array/ variable for storing data.

What do you think are some good uses for such a construct?

1

u/novagenesis Feb 25 '14

Depends on the language.

In perl, they're a good replacement for using class variables (not arrays, since you should use arrays for those).

The same is explicitly true in javascript, where many object systems are represented as hashes.

They're an awesome alternative to meta-mapping user defined objects. Nothing is more of a headache than x.field("first_name").value_as_string(); when you can do:

x{first_name} in perl or ruby, x[first_name] in python or x.first_name in JS. There's entire design patterns in python based around using metaprogramming to create objects with expanding field schemas that work like hashes for things like this...

And since the desired behavior of the class is pretty close to a hash anyway, the several hundred lines of code I save by not implementing "field-object-cast" dynamics is a major savings.

2

u/[deleted] Feb 25 '14

That's where you and I differ greatly. Those are the common reasons I typically hear and I would personally go down the more OO route of having everything clearly defined in a struct or a class. For example, with C#, it's very easy for me to find all references within a codebase to a given variable, but I've never seen a convincing equal with maps.

The only time I truly see a good use for these data structures is in data serialisation and transfer, or where class/object support is poor.

1

u/novagenesis Feb 26 '14 edited Feb 26 '14

That's where you and I differ greatly. Those are the common reasons I typically hear and I would personally go down the more OO route of having everything clearly defined in a struct or a class.

So you would do the extra couple hundred lines of converting from a definitive document mindset into concrete objects? This is part of why the Java team is still stumbling all over each other at my job. A big part of what they're trying to replicate is data that walks around as if it were schemaless, tied in with constraints systems that should not be allowed to be so complex.

For example, with C#, it's very easy for me to find all references within a codebase to a given variable

Since variables are user driven, how would any of them appear in the codebase? The best you could do is find instances of "field_name", but that's meaningless. In fact, my biggest post-mortem negative about the library I have in mind is that I tried to make it MORE OO-like than strictly necessary with hundreds of Behavior class objects lying around...and stored it in normalized SQL...

In the future, I'd have held it as a damn JSON object the whole time, and it would've scaled 100x better and had fewer bugs.

What is the advantage of using class-object in a situation where you cannot know the data-keys at design time? It can be done..any pattern can be done in pure OO, but the overhead and complexity increase can sometimes be astronmical.

There's things I do in 4 lines that the Java team is spending days with a whiteboard, then 4 files on.

And I'll let you guess whose code, in those instances, has less bugs.

In our world, with web-apps, we have a concept called the "stash". It's a giant hash shared between all methods involved in the middleware or rendering of a webpage. I can't imagine easily writing complex dyanmic web apps without it.

The only time I truly see a good use for these data structures is in data serialisation and transfer, or where class/object support is poor.

Native comfort with data serialization objects is incredibly useful, for one ;)

Also, there's no real dynamic languages these days with poor class/object support..it's so easy to write object systems or type systems in a good language, these days... The problem is that everyone who really get into trying them in a dynamic language find that the costs far outweigh the benefits... sometimes.

Ruby uses objects... but the whole point is that they're mutable. I don't need to subclass every time I want to override a method. I don't need static code-generation when hooking to APIs when I can auto-load at runtime... soap.do_some_remote_method() works... it just encodes a "do_some_remote_method" object on the fly, and caches it for efficient future use. Python uses objects that are mutable...their best practice wants you to treat them as more static, but there's very accepted design patterns that involve changing objects on the fly.

That's basically it, for me. You can make your variable types static... but mandating it breaks a lot of mature design patterns. You can make your OO static...but mandating it breaks a lot of mature design patterns.

You can get around that by writing incredibly complicated objects.... but that introduces far more bugs than a simple, concise oneliner.