Saturday, April 14, 2012

Coalesce and Inline Operators

As part of a platform review, I have been seeing a lot of code lately that has been in production for a while.  I know that sometimes people are just creating the code they know how to create and making the project run.  That is all good.  Sometimes it is good to try and learn a better way way to do some very basic things.  I know that personally the following options make code a bit more easy to follow and maintain.

Coalesce
There are a lot of long if statements that seem to be looking for the first usable value. (this is a composite of the idea...)


if (value1 != null)  
    variable = value1;  
else if (value2 != null)  
    variable = value2;  
else if (value3 != null)  
    variable = value3;  
else  
    variable = DefaultValue;  

Just to simplify and make more readable, try using the ?? (coalesce) operator.  Notice the nullable type restrictions, etc.


variable = value1 ?? value2 ?? value3 ?? DefaultValue;   


Inline If
Here is another one (again, just a composite)

if (something == somethingelse)  
  value = "1";  
else  
  value = "2";     

try making this one line, more readable.

value = (something == somethingelse) ? "1" : "2";      


I know that these are some basic ideas, but judging by the code bases I have been checking out, they are not widely used.  Perhaps, looking into some of these items as best practices would be beneficial.

Thursday, April 12, 2012

Visual Studio IDE Performance

Here are a few tips I found online for speeding up Visual Studio 2010 day to day while working.

Faster builds by unloading projects

A quick way to speed up your build process is to unload up-to-date projects that you are not working on at the current time. Just right-click the project in the Solution Explorer and choose Unload.

Building / Running
When you debug an application Visual Studio compiles the projects, then launches the project and attaches it to the debugger. You can lower the number of projects that Visual Studio builds during this process by checking the "Only Build startup projects and dependencies on Run" box under Tools|Options.

General IDE Stuff
By changing a couple of options in the code windows you may be able to speed up the IDE as far as just being responsive. You can try some of these and see what happens. They can save some time depending on how much you have open, etc.
* Disable Track Changes. (Tools\Options\Text Editor\General\Track changes)
* Disable Navigation Bar (Tools\Options\Text Editor\C#)
* Disable "Track Active Item in Solution Explorer" (Tools\Options\Projects and Solutions)
These switches are a trade-off sometimes. The track changes item will force you to manually navigate to your current file in the solution tree, etc.
Detect when file changes

In projects with tons of files, you can gain some speed by disabling the "Detect when file changes" option.
Start Screen

Visual Studio opens faster if you disable the start screen option. To disable the Start screen and just open to an empty environment, uncheck the "Show Page on startup" checkbox on the start page itself. This results in a pretty quick open.