Browse Tag: resharper

What I learnt from using TypeScript “for real”

I completed my first commercial Greenfield project using TypeScript over plain old JavaScript throughout, and there were some frustrations along the way.



  • TypeScript is awesome, for sure, but there needs to be improvements to tooling to streamline the experience.
  • TypeScript is strongly typed of course, but it doesn’t force you to code in this manner, which can result in shoddy code.
  • Tweaking is required to make bundling work properly.


Tooling Frustrations

When I started the project, I was using Visual Studio 2012 Update 4 with TypeScript 1.0 pre-installed.  I was brought in to work on this project on a short term basis, and this is how the machine was set up when I arrived.  Normally I wouldn’t choose to use Visual Studio 2012 (I like to use the latest tools where possible) but I decided to just go with it and make do.  Once deficiencies in tooling became clear, I did eventually push for an update to Visual Studio 2013 Update 4, which in fairness resolved most of my frustrations.

I have a license for JetBrains ReSharper, which has support for TypeScript 1.4 (and indeed that is the default setting when using ReSharper 9).  This was actually my first tooling problem.

When using Visual Studio 2012, you are using TypeScript 1.0 (unless you have proactively updated to version 1.4).  Naturally 1.0 is an older version and doesn’t support language features such as protected methods or ECMAScript 6 template strings.  ReSharper, however, does understand these features and offers suggestions to use them.  Trying to use these features results in build errors, which of course was confusing because I didn’t understand that I was actually using version 1.0 at the time (does it say anywhere? not to my knowledge).

Also, due to the aforementioned old version of TypeScript, I also encountered an issue with TypeScript definition files.  These files are designed to not only provide IntelliSense in Visual Studio when working with third party libraries such as AngularJS, but also are used during the build process for compile time checking.  Because the TypeScript definition files (sourced using NuGet via GitHub) were based on TypeScript 1.4, again hundreds of errors at compile time.  Interestingly, however, with version 1.0 it was actually possible to ignore these build errors and still generate the compiled JavaScript files.  Only once upgrading to version 1.4 later on did it become mandatory to fix these build issues.

Compile on save does what it says on the tin.  You write some TypeScript code, click Save, and the JavaScript file is generated automatically.  Well, actually, no.  There were dozens of occasions when this just didn’t work.  I had to literally open the scripts folder, delete the JavaScript files manually, re-open Visual Studio, and save the TypeScript file again before it would actually recompile the JavaScript file.  This was tedious and boring and didn’t seem to be resolved by upgrading to Visual Studio 2013 Update 4 (although it did become less frequent).  I have Web Essentials installed, which gives you a side by side view of the TypeScript and JavaScript files which I originally turned off.  I did eventually turn this back on so I could see if the files were being recompiled at a glace before refreshing the web browser.

On a side note, the tooling provided by Google Chrome developer tools is excellent.  You can debug the actual TypeScript source directly in the browser, and if you inadvertently set a breakpoint on a JavaScript file, Chrome will navigate you back to the TypeScript file.  Nice.


Bundling in MVC

The project I was working on used some features of ASP .NET and Web API.  One such feature was bundling and minification, which I always use to improve performance and reduce server load.  I wrote many based files and interfaces as you would expect, as both are fully supported in TypeScript.  However, what I later realised (perhaps I should have realised at the start, but hey) was that order is important.  The MVC bundler doesn’t care about order, so there’s a big problem here.  After switching on bundling, I went from 0 runtime errors to at least 20.  There are two approaches I could have taken to resolve the problem;

  1. Create two separate bundles, one for base classes, the other for derived classes (I don’t care for this)
  2. Create a custom orderer and use naming conventions (prefix base class files with the name “Base”) to ensure order.

I went for the latter option.  This involved having to rename all my base class files and interfaces (granted interfaces don’t actually generate any JavaScript, but I wanted to keep the naming consistent) and writing a custom convention based orderer.  Neither of these things were challenging, just time consuming.


TypeScript is inconsistent

I can only speak from my own experiences here.  I don’t claim to be a TypeScript or JavaScript master, I’m just a normal developer as experienced as anybody else.

I love the strongly typed nature of TypeScript, that’s the feature that appeals most to me.  I do however have the following qualms;


TypeScript does not force you to use the language features of TypeScript

This is either a good thing, or a bad thing.  I can’t decide.  To be honest, I wish TypeScript did force you to use its own paradigm.  You can easily write pure JavaScript inside a TypeScript file and it will all compile and “just work”.  This is a bit of a downfall for me because I’ve always found JavaScript to be frustrating at best (can you think of an application you’ve written in JavaScript that hasn’t eventually descended into mayhem?).

What I mean is this, I can create a local variable and assign it a type of Element;

var element : Element = angular.element('.myelement')

The type constraint is redundant, and ReSharper makes a song and dance about the fact and basically forces you to get rid of it (yeah, I know I could turn this off but at the end of the day ReSharper is right).  I can assign function parameters type declarations, but I don’t have to.  (I did eventually go back in fill these in where I could).  I know that I should use type declarations and to be honest I wish I did use this feature more, but I’m lazy.  Next time I’ll be sure to do this from the start as I think it adds further structure to the code.


TypeScript doesn’t know it all

The project I was developing was built in conjunction with AngularJS.  I’ve had an up-down relationship with AngularJS to say the least, but I decided to fully embrace it this time and not switch between jQuery and Angular (yeah I’m aware that Angular uses jQuery lite under the hood, I’m referring to not switching between using $() and angular.element()).  I made heavy use of Angular Scope and I often had a dozen or more child scopes running on any one page.  So as a result, I had lots of code that looked roughly like this;

var scope = angular.element('.myselector').first().children('.mychildren').scope()

That’s perfectly valid code.  Even though I have already added the appropriate type definition files and referenced appropriate at the top of the file, TypeScript still insists that scope() is not valid in this context and I get a compilation error.  So I’d have to exploit the dynamic functionality of JavaScript to get around this;

var children = angular.element('.myselector').first().children('.mychildren');
var scope = children.scope();

Ok its not a big deal.  The scope variable is now of type any, so there’s no IntelliSense and no compile time checking.  But hey, wait, isn’t that the main point of using TypeScript in the first place?  Now if I pass scope to another function, I’ll exacerbate the problem (due to the fact that the parameter will have to be of type any) and before you know it, I’m just writing JavaScript again.  Fail.

Positive note

I’ve ranted about what I don’t like about TypeScript, and the frustrations I’ve encountered so far, but honestly I can’t sign its praises enough.  I estimate that so far TypeScript has saved me at least 3 million hours of effort.  Ok, perhaps a slight exaggeration, but you get the point.  Writing TypeScript is easy and familiar because I am comfortable with JavaScript and strongly typed languages such as C#.  I did spend perhaps a few hours reading the documentation and experimenting in the excellent TypeScript playground (both of which are excellent by the way), but generally thanks to previous experience with the aforementioned languages, I found that I “just know” TypeScript and it has a very smooth learning curve.



TypeScript is awesome, there is no doubt in my mind about that.  TypeScript saves all the pain that naturally comes with JavaScript and its only going to get better over time.   The tooling issues and inconsistencies are relatively insignificant, once you get used to them.

15 reasons why I can’t work without JetBrains ReSharper

If you know me personally, you’ll know how much I love JetBrains ReSharper, I use it every day and I swear by it.  People often ask me what I like most about it, and here I often stutter.  The truth is, there is no one killer reason why I love ReSharper… it’s a combination of many small features that make it a tool I literally cannot work without.  I’m going to explain my 15 favourite features, and urge you to give it a try.  If you’re still not sold by the end of this, you’re never going to be converted.

Note that the reasons are in no particular order, other than the order in which they came to mind.  This post was written using Visual Studio 2013 and ReSharper 8.1 (EAP at the time of writing).

Top tip: Don’t fight with ReSharper, embrace it.  If there is a warning/error/configuration that irks you, change the setting! ReSharper is highly configurable and the team at JetBrains have done everything they can to make ReSharper work with you, not against you.

Common Myths and Moans

First things first… some people have used ReSharper in the past and for whatever reason, they have abandoned it.  Usually, they have two main complaints.

Myth: ReSharper is slow.

JP says: No its not. I have used ReSharper with solutions exceeding 350 projects, with no ill performance affects.  Perhaps poor performance was true in earlier versions of ReSharper, but I find this is certainly not true anymore.  With the hard work of the JetBrains team, improvements to Visual Studio itself, and increased performance of hardware over the last few years, ReSharper quietly works away in the background and is barely noticeable.  If ReSharper is slow for you, you may want to consider getting a new computer.  Enough said.

Moan: I don’t like what ReSharper does to IntelliSense.

JP says:  Personally, neither do I.  ReSharper out of the box turns off Visual Studio IntelliSense and enables its own version.  I’m sure it’s great and everything, but personally I’ve never quite been able to get used to it.  You can very easily restore the default IntelliSense by clicking ReSharper > Options > IntelliSense and select Visual Studio on the General option screen.

ReSharper Options

Be sure to restart Visual Studio for the change to take affect properly.

Convert loops to LINQ expressions and back again

When I was learning all about LINQ expressions, ReSharper helped me greatly.  ReSharper has a fantastic feature that rewrites your for and foreach loops into LINQ expressions.

Take the following example;

private static IEnumerable<Shortcut> Discover(string root)
    List<Shortcut> list = new List<Shortcut>();

<pre><code>foreach (string directory in Directory.EnumerateDirectories(root, &amp;quot;*.*&amp;quot;, SearchOption.AllDirectories))
    foreach (string link in Directory.EnumerateFiles(directory).Where(file =&amp;gt; _searchPattern.IsMatch(file)))
        list.Add(new Shortcut
                        Path = link, FileName = Path.GetFileNameWithoutExtension(link)

return list;


The method grabs a list of directories and iterates through them, grabbing each file in each directory and checking the file against predefined criteria.  There is nothing wrong with this code, but nested loops and get quite messy and hard to follow, especially if you’ve also got nested if statements (which is very common).

With 1 mouse click, the entire method can be refactored to a simple LINQ expression;

private static IEnumerable&lt;Shortcut&gt; Discover(string root)
    return (from directory in Directory.EnumerateDirectories(root, &quot;<em>.</em>&quot;, SearchOption.AllDirectories)
            from link in Directory.EnumerateFiles(directory).Where(file =&gt; _searchPattern.IsMatch(file))
            select new Shortcut
                        Path = link,
                        FileName = Path.GetFileNameWithoutExtension(link)

Much easier to read and understand for most people.  However, if you find yourself still struggling to get to grips with this style, the same 1 click will refactor the code back to its nested foreach loop style.  ReSharper will also do its best to add meaningful variable names (and it does a pretty good job).

ReSharper helps prevent multiple enumerations of IEnumerable collections

More and more these days, applications are written using Object Relational Mappers (ORM) and methods are written against interfaces to improve unit testability and separation of concerns.  ReSharper helps you identify performance implications of IEnumerable.

Take the following example;

IEnumerable<Shortcut> shortcuts = Discover(RemoteDesktopPaths);

if (shortcuts.Any())
    var first = shortcuts.First();

Essentially, every time you call shortcuts, it is going to be executed.  Meaning that, in the case of being mapped to a data store, you will execute the same query repeatedly.  Which is probably going to be an expensive operation.  ReSharper highlights the problem so that you know to resolve it.  (By say, casting IEnumerable to a List).

To var and back again

A very handy trick when trying to disentangle another developer’s questionable code, is the ability to convert a private field to var and back again.

Take the following example;

var shortcuts = Discover(RemoteDesktopPaths);

It is not clear by looking at this code what the return type of Discover is.  Visual Studio’s IntelliSense will show you the type if you hover over var, but for me this doesn’t go far enough.  If I put the cursor on var and hit alt-enter, ReSharper will automatically change var to the explicit type.  Likewise, if I have an explicit type that I want to change to var, I can hit the same keyboard combination.

Specify type explicitly

Possible ‘System.NullReferenceException’

An absolute diamond of a feature that has undoubtedly saved me from a house of pain over the years… ReSharper will identify and highlight code that can and probably will result in a Null Reference Exception.  Granted, its scope is normally limited to a single method, but is still infinitely useful.

Take the following example;

Possibly Null Reference Exception

Admittedly, the example is contrived, but not uncommon.  It’s easy to make subtle mistakes like this when writing code.  Sometimes you make assumptions that code is always going to be in a particular state, but there is always an edge case where this doesn’t hold true … and believe me, if it exists, the user will find it.

Go to implementation – Navigating your code

Go to implementation - navigating your code
Undoubtedly the most helpful code navigation feature In ReSharper, Go to Implemetation.

Unlike Go To Definition, which ships by default with most versions of Visual Studio, Go to Implementation takes you directly to the body of a method even when said method is on an interface.

Go To Definition in this scenario will only get you as far as the interface itself, meaning you have to go through and find the actual implementation, which isn’t always as easy as it sounds.  Go to Implementation is a real time saver.

Go to implementation – Diving under the covers

Go to Implementation takes things one step further.  Not only does it greatly simplify navigating your own code, it also allows you to go under the covers and have a look at other, previously inaccessible code.

Take, for example, the EnumerateFiles method provided by the Directory class in System.IO.  We did not write this method, we cannot (by default) see the code that drives it.   The .NET Framework as you may or may not know is open source, meaning you are free to read it (but not copy the code for your own use).  Normally, you would have to go and download the code, extract it, search for the file and open it direct.  Very long winded.  An alternative approach might be to use a tool like dotPeek to decompile the assembly and look at the code that way.  Again less than ideal.

Go to Implementation will automatically identify the assembly, decompile it, and display it in a normal code editor window for you to look at.

Click Go to Implemenation:

Go to implementation - diving under the covers

After a few seconds, the actual underlying code is presented to you as if it were part of your own project!

Under the hood

Find Usages

ReSharper has a feature to help find all the usages/references of a method/property/class everywhere in your solution.  Granted, Visual Studio also has this feature (Find All Reference’s), but ReSharper presents the results in a much more legible and therefore useful way.

Find all References;

Find all references

Find All Usages;

Find all usages

As an added win, if there is only one usage of the property/method/class in your solution, ReSharper will automatically navigate to it.

Redundant Code

One of my biggest (if not biggest) pet peeves when working with a team of other developers is regarding redundant code.  Redundant methods, properties, and variables to be precise.

Redundant methods are methods that are not called anywhere within the solution, or are unreachable.  Normally, these are private methods but the same can hold true for properties and fields.

Take the following class;

public class Environment
    public string Name { get; set; }
    public string PhysicalPath { get; set; }
    public string BackupPath { get; set; }

<pre><code>private void Save(object obj)
    using (Stream stream = new FileStream(&amp;quot;path.dat&amp;quot;, FileMode.CreateNew))

        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, obj);


The Save method is not being used, and it is inaccessible from the outside world.  It’s basically useles. So in the interest of keeping the code nice and tidy, this method should be deleted.  After all, if it were ever needed in the future, it should be in source control! (You should always use source control, there is no excuse!)

ReSharper indicates to you that this method can be safely removed by dimming the method/property name.  You can double check that there are no usages by using the Find all usages feature.

Automatically generate equality members

Undoubtedly a feature that will save you more time (and sanity) than any other, ReSharper can automatically generate all equality checking code in 2 clicks.

Take the following class;

public class Environment
    public string Name { get; set; }
    public string PhysicalPath { get; set; }
    public string BackupPath { get; set; }

I want to do the following;

  • Implement the IEquatable<T> interface so that I can have strongly typed equality checking
  • Override the standard Equals method to check for equality
  • Implement the equality operators (!= and ==)
  • Override GetHashCode (which is practically mandatory when overriding Equals)

To use, override Equals, set the cursor on the method name, and click Generate Equality Members.

Generate equality members

You are then prompted to select the properties/members you want to include, any additional options, and then click Finish.


All the code is generated automatically, and you’re free to get back to the more interesting stuff.

Add references to unknown assemblies

Quite often when writing code I find myself needing to add a references to an external assembly that I have not previously referenced.  What can I possibly mean by this? Well, take the following example;


I often make use of the application configuration file to persist settings in a simple way.  To access these settings, I need to use the ConfigurationManager.  Well, this class lives in System.Configuration.dll, which is not referenced by default.

Pressing alt+enter brings up the ReSharper menu, and hey presto! It knows that I need to add a reference;

Add a reference

To be completely honest, exactly how it knows this I genuinely have no idea, and granted it’s a bit hit-and-miss.  But when it does know what you’re talking about, it’s a massive time saver.

Convert full property to auto property

This is another feature that comes in particularly handy when dealing with legacy code.  ReSharper gives you the ability to refactor full properties to auto properties (and back again) simply by pressing alt-enter.  Take the following example;

private string _backupPath;
public string BackupPath
    get { return _backupPath; }
    set { _backupPath = value; }

It was very common to have code like this before the days of automatic properties.  Now this is considered dated, and it’s desirable to refactor.  But refactoring is time consuming and, quite frankly, boring.

To automatic property

With the end result:

public string BackupPath { get; set; }

Move string to resource

It’s becoming more and more common for developers to have to take into consideration localization/globalization when developing applications.  It’s no longer acceptable to write front end code in a single language.  ReSharper can help you here.

As part of the localization/globalization process, user facing strings have to be extracted to resources (and sometimes resource assemblies) so that they can be translated and the appropriate version displayed to the user in the appropriate language.

Move to resource

ReSharper highlights strings that can be extracted to resources, and gives you the option to move to a resource automatically using the good old alt+enter keyboard combination.

Move to another file to match type name

When developing new functionality (or indeed, updating existing functionality) it’s common to create new classes. There are a whole host of shortcuts available within Visual Studio to create these classes for you.  A particularly helpful and time saving feature provided by ReSharper is to move your classes to a new file within the same directory with a matching file name.

Take the following example;

public class Shortcut
    public Location Location { get; set; }

<pre><code>public string FileName { get; set; }

public override string ToString()
    return Location.Path;


public class Location
    public string Path { get; set; }

In the interest of not losing my train of thought whilst writing this code, I have added two classes to the same code file.  Nothing particularly wrong here, other than it’ll potentially make the class harder to find later on (especially by other developers).

Move to another file to match type name

ReSharper will pull out the class, create a new code file with the same name, and drop your class in there.  Job done.

Generate views (ASP.NET MVC)

One of my favourite features of ReSharper when developing ASP.NET MVC websites is around the creation of new views/navigating to existing views. Simply create your controller, and any missing views will be highlighted to you.

For example;

Create view

You can then use alt+enter to create the view, using your favourite layout engine.  Or, if the view already exists, you can simply ctrl+click on View to navigate straight to that view.

Code Quality Indicator Bar

Code Quality IndicatorReSharper has many built in code analysis tools to detect issues with code.  Warnings and errors are highlighted not only on the line that contains the error, but also on a handy overview bar that runs alongside the code editor window itself.

I find this particularly helpful when it comes to finding errors.  Often when working with large files, it’s good to see all the errors in the file as an overview.

Errors are highlighted in red, warnings (not build critical) in orange and suggestions/hints in green.  Hovering over the icon at the top of the bar reveals the number of warnings/errors and clicking scrolls the editor to the next warning or error.


JetBrains ReSharper has a lot of time saving features from; code analysis tools, navigation, refactoring and code generation … all designed to make you a more efficient developer.  Head over to JetBrains right now and get your free time limited trial, you won’t regret it!

I love to hear your feedback, please leave comments below if your enjoyed this post.