Archive for February, 2012

Turning automatic code generation upside down

Tuesday, February 14th, 2012 Miro Samek

Much ink has been spilled on the Next Big Thing in software development. One of these things has always been “automatic code generation” from high-level models (e.g., from state machines).

But even though many tools on the market today support code generation, their widespread acceptance has grown rather slowly. Of course, many factors contribute to this, but one of the main reasons is that the generated code has simply too many shortcomings, which too often require manual “massaging” of the generated code. But this breaks the connection with the original model. The tool industry’s answer has been “round-trip engineering”, which is the idea of feeding the changes in the code back to the model.

Unfortunately, “round-trip engineering” simply does not work well enough in practice. This should not be so surprising, considering that no other code generation in software history has ever worked that way. You don’t edit by hand the binary machine code generated by an assembler. You don’t edit by hand the assembly code generated by the high-level language compiler. This would be ridiculous. So, why modeling tools assume that the generated code will be edited manually?

Well, the modeling tools have to assume this, because the generated code is hard to use “as-is” without modifications.

First, the generated code might be simply incomplete, such as skeleton code with “TODO” comments generated from class diagrams. I’m not a fan of this, because I think that in the long run such code generation is outright counterproductive.

Second, most code generating tools impose a specific physical design (by physical design I mean partitioning of the code into directories, and files, such as header files and implementation files). For example, for generation of C/C++ code (which dominate real-time embedded programming), the beaten path is to generate <class>.h and <class>.cpp files for every class. But what if I want to put class declaration in a file scope of a .cpp file and not to generate the <class>.h file at all? Actually, I often want to do this to achieve even better encapsulation. A typical tool would not allow me to do this.

And finally, all too often the automatically generated code is hard to integrate with other code, not created by the tool. For example, a class definition might rely on many included header files. But while most tools recognize that and allow inserting some custom beginning of the file, they don’t allow to insert code in an arbitrary place in the file.

But, how about a tool that actually allows you to do your own physical design? How about turning the whole code generation process upside down?

A tool like this would allow you to create and name directories and files instead of the tool imposing it on you. Obviously, this is still manual coding. But, the twist here is that in this code you can “ask” the tool to synthesize parts of the code based on the model. (The “requests” are special tags that you mix in your code.) For example, you can “ask” the tool to generate a class declaration in one place, a class method definition in another, and a state machine definition in yet another place in your code.

This “inversion” of code generation responsibilities solves most of the problems with the integration between the generated code and other code. You can simply “ask” the tool to generate as much or as little code as you see fit. The tool helps, where it can add value, but otherwise you can keep it out of your way.

The idea of “inverting” the code generation is so simple, that I would be surprised if it was not already implemented in some tools. One example I have is the free QM tool from my company ( If you know of any other tool that works that way, I would be very interested to hear about it.

Online Embedded Software Store: a good idea?

Saturday, February 11th, 2012 Miro Samek

Have you visited the new online Embedded Software Store ( operated by Avnet and ARM? Did you buy anything there? What do you think?

Well, I visited the website, but frankly, I wouldn’t be comfortable buying software there.

For example, suppose you are interested in operating systems. That’s easy enough, because on the home page lists “New Products” in this category. Yesterday they listed uC/OS-II and CMS-RX RTOS. I clicked on uC/OS-II, which brought me to the product page for “uC/OS-II on the TI LM3S9Bxx – Product Line” by Micrium for $40,982.14. There is really not much of a product description, except for the “Product License”, which is a click-through EULA (End User License Agreement). Otherwise you can just add the product to the shopping cart and head out to check-out. Before you pay, you are presented with an order summary, where they list the products RoHS status, the packaging, as well as other equally “useful” information for software. You are also reminded that you are responsible for Duties and Taxes.

But, wait a minute. What are you buying here? First, you are not really buying the software, because you most likely already have it. It is available for a free download from Micrium (see So, you obviously don’t care about “shipping”. Rather, you buy the rights to use the software in your Product Line. But then the click-through EULA makes no sense. It has no binding signature of the vendor and it has no Product Line definition.

If I would really spend $40,000 for legal rights, I would accept nothing less than a contract signed personally by an officer of Micrium. A click-through contract is good, perhaps, for buying a 99-cent song online, but then you actually get the song. Here, you are about to spend $40K, which is like buying two cars with a mouse click, and you don’t get anything.

Well, perhaps I’m missing something here, but it seems to me that software is a bit more complex product than chips and boards. What do you think of this business model?