473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

pattern for if/else

Is there a software pattern for using if then else?
Thanks.

Aug 16 '06 #1
4 2366
There aren't any patterns that I know of, but Martin Fowler lists
several refactoring strategies related to conditional statements in his
book titled Refactoring. For example, you can replace a nested
conditional with guard clauses.

// Original
if (a)
{
return Foo(1);
}
else
{
if (b)
{
return Foo(2);
}
else
{
return Foo(3);
}
}

// Refactored
if (a) return Foo(1);
if (b) return Foo(2);
return Foo(3);

Brian

DBC User wrote:
Is there a software pattern for using if then else?
Thanks.
Aug 16 '06 #2
Thanks, I was told there is a patter may be I misunderstood.
Brian Gideon wrote:
There aren't any patterns that I know of, but Martin Fowler lists
several refactoring strategies related to conditional statements in his
book titled Refactoring. For example, you can replace a nested
conditional with guard clauses.

// Original
if (a)
{
return Foo(1);
}
else
{
if (b)
{
return Foo(2);
}
else
{
return Foo(3);
}
}

// Refactored
if (a) return Foo(1);
if (b) return Foo(2);
return Foo(3);

Brian

DBC User wrote:
Is there a software pattern for using if then else?
Thanks.
Aug 16 '06 #3
And please avoid this terrible terrible programming sin:

void Foo (bool a)
{
bool b;

if (a == true)
{
b == true;
}
else
{
b == false;
}
}

It might look stupid but you'd be surpised how many times I come across
this or variations on the same theme!

Nick
http://seecharp.blogspot.com/

Aug 16 '06 #4
That is a good piece of code nick :)
Thanks.
nick_nw wrote:
And please avoid this terrible terrible programming sin:

void Foo (bool a)
{
bool b;

if (a == true)
{
b == true;
}
else
{
b == false;
}
}

It might look stupid but you'd be surpised how many times I come across
this or variations on the same theme!

Nick
http://seecharp.blogspot.com/
Aug 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lawrence | last post by:
When users enter urls or other long strings it can destroy the formatting of a page. A long url, posted in a comment, can cause page distortions that make the page unreadable, till the website...
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
5
by: Abby Lee | last post by:
I ask the user to enter a time in the formatio 12:30 PM. onChange I send the string to this function. I'm using alert boxes to test it...and am always getting the "Does not work" alert box. What am...
2
by: Ed Brown | last post by:
I'm working on a VB.Net application that needs to do quite a bit of string pattern matching, and am having problems using the "LIKE" operator to match the same string twice in the pattern. For...
1
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
11
by: td0g03 | last post by:
Hello, I just have a few questions. The first one be how would you print a pattern. I could use the if else, but I remember my teacher talking about something like for(i=1;i<=size;i) ...
1
by: RedLars | last post by:
Hi, Given this class definition, public class Node { Node parent; object current; ArrayList children;
1
by: halekio | last post by:
Hi all, Please bear with me as I've only started programming in C# 2 weeks ago and this is my first contact with OOP. I ran into a situation where I needed to catch an event in an object that...
5
by: pramodkh | last post by:
Hi All I am trying to match a pattern in a file and insert a line. If the pattern matches then insert a line before the matching pattern line. for example, I have the following content in a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.