Mark Miller

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
  {
    a,
    e,
    i,
    o,
    u
  }

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:

KeyPressedChart3

 

EnumTemplateExpansion
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.

Published Jul 29 2016, 12:00 PM by
Bookmark and Share

Comments

Crono

Hmmm, interesting!

One thing I find myself doing often are data object assignments, in the likes of:

target.FieldA = source.FieldA

target.FieldB = source.FieldB

target.FieldC = source.FieldC

(...)

Is there any way I can create a template that will allow me to simply type both "source" and "target" once, then type each property name once to create the complete code line, and then either press ENTER for a new code line or semi-colon to end the assignment?

August 3, 2016 6:45 AM

Alex Z (DevExpress)

Crono,

You can add a custom template, which might be helpful in this case. Click the "CodeRush\Code Templates..." menu item, add a new template, and use the following expansion:

«BlockAnchor»«Field(source)»«Caret».«Link(Field)» = «Field(target)».«FieldStart»«Link(Field)»«FieldEnd»

August 17, 2016 3:15 AM

About Mark Miller (DevExpress)

Mark Miller is a C# MVP with strong expertise in decoupled design, plug-in architectures, and great UI. Mark is Chief Architect of the IDE Tools division at Developer Express, and is the visionary force behind productivity tools like CodeRush and Refactor!, as well as the DXCore extensibility layer for Visual Studio. Mark is a popular speaker at conferences around the world and has been writing software for over two decades.
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners