The return a detailed error string but ignore its contents and assume it's only ever the failure case you expected to occur but couldn't be bothered to handle properly pattern
-
private void Frobinate()
{
string result = CreateRecord();
if (result == "")
{
//Normal case
}
else
{
if (MessageBox.Show("Record already exists. Do you want to update it?", MessageBoxButtons.YesNo) == DialogResults.Yes)
{
UpdateExistingRecord();
}
}}
private string CreateRecord()
{
try
{
//do stuff to create a new record. Throws an exception for duplicates.
return "";
}
catch (Exception e)
{
return e.Message;
}
}This pattern is absolutely brillant because nothing else could ever cause a problem. Authentication can never be incorrect. The server can never be down. Bob can never be taking a break to oil the hamster wheels.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
-
private void Frobinate()
{
string result = CreateRecord();
if (result == "")
{
//Normal case
}
else
{
if (MessageBox.Show("Record already exists. Do you want to update it?", MessageBoxButtons.YesNo) == DialogResults.Yes)
{
UpdateExistingRecord();
}
}}
private string CreateRecord()
{
try
{
//do stuff to create a new record. Throws an exception for duplicates.
return "";
}
catch (Exception e)
{
return e.Message;
}
}This pattern is absolutely brillant because nothing else could ever cause a problem. Authentication can never be incorrect. The server can never be down. Bob can never be taking a break to oil the hamster wheels.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
-
Give the guy a break. At least he did try. :)
There are only 10 types of people in the world, those who understand binary and those who don't.
You caught that then?
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
private void Frobinate()
{
string result = CreateRecord();
if (result == "")
{
//Normal case
}
else
{
if (MessageBox.Show("Record already exists. Do you want to update it?", MessageBoxButtons.YesNo) == DialogResults.Yes)
{
UpdateExistingRecord();
}
}}
private string CreateRecord()
{
try
{
//do stuff to create a new record. Throws an exception for duplicates.
return "";
}
catch (Exception e)
{
return e.Message;
}
}This pattern is absolutely brillant because nothing else could ever cause a problem. Authentication can never be incorrect. The server can never be down. Bob can never be taking a break to oil the hamster wheels.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
Never knew it was a pattern and had a name! I can start using it now...
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
You caught that then?
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
Well sure. I couldn't just throw it away.
There are only 10 types of people in the world, those who understand binary and those who don't.
You could have made an exception in this case!
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
Never knew it was a pattern and had a name! I can start using it now...
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
If you're looking for other interesting desings, might I suggest the thousand plus line OnClick() method with at least ten levels of indentation pattern. Remember methodcalls have a performance overhead, so inline everything no matter how many copies you end up with.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
-
If you're looking for other interesting desings, might I suggest the thousand plus line OnClick() method with at least ten levels of indentation pattern. Remember methodcalls have a performance overhead, so inline everything no matter how many copies you end up with.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
An appropriate design pattern should be applied indeed. 10 levels of indentation is not good, even if they were hidden in methods. A loop over a list of conditions or strategies maybe. Besides, if we have a framework object Exception, then why return a part of it (Message) instead of a reference to the object itself (or null if no error)? Makes no sense to me. We should either return an error code or make it OO, depending on what is better in a given context. Mixing those two approaches is a bit... unusual. I am no expert, please correct if I'm wrong.
Greetings - Jacek
-
If you're looking for other interesting desings, might I suggest the thousand plus line OnClick() method with at least ten levels of indentation pattern. Remember methodcalls have a performance overhead, so inline everything no matter how many copies you end up with.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
-
Sadly, I know programmers who love to use that pattern. :sigh:
Just because the code works, it doesn't mean that it is good code.
It wasn't a hypothetical for me either. X|
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt