A Static Type Checking Limitation Of Dependency Properties

Introduction

WPF [4] is a Microsoft technology for building Windows forms applications that allow using the MVVM (Model –View-View Model) framework. Dependency properties are at the core of MVVM framework.

Third party static analysis [1] tools help developers to detect those flaws (syntax errors) in the source code which are left unattended by the compilers and parsers. These flaws may lead to run time errors, unhandled exceptions as well as unexpected and undesired behavior of programs. Static type checking, as part of static analysis, is the process of verifying the type safety of a program based on analysis of a program's source code.

There have been more than several static analysis tools which effectively detects different kinds of flaws. Coverity [2] is one of those static analysis tools. Different causes of flaws have been identified. One of those causes is Copy_Paste_Error. The Copy_Paste_Error , as obvious, is caused by the fact that developers sometimes copy a piece of code from one location/file and paste it to another location/file. Once the code is pasted to another location then the developer is supposed to edit or customize it accordingly but sometimes, the pasted code (or a part of it) is left unmodified as per the requirement, perhaps due to negligence. An illustration of these kinds of defects is presented at [3]. In this article I am identifying a static type checking limitation in the dependency properties. The reason why I call it a "Limitation" is that if a developer make this mistake (as illustrated on page 3) then the Visual Studio should be able to statically detect the line of code that has that mistake. Usually a developer happen to make this mistake due to Copy_Paste_Error.

Therefore, this "Limitation " appears to be an open problem for static analysis tools to address. The developers also need to be aware of this common copy/paste mistake to avoid it. This is illustrated in the attached sample application.

Using the Code

The attached code (sample project) is written in C# with Visual Studio 2010 IDE and can be directly opened. It also works with Visual Studio 2015. It can be downloaded from [5].

Case Study

In this case study I present a simple WPF application [5] with MVVM pattern that make use of a dependency property. I have created a button and a property called "Count" on the main WPF form. This button displays the value of the "Count" property. Each time the button is clicked, the number of times it is clicked is shown at the button. For instance, if the button is clicked 5 times then the digit 5 will be shown at the button and so on. The picture below shows the form when it has not been clicked even once.

WPF

On each click of the button, the application intends to calculate the amount of the product associated with this button. Therefore, if the button is clicked once and the amount of the product is $1.50 then the total amount will be $1.50. If the button is clicked 3 times, then the total amount will be $4.50. Note that the "Count" property is of "Integer" datatype, but the "Amount" property will be of "double" datatype, in this case. The image below shows the form after the button is clicked 3 times.

WPF

We can see in the code (sample project)  to define this "Count" property and bind it as per the requirement. We must write more than several lines of code.

If a developer defines a dependency property (CountProperty in this example) and later he intends to define another similar property (AmountProperty in this example) then it is often the case that the code of the dependency property defined earlier is copied and pasted in order to define new dependency property such that the newly pasted piece of code is modified to make it a new dependency property as per the requirement.

Therefore, in the pasted code, there is potential that a tiny piece of this code is left unmodified as per the requirement. I illustrate two such cases as below,

Case 1

Suppose, I would have copied all the lines regarding the "Count property" and then I paste and modify it to make it "Amount property". A little bit of pasted code may have been left unmodified (commented) as at line # 50 of MainWindow.xaml.cs of attached project, as below,

  1. public static readonly DependencyProperty AmountProperty = DependencyProperty.Register("Amount"typeof(double), typeof(MainWindow), new UIPropertyMetadata(0, new PropertyChangedCallback(OnAmountValueChanged)));  

The problem in this code is that, since the Amount property is of type "double" instead of "int" the initial value 0 should be 0.0

If a developer makes this mistake, then Visual Studio compiler does not detect it at compile time. I believe, this should be detected at compile time i.e. statically because the data type “double” is known at compile time. Moreover, if there is no exceptional handling code then this mistake of the developer would crash the application. If we comment the line # 49 and uncomment line # 50 then the repercussion can be observed.

Case 2

Suppose, I would have copied all the lines regarding the “Count property” and then I paste and modify it to make it “Amount property”. A little bit of pasted code may have left unmodified as per the requirement, given at line # 51 (commented) of MainWindow.xaml.cs of the attached sample project [5], as below,

  1. public static readonly DependencyProperty AmountProperty = DependencyProperty.Register("Amount"typeof(double), typeof(Not_MainWindow), new UIPropertyMetadata(0.0, new PropertyChangedCallback(OnAmountValueChanged)));  

The problem in this code is that, the "Count property" is copied from another "View" which is different from the "View" where it is pasted, hence the name of the "View" is not corrected.

If a developer makes this mistake, then Visual Studio compiler does not detect it at compile time. I believe, this should be detected at compile time i.e. statically because the data type “Not_MainWindow” is known at compile time. Moreover, if there is no exceptional handling code then this mistake of developer would crash the application. If we comment the line # 49 and uncomment line # 50 then the repercussion can be observed.

Conclusion

The reported issue in this article is found to exist in WPF- MVVM applications while trying to build a WPF- MVVM application in Visual studio 2010 and Visual Studio 2015 both. 

I have not confirmed whether the reported limitation in this article has already been addressed by any static analysis tools or not. If a static analysis tool has not implemented detecting this limitation, then it is open for the static analysis tools to look into.

References

  1. A comprehensive study of industrial static analysis tools, Par Emanuelsson.
    http://liu.diva-portal.org/smash/get/diva2:330560/FULLTEXT01.pdf
  2. Coverity PreventTM User’s Manual 2.4, 2006.
    http://scan.coverity.com
  3. Coverity Inc. The Coverity Scan, Static analysis, 2007.
    https://scan.coverity.com/o/oss_success_stories/61
  4. WPF Tutorial- Dependency Property
    https://www.codeproject.com/Articles/140620/WPF-Tutorial-Dependency-Property
  5. Sample Project
    http://ahmedjamil.com/static-analysis/sampleproject.rar