04 March 2008

Varification -- Using Implicitly Typed Locals

With the ReSharper 4 nightly builds available, some people are complaining about numerious suggestions to convert explicit type to "var" keyword. Of course, you can hide this suggestion by using Options / Code Inspection / Inspection Severity, or by using Alt-Enter and selecting "Change severity" option. But what's the deal with implicitly typed locals, anyway? Using var keyword can significantly improve your code, not just save you some typing. However, it may require discipline to apply good practices when using implicitly typed variables. Here is my list:


  • It is required to express variables of anonymous type. This is pretty obvious - you cannot declare local variable of anonymous type without using var.

  • It induces better naming for local variables. When you read local variable declaration with explicit type, you have more information at that moment and something like "IUnitTestElement current" makes sense. However, when this local variable is used later, you read "current" which takes some time to figure out the meaning. Using "var currentElement" makes it easier to read at any place.

  • It induces better API. When you let compiler deduce type from method return type or property type, you have to have good types in the first place. When you don't have explicit type in the initialization expression, you have to have best names for members.

  • It induces variable initialization. It is generally a good practice to initialize variable in the declaration, and compiler needs initializer to infer type for local variable declared with "var" keyword.

  • It removes code noise. There are a lot of cases, when implicitly typed local will reduce amount of text developer needs to read, or rather skip. Declaring local variable from new object expression or cast expression requires specifying type twice, if we don't use "var". With generics it can lead to a lot of otherwise redundant code. Another example would be iteration variable in foreach over Dictionary<TKey,TValue>.

  • It doesn't require using directive. With var, you don't have explicit reference to type, as compiler infers type for you, so you don't need to import namespace when you need a temporary variable.



To summarize the list above, by actively using var keyword and refactoring your code as needed you improve the way your code speaks for itself.

PS: Visit ReSharper at Visual Studio Gallery!

20 February 2008

Visual Studio 2005 and ReSharper 4 Nightly Builds

ReSharper 4 should work with Visual Studio 2005, and it will support C# 2.0 language features. If you happen to participate in our early access program, and you are using Visual Studio 2005, please note that you have to have .NET Framework 3.5 installed.

Also, we would be happy if you were reporting any features that belong to C# 3.0 but appear in Visual Studio 2005, like suggestions for lambdas or var. Please read information about our issue tracker beforehand.

Thank you.

18 February 2008

Better Development Tools -- Building Together

Dear early adopters!

We thank you kindly for your participation in ReSharper 4 early access program. It is going to be wonderful release for everyone, because of you. This time we receive the best response ever, and we are so excited about it!

Many brave developers, who are trying ReSharper 4 early builds with Visual Studio 2008, registered in our issue tracker and provided us with detailed, specific and very important information. This is really great, because we were able to reproduce many issues that were lurking around our code base for some time now. It is very important that you registered and provide us with non-anonymous feedback: bidirectional communication is at least 16 times more effective than one way exception submitting. One sample solution which manifests the problem is 256 times better than 1024 exceptions without a comment. And it is invaluable when you submit instructions to reproduce the problem (well, at least var value = int.MaxValue).

Thank you very much, we really appreciate your effort! With that kind of feedback we receive today, I believe we can build the best release of ReSharper, ever. Everybody wants better tools, right? ;)

Sincerely Yours, Ilya.

14 February 2008

ReSharper 4 Nightly Builds - Are You Geek Enough?



-- When ReSharper 4 will be ready?
-- It is ready when it is ready!



We in ReSharper team want to make sure that C# 3.0 language is fully supported by the product. We spend a lot of time on features and look deep into details of every language quirk. You know, compilers and language standards may have very strange relationships, sometimes very weird. Still, we want to ensure maximum pleasure developing in C# 3.0 when ReSharper is ready. And it is not ready yet.

However, due to the popular demand amongst professional developers using ReSharper and C# 3.0, we are going to open nightly builds to the public. That is, not fully fledged tool, but rather our current development bits. Which could be very well broken, buggy, hanging, slow and useless at times. We will not take any responsibility if the tool wipes out all your source code! But you have a version control system in place, don't you?

So, are you geek enough to take the pill and try ReSharper 4 early builds? Read the ReSharper 4 EAP notes before you decide!

If you feel you can survive pre-release build, you can go to ReSharper 4 Nightly Builds and download your copy of ReSharper development bits.

Good luck!

04 February 2008

Compiler Optimizations May Be Dangerous

Consider the following code:


static class Test
{
public static IEnumerable<int> Select<T>(this IEnumerable<T> list, Func<int, int> f)
{
foreach (var item in list)
yield return f(item.GetHashCode());
}

public static IEnumerable<bool> Where<T>(this IEnumerable<T> list, Func<int, bool> f)
{
foreach (var item in list)
yield return f(item.GetHashCode());
}

static void Foo(IList<string> list, IList<string> list2)
{
var lengths = from hashCode in list
where hashCode * 2 > 10
select hashCode;
}
}


This code doesn't use System.Linq extensions methods to do LINQ, and instead declares own Select and Where methods, which are rather crazy. This code compiles, but what is the type of "lengths" variable? It should be IEnumerable<int>, because that is what Select method returns. But if you look at actual compilation output, it turns out that compiler decided "select hashCode" to be very simple (actually, transforms into identity lambda hashCode => hashCode). And it removes the call to Select() method. And breaks my code. One can have some hard time understanding why their code doesn't work, or even doesn't compile.

I can force compiler to call my Select() method by surrounding hashCode in select clause with parenthesis:

var lengths = from hashCode in list
where hashCode * 2 > 10
select (hashCode);


It is not trivial to compiler anymore, and thus it emits correct code.

UPDATE: This behaviour is by design. See 7.15.2.5 Select clauses:

A query expression of the form
from x in e select v
is translated into
( e ) . Select ( x => v )
except when v is the identifier x, the translation is simply
( e )


I still find this very error prone...

31 January 2008

ReSharper 4 EAP Will Start in Two Weeks

ReSharper 4 Early Access Program will start within next two weeks, before Feb 15.

The delay is caused mostly by the complexity of lambda implementation, which we really want to do right so that the power of C# 3.0 and ReSharper can be utilized at a full rate. Now, when we have lambdas working, we will finish with LINQ processing, polish the code a bit and start nightly builds.

I'm going to post some feature highlights here during that period. This will cover C# 3.0 new features support, new features like Complete Statement and Recent Edits, improvements in intellisense like CamelHumps completion and other.

Stay tuned.

25 January 2008

One Line - Navigation in Parameter Information

When ReSharper's parameter information is displayed for a method, TAB and SHIFT-TAB will navigate caret through arguments you are passing.

14 January 2008

One Line - Find Usages Advanced

When you want to search for all overloads of a method, or search for type and its members usages, or search in referenced libaries in addition to source code - use Find Usages Advanced command (ReSharper | Search | Find Usages Advanced...).

20 December 2007

ReSharper 3.1 is Released!

We are proud to announce the release of ReSharper 3.1. The new version not only brings many bugfixes, improved stability and new features, but also provides the opportunity of free upgrade to v4.0 for new purchases.

This minor - but extremely upgrade-worthy - release extends support for various technologies and languages including ASP.NET, VB.NET and XAML, and introduces the amazingly powerful Solution-Wide Analysis. The new feature lets you instantly find compilation errors in your whole solution - not just in the currently opened file.

Download it now and review release notes.

18 December 2007

One Line - Filtering Popup Lists

When recent files list is open, you can start typing and list will get filtered to show only items which contains entered text. This feature also works in all "Go to" popup lists, like Go To Usage or Go To Inheritors.