Reputation: 530
I hope this is in the right place. I was having a discussion with a colleague about a method used to check business rules. In general there are two conditions:
Example 1: "If one fails the whole thing fails"
I have used the following technique for condition 1 in several cases. Essentially if one of those conditions is false then the whole thing fails
Dim blnTemp As Boolean = True
If Fail=False Then blnTemp = blnTemp AndAlso False
{code code code}
If Fail=True Then blnTemp = blnTemp AndAlso False
{code code code}
If Fail=False Then blnTemp = blnTemp AndAlso False
{code code code}
If Fail=False Then blnTemp = blnTemp AndAlso False
{yup, code code code}
If True Then
Execute("Execute Global Thermal Nuclear War")
End If
So I am wondering if anyone has any other techniques that they have used for rule checks?
Upvotes: 0
Views: 201
Reputation: 8116
I usually try to go with the "Fail fast, fail hard" idea. Basically leaving the method as soon as the first required condition is not met.
Public Function NuclearWar() as Boolean
If not is Foo() then return False
If (FooList is Nothing AndAlso FooList.Count = 0) then return False
(...) etc.
return True
End Function
For me, its clearer to read instead of going down each check and return the result at the end. If the "whole thing" fails if one of the conditions is false, that early jump off comes in quite handy - as long as you don't need to examine the result of the other checks which are left out because of the return.
@Comment
but if your conditions are separated by other work that needs to be done then one function call would not be sufficient. Or would it? What would you do
Well, readability and clean code (Which includes easy following of the control flow) is one of the things I care about. So I'd most likely group conditions which are related together. If the conditions are separated by other work, I'd abstract the conditions in a class, say LaunchConditions
.
Public Class LaunchCondition
Public Property IsLaunch As Boolean
' Add other properties you'd like to check
End Class
Public Class LaunchConditions
Private _launchConditions = New List(Of LaunchCondition)
Public ReadOnly Property CanLaunch As Boolean
Get
Return CheckConditions()
End Get
End Property
Public Sub AddCondition(ByVal condition As LaunchCondition)
' Add condition
End Sub
Public Sub RemoveCondition(ByVal condition As LaunchCondition)
' Add condition
End Sub
Private Function CheckConditions() As Boolean
' Your check logic
Return _launchConditions.Any(Function(x As LaunchCondition) Not x.IsLaunch)
End Function
End Class
Hope this gives you some ideas :)
Upvotes: 3
Reputation: 1537
For me, Linq does the job concisely and easily readable.
Dim nuclearWarConditions As IEnumerable(Of Boolean) = New Boolean() { _
True, _
True, _
True, _
False, _
True
}
If nuclearWarConditions.All(Function(condition) condition = True) Then
Console.WriteLine("Execute Global Thermal Nuclear War")
End If
Analogically, you may use
If nuclearWarConditions.Any(Function(condition) condition = False) Then
Console.Error.WriteLine("Cancelled Global Thermal Nuclear War")
End If
to reduce nesting.
Edit: Sorry in case i messed up the VB syntax. I'm not a native speaker :)
Upvotes: 2
Reputation: 21873
I'm not sure what the difference is between your two scenarios - isn't "all must be true" the same as "if one fails the whole thing fails"?
In any case, assuming your processing is already done, I wouldn't mess with all the If/Then statements, but go with something cleaner:
if (conditional1 && conditional2 && conditional3...)
{return true}
else
{return false}
Upvotes: 0