WRITING CODE
           that
 WRITES CODE
        STEVE MARSHALL
But why?
Code munging
Inline code expansion

Code munging
Mixed code generation

Inline code expansion

Code munging
Partial class generation

Mixed code generation

Inline code expansion

Code munging
Tier generation

Partial class generation

Mixed code generation

Inline code expansion

Code munging
Full domain language

Tier generation

Partial class generation

Mixed code generation

Inline code expansion

Code munging
Full domain language

Tier generation

Partial class generation

Mixed code generation

Inline code expansion

Code munging
Source code




Code munger




 Output file
Full domain language

Tier generation

Partial class generation

Mixed code generation

Inline code expansion

Code munging
Source code



 Inline code
  expander


Output source
    code



  Compiler
Full domain language

Tier generation

Partial class generation

Mixed code generation

Inline code expansion

Code munging
Source code



 Mixed code
 generator


Output source
    code



  Compiler
Full domain language

Tier generation

Partial class generation

Mixed code generation

Inline code expansion

Code munging
Definition file



 Partial class
                               Templates
  generator


Output base                  Cust...
Full domain language

Tier generation

Partial class generation

Mixed code generation

Inline code expansion

Code munging
Definition file                    Templates



                  Tier generator



                  Output source
      ...
Full domain language

Tier generation

Partial class generation

Mixed code generation

Inline code expansion

Code munging
What should I use?
Let me tell you
a story…
One for the team.
1.

Respect (and loathe)
hand-coding.
2.

Hand-code first.
3.

Control your source.
4.

Consider the
generator’s language.
5.

Integrate the
generator into your
workflow.
6.

Include warnings.
7.

Play nice with others.
8.

Document well.
9.

Consider the
cultural impact.
10.

Keep it maintained.
icanhaz.com/codegen-book
icanhaz.com/codegen

@stevemarshall
steve@nascentguruism.com


Stephen Wolfram photo:
flickr.com/...
Writing Code That Writes Code
Writing Code That Writes Code
Writing Code That Writes Code
Upcoming SlideShare
Loading in...5
×

Writing Code That Writes Code

3,967

Published on

The best developers are fundamentally very lazy. What if there was a way to write a fraction of the volume of code you currently write without losing any functionality, and massively increasing code quality (can you say '100% test coverage')?

Code generation allows us to do exactly that: we can write less code that does more. Imagine a world where all your code is unit-tested, fully documented, clearly commented, fully optimised, is a joy to maintain, and /still/ meets all the requirements. That world is exactly what code generation offers.

Why outsource code to someone halfway around the world when you can outsource it to the little goblins inside the magic box on your desk? Steve Marshall will show you the how, the why, and the 'what's that warm feeling inside?'

Published in: Technology, Business
1 Comment
4 Likes
Statistics
Notes
  • Great demonstration about the require to innovate company models; tips on how to represent them succinctly; along with the need to make development initiatives actionable. Superb use of photographs along with easy to understand illustrative examples.
    Sharika
    http://financeadded.com http://traveltreble.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
3,967
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
34
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide
  • Gonna talk about codegen.



    Steve Marshall
    - Interested in everything
    - Want to be a pirate
    - Y! Ents



    Why codegen
    - Don’t like repetition
    - Don’t like repetition
    - Interesting problem of scale
    - Lack of resources => be creative
  • Quality, consistency, single point of knowledge, eases bulk refactoring, improves architectural consistency, increases abstraction, increases morale.












    Quality

    Lots of code => inconsistencies
    Templates => consistent quality



    Consistency
    - Consistent naming
    - API style
    - No surprises



    Single Point of Knowledge
    Schema describes system simply
    Architecture documented, not embedded in code



    Facilitates Bulk Refactoring
    Faulty assumptions in hand coding mean refactoring lots of code
    Generation makes refactoring minimal
    Also: generation can compress project timelines => more time to design/prototype, less need to refactor



    Architectural Consistency
    - Generator encourages working within architecture
    - “generator doesn’t do that” can indicate that the feature doesn’t fit the architecture
    - Consistent, structured approach, regardless of team members joining/leaving



    Abstraction

    - Application logic is language-independent. Can easily port templates to other languages (eg. for performance, also: WSDL describing inputs/outputs)


    - Can easily review/validate abstract designs


    - Aids development of non-implementation code (tests, documentation, etc)






    High Morale
    Less gruntwork to do by hand
    More focus on interesting problems
    Higher morale
    Also: higher quality, consistent code => confidence and pride
  • Type of codegen…



    - Munging
    - Code expansion
    - Mixed codegen
    - Partial class gen
    - Tier gen
    - FDL
  • Type of codegen…



    - Munging
    - Code expansion
    - Mixed codegen
    - Partial class gen
    - Tier gen
    - FDL
  • Type of codegen…



    - Munging
    - Code expansion
    - Mixed codegen
    - Partial class gen
    - Tier gen
    - FDL
  • Type of codegen…



    - Munging
    - Code expansion
    - Mixed codegen
    - Partial class gen
    - Tier gen
    - FDL
  • Type of codegen…



    - Munging
    - Code expansion
    - Mixed codegen
    - Partial class gen
    - Tier gen
    - FDL
  • Type of codegen…



    - Munging
    - Code expansion
    - Mixed codegen
    - Partial class gen
    - Tier gen
    - FDL
  • Parses source code
    Produces some non-source output



    See documentation, linting, … Also: build header files, web service layers
  • Parses source for special keywords
    Replaces with production code



    eg. Embedding SQL in source à la Pro*C/SQLJ
  • Like inline code expansion, but output can be used as input (replaces in-place)



    Looks for specially formatted comments, fills comment-wrapped area with new production source



    eg. Marshalling code
  • Reads abstract definition with enough info to build classes
    Applies def’n to templates to output base classes
    Used with subclasses for custom behaviour





    Can be a stepping stone to tier generation
  • Generator builds a complete tier of your app (eg ORM)
    eg. Model-driven generation takes definition file and applies to templates



    Like partial class, but without special cases (or are codified in templates)
    Partial class generation requires extra code, tier doesn’t (but may have it)
  • Writing Code That Writes Code

    1. 1. WRITING CODE that WRITES CODE STEVE MARSHALL
    2. 2. But why?
    3. 3. Code munging
    4. 4. Inline code expansion Code munging
    5. 5. Mixed code generation Inline code expansion Code munging
    6. 6. Partial class generation Mixed code generation Inline code expansion Code munging
    7. 7. Tier generation Partial class generation Mixed code generation Inline code expansion Code munging
    8. 8. Full domain language Tier generation Partial class generation Mixed code generation Inline code expansion Code munging
    9. 9. Full domain language Tier generation Partial class generation Mixed code generation Inline code expansion Code munging
    10. 10. Source code Code munger Output file
    11. 11. Full domain language Tier generation Partial class generation Mixed code generation Inline code expansion Code munging
    12. 12. Source code Inline code expander Output source code Compiler
    13. 13. Full domain language Tier generation Partial class generation Mixed code generation Inline code expansion Code munging
    14. 14. Source code Mixed code generator Output source code Compiler
    15. 15. Full domain language Tier generation Partial class generation Mixed code generation Inline code expansion Code munging
    16. 16. Definition file Partial class Templates generator Output base Custom derived class code class code Compiler
    17. 17. Full domain language Tier generation Partial class generation Mixed code generation Inline code expansion Code munging
    18. 18. Definition file Templates Tier generator Output source code Compiler
    19. 19. Full domain language Tier generation Partial class generation Mixed code generation Inline code expansion Code munging
    20. 20. What should I use?
    21. 21. Let me tell you a story…
    22. 22. One for the team.
    23. 23. 1. Respect (and loathe) hand-coding.
    24. 24. 2. Hand-code first.
    25. 25. 3. Control your source.
    26. 26. 4. Consider the generator’s language.
    27. 27. 5. Integrate the generator into your workflow.
    28. 28. 6. Include warnings.
    29. 29. 7. Play nice with others.
    30. 30. 8. Document well.
    31. 31. 9. Consider the cultural impact.
    32. 32. 10. Keep it maintained.
    33. 33. icanhaz.com/codegen-book icanhaz.com/codegen @stevemarshall steve@nascentguruism.com Stephen Wolfram photo: flickr.com/photos/hybernaut/87907765/
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×