News for hackers. Stuff that matters.
Re:A bad citizen in Javaland (2006) by RyanZAG on Thursday December 03, 2015 @06:23AM (#10669024)
I wonder how a functional version of this would go..

You need transportation, so you define an engine and a set of wheels and a chair, and a whole universe. You define a way to have the engine, wheels and a chair work together. You try to sit in the chair, but it's too small. You can't change the chair because it's all immutable, so you create a new chair along with a new universe and build a new car with the new chair.

You need to get to work, but you don't want to pollute your universe with state, so you don't know where work is. So you put all of your state in your relational stateful database and forget that this part exists in your universe. Of course theoretically you could model your knowledge as a function based off the state of the universe, but you don't have budget to do that. You write comments about quick hacks to get around purity constraints. The hacks are never removed and you end up passing stateful maps of database tables around. You make a big todo note about how you should implement a new method for handling this that you've read in a new academic paper, but never get the 500 hours you need to implement it.

EDIT: For those not getting it, the article is describing the difference between the theory behind OO interaction between self contained actors and how this really plays out in actual code. Instead of doing the theoretical part of communication between actors, we have code which does getCoworker().getWallet(). In the same way above, I've tried to show the difference between theoretical functional programming where we can create pure functions and how this actually plays out in real functional code.

Re:A bad citizen in Javaland (2006) by danieldk on Thursday December 03, 2015 @06:44AM (#10669084)
You can't change the chair because it's all immutable, so you create a new chair along with a new universe and build a new car with the new chair.

No, if you read Okasaki, you knew you only have to reconstruct the part of the universe (up to its origin) that refers (directly or indirectly) to the seat ;).

In fact, you can safely have two universes with different seats, where most of the universes is shared.

Re:A bad citizen in Javaland (2006) by RyanZAG on Thursday December 03, 2015 @06:50AM (#10669105)
Oh that makes for a funny part of the ending too then.

While going about your work, you suddenly disappear along with your universe and everything in it. Someone somewhere in the world drank a cup of coffee, which means that he now has an empty cup instead of a full cup. A new universe had to be created with the full cup replaced, and unfortunately your universe had to be thrown away. You continue life as a clone in a new universe with an empty coffee cup somewhere in Nebraska.

Why did this happen? Someone picked a copy-on-write map implementation because it had a slightly faster O(logN) delete behavior and unfortunately the re-use function of the map wasn't implemented yet because the author has gone on to do a series on the use of monads in baking the ultimate croissant.

Re:A bad citizen in Javaland (2006) by Someone on Thursday December 03, 2015 @09:05AM (#10669571)
"Someone somewhere in the world drank a cup of coffee, which means that he now has an empty cup instead of a full cup"

That cannot happen in an immutable world. To fill an empty cup, you cons a cup and its contents. To get an empty cup again, you take the car of the cons. The full cup will only get garbage collected if you do not reference it anymore.

Re:A bad citizen in Javaland (2006) by catnaroek on Thursday December 03, 2015 @04:52PM (#10672954)
> To fill an empty cup, you cons a cup and its contents.

That simply has the wrong type in mathematically civilized languages like Standard ML or Haskell.

> The full cup will only get garbage collected if you do not reference it anymore.

This is orthogonal to functional programming. You can avoid the need for a garbage collector with substructural types.

Re:A bad citizen in Javaland (2006) by TeMPOraL on Thursday December 03, 2015 @06:55AM (#10669119)
Hence, Many World Interpretation of quantum mechanics.
Re:A bad citizen in Javaland (2006) by koenigdavidmj on Thursday December 03, 2015 @08:17AM (#10669371)
Sorting in O(n) time:

1. Apply a quantum permutation to your list.

2. Check if it's sorted.

3. If not, destroy the universe.

Re:A bad citizen in Javaland (2006) by evanpw on Thursday December 03, 2015 @10:13AM (#10669933)
Related (amazing) paper by Scott Aaronson: "NP-complete Problems and Physical Reality" (http://arxiv.org/abs/quant-ph/0502072). He proposes "anthropic computing" as a way of solving NP-complete problems in polynomial time: guess an answer, then kill yourself if the answer is wrong.
Re:A bad citizen in Javaland (2006) by dtech on Thursday December 03, 2015 @11:57AM (#10670658)
It would be very eerie if suddenly all the guesses started being right.
Re:A bad citizen in Javaland (2006) by kragen on Thursday December 03, 2015 @10:25AM (#10670022)
I am worried that someone will actually try this sometime and it will turn out not to work, either because MWI is wrong or because the destruction of the universe was incomplete — for example, collapsing the false vacuum would merely change the state of the things in the universe, not destroy the universe per se.

Fortunately, we don't have any currently promising avenues for nucleating a true vacuum or anything like that, so this algorithm is purely theoretical.

For now.

Re:A bad citizen in Javaland (2006) by arethuza on Thursday December 03, 2015 @08:40AM (#10669466)
That made me think of Greg Egan's Quarantine:

https://en.wikipedia.org/wiki/Quarantine_%28Greg_Egan_novel%...

Re:A bad citizen in Javaland (2006) by moron4hire on Thursday December 03, 2015 @08:47AM (#10669494)
Best-case runtime = 1. Worst-case = infinity.
Re:A bad citizen in Javaland (2006) by AndrewOMartin on Thursday December 03, 2015 @09:37AM (#10669745)
I think the joke is that in all cases but the best-case, you won't be around to observe it, this becomes practical once you can create a universe for every permutation and destroy all but the universe which notifies you that it's the best-case.

In all good text books this is left as an exercise for the reader.

Re:A bad citizen in Javaland (2006) by AnimalMuppet on Thursday December 03, 2015 @12:38PM (#10671041)
In a really good text book, this is left as an exercise that the reader won't do.
Re:A bad citizen in Javaland (2006) by danieldk on Thursday December 03, 2015 @09:38AM (#10669748)
No, checking whether a list is sorted is O(n) ;).

Bogosort is O(n) in the best case and O(inf) in the worst case).

Re:A bad citizen in Javaland (2006) by lisivka on Thursday December 03, 2015 @10:57AM (#10670257)
Optimize out the check, so it will be O(1) in some cases. ;)
Re:A bad citizen in Javaland (2006) by frozenport on Thursday December 03, 2015 @10:59AM (#10670274)
The convergence of this method depends on the underlying distribution for your random process. It could be O(never).
Re:A bad citizen in Javaland (2006) by jerf on Thursday December 03, 2015 @01:26PM (#10671495)
As Scott Aaronson points out, the easy solution to this is that you add a slight quantum chance that the algorithm will yield "Reject". As long as that chance is non-zero, but greatly smaller than the probability of any particular successful permutation, you will survive in the universe that says "Reject".
Re:A bad citizen in Javaland (2006) by theseoafs on Thursday December 03, 2015 @02:46PM (#10672040)
This sounds like a joke but this is exactly the proof that sorting is in NP.
Re:A bad citizen in Javaland (2006) by andrepd on Thursday December 03, 2015 @11:24AM (#10670443)
What is a quantum permutation?
Re:A bad citizen in Javaland (2006) by ninjakeyboard on Thursday December 03, 2015 @09:16AM (#10669630)
A new chair isn't created - the legs, seat, and back of the old chair are re-used with new screws.

If you want to replace a leg, you still need to disassemble the chair and reassemble it with new screws.

Re:A bad citizen in Javaland (2006) by platz on Thursday December 03, 2015 @10:52AM (#10670221)
I've never considered the many-worlds interpretation of quantum physics under the light of persistent data structures.. very interesting!
Re:A bad citizen in Javaland (2006) by cpeterso on Thursday December 03, 2015 @03:50PM (#10672530)
"If you want to make an apple pie from scratch, you must first create the universe." — Carl Sagan
Re:A bad citizen in Javaland (2006) by jt2190 on Thursday December 03, 2015 @07:55AM (#10669289)
I was about to dismiss this story as silly, but then it occurred to me that the reason this is so funny is not because Java is a bad language per se, but that it really didn't work well for programmers who don't understand Object Orientation. I've read more than my fair share of code that reads exactly like this story because of this lack of programmer understanding.

Consider:

You are born with no body parts.

    Person me = new Person();
Should anyone attempt to interact with you before your organs have finished arriving you will die.

    me.interact();
Throws a NullPointerException because the class designer improperly initialized the instance. The designer should have provided a constructor.

    public static Person() {
        this.organs = new Organs();
    }
One by one, your organs are pushed into you.

Instead, the class designer probably followed the JavaBean pattern and provided a setter method for Organs:

    public void setOrgans(Organs organs) {
        this.organs = organs;
    }
etc.

(For those that don't remember: The JavaBean idea arose because early versions of Java had no way of performing introspection, something that software tools really, really need. JavaBeans broke object encapsulation by design. Many programmers had to write beans to take advantage of their tools, and learned a very bad habit.)

Re:A bad citizen in Javaland (2006) by incepted on Thursday December 03, 2015 @09:08AM (#10669589)
> The JavaBean idea arose because early versions of Java had no way of performing introspection

Not quite. Java has had introspection since 1996. What JavaBeans brought is a consistent way of naming your methods so that classes can be introspected and made sense of in a way that allows composition and interaction.

Re:A bad citizen in Javaland (2006) by jt2190 on Thursday December 03, 2015 @10:23AM (#10670011)
I stand corrected. :)
Re:A bad citizen in Javaland (2006) by _asummers on Thursday December 03, 2015 @08:41AM (#10669471)
In modern Java, one uses things like dependency injection (Guice, Spring, whatever) for building up the services, and I find most programmers now abhor setters for their POJOs. Want a new Person object with a different name? Make a new one, it'll be okay, except in the most extreme of circumstances. Ephemeral memory is cheap. When an object comes back from a constructor, it should (in my mind at least) be immediately usable. People also enjoy the builder pattern, I've found. When you call .build(), it will do the internal checks to say if you screwed up, so you're guaranteed to never have a Person with no Organs. This is, of course a run time check so less helpful than a compiler error. Depending on the use case, it makes perfect sense, though.

Also side note, how do I do the code blocks in HN comments like you did?

Re:A bad citizen in Javaland (2006) by bencoder on Thursday December 03, 2015 @09:01AM (#10669557)
There's still a lot of getters everywhere though, which seemed to me to be the bulk of the analogy.

    this.wallet = colleague.getWallet();
Code blocks are done with 4 space indentation at the start
Re:A bad citizen in Javaland (2006) by reitanqild on Thursday December 03, 2015 @10:40AM (#10670142)
Which is not a fault of the Java language: trainwrecks (this->getThat()->getThose()-foreach(){ do stuff } is a general antipattern.

The correct way is IMO: colleague.getMoney() which can easily be implemented in Java, PHP etc.

I'm getting a bit tired of this constant pecking at Java (nothing personal). There are a number of questionable designs yes, there is a lot of ugly code out there, yes, but almost every alternative might have been worse:

Teaching everyone C? Good luck.

Switch to C#? Until recently that would prevent you from using Linux on your servers unless you were ready to put in technical effort and bet on MS not suing you or the upstream effort.

Perl?

PHP?

Catch my drift folks? Java filled a niche. It was almost as easy as PHP once you got started (although onboarding still is messy I think) and provided superior tooling.

Re:A bad citizen in Javaland (2006) by bencoder on Thursday December 03, 2015 @11:24AM (#10670439)
Despite the name, I didn't see the article as taking a pop specifically at Java. It seemed more about bad OOP in general. As you say, you'll see the same patterns in every OO language.
Re:A bad citizen in Javaland (2006) by lmm on Thursday December 03, 2015 @11:43AM (#10670562)
The story is explicitly about a bad citizen in Javaland.
Re:A bad citizen in Javaland (2006) by woah on Thursday December 03, 2015 @10:55AM (#10670241)
I think people just hate all the hoops you have to jump through to satisfy arbitrary constraints the language places upon you to conform to some pedantic definition of object orientation that only works well in textbook examples.

Not saying Java isn't a good choice for some things. Also, if functional programming languages were more popular, they would probably be subjected to the same ridicule for the same reasons.

Re:A bad citizen in Javaland (2006) by sevensor on Thursday December 03, 2015 @11:14AM (#10670364)
By the same token, I think the superior tooling is part of the reason people who aren't primarily Java users find it incredibly painful. I've seen Eclipse experts in action -- most of the work seems to be picking the right thing from a drop-down. All kinds of things just appear where they're needed. Accustomed as I am to typing source code, I can't quite get my head around it.
Re:A bad citizen in Javaland (2006) by workitout on Thursday December 03, 2015 @01:49PM (#10671666)
>> Which is not a fault of the Java language: trainwrecks (this->getThat()->getThose()-foreach(){ do stuff } is a general antipattern.

While I agree with you, isn't that what the new streams in Java does? The map reduce complex?

Re:A bad citizen in Javaland (2006) by steveklabnik on Thursday December 03, 2015 @11:42AM (#10670553)

  you only need two spaces, actually
https://news.ycombinator.com/formatdoc
Re:A bad citizen in Javaland (2006) by lmm on Thursday December 03, 2015 @11:42AM (#10670555)
Relying on dependency injection is even worse than setters. Will DI behave the same in tests as in real code? Are you sure?

The right thing is to use an actual constructor that takes constructor parameters. Guice and Spring can both handle this correctly (you just put the @Autowired on the constructor itself). The trouble is that in Java this requires ~twice as many lines as putting a magic annotation on your fields.

Re:A bad citizen in Javaland (2006) by pbh101 on Thursday December 03, 2015 @02:31PM (#10671920)
Especially in the multi-core era, this becomes more and more important. Thanks to the Java Memory Model specification, if you are using constructors (and yes, pretty much every DI framework by now has this feature), with just a bit of care[1] you can ensure that your object is never visible to any threads without it having been completely constructed.

[1] Make sure to not leak references to your object in your constructor. Most of the time that you would want to do this is starting a service or registering a handler, etc. The solution is to use a static creation method which first calls the constructor, and then registers/subscribes it, etc.

Re:A bad citizen in Javaland (2006) by krylon on Thursday December 03, 2015 @01:30PM (#10671533)
> (For those that don't remember: The JavaBean idea arose because early versions of Java had no way of performing introspection, something that software tools really, really need. JavaBeans broke object encapsulation by design. Many programmers had to write beans to take advantage of their tools, and learned a very bad habit.)

I have wondered what that was about. Thanks for clearing it up.

It is strange how the lack of a feature people really need (or want) can cause them to do weird things to work around that lack.

Re:A bad citizen in Javaland (2006) by joonoro on Thursday December 03, 2015 @08:43AM (#10669477)
Yeah same, to me it seemed like poking fun at bad OOP practices more than anything, and the last paragraph is just casting garbage collection in a funny way.
Re:A bad citizen in Javaland (2006) by darrenhobbs on Thursday December 03, 2015 @07:03AM (#10669144)
Well, this made my day! I'd almost forgotten that blog was still running. Achievement unlocked :)
Re:A bad citizen in Javaland (2006) by anthonybsd on Thursday December 03, 2015 @07:26AM (#10669216)
This is one of the best metaphors I've read in a long time!
Re:A bad citizen in Javaland (2006) by gravypod on Thursday December 03, 2015 @06:32PM (#10673544)
I feel that this is a representation of the fundamental misunderstanding of how object orientation is really done. In my opinion, if designed this way, this application was not well thought out.

If I was writing this, I would aim for this:

You wake up one day and you call to the heavens asking for transportation, you yell out the location in front of you, and with no response from the skies a self driving car appears.

You walk to the car and ask it to take you somewhere. Suddenly a pair of hands reaches out from the car, pulls you in, and takes you to your location. While you arrive, you are pushed out.

When you get to your job, you need money for lunch. You hand your wallet to your friend and your friend puts money into it giving it back to you. You keep your friends phone number on record if you would like to ask for money again. If he is busy, you can leave a voice mail and eventually he will throw money at your face hoping you catch it.

When you finally get home, you go into your bed and sleep until your bed wakes you up the next day to start again.

It's silly, like the original post, but I prefer to give handles and let abstracted code do it's magic. In all of these cases, I do not have the caller assume anything. It does what it knows it can do.

Some things need a more fine tuned application of this, but the idea is you just never assume something is true, only use what you know. That's where a lot of people mess up in java.

Re:A bad citizen in Javaland (2006) by GhotiFish on Thursday December 03, 2015 @08:45PM (#10674083)
note: It's called "A bad citizen...".
Re:A bad citizen in Javaland (2006) by CrimsnBlade on Thursday December 03, 2015 @12:05PM (#10670718)
Stuff like this is fascinating to me. I'm by no means an expert in writing code, I'm very much a beginner, but the creative potential that is there has always drawn me in.

If one was going to learn OOP correctly in Java (or Python) from the beginning, where would be a good place to start? Are there any textbooks or good websites for this?

Re:A bad citizen in Javaland (2006) by jdmichal on Thursday December 03, 2015 @12:54PM (#10671185)
The SOLID principles are all you need to know. [0] Then realize through them that inheritance as generally taught is actually a bad idea and use interfaces instead. Then realize that interfaces are just a weaker version of type classes and reach enlightenment.

[0] https://en.wikipedia.org/wiki/SOLID_%28object-oriented_desig...

Re:A bad citizen in Javaland (2006) by Gibbon1 on Thursday December 03, 2015 @03:12PM (#10672242)
I had this thought recently that inheritance was a bad solution to languages not supporting containers natively. And or a lame attempt at code reuse. (A root class is an API/Service with no versioning)
Re:A bad citizen in Javaland (2006) by rripken on Thursday December 03, 2015 @02:34PM (#10671942)
Is this in reference to the type classes as seen in Haskell or something else? I want to reach enlightenment but I'm stuck in Java for the time being.
Re:A bad citizen in Javaland (2006) by jdmichal on Thursday December 03, 2015 @05:24PM (#10673181)
Yes, as in Haskell type classes. Or Rust traits. Basically, anything that acts like an interface (defines a contract) but allows external definition. So you're not stuck wrapping your Integer in a ShowableInteger just to have an Integer that implements the Show interface.
Re:A bad citizen in Javaland (2006) by gerbilly on Thursday December 03, 2015 @04:00PM (#10672608)
OOP allows you to avoid code like this:

function makesound() { if( thing is a Cat ) return "meow"; else if (thing is a mouse) return "eep": else if (thing is a sheep) return "baa"; }

Code like this in an OOP language would be replaced by creating classes, such that the structure of the program implicitly executes the if else switch above, without you having to write it.

class Cat { makesound() { return "meow"; } }

class Mouse { makesound() { return :eep"; } } . . .

Then if all those classes inherit from a common animal ancestor, the calling code would simply say: animal.makesound()

This becomes very convenient when the program behaviour relies on a combination of two or more types.

In that case the if else example would have to contain nested and possibly repeated ifs (or calls to functions) to handle the logic for all the variations.

Note that inheritance in this example isn't to reuse code, just to establish the relationship between subtypes.

If you become tempted to use the type system as a clumsy code reuse mechanism, you will encounter alligators for sure.

Neither type of code is good or bad, just convenient for different purposes or not.

OOP is especially nice for writing simulations. See: https://en.wikipedia.org/wiki/Simula

Re:A bad citizen in Javaland (2006) by mintplant on Thursday December 03, 2015 @04:59PM (#10673008)
> Then if all those classes inherit from a common animal ancestor, the calling code would simply say: animal.makesound()

Which of course throws an exception in the case of Monk (inheriting from Animal -> Human), as it has taken a vow of silence.

Re:A bad citizen in Javaland (2006) by tacon on Thursday December 03, 2015 @01:24PM (#10671482)
Re:A bad citizen in Javaland (2006) by mrmondo on Thursday December 03, 2015 @06:28AM (#10669043)
I'd love to see an annotated version of this.
Re:A bad citizen in Javaland (2006) by TazeTSchnitzel on Thursday December 03, 2015 @12:51PM (#10671163)
It's not quite the same sort of thing, but this reminds me of 'My Life as a Forth Interpreter' (1986): https://news.ycombinator.com/item?id=7410883
Re:A bad citizen in Javaland (2006) by jlewallen on Thursday December 03, 2015 @10:04AM (#10669886)
This is great! I can't help but feel there's also an opportunity in a sequel to allude to the all too common "invent arbitrary abstract base classes to keep my code DRY" anti-pattern.
Re:A bad citizen in Javaland (2006) by jdmichal on Thursday December 03, 2015 @12:56PM (#10671201)
For learning purposes for those reading this, what would you consider the alternative?

I personally prefer just stuffing commonly used stuff into utility classes containing only public static functions. Makes them really easy to test, too.

Re:A bad citizen in Javaland (2006) by jlewallen on Thursday December 03, 2015 @02:25PM (#10671884)
Thanks for asking! My answer, of course, depends on the particular scenario. I've had success with simple static methods, especially early in a project. I'd also consider some kind of composition (has-a vs is-a). The latter can be especially nice as static methods gain complexity in the form of more parameters/variations. This also shines if there's meaningful alternative implementations that can be swapped or if you're writing business logic and some legitimate business concepts start to congeal out of the code being re-used.
Re:A bad citizen in Javaland (2006) by jokoon on Thursday December 03, 2015 @06:07AM (#10668981)
I can't understand java programmers telling me they don't like C.
Re:A bad citizen in Javaland (2006) by lmm on Thursday December 03, 2015 @06:45AM (#10669086)
A bad citizen in C counts to 32768 which corrupts the very fabric of the universe, replacing New York with half of Los Angeles and leaving 25 people repeating the same day indefinitely.
Re:A bad citizen in Javaland (2006) by TeMPOraL on Thursday December 03, 2015 @06:57AM (#10669124)
That sounds like a plot of a typical superhero comicbook. I guess we know what language Marvel's universes are written in.
Re:A bad citizen in Javaland (2006) by chris_wot on Thursday December 03, 2015 @06:59AM (#10669135)
Every time a character swears they speak in Perl.
Re:A bad citizen in Javaland (2006) by llogiq on Thursday December 03, 2015 @07:19AM (#10669190)
But the universe is certainly written in Brainfuck.
Re:A bad citizen in Javaland (2006) by jokoon on Thursday December 03, 2015 @08:56AM (#10669541)
Still better to correct this mistake than to think about the intricacies of the java abstractions.
Re:A bad citizen in Javaland (2006) by yuchi on Thursday December 03, 2015 @05:53AM (#10668927)
This is true horror.