Inside BossConstructor: “MJava”

Over the course of the last years I have tried many different programming languages and honestly, none of them completely satisfied me as a programmer. While each language has cool and interesting features, they usually also have some annoying downsides. I could write about this topic for a long time, but at this point, allow me to simply sum it up as “For me, Java turned out to be the best bet.”.

Pros and Cons of Java
From my perspective, programming BossConstructor in Java offers several advantages:
– Very fast execution time (usually comparable with C++).
– Complete cross-platform compatibility (Linux, Win, Mac) without much additional though.
– Availability of many gaming-related libraries (OpenGL, OpenAL etc.).
– Possibility of creating browser-based Java applets.

What I dislike about Java is that it forces the programmer to write a lot of redundant code which does not contribute to the functionality of the actual program in any way. Consider this basic example:


The first line repeats the class name, which is already given by the file name. In order to structure the code, both indentation and brackets are used. Finally, semicolons are required even if when most commands are single line.

Introducing “MJava”
In order to solve my “aesthetic problems with Java” while still being able to profit from Java’s features, I decided to write my own preprocessor. This preprocessor takes a .mjava-file and transforms it to a .java-file which can then be compiled by javac as usual. The whole process can be executed automatically using, for example, a makefile.


So, how does a .mjava-file look like? Consider this basic example which is functionally equivalent to the above Java-code:


In addition to removing brackets and making the code more readable (in my opinion), “MJava” allows for several shortcuts to eliminate annoying and tedious code patterns:


And these:

Finally, the huge import blocks at the beginning of each file are no longer required. You can simply define one file which contains all the imports your project requires and everything will be included in all of your files automatically.

For me, creating and using “MJava” has proven to be very advantageous. The resulting code looks much cleaner and writing new classes and methods feels more fluent and intuitive. 
There are, however, still several bugs and lacking features. Block comments do not work (yet) and the MJava-preprocessor is very strict about indentation. Another problem is that syntax errors in a program lead to javac issuing compiler errors including line numbers which do not correspond to the actual .mjava-file, which can make identifying errors more difficult.

If you want to give it a try, please feel free to download and compile the preprocessor here and let me know what you think. The archive includes a simple hello world-program and a basic makefile:

2 Comments Add yours

  1. Metalfusion says:

    Just out of curiosity: Is this preprocessor of yours compatible with IDE’s such as Eclipse? I couldn’t think of writing java or any other code without a proper IDE anymore. I guess once you get used to the many conveniences those can provide there really isn’t any going back to a simpler editor 🙂

    1. Well, in order to use it with Eclipse you’d have to spend some time to program syntax parsing/highlighting, linting and all the other required stuff which is probably not worth the effort. If you are interested I can send you syntax highlighting files for Sublime Text 2/3 though. Personally I have switched from Eclipse to the more basic Sublime Text some time ago because I feel like it is much faster and it allows me to focus more on the code. It is also useful if your project(s) involve many different programming languages (for me Java, MJava, R, Stata, knitr, sweave, Latex, html, css and more). And because personal preferences 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.