Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. The Lounge
  3. Style and form in generated code

Style and form in generated code

Scheduled Pinned Locked Moved The Lounge
designai-codingbusinesstutorialquestion
42 Posts 20 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • honey the codewitchH honey the codewitch

    I almost as a matter of course involve code generation in my larger projects. So that means I'm dealing with a lot of generated code. Typically, I don't design my generated code for manual modification. I design the tool that generates it to be customizable. Ergo, the tool will make the code do what you want. Change the tool parameters to change the code but leave the code alone. The reason being is it's almost always stupid to try to support generated code that a user has modified and for extremely complicated code, like parse tables, dfa tables, or business rule tables that have a lot of conditionals and branching. So the code is a black box. And because of *that* I throw a lot of traditional design assumptions out the window, or go meta with them. Comments are an example: Commenting the generated code is pointless. Commenting the code that generates the code is project-critical Constants are another example. It doesn't matter if you use hard coded values in your generated code if the values are const in the code that generates *it*. Generating private constants just adds to code size with not much upside. The exception is long constant strings. And gotos are totally acceptable, especially in state machine code, and in the absence of cross language CodeDom loop break statements. (CodeDom only supports for() and no while() nor do while(), no break, no continue. How many of you hate me for this, and who agrees with me here? :cool: I'm just curious.

    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

    U Offline
    U Offline
    User 11783308
    wrote on last edited by
    #41

    I do the opposite when I generate code. When emitting code, I automatically indent (really, fairly trivial) and even emit occasional comments. The reason is very simple -- I do not do so, so that users can maintain or modify the code, I do it so that I can maintain and modify the code generator. Just like any other code you write, code that you write automatically may need to be corrected for errors, large and small. Just because you do not directly maintain the generated code, does not mean that the code generator does not need to be maintained. Clean, formatted generated code helps substantially with that. And, if one or eleven years, your code generator is not still in use (or possibly not even available), but the generated code is still running. Then it DOES need to be maintained directly. Bottom line, treat generated code exactly like any other code that you write. BTW, GOTO statements are fine regardless -- just use them to replacing missing control structures. Every language has them, including C and C++. For example, break and continue only apply to the inner level. I quite frequently need to break from two or (more rarely) three levels. Using a GOTO is the only choice, when the language is insufficient.

    honey the codewitchH 1 Reply Last reply
    0
    • U User 11783308

      I do the opposite when I generate code. When emitting code, I automatically indent (really, fairly trivial) and even emit occasional comments. The reason is very simple -- I do not do so, so that users can maintain or modify the code, I do it so that I can maintain and modify the code generator. Just like any other code you write, code that you write automatically may need to be corrected for errors, large and small. Just because you do not directly maintain the generated code, does not mean that the code generator does not need to be maintained. Clean, formatted generated code helps substantially with that. And, if one or eleven years, your code generator is not still in use (or possibly not even available), but the generated code is still running. Then it DOES need to be maintained directly. Bottom line, treat generated code exactly like any other code that you write. BTW, GOTO statements are fine regardless -- just use them to replacing missing control structures. Every language has them, including C and C++. For example, break and continue only apply to the inner level. I quite frequently need to break from two or (more rarely) three levels. Using a GOTO is the only choice, when the language is insufficient.

      honey the codewitchH Offline
      honey the codewitchH Offline
      honey the codewitch
      wrote on last edited by
      #42

      I hear you, and to a degree I agree. I do format my code, and I don't go out of my way to make the generated code cryptic, but I do concentrate on maintaining the generator. If it generates code that needs to be tweaked, then it's the generator that needs to be tweaked, IMO. I'm running into that right now with a lexer generator I made that I'm using as a pre-build step in another project. I fixed the lexer generator. =)

      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups