Monday, June 21, 2010

Is Java dying? A response

This is actually a response to an article,  "Is Java dying? Dependency injection and other Java necessary evils", written by my friend Juanfri. Like his article was a response to another article that turned out to be too large, mine started out that way as well. In the end I decided to just post it to my own blog (that way it at least gets some attention).

I must say I liked the original article, but in the end I don't agree with some of it's assertions.

I don't agree for example that there is anything wrong with GWT using overlay types. The code example is disingenuous because it solves a different problem. In fact I have written the same code (using simple-json) this weekend and it's completely similar:


The integration in Ruby is nicer and simpler? Sure, but the mapping provided by GWT sure is useful and the implementation is normally completely hidden from view.

The examples about dependency injection, and his efforts to create a DI framework for Ruby are really funny, especially because they are so recognizable, all of us in this business have gone through the same moments (and will probably still do so a couple of more times during the years that come). Encountering a new technology and then using it for absolutely everything that you come across is normal.

It is the reason almost everything in Java is configured with XML, it was just the way it was done some years ago and will probably still stay that way for some years more. But the whole drive to use annotations shows that people are looking for different ways (and making the same mistakes where suddenly everything uses annotations). And Guice is a prime example where configuration is done using code instead of configuration files. Of course, with the limitations of the Java syntax the result will be less "beautiful" than the same in some other languages.

And finally I also don't agree that there is a huge movement to dynamically typed languages. Instead I would say there is a strong desire to find "The Next Big Thing", in this case "The Next Big Language".

I agree it probably has to do with the fact that Java is already with us for a long time and we have come to know its limitations, 15 years ago it was brilliant, today it's still nice but we know we can do much better.

And of course we know we can do better because there are several examples: C# is already somewhat better (but too similar, not revolutionary), dynamically typed languages like Groovy and Ruby have many advantages (but too messy and too much testing needed for my tastes), or functional languages like Haskell and Erlang that might become essential in the future where having large numbers of processors becomes the norm (but highly functional and therefore unknown to many programmers) and some that try to combine to best features of all like Scala.

Personally I don't really like dynamically typed languages for anything large. It's really cool to be able to work your way into the innards of any object in any way you like but real soon I feel the need to "formalize" in some way what I'm doing, some way of saying "I know what I'm doing, trust me" or even "this is the way to do it, don't try it some other way!".

At this moment I'm trying to learn Haskell and Scala because my "functional side" is really underdeveloped. Scala because it's not so much of a big step coming from Java and Haskell because I want some experience with a language that was made from a fully functional point of view from the beginning. The problem of course is that neither has yet attracted a huge following so I'm not really sure I'll ever be able to use any of them for work. But in the mean time at least I can enjoy learning something new and interesting.

In the end I think "The Next Big Language" doesn't exist yet, but it seems we're all, consciously or unconsciously, waiting for it.

Friday, June 11, 2010

Nexus One: after a few days

So far my impressions of this device are very positive. It's fast, it's very flexible, has loads of options (being a KDE user on Linux probably means I'm one of those people that just loves options) and looks nice to boot.

Like many people have commented all over the world, I think Google should try investing in better graphic and interaction designers for its operating system, Android, because in a first-impressions comparison the iPhone wins hands down in my opinion. The Android interface just seems more cluttered and chaotic and somehow less appealing and intuitive.

On the other hand finally having real multi-tasking is a blessing. Not being constrained anymore by Apple's weird decisions about what you can and cannot do with your phone is even better and the integration with all kinds of Google services by default is just the best part about it all.

But the Nexus One and Android do have some glaring faults. The touch buttons just below the screen are horrible: every time you write on the virtual keyboard and try to hit keys on the bottom row you run the risk of accidentaly hitting the Back or Home keys. That just plain sucks. I've tried an HTC Desire, which is basically the same phone, that has real buttons and it doesn't have that same problem because you need to press them with more force than you normally use for the touch screen.

Another big problem for me as a multi-lingual Dutchman living in Spain is that I very regularly switch languages while writing messages. I might be writing with several people at a time in three different languages and need to be able to switch with a single key-press. Not only is that not possible in Android, it doesn't even let you select a different language than the one you use for the operating system! So either you switch your entire phone from Spanish to English (including all Locale settings like dates, money and whatever) or you disable the keyboard corrections (which doesn't help too much, because I want the OS in English but I need the special keys for the Spanish language!).

But in the end, this being an Android phone from Google (which is definitely not Apple) , I'm sure that the problem can be solved looking around on the Market. Because where Apple says: "you can't do this", Google says: "go ahead, have fun".

So although the new iPhone 4 seems like a nice piece of hardware I'm glad to have "escaped" Apple's "clutches" (yeah, overly melodramatic, but hey, the post is already boring enough).

Thursday, June 10, 2010

Nexus One

Today I got my Nexus One and I decided to do one of those silly unwrapping things, just for shits and giggles.

So this is the box it comes in:


And here is what is inside:





Wednesday, April 7, 2010

/proc/self/fd/0

cat somefile | cp /proc/self/fd/0 someotherfile

I never knew this was possible!

Now you might ask yourself what this is good for but just the other day I was wondering how I could turn a copy of a drive partition made with dd into a sparse file. For example this copies the entire first partition of your first disk to a file:

dd if=/dev/sda1 of=partition.backup

Of course, depending on the size of the partition this file can get quite large. But knowing that most of the time a disk contains a large number of empty blocks and at the same time Linux supports sparse files wouldn't it be great if the file would only contain the blocks that actually have any data in them?

Now, the dd command doesn't support sparse files, but cp does have a --sparse=always option. So you could just use that to make a sparse copy of the file. But then you would have two copies of a potentially very large file on your system, which you might not have room for.

So I had been wondering if there was a way to pipe the output of dd to the input of cp. For dd this is simple because by default it writes it's output to the standard output if you don't give it an output file name, but cp does no tsupport reading from the standard input... until I found the above option on the ntfsclone man page. Cool stuff!

PS: Quite often free blocks on a drive aren't actually empty, but might still be filled with old data that used to be stored there. In those cases it doesn't help trying to make a sparse copy. To remedy that situation you can use zerofree before making the copy of the partition.

Tuesday, March 9, 2010

Curried and Confused

In the article Curried and Confused on coderspiel the author gives us this example of Scala code that people supposedly think is too difficult for regular folks:

def sum(nums: List[Int]) = (0 /: nums) { _ + _ }

He then goes on to say that it might be because of the /: symbol, but that this version does not make it any better:

def sum(nums: List[Int]) = (nums foldLeft 0) { _ + _ }

And this is the point where we differ completely. For me if the first version hadn't been called "sum" I would have had no idea what the code does. The moment I read the second version and I saw "foldLeft" I knew this was about an application of some function to a list of numbers resulting in a single result (hence the folding).

For me this isn't about reading (nums foldLeft 0) and not knowing it's the same as nums.foldLeft(0), you learn that once when reading about Scala's syntax and it will stick. Maybe you need to think about it a bit more the first couple of times, but it's not hard, the basics are still the same, the order is still the same, you can still think: "I've got this object nums on which I'm going to perform a foldLeft and... what's this 0 here? Oh must be an argument".

It also isn't about the whole closure bit where you suddenly see braces at the end of a statement, because getting used to Functional Programming you know that this is normally part of this brand new toolbox you've got. The _ + _ part does pose a bit of a problem for Scala first-timers, I agree.

But in the end for me the real problem is this /: symbol. What does it mean? If I see it, it doesn't ring any bells! There is no mathematical symbol that looks like this, in fact it looks like the symbol we've been using for ages now to denote division!

This is about readability, I always agreed with the Java-tenet where they would say that you tend to read code many many more times than that you write it. And it might be read by many people who don't have a way to look into your mind so it's important to write code in a way that it's function is as obvious as possible.

/: is NOT obvious.

But reading foldLeft is, once you've read at least once about folding and what it is used for. Because it doesn't matter in the end which language you use to implement the folding, it's a concept that's easy to grasp, but what happens if you have to go and learn by heart all the strange symbols that one can come up with? Especially when each language decides to use different symbols to express the same thing.

I now understand why they left out operator overloading in Java.
(yes, I know that in Scala this is not operator overloading strictly speaking, take your nitpicking somewhere else)

Monday, March 8, 2010

Regular expressions and backreferences

While trying to find a way to match quoted strings in a text taking into account that you can use both double and single quotes I first started looking at using two separate sub-expressions OR-ed together, but then I started wondering if it was possible to say something like "match a sequence that's exactly the same as that other sequence that you matched earlier". After some looking around I found the answer in backreferences. The following will match a single or double quoted string for example:

(["']).*?\1

Sunday, March 7, 2010

Heavy Rain

I started playing Heavy Rain today and I must say that I really like what I've seen so far! The quality of the graphics and the 3D models isn't too great, it's easy to see the typical "broken" lines where curves have been divided up into separate polygons for example. People's faces tend to have this glassy-eyed look and depending on the person can have this "plastic" look to them. Movement around the world can sometimes be a bit of a chore for somebody like me who's not used to a console's controller but to mouse and keyboard.


Now those were the main bad points. The good points are: it's just a fun, engrossing game with a great atmosphere. I'm not even sure if you could even call it a game as such, although it definitely has game aspects, like when you are an FBI agent looking for clues with your futuristic scanner glasses. Then there are the "action" sequences which are like those horrible console, press the right button at the right time, sequences... except that this time they aren't horrible. There doesn't seem to be a way to do anything really wrong, the game doesn't punish you for reacting 5ms too late, the sequence just takes a different turn.


As you can see in the video above you just walk around until some symbol appears indicating there's something to do there. There are lot's of little things to discover, most of them are just there for fun, to add some depth to the entire thing, but luckily most of those disappear after the first time you use/perform them so you don't get swamped all the time with symbols floating in the air. It also keeps you focused on the story, which turns this more into an interactive movie than just a graphically advanced adventure game. Which might turn some people off because (so far) it isn't very difficult, I don't think you will ever feel much of an achievement having encountered the solution to a particularly devious puzzle for example. But I still think it's worth it because the story is just so compelling. For a movie it might be your average psychological thriller about trying to catch a serial killer, but for a game it's great stuff that hasn't been done enough before.

Wednesday, February 17, 2010

Scala "puzzler"

November 2008 I encountered Scala and started looking into it. Thinking that some of my friends, who are also in the IT business, would be interested in hearing about my experiences I started writing them a series of mails with some of the things I learned. Mu idea is to publish them on this blog, translating them to English at the same time so it can reach a broader public.


Yesterday I bought the book "Programming in Scala" and I'm reading it at my leisure. And only a moment ago I encountered a warning that made me think "argh, why did they do that?".

It turns out there are 2 ways to define a function/method in Scala, one goes like this:

def multiply(x : Int, y : Int) = { x * y }

and the other one like this:

def multiply(x : Int, y : Int) { x * y }

the first one has 2 integer arguments and because of Scala's "type inference" it will also return an integer. This is something you don't have to specify yourself (you could do so and it would look like this def multiply(x : Int, y : Int) : Int but that's not important right now). The 2nd form also has 2 integer arguments, but because we left out the =-sign the result of this method is always "Unit" (like "void" in Java). In other words, the result of the expression will be thrown away quietly.

I think this is one of those things that can really bug you for hours without being able to figure out what is going wrong exactly. It seems like an unnecessary "shortcut", I would have preferred to explicitly define the result as being of type Unit in the case I really wanted to throw away the result.

Monday, February 15, 2010

Notion Ink´s Adam tablet

I've just watched this video and I must say: color me impressed! When you read the comparison with Apple's iPad you'll see that Apple will have a difficult time convincing people that their product is really worth the price they're asking for it. I mean, this Adam thing actually has all that you would expect in a product like this: regular USB ports, HDMI output and even a swivel around camera. Overall a really nice job, let's see if they can pull it off. They're supposed to come out in june so Apple willl have a 2 month head start.

Linux sound problems

Original post date: Fri Sep 07 12:00:00 2007
Ok, just learned that having two sound cards in your system can lead to all kinds of trouble.
Some time ago I had bought some headphones from Altec Lansing that came with an USB adapter. Not expecting it to work in Linux I plugged it in, but lo and behold it actually worked and I had now two sound cards in my system according to Fedora's Soundcard Detection utility. So far so good.
Soon after I removed the USB module and just connected the headphones directly like I had always done and life was perfect.
Until quite some time later I had to reboot my machine and suddenly I was getting all these weird ALSA errors from all kinds of programs (see for example the image of the pop-up that appeared at KDE startup).


The strange thing was that the Soundcard Detection program reported everything okay, the right sound card was detected, I could hear the sample and on top of that several programs did actually produce sound. So what was going on here?
Finally today I got a hint trying to run alsamixer when it gave me the following error:
alsamixer: function snd_ctl_open failed for default: No such device
Now this was something I could google for. So I ended up with several leads of which two led to the final solution:
  • ALSA user list which didn't really have a direct solution for my problem, but it made me look at the modules where I foundsnd-usb-audio. So what was that doing there when I had removed that USB adapter ages ago? Of course/etc/modprobe.conf was the "culprit" here. Somehow plugging in the USB adapter had inserted some lines into the file but removing the adapter hadn't deleted them!
  • LinuxQuestions.org pointed me to some files I had never heard off: /etc/asound.state and /etc/asound.conf. The first didn't seem too interesting, but the second had things like card numbers which made me think that maybe they had gotten mixed up somehow because for a short time I had two sound cards in my system. So I removed both and restarted the Soundcard Detection program. And like I had imagined the numbers were different in the new asound.conf!
So everything seems to work again. Now to figure out why the microphone only picks up sounds very faintly.

Linus and Windows

Original post date: Mon May 07 12:00:00 2007
Well, at least we know now how Linus feels about Windows:

I'm not claiming that the current code cannot work. It clearly *does* work for a lot of people. But I'm claiming that it's STUPID. So don't argue that "it works". Windows works, kind of. That doesn't make it less stupid and badly designed!

Poetic Linus

Original post date: Thu Nov 30 12:00:00 2006
Linus must have been in one of his poetic moods again ;-)


It's one of those rare 'perfect' kernels. So if it doesn't happen to compile with your config (or it does compile, but then does unspeakable acts of perversion with your pet dachshund), you can rest easy knowing that it's all your own d*mn fault, and you should just fix your evil ways.

Sunday, February 14, 2010

Slow HD due to CD polling

Original post date: Sat Nov 04 12:00:00 2006
Yesterday I upgraded our Dell Latitude to FC6, it was running FC4 and it was getting difficult to encounter some of the packages that I use on the desktop system (like Twinkle for example).
Well I can tell you that the installation went a whole lot better compared to the problems I had with the desktop machine!
There was just one thing that bothered me. Where the desktop actually seemed faster (as it was supposed to according to this, see the Performance section) the latop felt sluggish somehow. Applications were taking too long to load. When I tried measuring the drive's performance I got this:

which is way too slow for that drive. Then I remembered that I had the same problem before with this laptop and it was because of the polling KDED did of the CD rom drive (to detect media changes and such). Stopping KDED (which you can do in the KDE Control Center -> KDE Components -> Service Manager) would solve the problem.But with FC6 this didn't seem to help. I found an option called Enable CD polling in the Peripherals -> Storage Media of the Control Center, but the option was grayed out so I couldn't change it.
I finally found out today that it is HAL which does the polling nowadays, specifically the hald-addon-storage module. Killing it restored the expected performance:

So now it was time to figure out if something could be done about it. According to to this forum message it is a known problem with Dell laptops but it seems that the blacklisting it talks about doesn't work (anymore) in FC6.In the end I found out that HAL uses a set of rules to determine what to do with certain hardware which are stored in/usr/share/hal/fdi. By doing a lshal I could get a list of all the detected devices and find the information about the particular CD drive used in the laptop:

Using the information above I created my own fdi file information/30private/10-crappy-dell-cddrive.fdi with the following rules:



service haldaemon restart) everything was back to normal. Of course the system won't automatically detect any CDs being inserted into the drive, but I can live with that. (And yes, of course I filed a bug report)

Switching between Java versions

Original post date: Fri Jun 01 12:00:00 2007
It was always possible of course to install several Java versions on the same system, but switching from one to another has always been a bit of a nuisance. In Windows I started using batch files that I wrote specifically for the purpose of setting PATH and JAVA_HOME variables while for Linux Fedora you can use the alternatives command.
But I just found out that newer Java versions actually support switching versions directly from the command line:

 java -version:1.5+ ...
  
will require that the code is run with a VM of at least version 1.5 while:

 java -version:1.6* ...
  
will use any version 1.6 VM (so it won't accept 1.5 nor 1.7, but it will accept 1.6u1). Requiring a specific version of the VM is supported but is not recommended because it would tie you to that very VM not even allowing the use of VMs that have important bug or security fixes.Specifying a VM that can not be found on the system will result in an error of course:

 > java -version:1.4* -version
 Unable to locate JRE meeting specification "1.4*"

OpenJDK

Original post date: Tue May 08 12:00:00 2007
Yes, it's finally here! Welcome to the new Java era. Now let's see where this roller coaster will take us. Don't close your eyes! OpenJDK


Java Games - Bang Howdy

Original post date: Sat Jan 13 12:00:00 2007
This has already been a Java Application of the Day but I thought Bang Howdy deserves to be mentioned more. This is one of those examples that it is very much possible to write nice games using Java!



F3 and Flying Saucers

Taking a look at the latest information about that highly intriguing Java scripting language called F3 I found a reference to yet another interesting Java project I had not heard about before Flying Saucer. A strange name for a XHTML+CSS renderer but I'm not complaining, components like this are always very welcome!

Regular simplification

Original post date: Sun Nov 19 12:00:00 2006
Hey, I should do this more often! While writing the previous item I thought it would be nice to format the regular expression a bit by indenting it and adding some comment, this was the result:

# gobble any spaces
(\s*)
# main group
(?:
 # keyword - gather letters until first space or =
 ([^=\s]*)
 # option group
 (?:
  # option 1 - attribute has no quotes
  (?:
   # gobble spaces and =
   (\s*?=\s*?)
   # value - gather anything that's not a space or quote
   ([^\s"]+)
  )
  |
  # option 2 - attribute has quotes
  (?:
   # gobble spaces and =
   (\s*?=\s*?)
   # value - gather anything between two quotes
   (".*?")
  )
 )?
)
  
And while setting this up I saw that this was actually more complicated than need be so I started pruning. When I put back the resulting expression everything still worked so I must have done something right. The indented version looks like this:

# gobble any spaces
(\s*)
# keyword - gather letters until first space or =
([^=\s]*)
# optional group containing the value
(?:
 # gobble spaces and =
 (\s*?=\s*?)
 # option group
 (?:
  # value - (option 1) gather anything that's not a space or quote
  ([^\s"]+)
 |
  # value - (option 2) gather anything between two quotes
  (".*?")
 )
)?
  
And the resulting expression looks like this:

(\s*)([^=\s]*)(?:(?:(\s*?=\s*?)([^\s"]+))|(?:(\s*?=\s*?)(".*?")))?
  
A bit shorter than the first version but still way too cryptic in my book.

Regular confusion

Original post date: Sun Nov 19 12:00:00 2006
I have been working the last couple of days on adding syntax highlighting to my blog. For this I used the jEdit syntax package that I found on SourceForge. I had to change it a bit because it was a bit too focused on using it from within an editor but all in all it is not bad.
Only the XML highlighting didn't really convince me, it would just color everything blue except for the comments. So I thought I'd take a stab at parsing the attributes so keys and values would get their own colors.
But looking at the code I realized it would take some work doing it the way they did it so I decided to just at a regular expression at right point.
Thing is, regular expressions are great but when they finally result in something like this, I have to wonder "WTF am I doing???":

(\s*)(?:([^=\s]*)(?:(?:(\s*?=\s*?)([^\s"]+))|(?:(\s*?=\s*?)(".*?")))?)
  
And this is without the escaping that is necessary when putting this in a Java string! Luckily it's only a couple of slashes but it can soon get very messy.Oh, and what it does is figure out the attributes for an HTML or XML element. So if you have something like this:

<input type= checkbox name = "checkme" selected>

the regular expression will cut it up into the following tokens:
  • " "
  • "type"
  • "= "
  • "checkbox"
  • " "
  • "name"
  • " = "
  • "\"checkme\""
  • " "
  • "selected"
Of course not all of this is legal in XML, but the expression supports the most lax of the 2 formats.

Java GPL

Original post date: Mon Nov 13 12:00:00 2006
Wow, for me this definitely is an historic day, Sun will release the entire Java platform as open source! And they're not just using any license, they are actually releasing it under the GPL!
But even more incredible in my opinion is Jonathan Schwartz, director of Sun Microsystems, admitting that the final push for selecting the GPL was the Microsoft-Novell anouncement last week about protecting eachother's customers from patent violations:
By admitting that one of the strongest motivations to select the GPL was the announcement made last week by Novell and Microsoft, suggesting that free and open source software wasn't safe unless a royalty was being paid. As an executive from one of those companies said, "free has to have a price."That's nonsense.
Of course, Richard Stallman will have to revise his infamous essay "Free But Shackled - The Java Trap", but he doesn't seem tomind much, in fact he seems pretty pleased, commending Sun for taking a leadership role and saying that this makes Sun the world's biggest single contributor of open source software.
Personally I would like to see how the Linux distros will pick this up. RedHat for example has always been a big supporter of Java, spending a lot of time and effort to make sure that projects like GNU Classpath and GCJ work perfectly on their systems.
How will it all work out? Is there any use for the Classpath people to continue now that their goal of having a free implementation of the Java class libraries has been attained in one fell swoop?
And how will GCJ be integrated into all of this (because that's one project that is not going away for sure because native code compilation will always be more interesting to some people than relying on JIT compilation and the Hotspot optimizer)?
This is all so exciting! I can't wait for the future to arrive!

Syntax high-lighting test