473,765 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pattern for if/else

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

Aug 16 '06 #1
4 2385
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
2706
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 owner logs in and deletes the comment. To protect against that, I'd like to break up long strings in the comments (anything submitted by anonymous sources). One thing I'd like to add to the following function is the ability to break up long...
9
3217
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 files from gif # format to png format. Now you need to change the # html code to use the .png files. So, essentially
17
6644
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. So I shall have an Abstract Base called 'Car' implemented by Toyota, Ford, and Buick. Further I'd like to enable to client to say Car *factory;
5
2184
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 I doing wrong? function checkTime(pattern){ if(pattern.value.match(/ + : + + + /)) { alert ("works") } else alert(pattern.value + "Does not work")
2
6008
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 example, in the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim theString As String theString = "1234 TEST 5432 TEST ABCD" If theString Like "*TEST*TEST*" Then...
1
2723
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 Me.txtEmail.SetFocus Exit Sub End If Me.txtStatusBar.Value = "Parsing..." strEmail = Me.txtEmail.Value
11
2635
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) printf($); Code below will take the size of what I input and change the size of the pattern by adding more or less $
1
2373
by: RedLars | last post by:
Hi, Given this class definition, public class Node { Node parent; object current; ArrayList children;
1
4161
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 had no connection or reference to the object that triggered it. It goes something like this: (not syntactically correct..it's just for the idea)
5
5049
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 file: //This is my source file //this is where i want to insert a line class Class1
0
9398
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
10156
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...
1
9951
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
9832
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
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
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
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2805
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.