Tuesday, June 5, 2012

Syntax highlighting in less

I've been looking for this way to long not to make a note on my blog.

Syntax highlighting in less - Big thanks for the author of this blog post for making the contribution!

So what you do is:

a) you install the highlighter
apt-get install source-highlight
b) you enable less to use it by adding the following lines to your ~/.profile
export LESSOPEN="| /usr/share/source-highlight/src-hilite-lesspipe.sh %s"
export LESS=' -R '
And believe me: life is really good again :)

Tuesday, May 29, 2012

Turning to the dark side

It's been over 15 years since I've tried switching to Linux for the first time. I distinctly remember the first experience I've had that day when "to install" really meant "to compile the kernel". It was awful, bad and completely not approachable by mare mortals. That's how I met Linux.

The astounding thing about it though was that it kept nagging me over and over again. The sole idea of having an operating system for free and be able to customize it to your liking (although excruciatingly painful) stayed with me since that day. I was sold to the idea of using free software and some years later to give back something in return.

Some years later I've learned that there's nothing like a good Linux server and bought into the idea that as far as I am concerned Linux is The Operating System for servers but as far as Desktop is concerned it just doesn't measure up to the competition.

It's years later now, the new millennium, and I can finally enjoy a Linux distro prepared truly in a way that can compete with what I've already knew. It's Ubuntu 12.04 LTS - the first Linux I feel is worth using at all on a desktop.

So here's what I value the most and why I think you should just switch to Ubuntu 12.04 and never look back again:

  • Unity: User Interface that rocks! The first ever successful attempt to create a good looking and usable alternative to Windows
  • Memory consumption that doesn't eat all you have for breakfast
  • Hardware support that blows your mind
  • It's Linux inside all this meaning you can do whatever you please to it

My advise to you is: If you have not tasted it yet go ahead and try it out! It'll blow your mind how much space you can get in almost every aspect of your PC, starting with hardware requirements and ending with available screen space. It is truly astonishing!

Have a nice day!

Wednesday, May 16, 2012

Making Groovy scripts maintainable

Writing Groovy code is cool. Fixing them some time later can be hard as they tend to grow out off control pretty soon. Finding an install.gsh that has 100+ lines is not hard. And since Groovy is a very expressive language those 100 lines are not making the compiler happy - it's actually stuff to be done.

I have a bad feeling about this

In Java we're pretty much used to either creation of big, ugly, monolithic classes or going the completely opposite direction with Spring or some other dependency injection framework. In scripts we tend to rather go the first way which makes them unmaintainable and hard to understand. Why? Because historically shell scripts were created this way and we tend to think in this way. It's a script, isn't it?

These are not the droids you're looking for...

In fact Groovy scripts are not scripts at all. They are classes, that will have a main method. The only difference is that they are automatically compiled when needed. This means we can use all the good Java stuff when writing Groovy scripts: classes, external libraries through @Grab annotation and so on.

Here's an example:

Configuration.groovy:
class Configuration {
    String path
}
install.gsh:
#!/usr/bin/groovy

println new Configuration(path: "/tmp")

Since all the classes are in the default package (inaccessible from pure Java!) you don't need to import them manually. Should it make sense to split them into folders you can always do that:

org/example/Configuration.groovy:
package org.example

class Configuration {
    String path
}
install.gsh:
#!/usr/bin/groovy

import org.example.Configuration

println new Configuration(path: "/tmp")

Further more you can write tests for your classes, make sure they always work even if some mad cow enters the arena and goes Hulk on your code :)

May all your scripts be maintainable forever and for always!

Why things are so damn complicated these days?

I've been going over some older projects these last few months. Partially because this is part of my job as an architect (to know what's in the box and to propose better solutions if the existing ones are reaching to the limit of their usefulness) but partially because I've gotten really passionate about The Legacy. And I don't mean it like "oh, this is the legacy code we hate" and all that but rather to see and understand what causes today's problems and how we can make the world a better place. Well.. at least for us, programmers...

The endless possibilities

If I gave 10 people a general purpose task, let's say describing a simple domain I fear that I'd get back 20 different designs. I get that kind of impression a lot recently when faced with the same problems solved in many different ways by the same person. It is just a matter of time before that happens. Quite frankly that is a good thing: it means that person is trying something new and most probably learning from mistakes they previously made. But what about the code we have already committed? Is it really there to pollute the air for ever and ever? Is this what we call "Legacy Code" today?

We're screwed! What should we do?

Tuck the tail and run like hell! That always solves the problem, doesn't it? You made a mess first trying to do the right thing, then because you were under constant time pressure and later on because things were already so damn bad you didn't really care about this piece of crap. The easiest way out is through the front door. Usually the justification is "because I can't really develop myself any further" or "I'd like to learn more about this and that but the current place I'm in sucks so bad my boss will not allow me to". All those crappy statements are in fact about one and the same: "I've made a mess and as sure as hell I ain't gonna clean this stuff up!". If you think about it, it kind of makes sense, doesn't it? Why should you even remotely care what will the new guy think about your capabilities? You ain't gonna be there to witness it anyway, right?

Are we there yet?

I predict that some time very soon a lot of big systems is going to undergo a series of major setbacks. Anybody wanna take that bet? Would you rather sink with the ship or go down swinging?

I know it is not going to be very popular what I'll say right now but I'll say it anyway: For God's sake! Write clean code! It's not so damn hard, really! If you ain't got the skills then stop complaining about your boss that he/her is not giving you the development opportunities you'd like. It's your job to do it right and it is your job to develop yourself. Nobody is going to do that for you, not in a million years! And if you'll ever find yourself sitting in an open space bitching about the quality of code then don't forget that you have hands, eyes and a biological CPU to clean that up if you think it is a mess. Don't run from it - fix it! If you don't know if it will work after you do fix it - then fix it anyways! Clean code lives shorter, is easier to replace than that old, shitty stuff that stinks like hell every time you touch it!

So why are those things complicated again?

It's easy: we made them that way. We make them that way every single day (despite the heroic efforts of Uncle Bob and others). Period. They didn't create themselves - we're not quite there yet with the AI to procreate on its own. At least it is not a common scenario in the web database front-ends :)


May the force be with you!

Monday, April 30, 2012

Call to new graduates

This is the kind of posts that will be very specific to the country I live in, Poland. It should come as no surprise that the content will be in polish.

Profesor do młodych wykształconych bezrobotnych

I don't dare to add another word to what this letter says. It's all true. Period.

Thursday, April 5, 2012

Grails, Hazelcast, Hibernate and 2nd level cache

If you'd like to use Grails with Hazelcast caching solution you need to be aware that there is an issue with Hazelcast compatibility with the version of Hibernate used by Grails (3.6.10.Final).

Reading on the Hazelcast Wiki you'll be tempted to use the "new" ways and specify hibernate.cache.region.factory_class. Unfortunately that doesn't work. See this bug report for more information.

But there is hope! Instead you can specify hibernate.cache.provider_class and everything should work as expected. Hibernate does all the adapter heavy lifting and life is good again :)

Anyways, Hazelcast is plain ecstatic - you should really give it a try and get a feel of what a good library feels like when you use it. It's awesome!!!

Back to the roots

I've been going over some of my old stuff that I wrote back in the day in 1998 when I started to work professionally as a developer. Among other things (like using polish names for variables, classes and what have you and seeing absolutely no automated tests whatsoever) I've also spotted some nice stuff that I wanted to share with the class.

DDD was normal

Back in the day having a TTeacher (Delphi) class that knew about the schedule and could tell you which one is going on at a particular point in time was no big deal. Classes were not PODO, they actually knew how to do stuff. Just to give you another example here's something that struck me fairly recently:

Given a Company company and a Candidate candidate if the company decides to hire candidate then the output is an Employee employee. This could be written down more/less like this:
Employee employee = company.hire(candidate)
Now take a look at this and tell me how would you call this now a days? IT IS DDD at its purest! We have domain classes that can be retrieved from some source and operated upon.

Short methods were normal

Actually this isn't entirely true but it was pretty much the standard that a method contained calls to other methods that contained calls to other methods. I kind of felt the necessity too express things clearly or else the code would then be unreadable. So the whole "one-screen method" or "CCC < 5" was kind of natural even though nobody told me to do so. It just felt right.

Coding standard

This has surprised me most: All (and I mean it when I say "all") of the code has one and the same coding convention. There's no deviation whatsoever when there is no special case that needed separate consideration of formatting rules. And above all: it was based on VCL's code. Maybe not 100% equal but very, very close. Now that is some code you read like an open book just a after minutes of getting used to the coding style.

I, J and K were iteration variables - and nothing else!

Back then there was a number of variable names that were reserved for a certain task. "I, J, K" were always and without exception the iteration variables. There was no itinerary "i" or class "K". Similarly "S" was always a temporary string variable. The code was so damn short even though there were no closures to express things better!

What has changed?

Well, what has really changed after the year 2000? There were 3 major factors that influenced me to write worse code:

  • I stopped writing code for myself
  • The schedule became a lot tighter
  • The complexity sky-rocketed when I started writing calculation-intensive apps

With that in the picture when I look at the code I wrote after 2001 I feel like the IQ of the author just dropped 30-40 points!

The Corporate times

One very much disastrous time for me was the time when I started working for an international corporation. And I mean it in every single way one can possibly imagine. That was the place where I learned that even though the application is mission critical (people's life depends on it!!!) it is not really important that the quality of the code is kept at a reasonable level. It was really enough that the application worked around 60%-70% of the time! AAAAARGH!!! I remember fighting the original team members to the death trying to teach'em about quality of the code. That was the time I worked for the first time with "legacy code".

Java - the ultimate readability killer

As much as I hate Java the language for its verbosity and lack of advanced features like properties I'm inclined to say that the worse thing about Java are Hibernate and POJO-style programming model. Why?

Let's summarize what a POJO is, shall we? A POJO (plain old java object) is just a class. This simple and quite good idea turned into a disaster with Hibernate where most of the objects started to be those gigantic pile of annotation crap but besides that didn't contain anything else. That's how I came to learn POJOs as a programming style: dump, idiotic and trivial classes. So it is not a POJO anymore - you might say. And you'd be right! That'd be a dumb Java Object (DJO) but unfortunately when you see code that has to do with EntityManager, pure Hibernate or some other ORM (which is very likely) this is what you are going to experience..

Back to the roots

Now that I am old enough to look back and see the mistakes I made I've decided to find a way to get back to the programming style where a "Company" has the ability to "hire" a "Candidate" which would spit out an "Employee" but at the same time to keep things as separated as they should be for the sole reason of testability.

Spring sucks - Grails to the rescue

It should come as no surprise that my first attempt to mimic a rich domain object was undertaken in Spring. I admit I'm no spring expert and I still believe it is a vastly bloated library but I do get the idea behind it (unlike myself 3 years ago). But stuff is very much complicated with pure Spring. And one thing in particular - dependency injection in domain classes.

When I say that Spring sucks it really means that "Spring sucks by comparison to Grails". It is really great that spring does all the wrapping and all that but making domain classes automatically injectable borders on the infinity...

The whole point to this exercise is to have an object model that is usable without GUI and still makes a whole lot of sense.

Candidate.groovy:
class Candidate {
    String firstName
    String lastName
}
Company.groovy:
class Company {
    String name

    def hiringService

    Employee hire(Candidate candidate) {
        return hiringService.hire(candidate)
    }
}
Employee.groovy:
class Employee {
    String firstName
    String lastName
}
Now how about this:
Employee employee = company.hire(candidate)
Isn't that nice? And it is so damn readable!

The best part of all this is that in Grails domain classes are by default eligible for dependency injection which means that the hiringService is injected automatically. And I really mean AUTOMATICALLY! This means that when you do "new Company(name: "Test")" the "hiringService is automatically injected!!! This is the best thing ever!!!

I strongly recommend you try to look at your application from within the code and see if you can read it as you'd read plain text. If not there's something wrong with either your eyes or the code before you. If I was a betting man I'd go with the latter one :)

Write clean code!