Code Snippets on Steroids + Jet Fuel

CodeRush for Roslyn, 16.1 - Code Template Example

CodeRush is well-known for its code templates, guided by a set of easy-to-learn rules for writing code quickly. In our port to Roslyn, we kept CodeRush’s unrivaled code template technology and worked to amp up developer efficiency everywhere we could.

Code templates are like code snippets – they offer a shorter path between thought and actual code. They are like a compression mechanism for writing code. So your mind and your fingers do less work, and it takes less time to turn your ideas into working code.

For example, let’s say we want to create a new public enum called “Vowels”, with five elements, “a”, “e”, “i”, “o”, and “u”. Something like this:

  public enum Vowels

To create this by hand in C# it takes 11 keystrokes to enter those six names (“Vowels”, “a”, “e”, etc.) and another 27 keystrokes to create the syntax that makes it an enum:

  1. p
  2. u
  3. b
  4. l
  5. i
  6. c
  7. Space
  8. e
  9. n
  10. u
  11. m
  12. Space
  13. V
  14. o
  15. w
  16. e
  17. l
  18. s
  19. Enter
  20. Shift+
  21.          [    // for “{“ on a US keyboard
  22. Enter
  23. a
  24. ,
  25. Enter
  26. e
  27. ,
  28. Enter
  29. i
  30. ,
  31. Enter
  32. o
  33. ,
  34. Enter
  35. u
  36. Shift+
  37.          ]    // for “}“ on a US keyboard
  38. Enter

We could use Visual Studio’s code snippets to improve our efficiency, saving 10 keystrokes. Nice. In this case it still takes 11 keystrokes to enter “Vowels”, “a”, “e”, etc., and another 17 keystrokes to create the code that makes it a syntactically correct enum:

  1. p         // wait for Intellisense to suggest “public” – if it doesn’t you have more typing
  2. Tab     // expands VS snippet
  3. Space
  4. e         // wait for Intellisense to suggest “enum” – if it doesn’t you have more typing
  5. Tab     // expand “enum”
  6. Tab     // expand enum snippet
  7. V
  8. o
  9. w
  10. e
  11. l
  12. s
  13. Enter  // moves caret into enum
  14. a
  15. ,
  16. Enter
  17. e
  18. ,
  19. Enter
  20. i
  21. ,
  22. Enter
  23. o
  24. ,
  25. Enter
  26. u
  27. Down Arrow
  28. Enter

So using Visual Studio code snippets seems to improve the developer experience. We eliminate the need to hold the Shift key down when pressing the brace keys (simplifying our task and lowering cognitive load), but we’re still required to hit that comma key, which is small and requires more precision to hit (and is a more error prone key to hit) than the much larger Enter key, for example.

Now compare the code snippet experience with CodeRush template expansion, which looks like this (11 keys to name the elements, plus 8 keys to create the syntax):

  1. e
  2. Tab      // …or Space, depending on your settings. Either way, this expands the public enum template instantly.
  3. V
  4. o
  5. w
  6. e
  7. l
  8. s
  9. Enter   // moves caret into enum
  10. a
  11. Enter   // comma added automatically
  12. e
  13. Enter   // comma added automatically
  14. i
  15. Enter   // comma added automatically
  16. o
  17. Enter   // comma added automatically
  18. u
  19. ;        // smart semicolon ends the enum declaration and moves the caret outside

In all three approaches we are creating the exact same code. And each example demands a different level of developer effort.

So for each of these approaches, the minimum number of keys needed to declare only the syntax (e.g., no element names) are summarized in the chart below:



Using CodeRush’s “e” template to quickly create enums.

Wrap Up & Benefits

This gives you an idea of the kind of compression we’re talking about when you use CodeRush templates to write code syntax. In many of the CodeRush templates, including the enum template discussed here, we have achieved what we believe is the most optimally efficient way to write code. Fewest keystrokes. Lightest cognitive load.

There are a number of reasons why you might want to be able to write more code with less work:

  • Your fingers do less work. Less strain over the course of a lifetime may increase the longevity of your hands.
  • Typing fewer keys means fewer opportunities to introduce mistakes. Fewer mistakes means less time wasted correcting.
  • You can write code faster. Useful when you’re pair programming, competing in a hackathon, or presenting/coding live in front of your team or an audience.
  • Writing code faster also shortens the feedback loop between idea and realization, which can lead to a higher quality design.
  • The more experience you get writing code, the better you get. If you write more code in less time, your experience will come at a faster rate.

If you’re already using Visual Studio snippets, that’s a good sign. It shows you value getting more done with less effort.

Note that CodeRush templates tend to be more than twice as efficient as Visual Studio’s snippets. So if efficiency, longevity or coding speed is important to you – and you’re not using CodeRush templates – you might want to take a closer look.

Speaking of closer looks, Paul Usher has a CodeRush for Roslyn webinar on 2 August 2016. Check it out.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.