Thomas’ Functional Interpreter

I just finished a small functional interpreter for an university course. To be precise, the interpreter itself is not functional, but it understands a functional, LISP-like language/syntax. The whole project is written in Java 7 and provides an interface to all Java classes on the class-path, too.

I also tried to create a small IDE in Swing. I built an editor with syntax-highlighting and code-completion (thanks to the RSyntaxTextArea project), an environment inspector and a small debugger to keep track of the stack. Take a look at it.

I called it ThoFu for Thomas’ Functional Interpreter. I think it teats good… You can get some brief impressions from the slideshow below.

The whole project is open, you can find it as an Eclipse project on my Bitbucket account. It contains the actual code, all libraries, screen-shots, unit tests and (of course) some documentation like UML diagrams. Here are the links to the GIT repository:

Bitbucket: https://bitbucket.org/wordless/thofu-interpreter
Git: https://bitbucket.org/wordless/thofu-interpreter.git

Here is the executable JAR-file (Note: You need Java 7). Just download it, double-click it and go-ahead:

Executable program: ThoFu Interpreter.jar
Read-Me: README.md

And this is how it goes. First a simple “hello world” program:

Now, a more sophisticated one. Here’s a quicksort-algorithm for my interpreter. It contains some user-defined functions, multi-line statements and some comments.

And this is how the Java API works. The code will create a JFrame with a JButton and a JLabel. When you press the button, the listener will change the text of the label from “click me” to “hello“. The syntax is inspired by the Java Dot Notation by Peter Norvig. I used the Apache Commons Lang library for the reflection-tricks to load class and so on.

You can just copy and paste the code snippets into the editor or save them as common text files and open them. They are also in the repository. The JUnit tests contain many more examples as well.

For those who are interested in the topic, you can find my resources (libraries, tool and literature) in the Read-Me.

Best regards,
Thomas Uhrig

Tools to understand Java code

I have recently read a question on Stackoverflow whether there are any tools to help understanding Java code. Somebody asked it, because he started to use a pen and a sheet of paper to get the clue of some code given to him. Not uncommon.

But the problem was, that nobody tried to answer the question, instead giving him tips like “learn Java” or “read tutorials”. I think they have misunderstood the question – because there are definitely tools for that:

  • A good IDE just like Eclipse is one basic tool to understand code. It helps you in various ways:

    • It shows the structure of folders and files of the project. You can easily get an overview of everything. Just import the project and have a look what you got.
    • It gives you the ability of syntax-highlighting (trivial, I know), but also the ability to format code like your are used to in a second. That helps reading it, even (or especially?) when you have some experience in the language.
    • It shows you the JavaDoc comments right away at every position in your code. So you will always have the documentation with you when needed.
    • It provides tools such as a debugger, a call-hierarchy view and many more. All this tools will help you to get an understanding of how the code works.

    And it can to much more for you. So it’s really worth to get familiar with your IDE.

  • UML is a widely used standard for modeling and visualizing code. The purpose of UML is simple and exactly what was asked: it should help you to understand code. The strength of UML is the abstraction. You can easily visualize a big architecture without mention any detail at all. On the other hand, you can add any detail you want if it is important for the understanding of the system.

    The basics of UML are not hard to learn, a quick look at Wikipedia should be enough to draw a class diagram. But UML is also enough standardized, that it could not only be read by humans, but also by machines – so you can generate it (so called round-tripping)!

    Tools like ObjectAid helps you to generate class diagrams as easy as possible. Just install it in your IDE, create a new class diagram and drag all Java classes in it.

  • Tests, especially unit tests (e.g. JUnit tests) could be also very useful to understand the code. A good test is something like a additional documentation or a contract on code-level. It exactly defines how a method can be used (e.g. input parameters) and what it should make (that’s what the test is testing ;)). So, if you don’t understand what a method should do and you have tests, take a look at them. And what if you don’t have any tests? Write your own ones! This is a good approach to explore an API in a structured and repeatable way. Whenever you are not sure what the API does, try to create a test about your assumption.

So, what to do if there is a bunch of new code?

  1. Import the project to your IDE.
  2. Get an overview of folders, files, packages and so on.
  3. Explore the code: What’s in the JavaDoc? What types are used? What about the call hierarchy?
  4. Run the unit tests and try to understand what the methods are doing.
  5. Try to run the debugger for a test or two.
  6. Draw or generate a UMl diagram.

I have uploaded some screenshot in the slideshow above for the open-source RSyntaxTextArea library from Fifesoft. But you will also find some good tutorials in the internet for every point mentioned before. Try to get familiar with at least some of the tools.

Best regards,
Thomas Uhrig

Gelesen: Learning UML

“UML ist heute eine der dominierenden Sprachen für die Modellierung von Softwaresystemen” – sagt zumindest Wikipedia. Ähnlicher Meinung dürfte wohl auch Sinan Si Alhir sein, der über den O’Reilly Verlag sein Buch “Learning UML” veröffentlicht hat.

Learning UML ist – wie könnte es anders sein bei einem Werk über Modellierung – eine ziemlich klare Sache. Es gibt drei große Blöcke: die Einleitung zu Beginn, weiterführende Information am Ende und dazwischen Diagramme. Klassen Diagramme, Use-Case Diagramme, Deployment Diagramme und alles was die UML sonst noch so hergibt. Erklärt anhand eines Beispieles (welches sich durch das gesamte Buch zieht) und abgerundet durch Übungen am Ende.

Leider fehlt bei Sinan Si Alhir der große Überblick. Die meisten Modelierungselemente werden anhand von Fließtexten eingeführt, denen mühsam die wirklich wichtigen Informationen abgerungen werden müssen. Eine Übersicht über die UML und deren Elemente (z.B. um sich Gemeinsamkeiten in der Notation besser einprägen zu können) fehlen.

Wer jedoch ein wenig Zeit und Geduld mitbringt, kann dieses Buch als sehr angenehm empfinden. Es ist kein Nachschlagewerk, dafür erklärt es. Man muss mit Learning UML nicht unbedingt am Schreibtisch sitzen und “lernen”, man kann es auch einfach nebenher lesen. Das Thema sickert durch, wenn auch nicht immer in allen Fassetten. Alles in allem, ein zu empfehlender Titel.

Beste Grüße
, Thomas Uhrig.