473,769 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

deeply nested if..then..else pattern question

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
4 2523
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******@nospa m.com> wrote in message
news:e9******** *****@tk2msftng p13.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
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.Row s.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_are a)
resetPallet(m_p alletid, 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******** ******@TK2MSFTN GP14.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******@nospa m.com> wrote in message
news:e9******** *****@tk2msftng p13.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
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.Ro ws.Count > 0) Then
Throw New InvalidOperatio nException("Pal let is empty")
End If

If Not (Me.checkArea(m _Pallet) = True And _
Me.checkRemoved (m_Pallet) = True) Then
Throw New InvalidOperatio nException("isA rea>79")
End If

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

m_area = resetArea(m_are a)
resetPallet(m_p alletid, 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.Row s.Count <= 0 Then
Throw New InvalidOperatio nException("Pal let 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******@nospa m.com> wrote in message
news:e3******** ******@TK2MSFTN GP12.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.Row s.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_are a)
| resetPallet(m_p alletid, 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******** ******@TK2MSFTN GP14.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******@nospa m.com> wrote in message
| > news:e9******** *****@tk2msftng p13.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
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******** ******@TK2MSFTN GP15.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.Ro ws.Count > 0) Then
Throw New InvalidOperatio nException("Pal let is empty")
End If

If Not (Me.checkArea(m _Pallet) = True And _
Me.checkRemoved (m_Pallet) = True) Then
Throw New InvalidOperatio nException("isA rea>79")
End If

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

m_area = resetArea(m_are a)
resetPallet(m_p alletid, 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.Row s.Count <= 0 Then
Throw New InvalidOperatio nException("Pal let 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******@nospa m.com> wrote in message
news:e3******** ******@TK2MSFTN GP12.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.Row s.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_are a)
| resetPallet(m_p alletid, 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******** ******@TK2MSFTN GP14.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******@nospa m.com> wrote in message
| > news:e9******** *****@tk2msftng p13.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1790
by: Ori Y | last post by:
Hi , i'm having some difficulties regarding using the Python/c api. I'm trying to bulid a deeply nested datastructure (i.e dict within a dict or list within a dict) Building the data stracture seems ok on interperter, but after few calls to the method returning the structure, and aft applying the Ctrl+D to exit interperter i receive a 'segmentation fault'. ?# Further more when calling the program from python i receive the dictionary as a...
7
5675
by: Anthony Robinson | last post by:
Have been encountering an odd issue. Every now and again, certain packages of stored procedures just become invalid. I'm aware that dropping or altering an underlying table would render a package invalid, but we are doing no such thing... After banging my head on the wall for a bit I noticed that the two stored procedures that are experiencing this behavior are procedures that are called from within another procedure (they're not both...
3
6472
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
5
1335
by: Atley | last post by:
I am trying to set up a nested collection so I can run statements like this: me.lblquestions.text = mysteps.item(1).questions(1).Asked Any ideas on how to do this? I have created a Class(Steps) with a nested Class(Questions) inside of it and a Collection of that Class(Steps) and it works fine, but the Nested Collection(Questions) is escaping me. Help!
6
3149
by: Ivan Sergio Borgonovo | last post by:
First thanks to Tom Lane who helped me promptly. Now let's come to the problem: create or replace function testa( ) returns char(32) as ' begin if 1=2 then if 1=2 then
7
1320
by: Kay Schluehr | last post by:
I want to manipulate a deeply nested list in a generic way at a determined place. Given a sequence of constant objects a1, a2, ..., aN and a variable x. Now construct a list from them recursively: L = ]...]] The value of x is the only one to be changed. With each value of x a new instance of L is needed so that we actually have a function: L = lambda x: ]...]]
18
3645
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an abstract class that has the virtual function getId(). I then have a function that prints a special string when a bob meets another bob (all combinations of bobs has to meet and since the are schizophrenic they can also meet themselves):
6
1739
by: alainfri | last post by:
I am not sure if this group is the right place for this question but what I need is as follows. There is a piece of html. Throughout the html there are a lot of <brtags. The task is to replace all these <brtags with \n\r. The replacement must be performed only within <preblocks. I can do this using VBScript in the following way: Option Explicit
2
1530
by: mathieu | last post by:
Hi there, I am struggling to write a piece of code that would parse a simple xml file. I was wondering if there are good example (pattern?) to use when dealing with nested xml elements. For instance my xml looks like (*), with only one level of nesting. AFAIK I need to do the book keeping myself esp. when reading the CharacterData so that it is associated to the correct entry.
0
9416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9981
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9850
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.