News for hackers. Stuff that matters.
Re:How (not) to write Factorial in Java by dkarl on Wednesday January 26, 2011 @11:35AM (#2143778)
The biggest complaint I have with many Java developers is that they develop a whole bunch of really bad habits. Specifications are unclear, or they think someday the code may need to be extended into a different direction. So they write a whole bunch of overblown architectural nonsense, sight unseen, thinking that the additional crap someday will help out and make things easier.

This is not fair; Java has moved beyond this. In modern best-practices Java it is no longer necessary or even possible to write big piles of gratuitous OO spaghetti. Today, experienced Java programmers build their applications on top of huge, industry-standard, highly polished piles of architectural bullshit, wrestling with which leaves them with no time to write any code of their own.

Re:How (not) to write Factorial in Java by mmilkin on Wednesday January 26, 2011 @03:12PM (#2144911)
I am very very confused what this post has to do with Java? It has everything to do with premature optimization and implementation of Design Patterns unnecessarily... And since Design Patterns are not language specific the title of this post is wrong and misleading even the conclusion that the author tries to draw up is of dubious merit ..... The only part of the post that makes this specific to Java is the comment that the factorial function must be wrapped in a class.
Re:How (not) to write Factorial in Java by dkarl on Wednesday January 26, 2011 @03:32PM (#2145006)
You're right; convoluted architecture has as much to do with Java as polar bears have to do with the arctic. They're two completely different things.
Re:How (not) to write Factorial in Java by lukesandberg on Wednesday January 26, 2011 @07:23PM (#2145959)
Fun fact: its called the arctic because of the bears. The word arctic comes from a greek word meaning northern bears. http://en.m.wikipedia.org/wiki?search=Arctic

And that is also why the antarctic has its name...because there are no bears there, just penguins.

Re:How (not) to write Factorial in Java by al-king on Thursday January 27, 2011 @12:22AM (#2146700)
While it's certainly named for bears, that appears to be be a folk etymology: as the Wikipedia article you link suggests, the Arctic is named for the northern bear constellations Ursa Major and Ursa Minor, so called since the time of Homer. The word Antarctic has been in English since the C14th ( http://www.etymonline.com/index.php?term=Antarctic ); a continent opposite to the Arctic was hypothesised long before Antarctica was discovered in the 17th century. Antarctic, then, means "opposite of north" rather than "no bears" - though I suppose the Ursa constellations were also not in the north :) and it's a nice mnemonic.
Re:How (not) to write Factorial in Java by Retric on Wednesday January 26, 2011 @08:33PM (#2146159)
I think there is a correlation between limitations of Java and how people use it. If functions where a first class operator a lot of the architectural fluff would be truly pointless. However, because of language limitations people start to think of "flexibility" as a feature and try and make a an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp
Re:How (not) to write Factorial in Java by dspeyer on Wednesday January 26, 2011 @09:06PM (#2146258)
The design goal here is that you can get factorials by calling a function, but decide how to compute them (cached or not, with the ability to add new algorithms) by changing state. A dubious goal, but not completely insane.

In python, it would be 15 lines, and simple. No utils, factories, algorithms or singletons.

I imagine it could be done in more reasonable java than this, but could it really be done cleanly and compactly?

Re:How (not) to write Factorial in Java by nostrademons on Wednesday January 26, 2011 @06:53PM (#2145872)
See Lambda: The Ultimate Political Party:

http://www.nhplace.com/kent/PS/Lambda.html

Languages are more than just syntax and semantics. There's also a whole culture that grows up around the language, a set of "best practices" and common idioms. And the one that's grown up around Java seems to value abstraction and design patterns and convoluted, overgeneralized solutions.

This doesn't apply to all Java code, of course. You can write lean Java just like you'd write lean C or lean Python. But a non-trivial amount of the Java jobs out there expect you to layer frameworks on top of frameworks and jump through an amazing number of hoops just to get something done.

Re:How (not) to write Factorial in Java by abrenzel on Wednesday January 26, 2011 @04:43PM (#2145305)
As a man named William Shatner once said, "Brevity is the soul of wit." Or was that Shakespeare? The same is true for good programs, I think.

When we see problems like this, it's right to ask questions about what the language design encourages its programmers to do. Additonally, languages have "personality" just like people do, and those personalities evolve with the user base of the language as they adapt to the language's environment.

The question of whether this type of code is more common in Java than say Python or Perl is empirical, and my gut says it is. Java is the commoditized, corporate language par excellence. The features it implements and the way it implements them have attracted the kinds of programmers who would write code like that. That's not to say that Java code forces its users to write code that is so inelegant. That's just to say that it creates incentives that often push toward ugly code. C++ does that too. On the other side, Python (to name one example) seems to set up its environment in such a way as to encourage pretty code.

There's no clear-cut good or bad here, only interesting trends in the code produced in various languages.

Re:How (not) to write Factorial in Java by JanezStupar on Wednesday January 26, 2011 @03:27PM (#2144990)
There are some insanities (patterns?) that most certainly are. The Java I have seen written by the people I know and the coding practices and rituals they aspire to, make me feel uneasy. All the cool toys Java (and .net mind you) kids get to play with (IOC!, Factories!, Wrappers!, XML!, Objects above all!...) just feel like language band aids to me.

And indeed I also have a feeling that premature optimization and general architectual astronaucy are deemed very highly by Java folks.

Don't get me wrong - I think that JVM is a marvel. I could also find greater joy in writing COBOL for a living. (at least my coworkers wouldn't be rambling constantly about stuff that is wrong and don't matter).

Re:How (not) to write Factorial in Java by pixelbath on Wednesday January 26, 2011 @03:17PM (#2144937)
I believe the author was attempting to illustrate both how easy this is to do in Java, and how prevalent the practice is.

Every book I've read on Java shows even the "Circle inherits from Shape" examples to be ridiculously overarchitected and encapsulated (I am aware of why they are this way for illustrative purposes, however).

Re:How (not) to write Factorial in Java by mmilkin on Wednesday January 26, 2011 @03:52PM (#2145084)
Lets not get into the faults of Java and inheritance this thread might devolve into something it shouldn't :).

But its a good idea to do some of this stuff where and WHEN its appropriate. You can design a space shuttle to go to the grocery store doesn't mean you should.

I have seen my fare share of useless hard to understand and over engineered code in C, Groovy, Java and Lisp

A good book about this by Joshua Kerievsky is Refactoring To Patterns he talks exactly about this, the dangers of using patterns and the dangers of not (yes he uses Java in his examples)

Re:How (not) to write Factorial in Java by swannodette on Wednesday January 26, 2011 @12:40PM (#2144062)
Yeah experienced developers building scalable "bullshit" like GMail et al, ITunes Store, Android, etc.
Re:How (not) to write Factorial in Java by rbanffy on Wednesday January 26, 2011 @01:22PM (#2144297)
For every example of well used Java, I can easily come up with a dozen counter-examples of insanely complex, unreliable and unmaintainable messes I have encountered in my professional life.

It's not worth it.

Re:How (not) to write Factorial in Java by mseebach on Wednesday January 26, 2011 @01:39PM (#2144389)
To be relevant to the thread, those systems must be insanely complex, unreliable and unmaintainable, because the Java developers who made them has these habits that Java developers apparently have.

To the extent Java correlates with bad software, IMHO the causation is more likely to be that the environments (in the vein of clueless corporate management, waterfall, http://www.halfarsedagilemanifesto.org/) are more likely to pick Java for their development.

Re:How (not) to write Factorial in Java by dhimes on Wednesday January 26, 2011 @11:01PM (#2146522)
Well, maybe, but the cultural argument is also applicable. With my first Java app I did this kind of stuff because the examples of Java I found implied that this was a good thing to do. I didn't have a lot of experience in this area, so I thought, "these folks have been around the block, experienced the pain, and now have words for the wise." So I did it too.

I wonder if I will actually ever use one of those factories again in that app.

Re:How (not) to write Factorial in Java by rbanffy on Thursday January 27, 2011 @04:55AM (#2147286)
One of the things that annoys me in Java books is that every meaningful example is a couple pages long, most of it giving context to a function or method. The process of writing a meaningful may be so painful and risky Java programmers defend themselves by starting with code so generic they believe it won't need rewriting in the future.
Re:How (not) to write Factorial in Java by rbanffy on Wednesday January 26, 2011 @03:14PM (#2144923)
> because the Java developers who made them has these habits that Java developers apparently have.

That's the point. Java, it seems, make it tempting to "overarchitect" applications into excessively generic and modular beasts early in their lives when a much simpler implementation would be perfectly adequate and much simpler to extend and maintain.

Re:How (not) to write Factorial in Java by jbooth on Wednesday January 26, 2011 @03:07PM (#2144881)
You can do that in any language. Java's ubiquitousness means that there's a lot more bad code, especially when you look at some of the places it's become ubiquitous.

But you know what? Once we've established that you can write bad code in any language.. I'd prefer to deal with bad code in Java than almost any other language I can think of (and I've seen a fair bit). Maybe Python wins out, but I'd rather debug bad Java code than bad C++ or bad Ruby for sure.

Re:How (not) to write Factorial in Java by eru on Wednesday January 26, 2011 @05:45PM (#2145598)
I did not have to deal with bad Haskell, yet. But I wonder, how it would turn out. The type-system can give you some pretty strong guarantees (if you abstain from using back-doors), and thus make interfaces much less leaky. I.e. if a function has the type String -> Int, you know that it takes a String and gives an Int, and doesn't have any strange side-effects, like setting some random variable or sending away your credit-card data.
Re:How (not) to write Factorial in Java by nostrademons on Wednesday January 26, 2011 @06:59PM (#2145897)
Bad Haskell usually involves poor type abstractions, eg. instead of defining new data types for inputs that are logically distinct but physically identical, you just re-use existing types and add new constructors. Or you end up using tuples for everything. Or (horror!) you encode a bunch of things as strings instead of using the type system at all.

The nice thing about Haskell is that it's really, really hard to write things that accidentally ignore purity, which eliminates a whole class of errors and bad designs. The type system only helps you if you let it, though.

Re:How (not) to write Factorial in Java by eru on Thursday January 27, 2011 @08:35PM (#2150424)
Yes, that's sounds plausible.

I've looked at a friend's Haskell program for Huffman encoding, that used raw recursion in a undisciplined way instead of abstracting the control flow out into, say, filters, folds and maps. That was the closed I ever got to ugly Haskell.

Re:How (not) to write Factorial in Java by munificent on Wednesday January 26, 2011 @05:32PM (#2145521)
What programming language is that not the case for?
Re:How (not) to write Factorial in Java by rbanffy on Wednesday January 26, 2011 @06:50PM (#2145863)
Messes, perhaps, may be more frequent in PHP/ASP applications, but insanely convoluted and overarchitected solutions with unrequired levels of indirection and never used configuration XML files that have to be generated by an IDE are much more frequent in the Java/C# camp than, say, in the Python/Ruby/Perl/Smalltalk camp.
Re:How (not) to write Factorial in Java by joshwa on Thursday January 27, 2011 @02:04PM (#2149025)
c.f. Spring, EJB, JBoss, anything J2EE, etc.
Re:How (not) to write Factorial in Java by Zev on Wednesday January 26, 2011 @02:40PM (#2144726)
I've heard that Apple still maintains WebObjects for Objective-C for the iTunes Store (and for Radar).
Re:How (not) to write Factorial in Java by GeneralMaximus on Wednesday January 26, 2011 @09:56PM (#2146379)
That has nothing to do with the language and everything to do with the developers. What if the same developers used Common Lisp or Smalltalk or Haskell?

Gut feeling says they'd be even more productive. Terrifyingly so, in fact. The sad fact is, though, that we'll never be able to conclusively prove a statement like that.