Category: java

Java is the new ‘C’ part 2

We had adjourned to the kitchen, it was done in some old mock style from somewhen. Not Victorian, but nearly, pretty ugly. I whisked the green tea and followed the ancient ritual. The master had never subscribed to the nonsense about coders being caffene-fuelled chocolate junkies. Brain-dead, tired, and fat, not to mention incipiently diabetic. The master could be hard at times, but fair. The hot tea steamed up my glasses, the spittle-encrusted coat was being dried on a chair next to the Aga. We sat quietly for a few moments’ contemplation. I noticed that his collection of pipes still adorned the mantlepiece. I could see his need for the demon weed pulling at him, but he firmly turned his back on it.

So, master, you say that we at least have two types now, ints and strings? Why is this bad?

It isn’t bad, my young friend, just silly. Who needs typed variables any more? A variable should be able to be whatever it pleases, and have a bath twice on Sundays if you wish it. To pass things around in Java you have to create utilities that use Object types and then remember what type they are, or you have to hide some array-like construct (say a Vector) in an accessor class that returns the correct type – Java 5 has some syntatic sugar for this, to be sure, but it always generates a compiler warning message, which really drives me crazy. Java 5 isn’t mainstream yet, all of your legacy code is still riddled with anonymous objects flying around – it’s a mess. It’s said to be flexible, but it’s just a lazy anarchy inherited from C++.

He sighed, and you have to wrap the base types in wrapper classes – again Java 5 has removed some of this by doing it automatically. But why has it taken ten years to take this implementation pain away? Why has it taken ten years to do some of this? Why isn’t there syntax for associative arrays? Because the vision of the language designers was clouded by ‘C’ and C++. C# is just a continuation of this as well, as far as I can make out. I don’t have time to learn the Dark Side as well as the Grey.

And that brings us to the vexed question of the absence of lambda functions and variables that point to functions – a piece of brilliance that goes all the way back to Lisp, even ‘C’ could do pointers to functions, I used it all the time, but of course it scared people who wanted to use ‘C’ to write COBOL.

I was puzzled. I thought you didn’t like ‘C’, master?

Did I say I didn’t? I just said you had to be very disciplined and test things thoroughly. This palace you sit in was built by ‘C’ and Unix, all I said was you shouldn’t take the crazed rantings about simple and confuse it with complete.

Master, surely anonymous classes are the lambda functions you seek?

He paused for a moment and cracked some walnuts with his new biceps, while he considered this. Then he smiled and offered me some. I declined for my biceps have not been subject to his punishing training regime.

Grasshopper, you are beginning to learn. That is the first original thought I’ve heard from you; keep it up. So we could define some aribitrary interface that allows us to define an aribtrary, anonymous object that allows us to create our anonymous function depending upon, say, an if statement?

I nodded. He sipped his tea and reached meaningfully for the sugar bowl.

Ah, master, again, no sugar. You can do it but it involves creating objects and interfaces that type those objects.

He smiled again, not touching the bowl after all. Unecessary complexity, functions can’t just stand out there in the naked wilderness and do their jobs, you are forced to go class class with extra fries and a milk shake to write 5 times more lines of code than you would in Python, PHP, or Lisp. Like everything in Java, you can do it but it hurts, it takes time and its a pig to maintain because the way you are using the construct is too outre for the masses that may have to look after your code when you have finished.

… you just try getting a comment saying this is a lambda function construct through a code review. He started giggling to himself and started on another chorus of ints and strings.

Let us pause here, for a little while, Grasshopper, and we will talk about Java and the Web. Why is it so slow to develop in and what can be done about it.

I sat and watched the steam rise from the tea cups, content to be silent for a while. I wondered, how do you use ‘C’ to write COBOL?

Java is the new ‘C’ part 1

When I next saw the master he was on a new health kick, gone was the pipe and the toffee tobbaco and in was the low fat food and exercise. He still managed to fix me with a rheumy eye though.

Grasshopper, it’s been a long time, many heads have passed under the surgeon’s knife since we last talked. What have you been doing, my young friend?

I swallowed hard and tried to look him in the eye.

I’ve been using Java, master.

Java? You’ve been using Java, eh? Have you learned nothing?He was not far from incandescent with rage, I had expected this.

I interjected – I need to earn a living, master, and Java pays the bills.

So? Grasshopper, my young friend, don’t you realise Java is the new ‘C’?

Master, master, it has no pointers. How can it be the new ‘C’?

Well, now, well now – remember how everything was a 16-bit integer?

Yes, master, then it became 32 and then 64 …

But still, an integer?

Yes, master.

In Java – everything is a String, immutable, heavy, can’t be extended. A string.

I tried to interrupt. Master – surely everything is an object?

Only sometimes, even then … what is XML? Long strings, what is a properties file – a bunch of strings – what do you spend all day doing? Throwing strings around – In your JSP’s and anything else – Strings. He shuddered. I have nightmares about them.

Master, what is wrong with strings?

They are not a type, but a class. They should have been a type, or everything should have been a class. When you write code you’d like to just use strings and not have to worry about the rules, the way you can use an integer or a float. It’s that, if I was designing a language from the ground up, I wouldn’t mess with such petty distinctions. AND, Grasshopper, AND strings are final, which means you can’t add to them, or you have to wrap them in another class of your own. They were afraid of security issues where people override certain methods and put in malicious code. They could have just made the standard methods final, but that would have been too easy.

He paused and drew breath, I wiped the spittle off my coat.

… and they’re immutable – so they consume loads of memory when you manipulate them and people don’t realise. The documentation says use the StringBuffer class, because it is mutable, but there are hardly any decent examples showing how to do this, and if you want to call a standard libarary function (or SAX or whatever you like) with StringBuffer – you need to call toString – which makes it all a bit pointless. And that lovely syntactic sugar using + to manipulate strings doesn’t work with StringBuffer – gah! Rubbish! Time-wasting rubbish.

Not to mention – he was flying now, eyes almost popping out of his head – why does Java have a char type and a string type. What nonsense is that? Why does it have any of the other types – the bytes, shorts and all that rubbish inherited from ‘C’? Why would you ever want a short integer? Running out of memory? On a modern machine with modern amounts of memory? They force you to learn all that irrelevant crap to get certified – I’ve only ever had use for ints and strings – maybe Java is slightly better, maybe it has two things to play with rather than one.

He started musing to himself. Singing ints and strings to the tune of Police and Thieves, although I’ve never seen either scare a nation with their guns and ammunition.

Java is also very low-level, that ancient crusty ‘C’ way of doing things, no sugar that can be efficiently compiled. You have to keep typing the same array walking constructs in again and again. There isn’t any syntatic sugar for things like lists and dictionaries and walking arrays of objects. Oh, yes, there are classes that support them, but no syntax. In a usable langage like python I can say something like

[mapping-expression for element in source-list if filter-expression]

This will give me a portion of a list depending upon the if statement. PHP has similar ideas (although not as condensed). Python will let me split lists, look at them from the end etc. etc. and it also has dictionaries and syntax for them as a base type. Java gives me 200 different implementations of the dictionary interface – why should I have to care? Blinkin’ computer scientists, too clever by half.

The fire was really glowing in his eyes now. I was beginning to worry about ever getting my coat dry again.

And I still hate {} to delimit blocks of code; if should have end if, loop should have end loop, there’s a whole class of stupid errors that can’t happen if you do this. Again, Python uses indentation to do this – so it’s clear what goes where.

To me this is a flaw with PHP, in that it has too much ‘C’ heritage but its way of doing class- and instance- variables is vastly superior to Java:

self::$var ; // This is a class variable
$this->var // This is an instance variable

To use a global in a function you have to declare it

global $var ;

In the method that you want to use it in. The syntax is a little clunky but at least you can tell these kinds of variable apart, a whole class of bugs never appears. And the object stuff in PHP and Python is optional, you don’t have to worry about it if you just want to write a quick script to get something done.

I sighed and said, would you like me to make some tea, master? It was going to be a long day.

More from the master