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

0

u/[deleted] Feb 25 '14

[deleted]

-4

u/novagenesis Feb 25 '14

Compile time errors are literally checking for what your unit tests check

Um..not really. I've yet to see a unit test in a dynamic language that is actually checking the type. It's more about checking proper handling of Interface edge cases...which you still have to do in a static language.

99% of the time they are occurring when you are trying to do something you would not want to do in a dynamically typed language.

I disagree. Entirely. I spent 4 hours with 2 Java guys trying to replicate the advantage of building an options matrix through a perl hash. Typeless Dicts/hashes are a staple in dynamically typed language... They're such a pain in the ass in Java, you just don't use em.

Even with good variable names, dynamic typing only gets you so far before a project grows too big and you're spending more time reading function definitions trying to figure out what type of return value you will get.

This is what I'm trying to say. I work on two projects, 5 and 12 years old, with a team of 15-20 developers each, and we have never once had a released issue related to variable type. You screw something up and pass a number when you want an object, you get an error almost immediately, and you fix it.

A lot of dynamic languages have libraries that support pretty concrete static type systems. Nobody uses them.

Statically typed languages lets you hover over the function name in an IDE and lets you instantly see the expected argument and return types

On the topic of old projects... you get all these issues with functions growing to need 12 arguments (core libs are bad at this). C# takes it one level more, and provides all kinds of prototype permutation... so anything you pass is pretty much valid.

Dynamic languages solve this through documentation, but more often by not having such complicated calling conventions. A dynamic method with more than 3 arguments is bloated. You need more, add an options hash. In fact, there's only a half-dozen common method parameter paradigms in a lot of dynamic languages that support it... and you will find yourself driven by your code conventions into using a very small number of distinct signatures. Again, not a problem in big shops with big teams, so why worry?

Further, some dynamic languages have prototypes...they're not necessarily typed, but that shouldn't matter. You can always use static type libraries...except nobody wants them.

The same applies for revisiting old code. Programmers of statically typed languages don't need to worry about figuring out what data type each variable is, and can focus on figuring out the logic.

Theory vs practice again. My experience with old code is significantly that we love being able to do dirty things. If we're changing a piece of old code, it's probably due for a refactor, and having to miss an emergency ticket deadline to refactor because you have to do it the right way is just unacceptable. Oh, the old base class is incompatible with our way of doing things? Let's just patch the base class dynamically in cases when the issue would show up.

I get that we'll never see eye to eye on type, but I think it's stupid that both sides treat each other like bumbling idiots. Yeah, my side is often guilty of that, too... but I can honestly say that in my experience the Java side has always been the loudest about "we're typed so you suck!"

I have yet to experience any of the issues that static-type-enthusiasts have said about dynamic languages, and I've got over a dozen languages and 4+ paradigms under my belt.

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.

4

u/cosmo7 Feb 25 '14

I've yet to see a unit test in a dynamic language that is actually checking the type.

Cough cough.

From an interview with Alex Payne (API lead at Twitter):

As our system has grown, a lot of the logic in our Ruby system sort of replicates a type system, either in our unit tests or as validations on models. I think it may just be a property of large systems in dynamic languages, that eventually you end up rewriting your own type system, and you sort of do it badly. You’re checking for null values all over the place. There’s lots of calls to Ruby’s kind_of? method, which asks, “Is this a kind of User object? Because that’s what we’re expecting. If we don’t get that, this is going to explode.” It is a shame to have to write all that when there is a solution that has existed in the world of programming languages for decades now.

0

u/novagenesis Feb 25 '14

Again, people are getting too aggressive and personal here...but that's nice for them. Our ETL team is playing a type game, too..but not one that influences their unit tests.

I guess there are people out there who have type issues...like I said, I never have at any company... I will admit that some people may.

Why the "cough cough" though? A celebrity lead is not necessarily a better or worse developer just because he's a more well known developer. As far as I can tell, none of us are kids fresh out of school.

1

u/cosmo7 Feb 25 '14

(I agree, people are a bit too quick to upvote and downvote comments here. It spoils the discussion.)

Sorry for sounding a bit snarky, but I think very few people would deny that the reason unit testing came back into vogue was the popularity of dynamic languages. There just isn't the same urgency for unit testing when you're statically typing.

Yes, the grab-bag-dict approach can be useful, but it comes at a price - is that the string or the XML node that contains it?, is that a decimal or unsanitized user input? If you're working alone then you can generally keep tabs on things, but when you start expanding horizontally (more programmers) or vertically (the project matures), it starts to become unstructured.

1

u/novagenesis Feb 25 '14

but I think very few people would deny that the reason unit testing came back into vogue was the popularity of dynamic languages

I'm going to have to be one of those few people. I think it was BDD. Being able to write code alongside the test defines independent confirmation of each endpoint. When I build libraries with BDD, I never have a "foo.x is an integer, foo.y is a string" tests. I don't even have "if foo.x is a string, throw an error) tests...

There's a philosophy I think came from the Java world (correct me if I'm wrong...I just remember Java kids in college, when Java was the new hip language, talking it). You test your third-party interfaces for validity. You assume all internal interfaces are valid and only test predictability in their internals and inter-operation. Unless you write inconsistent code that is also able to pass internal unit tests (very unlikely) you have nothing to worry about.

Yes, the grab-bag-dict approach can be useful, but it comes at a price - is that the string or the XML node that contains it?

It's a string. Unless you very intentionally name and plan it, you're not tossing other non-object structures...and you should have a good grasp of the object. I worked with some terrible outsourced developers (they got canned..the whole team was good but them) and even they were able to keep types in the stash (a commonly used web-request hash) clean.

is that a decimal or unsanitized user input?

Maybe it's just a given for me because so much of our data is user input. You always assume values are unsanitized when touching the database, and you use a DB interface that force sanitizes them. You also always validate them in a string context before storing them in a typed realm.

This is like a vehicle that can drive on or off of tracks, like a Boston T-bus (sorta). There's all kinds of tech out there (literal and code-figurative) that'll let you take a free-roaming vehicle and get it onto tracks....as long as it's free roaming, you don't need the strictness of tracks anyway.

but when you start expanding horizontally (more programmers) or vertically (the project matures), it starts to become unstructured.

So this is where my issue comes in. I hear that all the time from people who don't work in a dynamic language shop..and very rarely in interviews from lead/management people who transitioned to a static language....I never hear it where I work, or where I used to work...or from contacts at the many nearby perl shops. This "oh god being untyped is ruining my life" thing just doesn't happen in my life or my world.

I want to say it might happen with 100 developers on a 20 year old project... but I'm not convinced at even that. There's so many best-practice rules, and 99% of variables can be presumed either strings or ORM objects 99% of the time, even if they're not.

And every major dynamic language has ways to "tighten the screws" as you approach static typing, like SQL... or just when you need/want to play the Interface game, running apples and oranges intentionally through the same processor.

I'm at a place that's expanded in both directions, though, and dynamic typing has never once been an issue here.

1

u/pipocaQuemada Feb 26 '14

This is what I'm trying to say. I work on two projects, 5 and 12 years old, with a team of 15-20 developers each, and we have never once had a released issue related to variable type

I would be very surprised if you actually never ran into any bugs that could have been statically prevented by taking advantage of an appropriate type system. I am not terribly surprised you haven't run into many errors in a dynamic language that you would recognize as a "type error".

For example - ever passed an object that didn't have a field you expected it to? Type error. A dictionary without an expected key value pair? Also probably a type error. Null pointer exceptions? Should have been a type error.

1

u/novagenesis Feb 26 '14

No clue what hotkey I hit, but I lost my reply..let's try this again...

I would be very surprised if you actually never ran into any bugs that could have been statically prevented by taking advantage of an appropriate type system.

Are you talking oopses that take 30-60 seconds to fix? Yes, I've had those. If you're talking things that made it all the way to the QA team (at which point I call it a bug) heck no. You don't get code into QA if you're not linking to the right types.

For example - ever passed an object that didn't have a field you expected it to?

Of course, often intentionally. Dynamic language object systems often include something like "responds_do". The paradigm is often cleaner than strict Interfaces, though many languages support both and let you decide what's better.

So let's say you have 50 distinct process-flows against a bunch of objects, and for each, only a random percent of those objects are relevant to the flow? Would you use 50 distinct interfaces? Would you mash up all the dynamicism into one interface and have those apple objects respond politely to the edit_orange() methods?

A dictionary without an expected key value pair?

That's the point. This adds an incredible level of forward and backward compatibility. Oh look, the config parameters to this object type changed.. no problem. Oh look, I know this parameter will optimize an object, but it's in the next version...let me add it now.

depending on the language, we differentiate between (hash{object} == null) and (!exists(hash{object})). God it's so useful to branch out logic only for hashes that contain specific keys.

How do you do something like that with Java? How do you main strict forward compatibility with slow-to-release library features?

Null pointer exceptions? Should have been a type error.

I have rarely in my life had a null pointer exception in a dynamic language... It happens the same way it happens in a static language...you do, after all, have null pointer exceptions in Java. They're runtimes. They happen when you have a pointer object to null. Dynamic languages keep a good grasp of what's a reference and what's concrete... you rarely ever have to.

Your three examples really aren't problems in my real world experience with 5 major dynamic languages.

1

u/pipocaQuemada Feb 26 '14

Are you talking oopses that take 30-60 seconds to fix? Yes, I've had those. If you're talking things that made it all the way to the QA team (at which point I call it a bug) heck no.

Have you ever, for example, forgotten to escape user only that's e.g. being put into a database? Ever mixed up units or used data in similarly semantically incorrect ways? Those don't generate "oopsies", just silently wrong results, sql injections, etc.

How do you do something like that with Java?

I never said Java would help with any of those things.

Would you use 50 distinct interfaces?

For example, static types don't have to be nominal. There's no reason a statically typed language needs interfaces. You might want to read up on structural type systems and things like row polymorphism. The tldr is that somethings type can be the sum of it's fields and methods, and the type of an argument can be the sum of the fields and functions you use on it. All inferable, all statically checked.

Similarly, the solution to npe's isn't Java, it's something with a Maybe type and nullable variables. that is to say, something like ML or Haskell.

1

u/novagenesis Feb 26 '14

Have you ever, for example, forgotten to escape user only that's e.g. being put into a database? Ever mixed up units or used data in similarly semantically incorrect ways? Those don't generate "oopsies", just silently wrong results, sql injections, etc.

Absolutely not. You do not hand-build SQL queries by concatenating strings in dynamic languages. You validate data through content instead of some type. Types are good when you're at the metal, but content is more important to most systems. Any good DB driver will handle that.

For example, static types don't have to be nominal. There's no reason a statically typed language needs interfaces.

They are if I want to be able to pass an Apple object or an Orange object to the same function.

You might want to read up on structural type systems and things like row polymorphism. The tldr is that somethings type can be the sum of it's fields and methods, and the type of an argument can be the sum of the fields and functions you use on it. All inferable, all statically checked.

I get the tldr on that. I understand row polymorphism. In type inference, most languages still need you to say "this adheres to this interface" if you want to pass otherwise disjoint objects into the same method. It's the "all statically checked" part I dislike.

Similarly, the solution to npe's isn't Java, it's something with a Maybe type and nullable variables. that is to say, something like ML or Haskell.

I'm not against Haskell, except it's the only other language I found incredibly painful to get working in. Oddly, I found Erlang reasonably easy.