For my day-job, I am lucky enough to work with many, many developers. Many times they are sharing their screen, and sometimes I pair-program through a problem. I realized that I almost take for granted, how efficient I am with Visual Studio. I don’t mean that in some braggart way, what I mean is, I see a noticeable difference in how efficient I’ve become with using Visual Studio. This also isn’t accidental, I go out of my way to make sure I’m fairly proficient, and that the tool doesn’t get in my way.
Part of my motivation to pursue this, is my personality. I get frustrated when I’m trying to do something and the tooling is getting in my way!
I don’t think a lot of developers “move in” to Visual Studio and make a point to leverage it and be more efficient! So, for what it’s worth, here are some things that help shave hours and hours, and aggravation from my development experience…
Why is this important?
Well, first you only have a fixed amount of time on the planet, so don’t you want to get the most of the time you have? If there is little cost to adding efficiency, why NOT do it? The second major reason is I see many developers live in a semi-constant state of frustration – frustration that can be easily cured! Visual Studio can be your best friend or your worst enemy!
I’ve discussed before that I am a very big believer in having predefined “systems” for things. I didn’t necessarily go into it there, so I’d like to write down some of the specific, small things I do for efficiency which I argue saves me HOURS over the course of a week of coding.
No, wait – this isn’t a commercial. I cannot begin to tell you how many hours ReSharper has saved me in development. Wait, I can start to tell you – in fact, I wrote a blog post about it. Here’s: how I use jetBrains ReSharper. If you are a professional software developer, I can’t stress how valuable a tool this is. For renaming, refactoring, setting up constructors, fleshing out new data types, etc – it’s just an amazing tool. You should consider getting it (outside of work) and at work, ask you employer to get you a license!
ALT+ENTER with ReSharper will fundamentally change how you code – for the better!
Dealing with class overhead – “10 second rule”:
This is my concept that dealing with: constructors, argument validation, code comments, .Dispose(), or method overloads really shouldn’t take more than 10 seconds each – generally. This is overhead and you should have a system to deal with it effectively and quickly.
This came up earlier today. If I am creating a new method which takes in a reference type – I also want to check it for null, and “publish the exception” via XML code comments. I don’t want it to be a big deal, I just want to have it be part of the process and just a tiny bump in the road. So, for me, I:
- Add the argument to the method definition
- Go to the first line of the method, type “argnull” <TAB><TAB> type the first 2-3 letters of the argument name until intellisense finds it, <TAB> and hit <ENTER>
- I’m already on the right line, so I hit the <HOME> key, then CTRL+<RIGHT> to go over to the exception name, hit ALT+ENTER and add the exception to the XML code comments (via ReSharper)
It takes well under 10 seconds for all of that, for me. I go out of my way to make sure this is fast and easy to do. This is overhead, and I don’t want to invest/waste my time on it. It’s important, but it’s also simple, repeating code which simply needs to be there but doesn’t require much additional thought.
If you don’t have a code snippet for checking for null arguments and don’t have ReSharper – then you are talking about maybe a minute or more (or much more) with: “What’s the syntax of that ArgumentNullException constructor again? Is it message, then param name or vice versa? Let me go find where else I had this and I’ll copy it… where else did I have this?” – then woops, a typo, then “meh, I don’t have code comments yet, so I won’t publish the exception.” Next thing you know, you’ve spent many minutes on something that should’ve been just a blip!
So, the alternative is it took 20x as long to do, you don’t have XML code comments, and worst-of-all you are completely side-tracked from what you were working on.
My whole approach for: constructors, argument validation, method overloads, handling .Dispose(), XML code comments is all about “under 10 seconds (or so)” so that I can get back to the meat of what I was working on. If these overhead things are bogging you down or worse, you are not doing them because they are tedious and time-consuming, you are doing something wrong! Visual Studio can (and should) help you! Find a system, use snippets and be efficient!
Here’s one more quick example – what about when you have constructor-injection? You want to pass in an argument, check for null, assign it to a property, and publish the null argument exception. How long would it take you to turn this:
I did this just now in about :20 seconds. I used a combination of code snippets and ReSharper:
- Created the repository argument
- Hit ALT+ENTER to “Initialize auto-property from parameter”:
- Changed the newly-created property setter to protected.
- Type “///” before the constructor to auto-create XML code comments and fill it out.
- In the constructor, typed “argnull” (my custom snippet) then <TAB><TAB>, typed repository and <ENTER> which handled my argument validation. This created my null check.
- Move my cursor to ArgumentNullException and hit ALT+ENTER to “Add exception description” to the XML code comments:
- Now, I have a proper, FXCop-compliant method with very, very little effort:
and we even see the “published” exceptions in Intellisense:
Hopefully you get that this is not about “who’s fastest!” This is all about getting the mundane work out of your way so that you can get to the business of writing your real code.
I used a combination of ReSharper and code snippets here to save me a few minutes of typing. This is just one method in one class on one day of coding. It really can add up over time!
Snippets are a facility in Visual Studio which lets you insert a block of code which often has replaceable “tokens”. The purpose being that you can create repeatable code blocks for things you use all the time.
How do you create and use snippets? See this blog post on it.
Most-importantly, and I see this all the time, when you use a snippet – TAKE YOUR DANG HAND OFF THE MOUSE! The whole point of a snippet is that you can quickly crank out code with the keyboard, and when you are done, you hit <ENTER>. If you click anywhere or try to click around, that messes up that feature. You TAB and SHIFT+TAB to navigate between all the tokens for that snippet, and hit <ENTER> when done!
Using the keyboard:
What I mean here is there are lot of shortcuts I use on a regular basis. For example:
- CTRL+M,O – rolls up all of the definitions in a class, making it so you can see the class at-once, in one view. This let’s be get the big-picture of the class or let’s me quickly navigate to members. CTRL+M,L does the opposite (expands all of the collapsed regions).
- CTRL+K,C – comments the current selection. This is true for ANY window you are in: code, HTML, XML, CSS, etc! CTRL+K,U does the opposite
- F12/Go To Definition – instead of hunting in Solution Explorer or looking for the open document, if you have the type you want in the editor, put your mouse cursor there and hit F12 (or right-click and choose Go to definition) to open that code file. CTRL+- brings you back to whatever file and location where you just were – like a “Back” button.
- CTRL+TAB – just like ALT+TAB in windows, let’s you scroll through all of the open windows. CTRL+SHIFT+TAB scrolls backwards
- HOME and END – this is especially useful when selecting text. How many times did you have text that went off the screen and you tried to use your mouse to select and the screen flashes because you went down to the next line. That’s crazy! Hold down the SHIFT key and hit END on your keyboard – that will select to the end of the line. Need the next line too? While holding the SHIFT key, hit the down arrow, then END to select to the end of the next line.
- UP and DOWN – moves your cursor obviously, but CTRL+UP and CTRL+DOWN scrolls the current file while keeping your cursor where it is!
- CTRL+. – this brings up the context menu for squiggly-line items (well, in C# at least). If you have a type where you need to “resolve” a Using/Import statement, this allows you to do that. If you have your cursor on an interface which is not implemented, this key combination will give you the option to Implement Interface. CTRL+. is basically the “right-click” option for a given keyword.
- CTRL+J brings up the intellisense for the item where the cursor is.
Using the mouse:
Another area where I try to leverage the IDE is taking advantage of the mouse features. For example:
- Double-click a word or variable and it selects it. Need to replace five “firstname” variables with “firstName”: double-click the correct one and do CTRL+C to copy. Now, for each of the bad ones: double-click, CTRL+V… double-click, CTRL+V, etc
- CTRL+click also selects a word or variable.
- Move a line or block of code with your mouse. With text selected, simply click and hold anywhere inside the selected text and drag it to another part of the screen!
- Copy a line or block of code with your mouse. Do the same thing as move, but hold down the CTRL key as you let go of the mouse drag! Want to dump out a bunch of variables to the console? Create several: Console.WriteLine(“”); then you can CTRL+click the variable, CTRL+drag it to the first “”. CTRL+click the next variable, CTRL+drag it to the next “”.
- ALT+select with mouse. I think it’s called “vertical selection”. You can click on a position in the editor, hold down the ALT key and drag down a few lines (or select text vertically and horizontally). That in itself might be useful, but the real power is that if you type, your typed text will appear on ALL of the lines that were selected. This becomes useful if you had to add a prefix to 7 variables on 7 lines. You can vertical-select the position just to the left of all of those variables and then type the prefix – it will be typed in front of all of them at the same time.
Using the screen:
Perhaps one of the more frustrating things for me to see when working on someone else’s machine is when we are forced into a small window on the screen. Two common scenarios are the Properties window and the Watch window.
How many times are you spending more than a minute in the property window and working like this (note the bottom right):
Literally working in 1/20th of the screen real estate, constantly clicking on scroll bars!! Instead – one of the first things I do is MOVE the Properties toolwindow to be at the same level as Solution Explorer so that it’s just another tab. You can click and drag the Properties title window to do this. So instead of this:
I do this:
Ahh… much better! Now the Properties window is a First Class Citizen and has plenty of room to breathe.
Another common scenario is the Watch window while debugging – which often looks like this (note the bottom left):
Again, right while you are in the middle to troubleshoot something, you are working on a small fraction of the screen, fighting scrollbars, constantly re-sizing the columns, etc.
Instead, and this is personal preference, I only ever look at one of those tabs at a time while debugging. SO, I merge ALL of those bottom tabs into one window. So instead of this:
I do this:
Much better, right?
I also see many people simply close all of these tool windows. I don’t like that either because then you are constantly up in the View menu looking for the menu item to re-open the window. You can dock, show, and hide any of these windows in any which way you can imagine… so DO IT!!
The bottom line for me here is that if you are spending 8hrs/day in Visual Studio, make it your own! So much of it is configurable. You can configure: toolbars, keyboard shortcuts, window location/docking/hiding, code snippets, etc. Hopefully some of these things help. I recover a lot of wasted time in life simply by bending Visual Studio to my will – it is so configurable!
I will continue to look for efficiencies and try to streamline my development activities. I hope some of these help you and if you have other shortcuts or tips, please share them in the comments below!