23 May 2007

Jedi Way - Coding in Reverse

It looks like "ReSharper Jedi" term is beginning to spread! So here is ReSharper Jedi Code, converted from original Jedi Code:

ReSharper Jedi are the guardians of quality and productivity in the .NET world.
ReSharper Jedi use their powers to improve and advance source code, never to degrade.
ReSharper Jedi respect all developers, in any language, with any tools.
ReSharper Jedi serve others rather than ruling over them, for the good of the .NET world.
ReSharper Jedi seek to improve themselves through knowledge and training.


So what would be the major skill ReSharper Jedi should master? I think it is Coding in Reverse. This technique is widely used in test-driven development, I believe. However, it is so effective that every developer should master this skill regardless of programming style. Main principle could be described as follows:

Use Before Declare.


1. Learn to like red code.
2. Always begin with expressing your goal.
3. Deredify your code.

There is one thing to dislike, though. Automatic completion as you type doesn't work well when there are undefined symbols. However it seems to be more about lack of support in tools than fundamental limitation of the technique.

Let's see how does it work.

Code in red indicates unresolved symbols which are just not there yet. At this point I can easily continue writing code for Main method and change my mind any time.


I've expressed my goal pretty well. See how I changed CommandLineParameter to ICommandLineParameter, when I decided that interface would be better. Also, I was able to use "foreach" Live Template to iterate over collection of undefined type.

Okay, now I have to deredify my code and create some methods and types. Where is my light saber bulb and powerful quick-fixes?


Now we need to fill methods with real code, and I will show how to use the Force by implementing ParseCommandLine. We again begin with typing our goal:


Several keystrokes, quick fixes, live templates, some typing and we get to another point:


I use the Force and create class which not only contains required constructor, but also implements interface!


I leave it as an exercise to finish the sample code for anyone who want to practice this technique. Implement ProcessParameter by using some ICommandLineParameter members before declaring them, then quick-fix your way into declaring them, then use Jedi skills and implement members on the SimpleParameter.

Have fun and may the Force be with you!

Note: Some or all of the features mentioned in this article may be available only in latest EAP versions of ReSharper.

22 May 2007

Coding Session with ReSharper

ReSharper team is approaching Feature Freeze milestone for version 3.0, so I didn't have time to write thoughtful posts. However, I needed to write primitive utility for doing some batch operations on the file system structure recursively, and I decided to record a small screen cast for part of the real coding session. It is about 8 minutes long and shows many things I use daily during development. I hope you will enjoy the movie :)

Download "Jedi Coding" from ReSharper Demo page.

Quality is not very good to keep size small. Music in background is "Voodoo People" by Prodigy from "Music For The Jilted Generation" album, I only repeated ending so it fit movie time.

Note: Some or all of the features seen in this movie may be available only in latest EAP versions of ReSharper.

08 May 2007

Code Completion with ReSharper

Completion with ReSharper is very different from what you have in Visual Studio. Some people refer to it as "broken". Indeed, when you start using ReSharper's intellisense, you may feel that something is "wrong". But it's just a little different and much better. I will describe completion features in ReSharper and you'll see how superior they are to what Visual Studio offers.

Look
Visually, completion list is not that different with default ReSharper settings. Here is original Visual Studio completion for type System.String, without any prefix:



Here is what ReSharper displays:



First evident difference is usage of bold font face. It emphasizes immediate members of type, those declared exactly in type for which completion was called. Note how GetType() is not bold, because it was inherited from System.Object. In large hierarchies, like windows controls, it is huge timesaver to be able to spot members specific to current type.

Another obvious difference is ability to show all overloads for a member in a signature popup, not just some random one with cryptic message (+2 overload(s)). You can instantly see that you can test end of line against string in a case-insensitive way and that it would be second boolean parameter.

You can also see, that ReSharper can suggest indexer (and correctly remove dot when inserting brackets). We also have some custom icons for parameters and local variables to distinguish them from fields.

If you wish, you can set completion font to match font in Editor and thus align completion items with what you type. You can tweak it on ReSharper/Options/IntelliSense/Code Completion page.

Narrow Down
More differences become visible when you start typing while completion list is open.



As you can see, list has become smaller and contains only items with the specified prefix, which is highlighted. For a large completion list, such as when you have windows control, it helps you quickly find what you need. It is even more important for type completion, but later about it.

Replace and Insert
The most confusing part and most difficult thing to adopt in ReSharper completion behavior is choosing between replacing and inserting. When you write new code, it doesn't matter, but if you are using completion inside existing expression it makes big difference. Consider the following example: we have method that replaces all spaces with underscores. Imagine that you need to modify the body to remove leading and trailing spaces before doing replacement.

        public string QuoteSpaces(string text)
        {
            return text.Replace(' ', '_');
        }


You position caret right after "text." and hit Ctrl-Space, then type few letters to locate Trim method.



Now, if you hit "Tab" key to complete the identifier it will remove "Replace" and insert "Trim", then put caret into parenthesis. Very useful when you need to change existing identifier to another one. Note the light red highlighting which denotes text to be removed, if you hit Tab. If you hit "Enter" key, it will just insert "Trim()" and let you specify parameters, if needed.

Name Suggestion
One more thing that ReSharper does for you is suggesting variable names when typing declaration.



It works for fields, parameters, variables, members and alike. Saves a lot of typing, I have to say. Name suggestion completion takes naming conventions into account, so be sure to set them in ReSharper/Options/Code Style/ C# or Visual Basic / Naming Convention.

Type Completion
Whenever you need a type at the caret position and you only have basic completion at hands, you have to type full type name and then import type via some sort of fix, e.g. Visual Studio smart-tag action. You also have to match case, and can you remember which one is right - DESCryptoServiceProvider or DesCryptoServiceProvider?

ReSharper comes to rescue with Type Completion, which shows all types matching prefix regardless of (un-)imported namespaces and inserts required using directives when you select a type.



Default shortcut is Ctrl-Alt-Space. Start using it now!

Smart Completion
ReSharper has deep knowledge about your code. Really! When you see variable declaration in gray, it is ReSharper who analysed execution flow and figured out that particular variable is not used. Some of this knowledge is available to you in completion. When it is known what type is expected at specific point, you may get filtered list of available symbols by invoking Smart Complete (default shortcut Ctrl-Shift-Space):



You can see that ReSharper suggested some static members of System.String and parameter "text". Well, you were going to return something of string type, so here you are!

Smart Completion is also smart enough to suggest anonymous and regular method creation:



Or even local variable creation in place of out parameter:



Conclusion
When you type code, completion is your best friend. Master all three completion kinds, practice for second-nature and you can achieve marvellous code generation speed.

Note: Some or all of the features mentioned in this article may be available only in latest EAP versions of ReSharper.