Rory Becker - DevExpress CodeRush Blog

July 2014 - Posts

  • New CodeRush Plugin – CR_CorrectReturnType

    In my previous post I was extolling the virtues of Convert to Function.

    You’ll recall that it is used to alter the return type of the current function, from void to the value of the expression on the return statement at the caret.

    MethodReturningIEnumerableInt

    Well what if you have a pre-existing function which returns a string, and you need to alter the function to return an int.

    Convert to Function is only available if the method is of type void. It is not available if the function returns anything else.

    Enter CR_CorrectReturnType

    My latest plugin, Correct Return Type provides the same functionality as Convert to Function, however it works where Convert to Function does not.

    If the active function’s return type is non-void, and differs from that of the return statement at the caret, then Correct Return Type will become available instead of Convert to Function.

    CorrectReturnType

    Availability

    As usual this plugin is available from both the Visual Studio Gallery and from GitHub.

    The source code is available on GitHub.

  • CodeRush – Feature Spotlight – Convert to Function

    Assuming you want to create a new function, there are a few ways you could build it.

    For now let’s focus on the function’s return type…

    Type First

    In this case, you know the type of the value which your function will return.

    You have decided that you want to return a integer.

    The simplest way to do this is using the mi<space> template, which will result in the code below.

    MethodReturningInt

    All that’s needed is:

    • An appropriate name.
    • Any required parameters.
    • The body of the method.

    …and you’re done.

    But what if, in that first moment, you don’t know what type you’d like to return from your method?

    Type Last

    If you’re not entirely sure of the type your method should be returning, don’t worry…. Just don’t specify one …. yet.

    Use CodeRush’s m<space> template to generate the method which has no return value. Otherwise known as a void proc.

    MethodReturningVoid

    As before you have to specify:

    • An appropriate name
    • Any parameters
    • The body of the method.

    However let’s suppose that you’ve done this and you have your brand new Return42 function.

    MethodReturning42

    You’ll notice that at this point our function doesn’t compile. This is, of course, because we are attempting to return an int from a method which specifies a return type of void.

    This is clearly unacceptable and needs fixing. Ordinarily we’d visit the signature of the function, highlight the word void and overtype it with int.

    This can be rather annoying. However CodeRush has our back.

    Simply place your caret on the return keyword and select Convert to Function from the smart tag menu.

    ConvertToFunction

    CodeRush deduces the type of the expression being returned, and alters the signature of the method to int rather than void.

    In our example, the involved type was easy to deduce and relatively simple, but the procedure works with more complex expressions as well.

    MethodReturningIEnumerableInt 

    Summary

    People aren’t all the same, and work in a myriad different ways.

    I’m one of those people who tend not to think in terms of the specific type I’m returning, until the point of actually returning a value.

    CodeRush doesn’t force me to think about this, and is happy to wait until I’m ready.

    When I am ready, CodeRush still has my back with tools to help achieve my goals in my way.

  • New CodeRush Plugin – CR_RemoveStringToken

    A new plugin… What does it do?

    Example 1

    So imagine we have the following code..

    RemoveStringTokenExample1

    Imagine now, that we decide that it’s not important to explain ‘where’ we were searching, when reporting this error.

    Normally we would…

    • Highlight the string token {1} (including it’s associated text).
    • Hit delete.
    • Highlight the “Y” argument (including it’s leading comma).
    • Hit delete.

    RemoveStringToken allows us to remove the token and it’s associated parameter in a single step.

    • We place our caret on the token we want to remove ({1} in this case)
    • Choose Remove String Token from the Code menu.

    RemoveStringTokenExample1a

    A little tidying is required to remove ‘ in ‘, but that’s a whole lot simpler than before.

    Example 2

    In this next example, things are slightly more complicated.

    RemoveStringTokenExample2

    Users aren’t terribly good with codes, so we’ve decided to remove token {1} from the message.

    The manual steps to achieve this are mostly the same as the previous example.

    • Highlight the string token {1} (including it’s associated text)
    • Hit delete.
    • Highlight the “Y” argument. (including it’s leading comma)
    • Hit delete.

    However this time there is an additional requirement.

    Since we’re removing a string token from the middle of the sequence, we need to renumber the tokens that are numerically higher than the one we’re removing.

    So this time our manual steps would also include:

    • Renumber references to {2} so they reference {1} instead.

    Of course if we had additional references to {1} in your string, we’d have to remember not to renumber those other tokens.

    I’m sure you agree that’s a bit of a pain.

    RemoveStringToken handles the token removal, the parameter removal and the token renumbering, all in a single step.

    RemoveStringTokenExample2a

    Once again there are some additional characters in need of a trim. but these are easily dealt with.

    Checks and Balances

    It’s worth noting that with any simple sequence of steps that a human might make, there is always the possibility of mistakes.

    RemoveStringToken checks several things automatically every time, so you don’t have to.

    • Arguments are only removed if there are no remaining references to the target token.
    • Tokens are only renumbered if there are no remaining references to the target token.
    • Console.WriteLine and String.Format are both supported.
    • If no tokens remain within the string, and the outer call is to String.Format, then this outer call is removed.

    Availability

    As usual this plugin is available from the Visual Studio Gallery and from GitHub.

    The source code is available on GitHub

     

  • New CodeRush Plugin–CR_ReverseOperands

    This plugin is a natural follow up to my previous CR_ReverseArgs plugin

    As you might imagine, this plugin changes the order of the selected operands.

    CR_ReverseOperands

    This might seem like a fairly trivial reason to create a plugin, but it happens often that it irritates me, and it does solve the problem. Hopefully it will also save you some time.

    Availability

    As usual this plugin is available from The Visual Studio Gallery and from GitHub.

    The source code is also available on GitHub.

  • New CodeRush Plugin - CR_ReverseArgs

    Hurrah, another new CodeRush plugin.

    The problem minor annoyance

    Occasionally I realise that I’ve passed a couple of arguments to a method in the wrong order.

    What follows is usually a complicated series of…

    • Move Left (Ctrl+Left)
    • Select Right (Shift+Ctrl+Right)
    • Cut Argument (Ctrl+X)
    • Move Right (Ctrl+Right)
    • Paste Argument (Ctrl+V)

    The navigation operations usually need to be repeated several times in order to get into the correct position.

    Additionally there’s that bit where you either…

    • Try to judge which comma to include in the cut/paste operation.
    • Or… Delete the comma, and enter it back again once the paste is complete.

    Add this to the fact that I usually have something useful on the clipboard, which I’d prefer not to have to burn whilst correcting the order of these arguments.

    The whole rigmarole is something to be avoided.

    Enter CR_ReverseArgs

    The new CR_ReverseArgs plugin allows you to just highlight the pair of arguments, and trigger Reverse Args from the CodeRush Smart Tag menu.

    Then CodeRush will do the rest.

    CR_ReverseArgs

    This CodeProvider should work with ints, strings, and all sorts of other objects. The 2 arguments need not be of the same type either. Feel free to switch whichever pairs you like.

    Note: This plugin is NOT a Refactoring. It will affect the functionality of your code.
    If you wish to move both the Arguments and their corresponding Parameters, then you should use the Reorder Parameters refactoring

    Availability

    This plugin is available immediately:

     

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