Software quality is not only about testing (be it unit, integration, system, usability, or any other kind).
It's also about writing code that detects faults as early as possible.
For instance, one thing that worries me about data binding in WPF is
the lack of type safety in property change notifications.
It's so easy to misspell the name of a property, or even get the case of one of the letters incorrectly...
You won't get a compile-time error, or event a run-time error;
you'll just get a program that doesn't work entirely correctly in some area of one of the windows
(of course it's a window that you never test, your clients never use, except for this one client...).
It's just not that easy to find this problem when testing the application,
or even if you find a problem, track it down to an invalid property name...
So, what we usually have is a class that implements INotifyPropertyChanged; something like this:
Public Class BasePropertyChanged
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal Sender As Object,
ByVal Args As PropertyChangedEventArgs)
Implements INotifyPropertyChanged.PropertyChanged
Private Sub OnPropertyChanged(ByVal PropertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
End Sub
End Class
First, we define a base class that knows how to notify about property changes,
and then we may have a class that uses that:
Private Class Source
Inherits BasePropertyChanged
Private _title As String
Public Property Title() As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = value
OnPropertyChanged("Title")
End Set
End Property
End Class
Obviously, you can implement the INotifyPropertyChanged interface in any other class
(that doesn't necessarily descend from BasePropertyChanged).
Well, at least until this stupid fashion of blocking multiple class inheritance finally passes.
But I digress... :-)
If you misspell Title in the code above, or misspell it in the XAML file,
or accidentally change it, something will brake; and this may remain broken for quite a while.
So, let's add some code that will at least check those property change notifications.
This will be a run-time check, but (really) simple unit tests will take care of that.
Or, even if you don't have unit tests, you'll get notified that a property notification is defined incorrectly
(making debugging that much easier).
Public Class BasePropertyChanged
Implements INotifyPropertyChanged
. . .
Private Sub OnPropertyChanged(ByVal PropertyName As String)
Dim objectType As Type = Me.GetType
Dim propertyInfo As PropertyInfo =
objectType.GetProperty(PropertyName, BindingFlags.Instance Or BindingFlags.Public)
If propertyInfo Is Nothing Then
Throw New ArgumentOutOfRangeException()
End If
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
End Sub
End Class
Here, we take our class; then we check, using reflection, whether it has the mentioned property.
And if it doesn't, it throws an exception.
You might prefer to Debug.Assert, for instance, instead.
Or to wrap that verification code inside #If DEBUG Then ... #End If
to exclude it from the release versions of the program.
Whatever's your preference here...
Top
|