<p>I'm working on a tool that will generate the source code for an interface and a couple classes implementing that interface.  My output isn't particularly complicated, so it's not going to be hard to make the output conform to our normal code formatting standards.</p>

<p>But this got me thinking: how human-readable does auto-generated code need to be?  When should extra effort be expended to make sure the generated code is easily read and understood by a human?</p>

<p>In my case, the classes I'm generating are essentially just containers for some data related to another part of the build with methods to get the data.  No one should ever need to look at the code for the classes themselves, they just need to call the various getters the classes provide.  So, it's probably not too important if the code is "clean", well formatted and easily read by a human.</p>

<p>However, what happens if you're generating code that has more than a small amount of simple logic in it?</p>

[{'answer_id': 63280, 'author': 'Scott Dorman', 'author_id': 1559, 'author_profile': 'https://Stackoverflow.com/users/1559', 'pm_score': 5, 'selected': True, 'text': '<p>I think it\'s just as important for generated code to be readable and follow normal coding styles. At some point, someone is either going to need to debug the code or otherwise see what is happening "behind the scenes".</p>\n'}, {'answer_id': 63298, 'author': 'Mattias', 'author_id': 261, 'author_profile': 'https://Stackoverflow.com/users/261', 'pm_score': 0, 'selected': False, 'text': "<p>It's quite possible that somebody in the future will want to go through and see what your code does. So making it somewhat understandable is a good thing.</p>\n\n<p>You also might want to include at the top of each generated file a comment saying how and why this file was generated and what it's purpose is.</p>\n"}, {'answer_id': 63301, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 2, 'selected': False, 'text': '<p>I think that depends on how the generated code will be used. If the code is not meant to be read by humans, i.e. it\'s regenerated whenever something changes, I don\'t think it has to be readable. However, if you are using code generation as an intermediate step in "normal" programming, the generated could should have the same readability as the rest of your source code. </p>\n\n<p>In fact, making the generated code "unreadable" can be an advantage, because it will discourage people from "hacking" generated code, and rather implement their changes in the code-generator instead&mdash;which is very useful whenever you need to regenerate the code for whatever reason and not lose the changes your colleague did because he thought the generated code was "finished".</p>\n'}, {'answer_id': 63302, 'author': 'Adrian', 'author_id': 7426, 'author_profile': 'https://Stackoverflow.com/users/7426', 'pm_score': 0, 'selected': False, 'text': "<p>Generally, if you're generating code that needs to be human-modified later, it needs to be as human-readable as possible. However, even if it's code that will be generated and never touched again, it still needs to be readable enough that you (as the developer writing the code generator) can debug the generator - if your generator spits out bad code, it may be hard to track down if it's difficult to understand.</p>\n"}, {'answer_id': 63317, 'author': 'Jacksonh', 'author_id': 6803, 'author_profile': 'https://Stackoverflow.com/users/6803', 'pm_score': 0, 'selected': False, 'text': "<p>I would think it's worth it to take the extra time to make it human readable just to make it easier to debug. </p>\n"}, {'answer_id': 63318, 'author': 'TK.', 'author_id': 1816, 'author_profile': 'https://Stackoverflow.com/users/1816', 'pm_score': 1, 'selected': False, 'text': "<p>I would say that it is imperative that the code is human readable, unless your code-gen tool has an excellent debugger you (or unfortunate co-worker) will probably by the one waist deep in the code trying to track that oh so elusive bug in the system. My own excursion into 'code from UML' left a bitter tast in my mouth as I could not get to grips with the supposedly 'fancy' debugging process. </p>\n"}, {'answer_id': 63322, 'author': 'Ron Tuffin', 'author_id': 939, 'author_profile': 'https://Stackoverflow.com/users/939', 'pm_score': 0, 'selected': False, 'text': '<p>Generated code should be readable, (format etc can usually be handled by a half decent IDE). At some stage in the codes lifetime it is going to be viewed by someone and they will want to make sense of it.</p>\n'}, {'answer_id': 63323, 'author': 'David Thibault', 'author_id': 5903, 'author_profile': 'https://Stackoverflow.com/users/5903', 'pm_score': 0, 'selected': False, 'text': "<p>I think for data containers or objects with very straightforward workings, human readability is not very important. </p>\n\n<p>However, as soon as a developer may have to read the code to understand how something happens, it needs to be readable. What if the logic has a bug? How will anybody ever discover it if no one is able to read and understand the code? I would go so far as generating comments for the more complicated logic sections, to express the intent, so it's easier to determine if there really is a bug.</p>\n"}, {'answer_id': 63327, 'author': 'Vargen', 'author_id': 6371, 'author_profile': 'https://Stackoverflow.com/users/6371', 'pm_score': 0, 'selected': False, 'text': "<p>Logic should always be readable. If someone else is going to read the code, try to put yourself in their place and see if you would fully understand the code in high (and low?) level without reading that particular piece of code. </p>\n\n<p>I wouldn't spend too much time with code that never would be read, but if it's not too much time i would go through the generated code. If not, at least make comment to cover the loss of readability.</p>\n"}, {'answer_id': 63329, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 1, 'selected': False, 'text': "<p><em>You will kill yourself if you have to debug your own generated code.</em>  Don't start thinking you won't.  <strong>Keep in mind that when you trust your code to generate code then you've already introduced two errors into the system - You've inserted yourself twice.</strong></p>\n\n<p>There is absolutely NO reason NOT to make it human parseable, so why in the world would you want to do so?</p>\n\n<p>-Adam</p>\n"}, {'answer_id': 63331, 'author': 'Romain Verdier', 'author_id': 4687, 'author_profile': 'https://Stackoverflow.com/users/4687', 'pm_score': 0, 'selected': False, 'text': '<p>If this code is likely to be debugged, then you should seriously consider to generate it in a human readable format. </p>\n'}, {'answer_id': 63348, 'author': 'Sklivvz', 'author_id': 7028, 'author_profile': 'https://Stackoverflow.com/users/7028', 'pm_score': 2, 'selected': False, 'text': '<p>Yes it does.\nFirstly, you might need to debug it -- you will be making it easy on yourself. \nSecondly it should adhere to any coding conventions you use in your shop because someday the code might need to be changed by hand and thus become human code. This scenario typically ensues when your code generation tool does not cover one specific thing you need and it is not deemed worthwhile modifying the tool just for that purpose.</p>\n'}, {'answer_id': 63354, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 0, 'selected': False, 'text': '<p>There are different types of generated code, but the most simple types would be:</p>\n\n<ol>\n<li>Generated code that is not meant to be seen by the developer. e.g., xml-ish code that defines layouts (think .frm files, or the horrible files generated by SSIS)</li>\n<li>Generated code that is meant to be a basis for a class that will be later customized by your developer, e.g., code is generated to reduce typing tedium</li>\n</ol>\n\n<p>If you\'re making the latter, you <em>definitely</em> want your code to be human readable.</p>\n\n<p>Classes and interfaces, no matter how "off limits" to developers you think they should be, would almost certainly fall under generated code type number 2. They will be hit by the debugger at one point of another -- applying code formatting is the least you can do the ease that debugging process when the compiler hits those generated classes</p>\n'}, {'answer_id': 63370, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>The whole point of generated code is to do something "complex" that is easier defined in some higher level language. Due to it being generated, the actual maintenance of this generated code should be within the subroutine that generates the code, not the generated code.</p>\n\n<p>Therefor, human readability should have a lower priority; things like runtime speed or functionality are far more important. This is particularly the case when you look at tools like bison and flex, which use the generated code to pre-generate speedy lookup tables to do pattern matching, which would simply be insane to manually maintain.</p>\n'}, {'answer_id': 63382, 'author': 'MarcE', 'author_id': 7262, 'author_profile': 'https://Stackoverflow.com/users/7262', 'pm_score': 0, 'selected': False, 'text': '<p>Like virtually everybody else here, I say make it readable. It costs nothing extra in your generation process and you (or your successor) will appreciate it when they go digging.</p>\n\n<p>For a real world example - look at anything Visual Studio generates. Well formatted, with comments and everything.</p>\n'}, {'answer_id': 63385, 'author': 'Jesse Millikan', 'author_id': 7526, 'author_profile': 'https://Stackoverflow.com/users/7526', 'pm_score': 2, 'selected': False, 'text': '<p>Look up <a href="http://www.c2.com/cgi/wiki?ActiveCodeGeneration" rel="nofollow noreferrer">active code generation</a> vs. <a href="http://www.c2.com/cgi/wiki?PassiveCodeGeneration" rel="nofollow noreferrer">passive code generation</a>. With respect to passive code generation, <strong>absolutely yes, always.</strong> With regards to active code generation, <em>when</em> the code achieves the goal of being transparent, which is acting exactly like a documented API, then no.</p>\n'}, {'answer_id': 63392, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Generated code is code, and there's no reason any code shouldn't be readable and nicely formatted. This is cheap especially in generated code: you don't need to apply formatting yourself, the generator does it for you everytime! :)</p>\n\n<p>As a secondary option in case you're really that lazy, how about piping the code through a beautifier utility of your choice before writing it to disk to ensure at least some level of consistency. Nevertheless, almost all good programmers I know format their code rather pedantically and there's a good reason for it: there's no write-only code.</p>\n"}, {'answer_id': 63435, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Absolutely yes for tons of good reasons already said above. And one more is that if your code need to be checked by an assesor (for safety and dependability issues), it is pretty better if the code is human redeable. If not, the assessor will refuse to assess it and your project will be refected by authorities. The only solution is then to assess... the code generator (that's usually much more difficult ;))</p>\n"}, {'answer_id': 63456, 'author': 'sven', 'author_id': 46, 'author_profile': 'https://Stackoverflow.com/users/46', 'pm_score': 3, 'selected': False, 'text': '<p>Yes!, absolutely!; I can even throw in a story for you to explain why it is important that a human can easily read the auto generated code...</p>\n\n<p>I once got the opportunity to work on a new project. Now, one of the first things you need to do when you start writing code is to create some sort of connection and data representation to and from the database. But instead of just writing this code by hand, we had someone who had developed his own code generator to automatically build base classes from a database schema. It was really neat, the tedious job of writing all this code was now out of our hands... The only problem was, the generated code was far from readable for a <em>normal</em> human.</p>\n\n<p>Of course we didn\'t about that, because hey, it just saved us a lot of work.\nBut after a while things started to go wrong, data was incorrectly read from the user input (or so we thought), corruptions occurred inside the database while we where <em>only</em> reading. Strange.. because reading doesn\'t change any data (again, so we thought)...</p>\n\n<p>Like any good developer we started to question our own code, but after days of searching.. even rewriting code, we could not find anything... and then it dawned on us, the auto generated code was broken!</p>\n\n<p>So now an even bigger task awaited us, checking auto generated code that no sane person could understand in a reasonable amount of time... I\'m talking about non indented, really bad style code with unpronounceable variable and function names...  It turned out that it would even be faster to rewrite the code ourselves, instead of trying to figure out how the code actually worked.</p>\n\n<p>Eventually the developer who wrote the code generator remade it later on, so it now produces <em>readable</em> code, in case something went wrong like before.</p>\n\n<p>Here is <a href="http://www.pragmaticprogrammer.com/the-pragmatic-programmer/extracts/wizards" rel="noreferrer">a link I just found</a> about the topic at hand; I was acctually looking for a link to one of the chapters from the <a href="http://www.pragprog.com/the-pragmatic-programmer" rel="noreferrer">"pragmatic programmer" book</a> to point out why we looked in our code first.</p>\n'}, {'answer_id': 63672, 'author': 'squadette', 'author_id': 7754, 'author_profile': 'https://Stackoverflow.com/users/7754', 'pm_score': 1, 'selected': False, 'text': '<p>One more aspect of the problem which was not mentioned is that the generated code should also be "version control-friendly" (as far as it is feasible).</p>\n\n<p>I found it useful many times to double-check diffs in generated code vs the source code.  </p>\n\n<p>That way you could even occasionally find bugs in tools which generate code.</p>\n'}, {'answer_id': 63729, 'author': 'Wouter Lievens', 'author_id': 7927, 'author_profile': 'https://Stackoverflow.com/users/7927', 'pm_score': 0, 'selected': False, 'text': '<p>It depends on whether the code will only be read by a compiler or also by a human. In addition, it matters whether the code is supposed to be super-fast or whether readability is important. When in doubt, put in the extra effort to generate readable code.</p>\n'}, {'answer_id': 63833, 'author': 'Jon Mills', 'author_id': 7486, 'author_profile': 'https://Stackoverflow.com/users/7486', 'pm_score': 0, 'selected': False, 'text': "<p>I think the answer is: it depends.</p>\n\n<p>*It depends upon whether you need to configure and store the generated code as an artefact. For example, people very rarely keep or configure the object code output from a c-compiler, because they know they can reproduce it from the source every time. I think there may be a similar analogy here.\n*It depends upon whether you need to certify the code to some standard, e.g. Misra-C or DO178.\n*It depends upon whether the source will be generated via your tool every time the code is compiled, or if it will you be stored for inclusion in a build at a later time.</p>\n\n<p>Personally, if all you want to do is build the code, compile it into an executable and then throw the intermediate code away, then I can't see any point in making it too pretty.</p>\n"}]
['https://Stackoverflow.com/questions/63257', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1409/']