Krista's Coding Corner


Auto generated code –bad thing or heaven?

Some don’t think auto generated code could be done otherwise and some think that it is the worst invention ever. Both can be right.

There probably are many definitions on what is actual auto generated code but I have no idea about them. So, I’ll just consider any code that isn’t directly man-made as auto generated.

Good things or bad things first?

Let’s start with the bad ones, so we get a more cheerful ending :)

Auto generated code can be, and quite often is, totally unreadable. It is hard to debug or improve as it takes a lot of effort just to understand it. This is because computers and humans aren’t too much alike and humans can easily adapt into different situations. Computers again, well, we have to make sure that the generated code works, so there might be a lot of extra things “just in case we’ll have this and this exception”. In auto generated code we are prepared for the worst (or we should be because if the worst happens, auto generation can’t otherwise deal with the situation). And if there is any variable names or so, they can be really awful as the generator doesn’t necessary know what you are actually doing. It just gives IDs, not proper names.

Because all the extras and just the fact that auto generated code is tried to be made quite universal, the code is quite often slow and takes a lot of memory. It isn’t optimized code –something a human can do without even thinking because he is thinking just the case in front of him. This of course isn’t always true: some auto generated code can be designed exactly to be really really optimized and the generator might be used in making regular code into high speed :) It depends on what the generator is made for.

Auto generation can also have some unwanted side effects: if a person just needs to push a button, what happens if he then would need to know what the code really does or fix something in the code? It might be so that all he knows is just to push the button, nothing else.

Good things: auto generated code can be really easy and fast to create which saves time and in that way it saves money. And in some cases it might actually be quite impossible to do some code in any other way (see: Malbolge).

Here is one example: a machine has to be programmed to make holes into a plate. We need this kind of a pattern of holes:

Now, in which order should the holes be made?

Here is one auto generated answer I have got from a professional program that helped me A LOT while I was doing this sort of code generating:

Doesn’t look so good? Yes, it doesn’t. But in 9/10 cases the program did its job. Maybe not perfectly but well enough. If we go for perfection nothing will really be done as perfection is a quite high aim.

In a situation where the program didn’t perform so well, I did the order manually. It took time but sometimes it was worth it. It is a part of experience to know when to make something better and when not. In this case the answer was of course clear.

So, this just looks so much better and it is way faster way to make the holes. It is so much faster and better for the machine's mechanics that it is well worth making things right.

If we go really deep into this sort of machine programming, we actually will notice that even though the order would be made manually, the program creates (== auto generates) the actual code the machine reads. This code could also be done purely by human but it takes even more time than creating just the order. This code is called NC-code (numeric control-code) and it used to be the way to program machines, and in history all that code was made by humans. Sometimes I also had to create NC-code by myself, without a program, if the program was doing something funny or if there was some really hard nut to crack. :)

If you read the post about making life easier, you might remember the batch-files. I have created batch-files with Python. This is one way to auto generate code. I used another program (written by me) to create new code to myself == the batch-file. There is nothing wrong in that. Actually, I would recommend that as it is sometimes easier and faster way.

I think I have mentioned that this web page’s source code is auto generated. There is a php-software that reads a file from a hard drive and then it looks for special marks in the file (like line breaks and my own marking about what is title and date). Then the program knows how I want my text to look and it creates a html-document about the information == this page. But it doesn’t create the style sheet.

Auto generated web pages may not be a good choice in that sense that if the structure is auto generated, it may not be stable. I auto generate only the content into the page BUT the page design is hand made. There are software that give you an option to just drag and drop pieces of elements into a page. Normally these won’t work so perfectly as the world is full of exceptions if we look at how people are viewing web pages and auto generating hardly ever can cope with this all.

If you auto generate something, you’d better also know how to do it yourself. Auto generating can be handy but do you prefer to be just someone who pushes the button and is on the mercy of the program? Or do you want to be someone who controls the program and can side step it when needed? :)

Don’t make yourself lower life form than a machine –whatever the machine is. There still is no need for that ;)

blog comments powered by Disqus