The opinions expressed in these materials are my own and are not necessarily those of Microsoft.
Copyright © Microsoft Corporation. All rights reserved. Unless otherwise indicated, all source code provided is licensed under the Microsoft Public License (Ms-PL).
SKU: Premium, Ultimate
Versions: 2008, 2010
As we continue looking at code metrics in Visual Studio (see vstipTool0129, “Calculating Metrics”), it’s time to look at class coupling. Class coupling also goes by the name Coupling Between Objects (CBO) as originally defined by [CK94]. Basically, class coupling is a measure of how many classes a single class uses. A high number is bad and a low number is generally good with this metric. Class coupling has been shown to be an accurate predictor of software failure and recent studies have shown that an upper-limit value of 9 is the most efficient [S2010].
According to the MSDN documentation, class coupling “[m]easures the coupling to unique classes through parameters, local variables, return types, method calls, generic or template instantiations, base classes, interface implementations, fields defined on external types, and attribute decoration. Good software design dictates that types and methods should have high cohesion and low coupling. High coupling indicates a design that is difficult to reuse and maintain because of its many interdependencies on other types.”
The concepts of coupling and cohesion are clearly related. To keep this discussion on topic, I will not get into depth with cohesion other than to give a brief definition from [KKLS2000]:
“Module cohesion was introduced by Yourdon and Constantine as ‘how tightly bound or related the internal elements of a module are to one another’ [YC79]. A module has a strong cohesion if it represents exactly one task […], and all its elements contribute to this single task. They describe cohesion as an attribute of design, rather than code, and an attribute that can be used to predict reusability, maintainability, and changeability.”
Let’s look at class coupling in action. First, create a new console application and create a new class called Person with some properties in it then immediately calculate the code metrics:
Notice the class coupling is 0 since this class doesn’t use any other classes. Now create another class called PersonStuff with a method that creates an instance of Person and sets the property values. Calculate the code metrics again:
See how the class coupling value goes up? Also notice that, no matter how many properties we set, the class coupling value just goes up by 1 and not by some other value. Class coupling measures each class only once for this metric no matter how much it is used. In addition, can you see that DoSomething() has a 1 but the constructor, PersonStuff(), has a 0 for its value? Currently there is no code in the constructor that is using another class.
What if we put code in the constructor that used another class? Here is what you get:
Now the constructor clearly has code that uses another class and the class coupling metric shows this fact. Again, you can see the overall class coupling for PersonStuff() is 1 and DoSomething() is also 1 to show that only one external class is being used no matter how much internal code we have that uses it.
Next, let’s create another new class. I’ll call my class Blah and create some properties inside it:
Now consume the class in our DoSomething() method within the PersonStuff class and calculate code metrics again:
As you can see, the class coupling for the PersonStuff class goes up to 2 and, if we drill into the class, we can see that the DoSomething() method has the most coupling in it but the constructor still only consumes 1 class. Using these metrics we can see the overall max number for a given class and drill down into the details on a per-member basis.
As I mentioned with cyclomatic complexity (see vstipTool0131, “Code Metrics - Cyclomatic Complexity”), there is no limit that fits all organizations. However, [S2010] does indicate that a limit of 9 is optimal:
“Therefore, we consider the threshold values […] as the most effective. These threshold values [for a single member] are CBO = 9[…].” (emphasis added)
When using code analysis, the Extended Design Guideline rule set contains a maintainability area:
Inside the maintainability area is a rule for class coupling:
This rule issues a warning when the class coupling is excessive. You can learn more about the rule here:
Unfortunately, the documentation doesn’t talk about the thresholds set for this rule but I did find a description on the Code Analysis Team Blog (http://blogs.msdn.com/b/codeanalysis/archive/2007/11/15/code-metrics-as-check-in-policy.aspx): “Warning at above 80 for class and above 30 for a method”. These seem abnormally high but at least provide an extreme upper limit. If you hit this warning then something is almost certainly wrong.
Chidamber, S. R. & Kemerer, C. F. (1994). A Metrics Suite for Object Oriented Design (IEEE Transactions on Software Engineering, Vol. 20, No. 6). Retrieved May 14, 2011, from the University of Pittsburgh web site: http://www.pitt.edu/~ckemerer/CK%20research%20papers/MetricForOOD_ChidamberKemerer94.pdf
Kabaili, H., Keller, R., Lustman, F., and Saint-Denis, G. (2000). Class Cohesion Revisited: An Empirical Study on Industrial Systems (Proceedings of the Workshop on Quantitative Approaches in Object-Oriented Software Engineering). Retrieved May 20, 2011, from Université de Montréal web site http://www.iro.umontreal.ca/~sahraouh/qaoose/papers/Kabaili.pdf
Subramanyam, R. & Krishnan, M. S. (2003). Empirical Analysis of CK Metrics for Object-Oriented Design Complexity: Implications for Software Defects (IEEE Transactions on Software Engineering, Vol. 29, No. 4). Retrieved May 14, 2011, from University of Massachusetts Dartmouth web site http://moosehead.cis.umassd.edu/cis580/readings/OO_Design_Complexity_Metrics.pdf
Shatnawi, R. (2010). A Quantitative Investigation of the Acceptable Risk Levels of Object-Oriented Metrics in Open-Source Systems (IEEE Transactions on Software Engineering, Vol. 36, No. 2).
Edward Yourdon and Larry L. Constantine. Structured Design. Prentice Hall, Englewood Cliffs, N.J., 1979.
Several people have asked me how to convert from VMWare to VHD format. Here are the steps to get you going:
1) Download VMDK to VHD converter free tool from http://vmtoolkit.com/files/folders/converters/entry8.aspx I had successfully converted disks 29Gb in size
2) “Prepare” your VMWare image by following steps in http://searchservervirtualization.techtarget.com/tip/0,289483,sid94_gci1239063,00.html or http://www.jaylee.org/post/2008/10/How-to-convert-a-big-VMWare-VMDK-into-an-Hyper-V-VHD.aspx
3) Install Virtual Server 2005 (Std/Ent, R2 or not – but if R2, select Enterprise and install the one with SP1 – it’s all free and on the web…)
4) Make sure your VMWare image contains at least one IDE drive before final shutdown (VS2005 will need it). Take note of drives inside VM (C,D,E…)
5) Convert each one VMW files into VHD
6) Attach “system” disk as a main disk in new Virtual Server image
NOTE: You could roll out Virtual Machine Manager and use the “conversion” feature in one step instead of six.
Thanks to Oleg Kofman from MS who provided me this info...
If you haven't come across this yet you will at some point. There is a known issue with Vista and activation. You can get the details for common errors at this KB article, but the steps are pretty straightforward:
1) Go to Control Panel...System and Maintainence...System
2) Scroll to the bottom of the screen
3) Click on Change Product Key
4) Type in your key
5) Click Next and follow the rest of the steps in the wizard
That's it! Soon you'll be enjoying Vista and be activated :)
Keyboard: F5; CTRL + F5Menu: Debug -> Start Debugging; Debug -> Start Without DebuggingCommand: Debug.Start; Debug.StartWithoutDebuggingVersions: 2008,2010Published: 11/1/2010Code: vstipDebug0037
While researching this one I came across a LOT of misinformation concerning what is actually happening here. There seems to be a great deal of confusion as to what is actually happening when you use Start Debugging (F5) versus Start Without Debugging (CTRL + F5):
Starting With Debugging
Let's start with the basics: When you press F5 (Start Debugging) in Visual Studio it launches your application, attaches the Debugger, and let's you do all the "normal" things you would expect:
According to the documentation (http://msdn.microsoft.com/en-us/library/k0k771bt(v=VS.100).aspx) here is what the Debugger does:
"The Visual Studio debugger is a powerful tool that allows you to observe the run-time behavior of your program and locate logic errors. The debugger works with all Visual Studio programming languages and their associated libraries. With the debugger, you can break, or suspend, execution of your program to examine your code, evaluate and edit variables in your program, view registers, see the instructions created from your source code, and view the memory space used by your application."
The Debugger: Release Builds
One popular misconception is the Debugger doesn't come into play for Release builds. This isn't true. Set a Breakpoint in some code for a Release build and then press F5 to see if is stops there:
Of course it does! The Debugger is attached! That is what is happening. Now, there are some things that aren't happening as well. For example, you can't use the System.Diagnostics.Debug class methods to send messages to the Output window since the compiler strips them out for Release builds:
Start Without Debugging
This is exactly what it sounds like. It starts the application WITHOUT THE DEBUGGER ATTACHED. That's it! Nothing else. It just doesn't attach the Debugger. Otherwise everything else is the same. So, the practical implications of this are obvious: Without the Debugger attached when the application runs it will not hit Breakpoints, emit Debug messages, etc. So now let's deal with the biggest myth about Start Without Debugging.
Myth: Start Without Debugging Creates a Release Build
Nope. It uses the build you are currently on. So if you are using a Debug build and you press CTRL + F5 then it will run that Debug build. The easiest way to test this is to use conditional compilation to see if you are using a Debug build:
In this example, the Console statement will run but the Debug statement will NOT run because the Debugger is not attached. So I will get this output:
And, if I attach the debugger after pressing CTRL + F5, to this process:
Then this is what I get:
The Debug messages haven't been stripped out because I'm using a Debug build of the application.
Okay, so the obvious question is "Why?". I mean, why would they give us this option? Well the most obvious answer is so we can run the application without having to hassle with disabling breakpoints, etc. to do a quick "Smoke Test" and see if it runs. There may be other reasons you have for wanting to run without the Debugger attached as well. So now you have a better idea of the difference between Start With and Start Without Debugging. Go forth and debug away!
Keyboard: CTRL + G Menu: Edit -> Go ToCommand: Edit.GoToVersions: 2008,2010Published: 3/8/2010Code: vstipEdit0026
You can go to any line number in your code by simply pressing CTRL + G. You will get this dialog:
Just type in your desired line number and click OK. The Cursor will move to the line number you typed.
I ran into this the other day (http://github.com/indeyets/syck/blob/master/COPYING) and thought I would share
Copyright (c) 2003 why the lucky stiff
[BSD license text deleted to save space]
D&R (Death and Repudiation) License
This software may not be used directly by any living being. ANY use of this
software (even perfectly legitimate and non-commercial uses) until after death
is explicitly restricted. Any living being using (or attempting to use) this software
will be punished to the fullest extent of the law.
For your protection, corpses will not be punished. We respectfully request
that you submit your uses (revisions, uses, distributions, uses, etc.) to
your children, who may vicariously perform these uses on your behalf. If
you use this software and you are found to be not dead, you will be punished
to the fullest extent of the law.
If you are found to be a ghost or angel, you will be punished to the fullest
extent of the law.
After your following the terms of this license, the author has vowed to repudiate
your claim, meaning that the validity of this contract will no longer be recognized.
This license will be unexpectedly revoked (at a time which is designated to be
most inconvenient) and involved heirs will be punished to the fullest extent
of the law.
Furthermore, if any parties (related or non-related) escape the punishments
outlined herein, they will be severely punished to the fullest extent of a new
revised law that (1) expands the statement "fullest extent of the law" to encompass
an infinite duration of infinite punishments and (2) exacts said punishments
upon all parties (related or non-related).
Menu: Project -> Add New Solution Folder; [Right-Click Solution] -> Add -> New Solution Folder Command: Project.AddNewSolutionFolderVersions: 2008,2010Published: 3/27/2010 Code: vstipProj0009
Did you know there are special folders to help you organize large solutions? There is! They are called, appropriately enough, Solution Folders. To create one just Right-Click on your solution (or go to Project -> Add New Solution Folder) and you will see this in Solution Explorer:
Simply give the folder a name and you are good to go. But so what? I mean, what can you actually DO with these things? Here is a list of stuff you can do:
Move or add projects to them. Solution Folders can be nested to create greater organizational structure.
Add, delete, or rename Solution Folders at any time, if the organizational requirements of the solution change.
Unload all projects in a Solution Folder to make them temporarily unavailable for building.
Collapse or hide entire Solution Folders so that you can work more easily in Solution Explorer. Hidden projects are built when you build the solution.
Build or rebuild all the projects. The projects are built in the order specified by the project dependencies.
Solution Folders are an organizational tool in Solution Explorer; corresponding Windows folders are not created. Microsoft recommends that you organize your projects on disk in the same way that you organize them in the solution. But that is your call :)
I LOVE statistics and was recently asked about calculating the mean, median, and mode using SQL Server. I found this great article by George Mastros that shows you how:
NOTE: This is an older feature. I’ve updated the information for VS2013.
This one is a little known feature you can take advantage of when working with your code. Assuming you have some unformatted code:
You can format the entire document by going to Edit -> Advanced -> Format Document (CTRL+K, CTRL+D) to get this:
You can also do this with just a selection of code:
Go to Edit -> Advanced -> Format Selection (CTRL+K, CTRL+F) to get this:
I won’t go deep into it here but it’s important to remember that the automated formatting follows the default rules you can easily change by going to Tools | Options | Text Editor | [Language] | Formatting in most cases. For example, to change the HTML rules go to Tools | Options | Text Editor | HTML | Formatting:
In the case of HTML, you can go pretty deep into the formatting options by clicking on the Tag Specific Options button:
Formatting the document or selection is a great way to make your code more readable. Enjoy!
This is one change to Visual Studio that I’m not a fan of and I still continue to see a ton of posts on this change so thought I would share here as well.
I always try to give credit where credit is due and I believe the earliest mention of this fix is by Richard Banks in his post 6\1\2012 here: http://www.richard-banks.org/2012/06/how-to-prevent-visual-studio-2012-all.html
Okay so you really, really don’t like the Uppercase menu in VS2012 because you feel like it is shouting at you:
You would rather go back to mixed case for your menu:
Not a problem! Below are ways you can fix this feature in VS2013.
WARNING: Making changes to the registry can cause unexpected results so do any changes at your own risk!
NOTE: Watch your spelling if you are typing this manually.
If you like to “simplify” this a bit you can use PowerShell to run a nice script to make this happen. However, if you haven’t used PowerShell before this may not be faster for you.
Set-ItemProperty -Path HKCU:\Software\Microsoft\VisualStudio\12.0\General -Name
SuppressUppercaseConversion -Type DWord -Value 1
Restart Visual Studio and you are good to go.