views:

85

answers:

2

So Jeff Atwood rightly complained about Visual Studio not performing background compilation see: http://www.codinghorror.com/blog/2007/05/c-and-the-compilation-tax.html

The solution from most sources seems to be Reshaper which will incrementally perform background compilation as you write. This leads to their great realtime re-factoring tips and error detection.

But what I don't understand is with R# continually compiling my code, why does it take so long when executing a compilation via VS (i.e. Ctrl + Shift + B or similar). What I mean by this is, if R# has already compiled my code then why would I need a recompilation?

My assumption is of course that R# is not overriding the assemblies in my bin directories but instead holding the compilation results in memory. In which case, is it possible to tell R# to simply override my assemblies when compilation is successful?

+4  A: 

I don't know about "rightly complained" - that's an opinion I happen to disagree with:)

However, the VB.NET (and probably Resharper c#) background compilers do not actually compile full assemblies - they cannot! If you think about it, the natural state of your code while you are working is not compilable! Almost every keystroke puts your code in an invalid state. Think of this line:

var x = new Something();

As you type this, from the key "v" to the key ")", your code is "wrong". Or what if you are referencing a method you haven't defined yet? And if this code is in an assembly that another assembly requires, how would you compile that second assembly at all, background or not?

The background compilers get around this by compiling small chunks of your code into multiple transient "assemblies" that are actually just metadata holders - really, they don't care about the actual effects of the code as much as the symbols defined, used, etc. When you finally hit build, the actual full assemblies still need to be built.

So no, I don't believe it's possible because they're not built to do actual full compilation - they are built to check your code and interpret symbols on the fly.

Philip Rieck
+1 Also, doesn't 2010 now have background C# compilation?
Kent Boogaart
actually, it may have been introduced in vs2008 SP1, if I remember correctly (which I never do)
Philip Rieck
So when you say small chunks of code what exactly do you mean? In my mind, the minimum required to determine the validity of a class is the classes code itself and any external dependencies required by that class. i.e. if ClassA news up a ClassB will R# attempt to compile just these two class files?
Owen
+1  A: 

Reshaper which will incrementally perform background compilation as you write

It doesn't, it just parses the source code. The exact same thing Visual Studio already does if you don't have Resharper, that's how it implements IntelliSense, its own refactoring features and commands like GoTo Definition and Find All References. Visual Studio also parses in the background, updating its data while you type. Resharper just implements more bells and whistles with that parsing data.

Going from parsing the code to actually generating the assembly is a pretty major step. The internal format of an assembly is too convoluted to allow this to happen in the background without affecting the responsiveness of the machine.

And the C# compiler is still a large chunk of unmanaged C++ code that is independent from the IDE. An inevitable consequence of having to have the compiler first. It is however a stated goal for the next version of C# to provide compile-on-demand services. Getting true background compilation is a possibility.

Hans Passant
What do you mean it just parses the source code, as in just the file in the IDE tab/window? I can't see how this is enough for it to make the re-factoring recommendations that it does? Same goes for intellisense, without knowing what methods hand of a dependency how can it bring up accurate intellisense? The IDE must have parsed the dependent classes/interfaces to know that these methods exist, or am I missing something?
Owen
Yes, they both know how to read the metadata from the reference assemblies.
Hans Passant
Both what? Erm, what?
Owen
Link: http://en.wikipedia.org/wiki/.NET_metadata
Hans Passant