Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Code Generation for Fun & Profit (Swift Edition)

Code Generation for Fun & Profit (Swift Edition)

Presented in April '17 Swift Delhi meetup.
https://www.meetup.com/Swift-Delhi/events/239248482/

Ragunath Jawahar

April 22, 2017
Tweet

More Decks by Ragunath Jawahar

Other Decks in Programming

Transcript

  1. Tracking Code (Swift) let properties  = [ "Gender": "Female", "Logged

     in": false, "Source": "Twitter" ] Mixpanel.mainInstance() .track(event: "Signup", properties: properties)
  2. Tracking Code (Swift) let properties  = [ "Gender": "Female", "Logged

     in": false, "Source": "Twitter" ] Mixpanel.mainInstance() .track(event:  "Signup",  properties:  properties)
  3. Tracking Code (Swift) let  properties  =  [ "Gender":  "Female", "Logged

     in":  false, "Source":  "Twitter" ] Mixpanel.mainInstance() .track(event: "Signup", properties: properties)
  4. Tracking Code (Java) MixpanelAPI mixpanel = MixpanelAPI.getInstance(this, token); try {

    JSONObject payload  = new JSONObject(); payload.put("Gender", "Female"); payload.put("Logged  in", false); payload.put("Source", "Twitter"); mixpanel.track("Signup", payload); } catch (JSONException exception) { Log.e("Frodo", "JsonException", exception); }
  5. Tracking Code (Java) MixpanelAPI mixpanel = MixpanelAPI.getInstance(this, token); try  {

    JSONObject payload  =  new  JSONObject(); payload.put("Gender",  "Female"); payload.put("Logged  in",  false); payload.put("Source",  "Twitter"); mixpanel.track("Signup",  payload); }  catch  (JSONException exception)  { Log.e("Frodo",  "JsonException",  exception); }
  6. Tracking Code (Java) MixpanelAPI mixpanel =  MixpanelAPI.getInstance(this,  token); try  {

    JSONObject payload  = new JSONObject(); payload.put("Gender", "Female"); payload.put("Logged  in", false); payload.put("Source", "Twitter"); mixpanel.track("Signup",  payload); }  catch  (JSONException exception)  { Log.e("Frodo",  "JsonException",  exception); }
  7. Tracking Code (Java) MixpanelAPI mixpanel =  MixpanelAPI.getInstance(this,  token); try  {

    JSONObject payload  =  new  JSONObject(); payload.put("Gender",  "Female"); payload.put("Logged  in",  false); payload.put("Source",  "Twitter"); mixpanel.track("Signup", payload); }  catch  (JSONException exception)  { Log.e("Frodo",  "JsonException",  exception); }
  8. Requirements • Accuracy • Accommodate Changes • Don’t Leak Cognitive

    Resources • Testability • Support Multiple Apps and Platforms
  9. Java Poet – Method MethodSpec mainMethod = MethodSpec.methodBuilder("main") .addModifiers(Modifier.PUBLIC, Modifier.STATIC)

    .addParameter(String[].class, "args") .addStatement( "$T.out.println($S)", System.class, "Hello,  World!” ).build();
  10. Java Poet – Method MethodSpec mainMethod =  MethodSpec.methodBuilder("main") .addModifiers(Modifier.PUBLIC,  Modifier.STATIC)

    .addParameter(String[].class,  "args") .addStatement( "$T.out.println($S)", System.class, "Hello,  World!” ).build();
  11. Java Poet – Method MethodSpec mainMethod =  MethodSpec.methodBuilder("main") .addModifiers(Modifier.PUBLIC, Modifier.STATIC)

    .addParameter(String[].class,  "args") .addStatement( "$T.out.println($S)", System.class, "Hello,  World!” ).build();
  12. Java Poet – Method MethodSpec mainMethod =  MethodSpec.methodBuilder("main") .addModifiers(Modifier.PUBLIC,  Modifier.STATIC)

    .addParameter(String[].class, "args") .addStatement( "$T.out.println($S)", System.class, "Hello,  World!” ).build();
  13. Java Poet – Method MethodSpec mainMethod =  MethodSpec.methodBuilder("main") .addModifiers(Modifier.PUBLIC,  Modifier.STATIC)

    .addParameter(String[].class,  "args") .addStatement( "$T.out.println($S)", System.class, "Hello,  World!” ).build();
  14. Java Poet – Method MethodSpec mainMethod =  MethodSpec.methodBuilder("main") .addModifiers(Modifier.PUBLIC,  Modifier.STATIC)

    .addParameter(String[].class,  "args") .addStatement( "$T.out.println($S)", System.class, "Hello,  World!” ).build();
  15. Java Poet – Generated Class package in.kitecash; import java.lang.String; import

    java.lang.System; public class Greeter { public static void main(String[] args) { System.out.println("Hello,  World!"); } }
  16. CODE GENERATION TIPS • Doesn’t have to be DRY •

    Iterate through the design several times • Add “Auto-Generated” comments and documentation • Prefer abstract classes over interfaces • Extract platform-specific code and provide abstraction • Communicate with Business Intelligence team