Showing posts with label jedi. Show all posts
Showing posts with label jedi. Show all posts

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.

24 April 2007

Jedi Way -- Implementing Members

Object-oriented program is easy to read, if written properly. At every point you deal with appropriate level of abstractness and you don't have to deal with implementation specific details most of the time. However, when you have large type hierarchies and you are going to modify some aspect of a top level interface -- you may be in trouble. You have to thoroughly investigate all implementing types and provide method body for each one. ReSharper can help, of course.

Jedi Trick Level 1
Invoke Type Hierarchy and select Derived Types in the toolbar. You will see hierarchy of types derived from the interface. Use "Go To Next/Previous Occurence" command (Ctrl-Alt-Down/Up) to navigate between types. As soon as you have type which requires implementation in the code editor, Code Analysis will show red squiggly. Invoke Quick Fix to implement member and type in method body.

Jedi Trick Level 2
Use Context Action's power to implement members. Position caret on the newely created member, IsAvailable in this case. You will see the light saber bulb which is activated with Alt-Enter. Select "Implement member" and you will be prompted with the list of types. You can select "All above types" and get method body throwing NotImplementedException for every type.

Jedi Trick Level 3
Use Quick Fix power to implement members. Type in method body right in the interface, as if it were implementation. Code Analysis will show red squiggly, because interface member cannot have body. But you will also get two Quick Fixes via red light bulb - remove method body or use body for implementations. As soon as you select second one, body will be copied to appropriate implementations and removed from interface.

Jedi Trick Level 4
Available to Yoda only.

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