By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,180 Members | 1,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,180 IT Pros & Developers. It's quick & easy.

deeply nested if..then..else pattern question

P: n/a
I had read an article at one time that suggested a pattern to get around deeply nested if..then..else hell...

Can anyone point me in that direction?

select case statements wont work for me in this instance

--
--Eric Cathell, MCSA
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Eric,
Where you thinking of the "Replace Nested Conditional with Guard Clauses"
refactoring?

http://www.refactoring.com/catalog/r...rdClauses.html
Can you post a sample of your nested ifs? Maybe then I or someone else on
the group could offer suggestions on how to get around them...

--
Hope this helps
Jay [MVP - Outlook]
T.S. Bradley - http://www.tsbradley.net

"ECathell" <ec******@nospam.com> wrote in message
news:e9*************@tk2msftngp13.phx.gbl...
I had read an article at one time that suggested a pattern to get around
deeply nested if..then..else hell...

Can anyone point me in that direction?

select case statements wont work for me in this instance

--
--Eric Cathell, MCSA
Nov 21 '05 #2

P: n/a
Thanks for the response!
Here is the code after I worked on it some. I had had the 2 ifs as 4 ifs.
Before that it was even worse(as I learn my code gets neater..hehe.) I would
still like to break this down in a way that I could return a more
informative error message. I may eventually break it out into custom
exceptions. But I would still need 4 if...thens instead of 2...

Me.m_Pallet = loadDatatable(m_palletid, m_Pallet)
If Me.m_Pallet.Rows.Count > 0 Then
'isArea>79
If Me.checkArea(m_Pallet) = True And Me.checkRemoved(m_Pallet) =
True Then
'Is the Pallet Issued to Production?
If Me.checkIssued(m_Pallet) = False And Me.checkPicked(m_palletid)
= False Then
m_area = resetArea(m_area)
resetPallet(m_palletid, m_area)
m_fixed = True
End If
Else
MessageBox.Show("Error With Pallet.Cannot run Reconcile Fix")
m_fixed = False
End If
End If
--
--Eric Cathell, MCSA
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:eq**************@TK2MSFTNGP14.phx.gbl...
Eric,
Where you thinking of the "Replace Nested Conditional with Guard Clauses"
refactoring?

http://www.refactoring.com/catalog/r...rdClauses.html
Can you post a sample of your nested ifs? Maybe then I or someone else on
the group could offer suggestions on how to get around them...

--
Hope this helps
Jay [MVP - Outlook]
T.S. Bradley - http://www.tsbradley.net

"ECathell" <ec******@nospam.com> wrote in message
news:e9*************@tk2msftngp13.phx.gbl...
I had read an article at one time that suggested a pattern to get around
deeply nested if..then..else hell...

Can anyone point me in that direction?

select case statements wont work for me in this instance

--
--Eric Cathell, MCSA

Nov 21 '05 #3

P: n/a
ECathell,
As I suggested, rather then nest the ifs, I would use guard clauses,
something like:

Private Sub Checks()
m_pallet = LoadDataTable(m_palletid, m_pallet)

If Not (Me.m_Pallet.Rows.Count > 0) Then
Throw New InvalidOperationException("Pallet is empty")
End If

If Not (Me.checkArea(m_Pallet) = True And _
Me.checkRemoved(m_Pallet) = True) Then
Throw New InvalidOperationException("isArea>79")
End If

If Not (Me.checkIssued(m_Pallet) = False And _
Me.checkPicked(m_palletid) = False) Then
Throw New InvalidOperationException("'Is the Pallet Issued to
Production?")
End If

m_area = resetArea(m_area)
resetPallet(m_palletid, m_area)

End Sub

Public Sub Main()

Try
Checks()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

Each "guard clause" is its own If statement, if the statement fails it
throws an exception, you can of course use more descriptive Exception and
message.

Generally rather then use "If Not" as above, I would invert the condition:

If Me.m_Pallet.Rows.Count <= 0 Then
Throw New InvalidOperationException("Pallet is empty")
End If

I kept your conditions "as is", so you could see what I did.

--
Hope this helps
Jay [MVP - Outlook]
T.S. Bradley - http://www.tsbradley.net
"ECathell" <ec******@nospam.com> wrote in message
news:e3**************@TK2MSFTNGP12.phx.gbl...
| Thanks for the response!
| Here is the code after I worked on it some. I had had the 2 ifs as 4 ifs.
| Before that it was even worse(as I learn my code gets neater..hehe.) I
would
| still like to break this down in a way that I could return a more
| informative error message. I may eventually break it out into custom
| exceptions. But I would still need 4 if...thens instead of 2...
|
| Me.m_Pallet = loadDatatable(m_palletid, m_Pallet)
| If Me.m_Pallet.Rows.Count > 0 Then
| 'isArea>79
| If Me.checkArea(m_Pallet) = True And Me.checkRemoved(m_Pallet) =
| True Then
| 'Is the Pallet Issued to Production?
| If Me.checkIssued(m_Pallet) = False And
Me.checkPicked(m_palletid)
| = False Then
| m_area = resetArea(m_area)
| resetPallet(m_palletid, m_area)
| m_fixed = True
| End If
| Else
| MessageBox.Show("Error With Pallet.Cannot run Reconcile Fix")
| m_fixed = False
| End If
| End If
|
|
| --
| --Eric Cathell, MCSA
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
| message news:eq**************@TK2MSFTNGP14.phx.gbl...
| > Eric,
| > Where you thinking of the "Replace Nested Conditional with Guard
Clauses"
| > refactoring?
| >
| >
http://www.refactoring.com/catalog/r...rdClauses.html
| >
| >
| > Can you post a sample of your nested ifs? Maybe then I or someone else
on
| > the group could offer suggestions on how to get around them...
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > T.S. Bradley - http://www.tsbradley.net
| >
| > "ECathell" <ec******@nospam.com> wrote in message
| > news:e9*************@tk2msftngp13.phx.gbl...
| > I had read an article at one time that suggested a pattern to get around
| > deeply nested if..then..else hell...
| >
| > Can anyone point me in that direction?
| >
| > select case statements wont work for me in this instance
| >
| > --
| > --Eric Cathell, MCSA
| >
| >
|
|
Nov 21 '05 #4

P: n/a
Thanks again for your help Jay. The exceptions were the way I was
leaning...Then I can be really specific...

thanks again...
--
--Eric Cathell, MCSA
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:Ot**************@TK2MSFTNGP15.phx.gbl...
ECathell,
As I suggested, rather then nest the ifs, I would use guard clauses,
something like:

Private Sub Checks()
m_pallet = LoadDataTable(m_palletid, m_pallet)

If Not (Me.m_Pallet.Rows.Count > 0) Then
Throw New InvalidOperationException("Pallet is empty")
End If

If Not (Me.checkArea(m_Pallet) = True And _
Me.checkRemoved(m_Pallet) = True) Then
Throw New InvalidOperationException("isArea>79")
End If

If Not (Me.checkIssued(m_Pallet) = False And _
Me.checkPicked(m_palletid) = False) Then
Throw New InvalidOperationException("'Is the Pallet Issued to
Production?")
End If

m_area = resetArea(m_area)
resetPallet(m_palletid, m_area)

End Sub

Public Sub Main()

Try
Checks()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

Each "guard clause" is its own If statement, if the statement fails it
throws an exception, you can of course use more descriptive Exception and
message.

Generally rather then use "If Not" as above, I would invert the condition:

If Me.m_Pallet.Rows.Count <= 0 Then
Throw New InvalidOperationException("Pallet is empty")
End If

I kept your conditions "as is", so you could see what I did.

--
Hope this helps
Jay [MVP - Outlook]
T.S. Bradley - http://www.tsbradley.net
"ECathell" <ec******@nospam.com> wrote in message
news:e3**************@TK2MSFTNGP12.phx.gbl...
| Thanks for the response!
| Here is the code after I worked on it some. I had had the 2 ifs as 4
ifs.
| Before that it was even worse(as I learn my code gets neater..hehe.) I
would
| still like to break this down in a way that I could return a more
| informative error message. I may eventually break it out into custom
| exceptions. But I would still need 4 if...thens instead of 2...
|
| Me.m_Pallet = loadDatatable(m_palletid, m_Pallet)
| If Me.m_Pallet.Rows.Count > 0 Then
| 'isArea>79
| If Me.checkArea(m_Pallet) = True And Me.checkRemoved(m_Pallet) =
| True Then
| 'Is the Pallet Issued to Production?
| If Me.checkIssued(m_Pallet) = False And
Me.checkPicked(m_palletid)
| = False Then
| m_area = resetArea(m_area)
| resetPallet(m_palletid, m_area)
| m_fixed = True
| End If
| Else
| MessageBox.Show("Error With Pallet.Cannot run Reconcile Fix")
| m_fixed = False
| End If
| End If
|
|
| --
| --Eric Cathell, MCSA
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
| message news:eq**************@TK2MSFTNGP14.phx.gbl...
| > Eric,
| > Where you thinking of the "Replace Nested Conditional with Guard
Clauses"
| > refactoring?
| >
| >
http://www.refactoring.com/catalog/r...rdClauses.html
| >
| >
| > Can you post a sample of your nested ifs? Maybe then I or someone else
on
| > the group could offer suggestions on how to get around them...
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > T.S. Bradley - http://www.tsbradley.net
| >
| > "ECathell" <ec******@nospam.com> wrote in message
| > news:e9*************@tk2msftngp13.phx.gbl...
| > I had read an article at one time that suggested a pattern to get
around
| > deeply nested if..then..else hell...
| >
| > Can anyone point me in that direction?
| >
| > select case statements wont work for me in this instance
| >
| > --
| > --Eric Cathell, MCSA
| >
| >
|
|

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.