How do I make TDD in Visual Studio less painful? - visual-studio

I've become a bit of a TDD zealot lately. Explaining the concept isn't the hard part. Most people respect the purpose behind it. However, when I try to demonstrate the actual process behind the idea, the biggest complaint is:
"When I'm writing my initial tests, I hate how Visual Studio intellisense
tries to guess what I'm doing. It's obviously going to be wrong because I haven't
written the API yet. It takes me out
of context when I have to fix all the things that
Intellisense has guessed incorrectly."
which, unfortunately often comes out as:
"TDD (or Visual Studio) sucks"
It's never really bothered me before (I just delete the stuff it guesses wrong), but I see the problem. What has everyone else done to get around this issue? Obvious answers are:
Turn off intellisense when writing tests, then turn it back on when writing the API.
Write your tests in something like Notepad++, then copy into VS when you think you're happy
Stop whining
P.S. I looked everywhere on SO (and elsewhere) for this question so feel free to flag as duplicate if I missed it... As if you needed my permission ;-)
Edit: And, yes, I have Resharper, it's awesome.

Learn to use the Esc key and start to think about what you are programming. Intellisense is a double edge sword in that it both speeds up your coding but it also causes people to stop thinking about what they are doing. TDD is all about thinking, not mindlessly allowing the IDE to do your work for you. When the little pop-up Intellisense box appears, just hit Esc and it goes away without filling anything in.
Also, get Resharper. This is absolutely mandatory for programming in Visual Studio, but doubly so for TDD.
(Btw, Visual Studio does suck).

Learn to ignore the Intellisense, and get ReSharper. Then write the code you want and let ReSharper create the shells into which you'll eventually author your implementation code.

Err... Correct me if I am wrong but I personally love intellisense on the fly code analysis. Especially in conjuction with Resharper it makes TDD a real fun process. You write a test with not yet defined classes and methods, then extremely fast generate stubs using Resharper, make it compile, then add functionality to the point when your test runs successfuly, then you refactor as needed and rinse and repeat.

Turn Intellisense off temporarily.
From the menu it's: Tools..Options..Text Editor and then turn off "Auto List Members" and "Parameter Information".

And if your company is willing, have them purchase NCrunch extension for you guys. If they are not, try convincing them. It is a concurrent test runner and it cuts traditional TDD time in half (for me). Not having to go back and run the test after writing the implementation, or getting what the exception (assert exceptions) are right on the editor, and getting feedback right away as you write your code is simply amazing! I think this tool makes a switch to TDD much easier. I feel TDD with RGR approach is quite tedious and time consuming (without NCrunch), very much worth it though. As a point in Joel Test says, "buy the best tools money can buy" (I am aware of company budget approval is another tedious thing).

Related

Is it possible to Edit and Continue in Visual Studio 2010 without pausing execution?

I recently watched a bit of Notch's Ludum Dare live stream. He uses Eclipse's hotswap feature extensively while he works on his game. (here is a video of what I am referring to http://www.twitch.tv/notch/b/302823358?t=86m30s)
I would like to do the same with C# and XNA. Fortunately, Visual Studio has an Edit and Continue feature. However, I would like to edit my rendering code while it is running instead of pausing it.
Is it possible for me to setup Visual Studio to do the same? I noticed that there is a checkbox for Break all processes when one process breaks. Is it maybe possible to set up my rendering loop in another thread so that the game keeps rendering while I make a change?
Update: This answer is now available as a video.
I've been struggling to find a way to do this. I know this doesn't answer your exact question. But really what you are looking for is a workflow where you can make code changes with zero (or near-zero) delay, and I think I've figured out the closest you can get with just Visual Studio. (And therefore avoiding a huge engineering effort and dealing with "abnormal" projects).
The way to achieve this workflow is actually astonishingly simple, once you think of it:
Use shortcut keys!
The first way I came up with is to just use the normal edit-and-continue method of setting a breakpoint. Only by using the keyboard you can do this considerably faster. This only works with code being called in a loop (eg: draw/update). Click the code you want to modify, add a breakpoint (F9), the breakpoint will almost immediately be hit, modify your code, remove the breakoint (F9), and then run the code again (F5).
This is pretty good. You don't have to use the mouse to hit the relatively small "Add breakpoint" target in the left hand column. But it does move the input focus to the beginning of the line, so you generally have to use the mouse again to fix that before you can start editing.
I want something faster. So I came up with a better solution:
Again, using the keyboard: Press Ctrl + Alt + Break to "Break All". This enters the debugger almost instantly, without having to worry about setting a breakpoint or if the code you want to modify is running in a loop. This will change the editor window and caret focus to the document where execution break, but you can then immediately fix it by pressing Ctrl + - for "Navigate Backwards".
Then you can make your edits and simply press F5 to see them in action. You only have to use the mouse once (or not at all) to initially pick where you want to start typing - just as you would expect.
Admittedly Ctrl + Alt + Break and Ctrl + - are horrible key combinations for something you want to be able to do extremely quickly. And it would be better if there was just one key to press.
If you have the full Visual Studio, you could probably turn it into a macro or add-in. Express doesn't have those - so the best you can do is modify your key bindings (Tools, Customise, Keyboard...) and bind it to two keys that are adjacent, that you can press in quick succession. Or use an external macro utility.
Personally I have set up the two key combinations to be pressed in succession (you don't seem to need a delay between the two) by a macro set to a spare button on my mouse. Which works reasonably well - as I'm usually selecting text at the same time. I might add a keyboard macro later too.
So far I've identified two minor pitfalls of this method:
When you run the app again, Visual Studio gives it focus. It would be nice if it kept focus. Adding a left mouse click to my macro is a partial solution to quickly re-editing code.
"Navigate Backwards" does not retain the text selection, only the caret position.
i can tell you how, but you arn't going to like it:
1) run your game executable in a visual studio instance (game.exe --> vsInstanceA)
2) code your modifiable code in a seperate dll, using a seperate visual studio instance (modifiable.dll --> vsInstanceB)
*note that doing this lets you compile your modifiable.dll, thus doing compile time error checking, etc.*
... and now is where it get's tricky ...
3a) game.exe needs to reference the modifiable.dll. NOT the .vcproj, the actual dll
3b) when you hit a "magic key" (have game.exe look for a key-press), have game.exe unload the modifiable.dll, and reload it. You can easily do this via the Assembly and AppDomain classes provided in mscorlib. Of course this means you need to unload any dependant systems in game.exe.
note, there's a lot of hand-waving in this 3b) section, it's quite a lot of work but pretty straight forward (example google search: https://www.google.com/search?q=dynamic+load+dll+.net)
... and after that, you are good to go ...
3-alt) some other choices if 3b) doesn't suit you:
you can invoke msbuild.exe to rebuild modifiable.dll when you press
"magic key"
you can use the CSharp compiler dll's to dynamically
recompile each class instead of the entire modifiable.dll
but unfortunatly, in my 10+ years of .net development, this is the only way, unless someone has created a 3rd party product to do it (IE: they do what i mentioned, for you)
This may not be exactly what you're looking for but it's what I've been doing. Just throw in a break point while your game is running. Once it's stopped, you can edit things, remove the break point, and then hit F5 to continue.
For the most part, this allows you to edit things while the game is running and continue running the game afterwards but it doesn't work for everything. You can't add or remove class level things and also can't add/remove entire functions or the parameters. Mostly, I do this to test out different numbers and equations for smaller things that get hit every update.
To answer the original question "in Visual Studio 2010 without pausing execution?"
No. For Java there is JRebel which allows this.
I can quickly imagine why Microsoft haven't done this - if you have some sort of committee or group of people designing things - it's easy for even one person to come up with good arguments where this "dynamic software updating"/hotswap will break. It's just too easy to shoot down. Or if they proceed with it, it will be through some entirely new "enterprise framework" that requires lot of steps, work and understanding to get anywhere, because they want to use it for complicated online scenarios.
I'd prefer there was some simple way to do this - I'll take the risks, and put up a lot of disclaimers that if you want hotswap services that talk to external systems then you need to use that complicated framework or whatever.
The most typical scenario where this could safely be used with low risk is prototyping and tweaking some sort of engine with either high startup cost or more likely, to study behaviour if something running in the engine (like rules) is buggy and to exhibit the bug, a lot of things has to happen and it's possible you don't encounter the bug always. That is the case where having this could save weeks of debugging time, atleast would have in my case.
I'm used to work in Java using IntelliJ Idea + DCEVM (Dynamic code evolution VM).
Unfortunately i haven't found anything getting closer to that configuration at Microsoft C# platform. It really sucks and you can't do much about it. You can use some user defined macros, but you can't still do much in Edit and Continue mode without restarting Debug session).
However if you can, try to use newest .NET Core which offers much better productivity using Dotnet Watch. Still not so fast as DCEVM but much better than traditional VS + .NET hot reload experience. Details here.

Visual Studio Test Pro + TestComplete

So first off, I did a terrible job researching Test Pro before talking bosses into buying it.
The intent was to start using it on some of our SL apps since TestComplete has been troublesome for our tester.
My mistake was assuming and not looking to confirm if Coded UI tests where part of that package. I have to say I really like all the features, but it seems silly not to include the part need for true automation. So enough self pity and ranting. :-)
Has anyone had any good or bad experience combining TestComplete scripts in with VS Test Pro 2010? I would like to salvage something out of my screw up, but I'm afraid it may complicate the overall process to a point where we're not gaining anything.
Also I would like to avoid buying another VS Premium.

How do i implement intellisense for my language in visual studio?

I mention that i am designing a language. The plan was always to not implement it but to design it but i am considering implementing it if i think i could do it in a reasonable amount of time.
How would i have my language use intellisense in visual studios? BooLangStudio has it http://www.codinginstinct.com/2008/05/boo-in-visual-studio.html, http://www.codeplex.com/BooLangStudio
It all depends on how much time you want to spend on it. I have about 2,000 hours of work in my Visual Studio IntelliSense projects resulting in exactly one mostly-complete language service. That said, it's not your "average" IntelliSense extension to Visual Studio - see the feature set for more info.
Here are some good resources to look at. I have a tendency to write with an assumption that users are already familiar with both the Visual Studio Extensibility basics and parsing with ANTLR. If you aren't, you should probably start at www.antlr.org and with my "ANTLR port" of one of the simple Visual Studio language service tutorials.
Here are some posts showing how serious I am about the subject. :D
How does code completion work?
Smart code-completion original and revisited. Clearly I take the latter more seriously than other people (voted down and voted to close?!), but I believe doing so just gives my users a better product. :)
High-speed incremental lexing for syntax highlighting original (under IScanner-friendly lexers), cleaned up (significantly - vastly preferable to the original), and made yet again 6x faster in 1/4 the memory.
Little things like sane commenting/uncommenting and brace matching do make a difference. On a side note, once you use one that's well-behaved, the others (including the ones for some Microsoft languages in Visual Studio) are rather annoying.
If you can, read all the IntelliSense-related posts on my blog.
Smart indent is a PITA (I mean really smart, e.g. the C# language service in VS2008). I have a love/hate relationship with it. I'm annoyed now because writing this bullet made me think about it. Argh. My insight here makes me a bit forgiving of them breaking it for VS2010 Beta 1, but it doesn't keep me from missing its awesomeness.
PS: I can now build a syntax highlighter for a new language commenting/uncommenting in 1 day. In the same day I'm ofter able to get the type & member bars in as well.
This is a good place to look for Visual Studio Extensibility.
Also, here. But, the first link has a video specific to adding Intellisense to your language service.
As of 4-4-2017: This article seems to give all of the basics for creating a language extension, which includes highlighting words and providing intellisense (code completion).
https://code.visualstudio.com/docs/extensions/example-language-server

Do you find source code analyzers useful?

Do you use source code analyzers? If so, which ones and for which language development?
Do you find them helpful in solving potential bugs in your code? Or are most of their warnings trivial?
After prolonged use, do you find your code quality to be higher than before?
I use a few static analysis tools in Java. FindBugs is the first line of defense, catching a lot of common errors and giving pretty useful feedback. It often spots the silly mistakes of tired programmers and doesn't place a high burden on the user.
PMD is good for a lot of other more niggly bugs, but requires a lot more configuration. You'll find that PMDs defaults are often over the top. There are too many rules that are probably beneficial on a tiny scale but ultimately don't help other programmers maintain your code. Some of the PMD rules often smack of premature optimisation.
Probably more useful is the CPD support in PMD. It attempts to find code that has been duplicated elsewhere, in order to make refactoring much easier. Run over an entire project, this really helps determine where the biggest priorities are for cleaning up code and stopping any DRY violations.
Checkstyle is also handy, making sure your coders conform to some coding style standard. it has a bit of overlap with PMD but is generally much more usable.
Finally, Cobertura is a great test coverage suite. Very handy for finding out where the unit tests are lacking, and where you should be prioritising the creation of new tests.
Oh, and I've also been testing out Jester. It seems to be pretty good for finding holes in tests, even where the code has some coverage. Not recommended yet, simply because I've not used it enough, but one to test out.
I run these tools both from within Eclipse and as part of an automated build suite.
For C, I use MEMWATCH. It's really easy to use and free.
I've used it to find many memory bugs in the past.
I used resharper and MS TS (basically FXCop) and both of them quite usefull especially in the following areas :
Identifying dead code
Wide Scope
Performance improvements (related with globalization etc.)
Recommendations are not always great but generally improved the quality of the code.
I'm a long term user of PC-Lint for C and C++ and find it very helpful. These tools are most useful when taking over a code base you are unfamilier with. Over time you hit a law of diminishing returns, where the number of new bugs you find tends to trail off.
I always still to a full project lint on a big release.
Edit: There is a nice list of relevent tools on Wikipedia here
I'm pretty happy with ReSharper. Not only does it give useful bits of information while coding (e.g. needless casts, apply readonly and so forth) but its refactoring features are excellent for rearranging the code very quickly.
It doesn't cover everything, so FxCop (or similar) is a decent addition to the toolbox. However, as Resharper gives immediate feedback, the turnaround time is really good. (I'm aware that FxCop can be run from VS, but its just not the same imo).
I find analyzers somewhat useful, i use the buildin to visual studio (ex. /analyze for c/c++ and the custom rules for .net), occasionally i use stylecop and codeitright for c# mostly for guidelines how things should be.
I don't think there is a perfect tool for everything, that finds every bug, but i think the tools help to find some bugs, not untraceable, but believe me you would spend a ton of time finding them.
Yes your code quality is SOMEWHAT better than before, but i also believe manual debugging is still needed alot. Source analyzers are not the ultimate cure they are a good medicine though. If there was a tool that you just execute it and find any kind of bugs and fixes it for you would cost millions.
Some programmers that i know swear that IBM Rational PurifyPlus is superb, but that is their opinion i just had 2-3 sessions with the tool.
But always remember one of the basic principles of programming logical errors are the hardest for find and fix, so long debugging hours are inevitable. A good code analyzer combined with unit testing may work miracles thought.
PS. i tend to produce far less errors in C# than in C++, someone may say i am wrong but although i use c++ more years than c# i find the "code it and i will take care of it" gc approach of C# far easier than c++ especially for projects you rush thing to finish at the time limit/deadline, which EVERY project is like this days...
I use StyleCop for C#. It's a great tool to keep consistent code style that leads to better code quality. Also ReSharper does some code analysis but it's pretty basic.

DevExpress Refactor Pro vs JetBrains ReSharper

In my department, we are currently using ReSharper 4.0 and deciding whether to upgrade to 4.5 upon its release next week. I personally am a huge fan of ReSharper however a number of my colleagues have pointed out that they have been using a plug in from DevExpress called Refactor Pro that performs similar functionality.
http://www.devexpress.com/Refactor
http://www.jetbrains.com/resharper/beta.html
Has anyone previously compared these tools and hold any strong views on which tool would give us the greatest increase in productivity and why?
In my department, we also use ReSharper. Today, I installed 4.5, but had already used 3.something, 4.0 and 4.1 before. It really offers many great refactoring and code-writing supporting functions, renaming methods and functions, reordering parameters...
What I really like is that according to your corporate code style, you can configure ReSharper to give you hints on style violations in different severity levels (and quickly apply according changes, like MS StyleCop, but much easier to configure and more subtle).
My absolute favorite feature is Class-Searching by entering only the CamelCases, i.e. you type TSHWLOV and ReSharper will know that you mean the class from some referenced library named 'TerrificSearchHelperWithLotsOfVoodoo'.
Last year I have tried the DevExPress CodeRush/RefactorThis-Alternative, after I was quite impressed by the things that Oliver Sturm did with it on BASTA Spring 08.
The interface catchier and more impressive than Resharper, there are huge arrows flipping around your IDE and things like that, though the core functionality is rather similar.
I had the feeling that CodeRush is more focussed on code creation than on refactoring, i.e. more shortcuts for tasks like creating variables etc.
My favorite feature there was a sidebar, which always shows you all keyboard-shortcuts available in your current context. This makes you learn those commands quickly, where in ReSharper you have to look up most of them in nested submenus.
Both suites are really powerful and it after months of using them you will probably still discover new functions, which you have always needed without knowing it.
However, I decided in favor of ReSharper mostly because of I was more familiar with it and DevExpress was using much resources on my notebook and occasionally even slowed it down. By now, I use a much more powerful machine again, maybe I will give it another try soon.
I personally prefer CodeRush.
I find R#'s interface drives my (admittedly minor) OCD tendencies crazy. The little lightbulb insists on appearing on the far left of the screen, even if it's advice pertains to something in the middle or on the right. I find it garish and distracting.
By contrast, CodeRush's equivalent smart tag is lower contrast and smaller. It is therefore capable of locating itself within the code without distracting from said code. I find I can ignore this SmartTag when I need to, and it is always right in front of me when I decided I need it.
It is mainly this, that has prevented me from wanting to explore R# any further.
As far as CodeRush's feature set: Code Analysis, Refactorings, CodeProviders, Templates, TabToNextReference, QuickNavigation and many more.
I especially like CodeRush's extensibility which has allowed myself and several others to create many plugins for use within it. (http://code.google.com/p/dxcorecommunityplugins/)
CodeRush also has some top quality support and a very active community of users.
Certainly neither R# or CodeRush will suit all users. Every one has their own preferences. However, if you've not tried both, you should certainly do so.
If you come from the R# side of the equation and are looking to test out CodeRush, then you may find the compatibility plugin useful (http://code.google.com/p/dxcorecommunityplugins/wiki/CR_ReSharperCompatibility)
In addition CodeRush has a Free edition 'CodeRush Xpress' which Microsoft commissioned DevExpress to create, and which they have licensed on behalf of every user of Visual Studio 2008. This partnership will continue into VS2010 upon it's release.
If you have any questions regarding CodeRush you can find details for contacting me at the bottom of my community wiki page.
I should say that I do not work for DevExpress. I am what you might call a DevExpress MVP. To maintain this position, I answer questions in the DevExpress forums (and nothing else).
Everything I say is my own honest opinion.
If you have any questions, feel free to contact me :)
I'm using DevExpress which has a lot of "hidden" functionality, so you need to read the manuals to know how to activate some of the functionality. I find it very passive and subtle.
I used ReSharper and found it buggy and very invasive in my coding style. Bracket-closing drove me nuts, it ignored my preferences and couldn't cope with how I write my lines of code - which may not be most efficient but it's one of those things that ain't gonna change!
I've tried both, and really didn't get along with ReSharper. I found it to be just too intrusive for my coding style. When I switched to CodeRush / Refactor! it was like I'd found the perfect aid to my productivity. The refactorings are, for the most part, exactly what I wanted to see from this kind of product.
It is, however, horses for courses and you may well find that you prefer ReSharper. The best advice I can give is to try the other products and see which you prefer.
The only correct answer is to use both, of course! I do. You need a beefy laptop though. If I had to choose only one, I'd choose ReSharper... I think the static code analysis is a lot better.
ReSharper has a lot of great features, and DevExpress has a lot of great features.
When you put the two together, you end up w/ whole lot of AWESOME.
You need to jump through a few hoops to make them play nicely together: http://frazzleddad.blogspot.com/2010/01/making-devexpress-resharper-play-nicely.html
R# 4.5 is a free upgrade if you have a 4.0 license. So I'd suggest to get and use it - changing the "productivity tool" is always a pain because you'll have to get used to different ways, keyboard shortcuts etc. of doing things - no matter how good the tool actually is.
I have a personal copy of Refactor Pro but I use R# 4.1 at work with the StyleCop add-in as that is the team standard tool. I like RfP's arrows and code positioning stuff which is better than R#. Otherwise the tools are very similar.
However, at this time, its the StyeCop add-in that swings it in favor of R# for me.
Cheers
Benjy

Resources