473,379 Members | 1,542 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,379 software developers and data experts.

Other way to write this?

This is a question of style I guess..
I have something like this:

if (ShowDialog()) {
if (Dialog->ShowMessage(...) == KEY_OK) // User pressed ok button
PerformAction();
}
else
PerformAction();

--

The idea is that if ShowDialog() is true, then the program shows a dialog
with some info before proceeding. Otherwise, the action is performed
immediately.
What I don't like is repeating the PerformAction() sentence.. is there a way
to avoid this more 'elegantly'? I mean, refactoring in a way to make
PerformAction() appear only once, regardless the ShowDialog() return value?

Thanks in advance.
Aug 18 '05 #1
7 1174
Alex wrote:
This is a question of style I guess..
I have something like this:

if (ShowDialog()) {
if (Dialog->ShowMessage(...) == KEY_OK) // User pressed ok button
PerformAction();
}
else
PerformAction();


Maybe something like

if (!ShowDialog() || Dialog->ShowMessage() == KEY_OK)
PerformAction();

V
Aug 18 '05 #2
* Alex:
This is a question of style I guess..
I have something like this:

if (ShowDialog()) {
if (Dialog->ShowMessage(...) == KEY_OK) // User pressed ok button
PerformAction();
}
else
PerformAction();

The idea is that if ShowDialog() is true, then the program shows a dialog
with some info before proceeding. Otherwise, the action is performed
immediately.
What I don't like is repeating the PerformAction() sentence..
What I don't like is the use of conditions with side-effects.

That is, incidentally, what makes it hard to rewrite.

is there a way
to avoid this more 'elegantly'? I mean, refactoring in a way to make
PerformAction() appear only once, regardless the ShowDialog() return value?
if( !ShowDialog() || ShowMassageOK() ) { PerformAction(); }

Thanks in advance.


T'was nothi
n
g...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 18 '05 #3
"Alex" <no@domain.com> wrote in
news:de**********@nsnmpen2-gest.nuria.telefonica-data.net:
This is a question of style I guess..
I have something like this:

if (ShowDialog()) {
if (Dialog->ShowMessage(...) == KEY_OK) // User pressed ok button
PerformAction();
}
else
PerformAction();

--

The idea is that if ShowDialog() is true, then the program shows a
dialog with some info before proceeding. Otherwise, the action is
performed immediately.
What I don't like is repeating the PerformAction() sentence.. is there
a way to avoid this more 'elegantly'? I mean, refactoring in a way to
make PerformAction() appear only once, regardless the ShowDialog()
return value?


if (!ShowDialog() || (Dialog->ShowMessage(...) == KEY_OK))
{
PerformAction();
}
Time to brush up on your boolean logic... :)
Aug 18 '05 #4
Alex wrote:
This is a question of style I guess..
I have something like this:

if (ShowDialog()) {
if (Dialog->ShowMessage(...) == KEY_OK) // User pressed ok button
PerformAction();
}
else
PerformAction();

--

The idea is that if ShowDialog() is true, then the program shows a dialog
with some info before proceeding. Otherwise, the action is performed
immediately.
What I don't like is repeating the PerformAction() sentence.. is there a
way to avoid this more 'elegantly'? I mean, refactoring in a way to make
PerformAction() appear only once, regardless the ShowDialog() return
value?

Thanks in advance.


What about:

if ( // we perform the action if
( ! ShowDialog() ) // the message is skipped
|| // or
(Dialog->ShowMessage(...) == KEY_OK) // the user accepts
){
PerformAction();
}
Kai-Uwe Bux
Aug 18 '05 #5
A different solution to the suggested ones, and not applicable in as
many situations, but you may want to consider it:

class CancelOperation { };

try
{
if (ShowDialog() && Dialog->ShowMessage(...) != KEY_OK)
throw CancelOperation;
PerformAction();
}
catch (CancelOperation e)
{
}

Aug 18 '05 #6
Jules wrote:
A different solution to the suggested ones, and not applicable in as
many situations, but you may want to consider it:

class CancelOperation { };

try
{
if (ShowDialog() && Dialog->ShowMessage(...) != KEY_OK)
throw CancelOperation;
PerformAction();
}
catch (CancelOperation e)
{
}


Could you (for everybody's benefit) show in what conditions your
solution would be superior to other ones? Thanks.

V
Aug 18 '05 #7
"Alex" <no@domain.com> wrote in message
news:de**********@nsnmpen2-gest.nuria.telefonica-data.net...

if (ShowDialog()) {
if (Dialog->ShowMessage(...) == KEY_OK) // User pressed ok button
PerformAction();
}
else
PerformAction();


if (!(ShowDialog() && Dialog->ShowMessage(...) != KEY_OK))
PerformAction();

This doesn't repeat the PerformAction call, but it is also less clear.

JFA1
Aug 19 '05 #8

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

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
1
by: bscofield | last post by:
Hi, I'm a db app. developer and I am re-writing some helps in javascript & HTML. The Javascript is for form flow and design and HTML just to display the helps. I use a seperate app. development...
0
by: Sarah Akers | last post by:
GgF ----gL5cJ72EqiGIQ0SK65Rz Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <head> <style type=3D"text/css">.eyebrow { FONT-WEIGHT: bold; FONT-SIZE: 10px; TE=
2
by: Bruce | last post by:
I have sucessfully used document.write in a new window, but I now wish to write to an already open & framed window. How do I do it. Please show example, as I have trouble getting my head around JS....
1
by: cwdjrxyz | last post by:
I am now writing many new pages in xhtml 1.1. They are served as true xhtml by setting the mime type of the server to application/xhtml+xml for the .xhtml extension. Since IE6 and some earlier...
5
by: AC [MVP MCMS] | last post by:
Any pointers on how to (1) read a Base64 encoded string from a text file and (2) write it to a binary file? I have a ton of files that are being generated from a legacy system. Each file...
7
by: Benjamin.von.Ardenne | last post by:
Hi guys, just to keep it short and simple - I'd like to "monitor" a file that is opend in Word e.g. and wait for it to be closed but I didn't find a way to check the current access state of a file...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: cj | last post by:
I've often used methods exposed by software purchased for other uses to automate tasks. For instance back in the late 90's I found the terminal emulation package the company exposed methods that...
40
by: RvGrah | last post by:
I've been writing in C# for about 4 years now, coming from VB.net and VB6 before that, in which I know I'm not alone. I found learning C#, at least to the extent that I use it in developing...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.