October 2010 - Posts

CodeRush for Visual Studio – New Options Layers

CodeRush has recently changed the way it reads and processes your options, adding new layers which make it much easier to bind settings to a particular solution or team. This post will explain those changes and show how you can get the most out of this new functionality.

How did Options previously work?
CodeRush used to just store a single set of options in a single place.
This made is easy to backup your options and take them with you, or have them easily replicate to different physical locations (Office, Home, Laptop)

While this on its own is awesome, we wanted to do more.

For example, you might want to:

  • Share options with team developers working on the same solution. It's always helpful if as many people as possible can create and manipulate code according to a single standard and set of pre-configured tools.
  • Use different options for different solutions. Not everyone works for a single employer or sits on a single team where standards are universally agreed. Also, it often makes sense to have the same Template Name Variable shortcut reference different classes in different solutions (for example, you might want "bb" to reference your custom "BigBus" class in one solution, while referencing your custom "BelgianBeer" class in another).
  • Override select team options, templatesshortcuts, etc. on a personal basis. 

How do the new settings work?
The new Options layer lets you create and easily manage multiple sets (or partial sets) of options layers, which we combine at the appropriate times to provide you with a truly seamless experience.

We pull options from at least three distinct layers and compose them for you so that you get the experience you need.

The three layers are:

Solution Personal Options: This is the first place CodeRush looks for options. If it finds that you have personally overridden an option, it looks no further and applies that option to your session. These options are found within the ‘.cr\personal’ folder in your solution folder. Note: if you use these options, your personal overrides should be excluded from source control. These are intended to represent your personal preferences on the given solution, and as such, it's probably a good idea to NOT force them on your colleagues. 

Solution Team Options: Assuming no Solution-based Personal override options are found, CodeRush looks to the Solution-based Team options. These are found within the ‘.cr\team’ folder beneath your solution folder. Note: Now this folder we do recommend adding to source control, so that team options can percolate throughout everyone on your development team.

Team settings are a great place to establish a consistent coding style. Variable naming conventions, brace positioning, indentation style and other vitally important determinations make sense here.

Personal Options: If CodeRush does not find any Solution-based options (personal or team) , then it will look at your traditional CodeRush settings. I like to think of this as the machine level options, since I’m the only one who uses my machine.

There's actually a fourth layer, which are the default settings that come with CodeRush out of the box.

So how do I modify the options in each of these layers?

I’m so glad you asked.

As always, your options can be accessed from the CodeRush | Options... menu.

CodeRushMenuOptionsHighlightedTornEdge

When you reach the Options Dialog, you will notice a new section at the bottom allowing you to indicate which layer to save any current option changes. This defaults to your Personal Options layer. This is the same location where previous versions of CodeRush have saved their options.

OptionsQuickSetupPersonalOptionsHighlightedTornEdge

Options Layers

Alternatively (and perhaps more interestingly), you can visit the options by way of our new Options Layers... menu option:

CodeRushMenuOptionsLayersHighlightedTornEdge

This will present you with our new Options Layers window.

CodeRushOptionsLayersScreen

Each of the listed entries in this window, when double-clicked, will launch the CodeRush Options dialog but only targeted at the layer in question, allowing you to see options at only that layer (and default values from layers below) and alter them in isolation for that targeted layer.

When the Options Dialog is launched in this manner, the previously-mentioned "Layer to save changes:" option will be gone. And there will be an additional section at the top left of the dialog, which will show you the currently targeted layer, as well as its physical location on disk.

CodeRushOptionsPersonalOptionsHighlighted

When you save options in a dialog launched in this manner, the changes you make are only saved in the that targeted layer.


Custom External Option Sets

Obviously it would be a bit of a pain to have to setup your personal navigational options for each and every solution, so we give you the facility to add your own layers which are a higher priority than those that we ship.

If needed, you can create one or more independent folders containing personal overrides which you can store outside (independent of) any single solution, and attach it to one or more solutions, rather like enabling one or more sets of StyleCop rules.

To add a new custom layer:

  1. Open the Options Layers dialog.
  2. Select the layer you would like to override.
  3. Click the "+" button.

    OptionsLayersWithPersonalOverridesAddHighlightedWithTornEdge
  4. Specify a folder. Either an empty one to store new settings, or a folder with pre-existing settings. 

    OptionsLayersPickNewFolder

At this point you’ll have several layers of options each contributing to your CodeRush experience:

  1. Personal Options – Your default CodeRush options held at the user level for any given machine.
  2. Solution Team Options – The options that you wish to impose on a team working on this solution, held within the ‘.cr\team’ subfolder underneath your solution.
  3. Solution Personal Options – Your personal overrides for this particular solution, held within the ‘.cr\personal’ subfolder underneath your solution.
  4. CodeRush Personal Settings – A prebuilt collection of settings that you prefer to be in place for a selection (but not necessarily all) of your solutions. these can be located anywhere convenient (like say a Dropbox or OneDrive folder).

So now you should have everything you need to operate with the correct settings under all circumstances. We hope you enjoy the new layers options, and if you have any questions don't hesitate to ask!

CodeRush for Roslyn: Webinar links

Following our recent 17.1 webinar on CodeRush for Roslyn, I wanted to follow up with a summary of the links posted in the chat during the Webinar.

Setup

CodeRush Setup Wizard

Previous CodeRush Features of the Week

T is for Test
Tab To Next Reference
Templates
Debug Visualizer


Our "Coding at the Speed of Thought" series

Introduction - CodeRush at the Speed of Thought
Declaring Types with Templates (CodeRush at the Speed of Thought)
Template Type Shortcuts (CodeRush at the Speed of Thought)
Declaring Properties (CodeRush at the Speed of thought)
Declaring Methods (CodeRush at the Speed of thought)
Inside Methods (CodeRush at the Speed of Thought)
Declaring Events (CodeRush at the Speed of Thought)
Declare from Usage (CodeRush at the Speed of Thought)

Feature callouts

Declare Menu – Overview - Documentation
Symbol Naming Assistant – Overview - Documentation
Clipboard History – OverviewDocumentation
Code Metrics – OverviewDocumentation
Smart Duplicate Line – OverviewDocumentation
Drop Marker before Jump - Overview - Documentation

Misc Links
Extract Interface – Documentation
Widen Scope – Documentation 
Widen Scope (Promote to field) – Documentation
Move Type to Namespace – Documentation
Reorder Parameters – Documentation

CodeRush for Roslyn: XAML support

XAML is an XML-based language developed by Microsoft, which is widely used in WPF and UWP applications. Visual Studio 2017 helps a lot with editing XAML: features like IntelliSense, tag auto-close and code fold are very useful and increase productivity, but what if your IDE could help you even more?

With the help of CodeRush for Roslyn, Visual Studio will better understand the relationship between the markup and the code. This will help you in many aspects, especially, navigation.

Let’s take a look at the most useful CodeRush for Roslyn features for XAML. Most examples are shown using the source code of the Material Design In XAML Toolkit.

Find References

You can use the References window in XAML. It will search for references in code and markup files across the solution.

Tab to Next Reference

What’s the fastest way to find symbol references within a file? You can use search, but it’s not very smart and may find too much. We invented the fastest way to find symbol references in a file. Just hit Tab while you are on an identifier and you will immediately jump to the next reference to it. Keep hitting tab and jumping to successive references. If you accidentally overshoot the reference you’re looking for, don’t panic. Just use Shift+Tab to move back through the references already seen. All references are highlighted for you to have a clear vision of your code while moving among symbol usages.

Jump to Resource

Seeing an image or resource dictionary in your code and wondering what is it? With CodeRush for Roslyn, you can press F12 to instantly open it.

Jump to Symbol Declaration

Navigates to the declaration of an identifier (in markup or Code Behind) from any reference in XAML file: tag, markup extension, templated parent, etc.

Templates

The whole power of CodeRush Templates is available in XAML! You can use templates from the template library and even create your own to maximize your productivity.

Let’s see some examples in action.


XAML Markup Formatting

Format XAML Document

XAML markup often has deep and complex structure, and if, while editing, the formatting is less than optimal, you may experience difficulties understanding its structure. You can fix it manually, but that can take a lot of work. You could use an online XML formatter, but that would involve copy/pasting and switching out to a browser. Now there’s a better option: you can format any XAML document right from the Visual Studio in a single click.

FormalXAMLDocument

Of course, the formatting style is configurable, you can easily tweak it according to your own preference.

XAMLFormattingOptions

Break Apart/LineUp Attributes

One of the most useful XAML Formatting features allowing you to break apart or line-up attributes is available separately.

XAMLBreakApartAttributes


Refactorings

Import Type/All Types

From time to time we all need to reuse some code or markup from the Internet or from older projects. With the CodeRush for Roslyn, you can paste a piece of markup into your file and make it work with a single call to Import All Types refactoring.

Optimize Namespace References

The best code not only works flawlessly, but also looks great. Namespace References is the very first section in every code file and often it leaves space for improvement. The Optimize Namespace References refactoring can make it perfect in a single click: it automatically sorts references and removes the unused ones.

Convert Nested Element to Attribute/Attribute to Nested Element

Using these refactorings, you can extract an attribute from a XAML tag and make it a child element. You can also perform the reverse operation — inline a child element as an attribute of the parent tag. This refactoring can increase XAML readability when there are excessive attributes or when the nesting level gets too deep.

XAMLConvertNestedElementToAttribute


Visualization Features

Show Color

Computers and humans understand colors differently and in most cases a computer can show colors the way we used to them. But in software development we still have to speak with the computer in its language.

To change this state of affairs, we have the Show Color feature that adds a color preview to all colors you use in code.

XAMLShowColor

You can even edit a color visually by clicking any preview stripe.

CodeRushColorPicker

Structural Highlighting

Structure is an essential part of all XML-based languages, and sometimes the formatting is not enough to see the structure clearly. Structural highlighting draws lines to aid visualization of this hierarchy.

XAMLStructuralHighlighting

Region Painting

And we make regions look awesome!

XAMLRegionPainting


Markup Creation Assistance

Selection Embedding

Surround something with a tag or region in a single click. You can even use Ctrl+W (Selection Expand) to select the whole block from its opening or closing tag.

XAMLSelectionEmbedding

Paste Vector Drawing as XAML

Imagine you have a vector drawing which you want to use in your app. For instance, you have it in Adobe® Illustrator®. With CodeRush for Roslyn, you can copy the drawing (or part of it) and paste it right into a XAML file.

Select the required pieces and copy them to the clipboard.

XAMLCopyFromBlend

Then switch to Visual Studio and paste the clipboard contents into a XAML file. The drawing will be pasted in a vector format ready for you to scale and modify it as you need.

XAMLPasteVectorIntoStudio

We support Microsoft® Visio®, Microsoft® PowerPoint® and Adobe® Illustrator®, but other vector editors also have a good chance to work.

Smart Duplicate Line

Use Shift+Enter to duplicate the current line. The Smart Duplicate Line feature watches and learns from your changes to improve the duplicate line experience, creating text fields for the parts of the line we expect you to change, and automatically incrementing numeric values if we detect an incrementing pattern.

XAMLSmartDuplicateLine

CodeRush for Roslyn: ForEach to Linq

One really useful Refactoring provided by CodeRush for Roslyn is “ForEach to Linq

This refactoring is available whenever you have a ForEach that is iterating over a master list and attempting to select either a subset, or a single item, from that list.

Consider this legacy code example, where we have a list of orders and we want to extract a list of those orders which need Next-Day delivery.

ExtractPriorityOrdersForEach

Obviously that is fairly excessive. It’s also fairly complex to read.

  • There is a result set being prepared.
  • There is iteration over an incoming source set.
  • There is a condition being evaluated for each item in the source set.
  • There is the fact that passing items are being added to this result set.
  • Finally there is the returning of this result set at the end of the method.

So naturally we want to make this easier for you.

ForEach To Linq

So if you position your caret immediately left of the foreach, you will be able to select ForEach to Linq (Preview below)

ExtractPriorityOrdersForEachPreview

Once selected, this refactoring will reduce your unsightly loop to a simple LINQ Query.

ExtractPriorityOrdersPost

After this it’s pretty simple to reduce this to a single simple call.

ExtractPriorityOrdersForEachReduced

I think we can all agree that this is much easier on the eyes and on the brain as well.

Everything has been reduced to a simple LINQ query.

Depending on your needs you could change this function to return an IEnumerable<Order> and strip the ToList() off the end of the expression.
At that point you may even decide that the function, which previously hid your complexity, is now no longer necessary at all, and you can Inline and Delete this method.

Another example is with the following type of legacy search code:
Here we iterate over a list searching for the first item to match, in this case, on the basis of a name.

FindCustomerByNameForEachPre

The ForEach to Linq refactoring is again available from the foreach keyword.

FindCustomerByNameForEachPreview

Acceptance quickly removes the complexity of the original loop, replacing it with a simple Linq query.

FindCustomerByNameForEachPost

A quick Inline Temp later and the previously overly complex iteration and select, together with it’s Result variable has been completely eliminated in favour of a single statement which

FindCustomerByNameForEachTidy

In both of these examples, CodeRush has reduced the complexity of your code, helping you to replace old world manual loops with a concise expression of the results you want.

These are just a few of the ways that CodeRush for Roslyn can simplify your life.

If you don’t already have your copy of CodeRush for Roslyn, hop along to https://www.devexpress.com/products/coderush/ and pick up your free 30 day trial.
The paid version of CodeRush for Roslyn can be had for as little as $49. That is amazing value.

CodeRush for Roslyn – Writing Tests Fast

So we’ve proved that CodeRush has the fastest .Net Test runner.

Next up I’m going to show you that CodeRush is also the quickest way to write tests.

Note: For the purposes of this post I’m going to assume that you’ve enabled Templates, and that you are using the Spacebar to trigger their expansion.
If you have not enabled Templates, you can use the setup wizard to do so now.

Writing Tests using CodeRush Templates

So how simple could it possibly be to start writing a new test?

How about t<space> ?

That’s only 2 keys!

Don’t believe me? Ok try this

  • Create a class library and add a reference to your favourite testing framework. I’m going to use NUnit which I’ve added via nuget.
  • Create a new class file and delete everything in it (Keep the namespace if you want.)
  • Choose an empty line and type the letter ‘t’ and then press the spacebar.

You’ll be presented with a new test fixture with the skeleton of a new test.
The name of the Test Fixture is already selected.

  • Change the name of the fixture to whatever you like.
  • Hit [enter] to confirm when you’re happy.

Your selection moves to the name of the test.

  • Change that to whatever makes the most sense to you.
  • Hit [Enter] to confirm once again.

Your caret is repositioned once again within the body of the test that you’ve just created.

This is roughly what you can expect this experience to be like.

 CRR_Testing_TestWithFixture

Effort

If you count the characters I typed (t, [space], M, y, T, e, s, t, S, u, i, t, e, [Enter], M, y, F, i, r, s, t, T, e, s, t, [Enter]) You’ll come to 26 characters.

However 22 of those were the names of my fixture and test, (which we’d have to type no matter what) so really we’re only talking about 4 characters. 2 to generate the frame of the test (and the fixture) and another 2 to confirm we’re happy with what we’ve typed and to move on to the next bit of defining our test.

Whichever way you look at it…. That’s not a lot of effort.

Complexity

How easy or hard was that? Could you do it again? What do we have to remember?

Well we have to remember that the test template is triggered with t and then [space] (or tab if you’ve got your preferences set that way).

That’s literally it.

Context

Things get even simpler when you realise that you don’t always have to learn new template shortcuts for new purposes. CodeRush’s templates are context-sensitive. Which means that the same trigger can yield different expansions under different circumstances giving you even less to learn.

For example, if you invoke the t<space> template from within a test fixture, CodeRush spots that you already have the fixture and only generates a new test.

CRR_Testing_TestWithoutFixture

Assertions

Ok well that was simple. What else can we do?

Well what do you need to do in a test when you get right down to it?

  • You setup a scenario.
  • You assert something about the scenario.

Well your setup will be specific to your needs, but assertions are mostly the same.

So does CodeRush have some tricks to help us assert a little quicker? …  Of course it does Smile

So what sort of things do we typically assert?

Well assertions (and therefore Tests) are all about checking something that should a certain way, and failing  (ie getting the developer’s attention) if that turns out not to be the case.

We assert equality and inequality

You Want To Template You Use Result
Assert Equality ae<space>
Assert Same as<space>
Assert Not Same ans<space>

…we assert that things are true and false

You Want To Template You Use Result
Assert True at<space>
Assert False af<space>

…and we compare with null

You Want To Template You Use Result
Assert Null an<space>
Assert Not Null ann<space>

As you can see CodeRush has you covered and always with a template that is intuitive and easy to remember.

I’m sure you’re starting to see the benefits of CodeRush templates. I’ll leave you there for now, and let these templates soak in. Just know that no CodeRush templates are hard wired. They can all be altered or tweaked to your needs, and you can even create your own… We’ll explore how to do that in a future post.

Get CodeRush for Roslyn Today

If you’d like to try out CodeRush for Roslyn, a 30 day trial can be found here on the Visual Studio Gallery.

CodeRush for Roslyn can be yours for as little as $49

More Posts