DSLs: Introducing Slang

28. July, 2008

Did you ever ask for a more compact way to express something in your favorite programming language? Say hello to DSL (Domain Specific Language). A DSL is a slang, a compact way to say what you want. When two astronauts talk, they use slang. They need to get information across and presto. “Over” instead of “I’ll now clear the frequency so you can start talking.” And when these guys do it, there’s no reason for us not to.

Here is an artical on Java World which gives some nice examples how to create a slang in Java and in Groovy. Pizza-lovers of the world, eat your heart out.


The Code Reuse Myth

22. July, 2008

The post “The Code Reuse Myth” by James Sugrue got me thinking.

The main problem with code reuse is that our programming languages don’t support it. We sacrificed this to the gods of efficiency four decades ago and, while a few people dared to question the practice, all of them were struck down by unexpected lightning out of the blue, so far.

As James said, “context” is the keyword. What we’d need is a programming language where you can adapt concepts to a context that goes beyond “object instance” or “class” or “application”. What we need is an efficient way to say “collect all objects and sort them by ID” where the context defines what “ID” is. What we need is an efficient way to describe a model (relations of basic data types) and then have some tool map that efficiently to reality so we can reuse parts or fragments of the model in different contexts.

OO can’t do that because it’s limited to factories and inheritance. Traditional preprocessors can’t do it, because they can’t see the AST. Having closures in a programming language is the first, tiny step in the direction to be able to push external context into existing code. This allows us to put the code to access the database into a library and influence what it does per row of the result with a closure.

But to be a real new paradigm, we need “closures” in data types as well. This means being able to reuse fragments of code and data structure definitions in a new context. These fragments need to pull along all the algorithms and structures they need without the developer having to pay close attention what is going on until the point where the result needs peep hole optimization because of performance issues.

Example:

fragment Named {
    String name

    String toString () {
        return "name=${name}"
    }
}

class File : Named {...}

class Directory : Named, File {...}

Looks simple but with OO, this will get you in a lot of trouble: Directory gets a name from the class File and from the fragment Named (this is an artificial example, bear with me). Which one should the compiler chose? In OO, I can’t say “I don’t care” or “Merge them”.

With real fragments, you could say “Directory is a File in the sense that it supports a lot of the operations of a file (like rename, delete, get last modification time) but not all (you can’t execute a directory or open it for reading).” So the example would look like this:

class File : Named { 
    void delete () {...}
    Reader openForReading () {...}
}

class Directory : Named, File.delete {...}

Now, a Directory has a name and it “copies” the method “delete” from File along with anything this method would need. Internally, the compiler could create an invisible File delegate or it could clone the source or byte code of the File class, or whatever. Or, even better, we could say “give me a copy of File.delete() but replace all references to the field File.path with Directory.path.”

The main goal would be to allow to use the compiler as a cut’n’paste tool which checks the syntax and allows me to say “copy that method over there and replace xxx with yyy”. Because that’s why we think that code reuse could work: We see the same code over and over and over again and each time, the difference is just a tiny little bit of code which we can’t “patch” because the language just doesn’t allow it.


Are Bad Tests Worse Than No Tests At All?

9. July, 2008

In his article “Are Bad Tests Worse Than No Tests At All?“, olivstor writes:

Are the drawbacks to bad tests worse than having no coverage at all?

I think the answer is that in the short term, even bad tests are useful. Trying to squeeze a extra life out of them beyond that, however, pays diminishing returns.

Just like other software, your tests should be built for maintenance, but in a crunch, you can punch something in that works. It’s better to have bad tests than to have untested code.

Tests are like any other code: They can go bad.

In my career, I’ve found that it’s surprisingly hard to write good tests if you have no experience in doing so. People starting to write tests make them too complex, too long, let them have too many dependencies and they take too long to run.

If you’re in such a situation, you have to face the fact that you just programmed yourself in a corner and you must spent the effort to get out of there. Tests are no magic silver bullet. They are code and follow the usual rules of coding: When it hurts, something is broken and it won’t stop hurting unless you fix it.

So in this sense, I agree that bad tests are better than no tests because they tell you early that you need to fix something. That’s what their core purpose is.

Management might argue that you’re spending too much time on testing. I’ve never had a problem to sell myself to them. I usually figure that I spend 50% of my time (or more!) writing tests and 50% actual coding – and I’m still much faster than those who write code 80% of the time or more. What’s more: when my code goes into production, it’s is rock solid or at least easy to fix when something comes up. In 99% of the cases, the things I need to fix were those which I didn’t test.

This is a positive reinforcement loop which drives me to test more and more because it stops the hurting. If your tests cost more than they seem to return, you need to fix them until you get the same positive feeling when you think about them.


Starting Your Own OSS Project

8. July, 2008

If you’re planning to roll your own little OSS toy project, you should read the article “Party of one: Surviving the solo open source project” by Kirill Grouchnikov. Very good points on what to do and what to avoid and why.


Collaborative Editing

21. June, 2008

Mustafa K. Isik has a video on vimeo demonstrating collaborative editing of a Java class in Eclipse. I’m not sure where this leads and my experience winces at the idea but it would sure help if the superman of your team could attach to your editing session, see the code you’re working on and help you fix it (instead of having to get up and coming over).


How to Work Better

17. June, 2008

From the wall of a corporate building in Zurich:

  1. Do One Thing at a Time
  2. Know the Problem
  3. Learn to Listen
  4. Learn to Ask Questions
  5. Distinguish Sense from Nonsense
  6. Accept Change as Inevitable
  7. Admit Mistakes
  8. Say It Simple
  9. Be Calm
  10. Smile

Eye Candy For Developers

16. June, 2008

There are two new projects on the ‘net which might influence the way we see software in the future (in the literal sense). code_swarm can visualize the CVS history of a project in a cunning way and Wordle creates tightly packed tag clouds.

What I find especially interesting about Wordle is that it creates the tag cloud in real time (as you watch) using a Java applet. If you happen to have Java 6 installed, the animations are very smooth, almost like Flash.


The Craft of Text Editing

12. June, 2008

While looking for a decent code base for a text editor on Google Code (tip: search for “label:editor”), I found a wiki page in the epytor project which links to “The Craft of Text Editing“, by Craig A. Finseth. That document not only explains all the basic algorithms behind the scenes of a text editor but also compares different implementations and contains a wealth of useful general comments.


Rich Applications in 2008

11. June, 2008

For about five years, I’m trying to write a small OSS application to help me write SciFi stories. Today again, I ran into this familiar pain: There is just no good OSS framework to write applications. Here is what I tried:

PyQt 4.4. This is nice but you need to do a lot of manual plumbing to connect your Python model to the Qt UI. Why are there no support classes to display a Python list in a QListView? Or to map fields from objects in a list to a QTableView? You can’t even simply connect a field of an object to a basic text widget.

There are other Python frameworks (wxWidget, for example) but they are even worse (less features, more complex to use, no data binding as well).

So let’s look at Java. What do we have? Spring Rich Client is more or less dead (they say they are working on Spring Desktop but there isn’t even a web site, yet). Also, this project is locked to Swing. Not a killer feature in 2008.

So let’s look at Eclipse. We have SWT, Data Binding and … well … EMF. Some love EMF, some like it, others … you get the idea. Also, this solution again asks for a lot of plumbing (manually connecting objects with the UI). And while I gave Eclipse 3.4 RC4 a try (to see if EMF got usable in the last three years), it crashed in USER32.dll. *sigh*

Then, there is TurboGears. Automatic data binding, automatic generation of a nice UI, cool. Web only … *sigh* Maybe I should try it with Firefox and XUL.

Also, none of these frameworks offer a simple WYSIWYG editor which can be extended (for example with a typing help, tooltips, proportional font support).

After ten years of OSS application frameworks, you’d expect that there was at least a single choice which allows to build a toy application with ten simple objects within a few minutes.


Rewind for GDB

17. April, 2008

Every developer has used a debugger once in a while. And sometimes, you had this “stepped once too many” problem: You ran your code too far. Since there is no way to go back, your only option was to start all over again.

A guy called “teawater” has just published a patch for GDB which does just that: It allows to reverse the program execution. Kind of an undo for the CPU registers, stack and memory. Can’t wait to see this for Java!