
Why and how I write Java - johnyzee
http://stevewedig.com/2014/02/17/why-and-how-i-write-java/
======
andrewflnr
Having been exposed to OCaml, I'm starting to think that if you have null
references, you don't really have static typing. I guess I'll have to look at
guava, but it can only help so much in interacting with other code.

~~~
dasil003
As a rubyist by day this is the thing I find so exciting about Haskell. I
write dynamically-typed code all day, and the most common bugs are not because
of passing an Apple to an Orange interface—that just isn't very common—it's
because of cascading nulls. Of course Java's static checks are worth
_something_ , but not much more than the most cursory of checks that my basic
test coverage does in Ruby.

~~~
frou_dh
My takeaway from looking into OCaml was that pattern-matching with
exhaustiveness checking was the jewel, as contrasted with primitive C-style
if/else chaining or switches.

~~~
andrewflnr
Right, exhaustiveness checking is how the compiler makes sure you handle
missing values (option types) correctly. I guess you could say that's one of
the bigger payoffs.

------
Xdes
>Diamond operator

Wow. So instead of adding something like var in C# or auto in C++ they went
the opposite way. Makes no sense to me.

~~~
eropple
The diamond operator isn't variable type inference, it's parameter type
inference for generics.

It should have var/auto, for sure, but this is a different thing.

------
primitivesuave
I think one thing that this post touches on is the effectiveness of Java as a
programming language for large projects. Although personally I've moved on to
Python since it greatly simplifies web-related tasks, a lot of our legacy
projects are in Java and I still hear plenty of people sipping the Java
haterade. So I thought I'd share some of the most common things I hear.

 _Only my grandpa writes in Java anymore._

I hired a junior developer a couple months ago who absolutely refused to write
code in Java, swayed by the vitriol that is often spewed by highly opinionated
developers against Java. Instead of taking the time to learn the language, he
read up on all the Java Versus Everything Else rants out there and emailed me
links to them, trying to convince me that his arrival meant our code base
should be rewritten in a "more modern language". Needless to say that he
didn't last very long.

The fact remains that Java is an incredibly mature language that runs much
faster than most other solutions, and makes it significantly easier to handle
the complexity of a large project. There is an enormous ecosystem of libraries
for Java, and it would be foolish to move to a less mature language which
needs more wheel reinvention.

 _Garbage collection is bad._

There are the obvious downsides to garbage collection, e.g. Android apps
running on low-memory devices, but for an enterprise application running on a
server you shouldn't be breaking balls about garbage collection. Obviously it
would run 10 - 30% faster in C, but we live in a day and age where it is
cheaper to provision extra servers than it is to pay people for the extra man-
hours they spend gdb-ing for segfaults and valgrind-ing for memory leaks. It
is much easier to pick up a good Java developer than a good C developer,
because the good C developers have already been picked up by the big companies
that actually need the performance advantage. And from my experience working
in a company on an enormous C code base, I know firsthand how easy it is to
pick up a really shitty C developer.

 _It seems like most of my Java code is just checking for errors._

Would you rather not check for errors? What I mean is, would you rather allow
a single developer's "return null" statement that was absentmindedly used to
handle a rare edge case to completely fuck your application when that edge
case is actually reached? Pardon my French, but I'd rather take the time to
throw/catch exceptions than get fucked by a gung-ho developer's null, since
Java will force exceptions to be caught and handled at some level. With a
well-architected solution, error handling can be centralized and developers
can more easily contribute to the codebase if the throw-catch paradigm is part
of the coding standard.

 _Java 's GUI capabilities really suck._

This one can't really be refuted - Java's Swing UI components are indeed quite
shitty. But you can work around them by creating your own Swing components,
and enjoy the benefits of a cross-platform solution.

I know this rant was long, but a lot of it was taken from emails that I've
sent to my devs in our Java vs. no-Java threads. I'm no Java fanboy, but I am
better than most at seeing past the haterade to where it works and where it
doesn't work. Maybe I'm wrong, but my experience is a bit more practical than
just reading an article that nitpicks at Java's perceived downfalls.

~~~
kamaal
>>I hired a junior developer a couple months ago ... his arrival meant our
code base should be rewritten in a "more modern language". Needless to say
that he didn't last very long.

Aha, Let me tell you what the problem is here. This junior guy and most of his
gang else where are starting companies with those 'Everything Else' languages.

All these 20 hr/day working kids are working on all these cool shiny projects,
building billion dollar companies with these new language. While Java can
perfectly manage to hang on and survive in ICU thanks to doctors like you. The
new languages will get lions share of popularity, new projects and all they
need to make it best language of the current times.

Needless to stay you can still work on Java. But it will mostly legacy code,
and by and large unimportant projects which no one really cares about in any
company. People working on such stuff are easily disposable. It gets difficult
to get jobs by the day. Even your existing company will find it difficult to
give you raise.

This happens every now and then. Fighting this as a whole is impossible. They
best way out is adapt to the new stuff and move on or risk getting obsolete.

~~~
primitivesuave
You seem to enjoy generalizations and hyperbole quite a bit.

First of all, such a small fraction of companies actually end up worth
anything at all, so jumping to the conclusion that an arrogant prick who
couldn't adopt to an unfamiliar development environment will go out and build
a billion dollar company is incredibly unlikely. Maybe I'll be completely
wrong, but the odds are undoubtedly in my favor.

Second of all, you clearly didn't read my post where I talked about Python
being our new weapon of choice. We aren't going to rewrite our legacy Java
codebase in Python or something else because there is absolutely no need for
it, and those codebases put the bread and butter on the table.

Third, when you're running the company, you have to consider things from a
business perspective rather than a technical one. I'm a developer myself so I
understand technical details and limitations, but my first question about the
product is "does it work?". Nobody is going to give two shits what language or
optimizations you used for a product if it doesn't work. They aren't going to
pay you a dime, either.

Your comment really exemplifies my point, that people do nothing but sip the
Java haterade without realizing what it is good for. Honestly I couldn't care
less- I'm not trying to change your way of thinking, and based on your radical
assertions that Java is dead, neither are you. But in case some other aspiring
dev is reading this, I'd strongly suggest to not be close-minded about
powerful tools like Java that have mature ecosystems and an excellent
community around them.

~~~
kamaal
>>We aren't going to rewrite our legacy Java codebase in Python or something
else because there is absolutely no need for it

Neither would I, And if I were in your place I would keep Java running. And
that was my whole point. Want to work on legacy stuff? Or the newer and better
projects?

By the way, Python isn't that new these days either. Start up's I know of are
going the Go, Clojure and Scala way.

>>when you're running the company, you have to consider things from a business
perspective rather than a technical one.

Which is why for the new projects it makes sense to chose the latest tools
which have little tech debt, which have fixed problems with older tools and
will hang on for longer time.

>> people do nothing but sip the Java haterade without realizing what it is
good for.

I don't debate that at all. But frankly speaking Java being good doesn't mean
there are better things than that.

>>Honestly I couldn't care less- I'm not trying to change your way of thinking

Neither am I, I have seen first hand what happens when you stick to legacy
tools. The jobs dry out, the projects you can work on move towards legacy end
and lastly your career as a large suffers.

>>but in case some other aspiring dev is reading this, I'd strongly suggest to
not be close-minded about powerful tools like Java that have mature ecosystems
and an excellent community around them.

If a young dev is reading this, please treat tools as tools and have to
special attachment to them. Learn to adapt and move on to newer things as and
when necessary.

------
cgh
For me, this was the most amazing thing about this post:

"Programming with explicit memory management: This bucket includes C, C++,
Rust, D, etc. I haven’t needed to explicitly manage memory, so I haven’t used
these."

I guess I'm just old.

~~~
sramsay
Maybe I am too, but that phrase just bugged me. Surely there are other reasons
to use these tools than just "explicit memory management?"

I suppose I could say, "I haven't used any JVM languages, because I haven't
felt the need to gouge my eyes out over memory errors I can't control."

~~~
jtheory
> Surely there are other reasons to use these tools than just "explicit memory
> management?"

Sure, though he does mention he's biased by the projects he works on
(generally web apps/services -- not device drivers, image processing, realtime
anything, etc.).

> I suppose I could say, "I haven't used any JVM languages, because I haven't
> felt the need to gouge my eyes out over memory errors I can't control."

The phrase that bugged you wasn't cast as an insult, though, and yours is...
mysterious; Java's GC is quite stable, so any memory-related errors are under
the coder's control. "Memory errors" being basically just "using too much
memory", as I've never seen any other type of memory error in Java.

Maybe you could say "I haven't used any JVM languages, because I've never felt
the need for automated memory management"? That assumes Java has nothing else
to offer.

------
jayvanguard
> With Java I have one language that can target servers, Android, and browsers
> via Google Web Toolkit (GWT).

Credibility shot. He's a fan of GWT. That explains a lot.

~~~
wedig
I really like GWT. I will be writing some posts that will go into more detail.

~~~
jayvanguard
I kid... partly. What I can't get over is a common justification for using GWT
seems to be "but you can program in java and Swing then!", which only holds
for fans of Swing. It seems is most often advocated by people uncomfortable
with Javascript development and little UI development experience outside of
Java.

I don't think Swing is terrible but I do think it isn't particularly
productive for user interfaces compared to Javascript or even Objective-C, C#,
etc.

So if all you have a Java/Swing hammer then GWT is great, otherwise not so
much.

In any event, I look forward to your post.

~~~
wedig
No worries then :) I'm comfortable with JavaScript and I've never used Swing.

I just use GWT to reach the browser, not to embrace it as an all encompassing
framework or anything. I use Bootstrap as the UI starting point.

------
mwcampbell
Do you use a framework for routing and handling HTTP requests, or just
servlets? What about accessing the database, if any? I assume you're not using
Java EE.

EDIT: For iOS, [http://j2objc.org/](http://j2objc.org/) can help you share
code between platforms, but you'll still do the UI and other platform-specific
things in ObjC.

~~~
wedig
I use GWT-RPC because it is easy. For other clients I wanted to try Thrift. I
like what Evernote did: [https://blog.evernote.com/tech/2011/05/26/evernote-
and-thrif...](https://blog.evernote.com/tech/2011/05/26/evernote-and-thrift/)

Thanks for the j2objc tip! I will definitely be looking into that. I thought
Thrift was as far as I could reach into iOS without Objective-C.

I haven't used J2EE. My experience with Django years ago made me framework
adverse. I will be posting a sample app in a month or two that shows what I
mean.

------
smegel
> I use Java for application programming. I recommend Python or Ruby for
> learning programming.

Define "application". There are plenty of cases where Python or Ruby makes
sense.

And Java actually makes sense as a learning language aswell, as it has more OO
depth which is what most courses focus on largely, without the pain of trying
to teach students C++.

~~~
_pmf_
> Define "application".

I think he/she defines application as "application", as opposed to the web
folks' definition that a web site having a text box, a button and 1 mb of JS
libraries is an application.

------
banachtarski
> High efficiency languages include C, C++, Rust, D, etc. I would love one
> language to rule them all, but it seems that for now there is still a gap
> between what language to choose application programming and for high
> efficiency programming.

What is this supposed to mean?

~~~
frenchy
For most programming, as long as your algorithms are not stupidly slow,
clarity is more important than efficiency. In certain situations (video games,
embedded devices with few resources) this is less true, and things like the
ability to do manual memory management becomes a lot more important.

~~~
banachtarski
This is more or less how I interpreted the statement. But grammatically it's
hard to parse and I felt it needed a lot of clarification. You're assuming the
memory model was the cause of a loss of application clarity but I know many
programmers that don't actually feel this is the case.

------
gebe
As a sidenote regarding iOS, you could checkout
[http://www.robovm.org/](http://www.robovm.org/). It's already used for
running libgdx (a Java game framework for a number of platforms) games on iOS
for example.

------
blueblob
Not sure if the author of this post is on or not but what did you mean when
you say you use java for scripting?

~~~
wedig
I took this out because it was unclear. I meant scripting in the sense that
Python and Ruby are called "scripting languages". One possible policy is to
use Java for your application, and something like Python to write scripts for
stuff like devops or whatever else "scripting" is. Instead of this, I prefer
use one language as much as possible, currently Java.

~~~
greatsuccess
I rolled my own java scripting tool. Its pretty easy to do, but I think that
most folks do this with Groovy now.

~~~
vorg
Quick & dirty scripts for testing and such where speed doesn't matter is
Groovy's strength. Its managers at VMware are trying to use the Groovy distro
as a delivery channel to sneak other unrelated software into businesses, such
as a static compiler and type checker which were written by one person only
and have virtually no commercial use. There's many other higher quality
languages out there for JVM static compilation, e.g. Java 8, Scala, Ceylon,
Kotlin, each of which have far larger teams working on them.

------
Untit1ed
Eclipse is silly no matter what language you use... yet he uses Eclipse?

------
greatsuccess
A little surprised Spring is not mentioned. Composition in Java is a nightmare
without Spring, and you can do some really interesting things in configuration
that would take you days of writing initialization code in very little time.

If not for Spring, I'm not sure I would want to construct things in Java at
all.

~~~
epper
He does mention Dependency Injection briefly. Anyway Spring is just one option
(e.g. Guice is another one).

~~~
greatsuccess
True, I just dont like Google's take on Java so I dont use any of their
libraries.

