Comment On Catch and Release

One thing that is quite clear is that many of the people that write in are called Don. The corollary is that if you want to see less bad code, change your name. What isn't clear (to me, from the submission) is what Don's company does. [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Catch and Release

2007-01-25 09:04 • by Cyrus (unregistered)
I don't think they quite understand stack unwinding; though I'm interested to hear someone defend this, could be interesting.

Captcha: Bling, word.

Re: Catch and Release

2007-01-25 09:07 • by rmr
I especially like the fact that every email sent out will include %%FirstName%% and %%LastName%% because of the ToLower() call in the swtich.

I love the switch statement!

2007-01-25 09:08 • by Benanov (can't login) (unregistered)
It's completely useless! String.Replace(string1,string2) is s/string1/string2/g

Captcha: ewww (how true)

Re: Catch and Release

2007-01-25 09:13 • by pete (unregistered)
my great new list implementation:


public static Exception obfuscateArray(String[] members)
{
Exception e = new Exception(Integer.valueOf(mebers.length));

for(int i=0; i < members.length; i++)
{
try
{
throw new Exception(members[i], e);
}
catch(Exception x)
{
e = x;
}
}
return e;
}

Re: Catch and Release

2007-01-25 09:16 • by ntheory (unregistered)
113365 in reply to 113364
"mebers" causes an error. Oh well, guess I can't put that into production yet.

Re: Catch and Release

2007-01-25 09:16 • by Mikkel Høgh (unregistered)
This is hilarious...
Who said it wasn't possible to make a simple mater of string replacement impossibly complicated - even in C#.

Re: Catch and Release

2007-01-25 09:17 • by shenme (unregistered)
Is there some reason you'd code '%' two different ways, Unicode "\u0025" or 'normal' "%" ?

Is there some rule saying you couldn't just say "switch (match.Value.toLower())" and then 'case "%%username%%":' ?

Does 'Body.Replace("%%password%%",' do case-insensitive matching, in case the instance was "%%pASSword%%" ?

--
I'm a pessimist about probabilities; I'm an optimist about possibilities.
Lewis Mumford (1895-1990)

Re: Catch and Release

2007-01-25 09:17 • by maratcolumn1
Like many of my managers liked to say, "Our program should never fail." Hence:
try {

anyOfMyFunctions();
} catch (Exception e) {
System.out.println("Exception!");
}


P.S. I'll kill you if you tell them about Throwable and Error.

Re: Catch and Release

2007-01-25 09:22 • by Odhran (unregistered)
What a bloody awful piece of code!!

They obviously never heard of XSL transforms either ... for shame!

Re: Catch and Release

2007-01-25 09:39 • by XIU
113371 in reply to 113367
shenme:
Is there some reason you'd code '%' two different ways, Unicode "\u0025" or 'normal' "%" ?

Is there some rule saying you couldn't just say "switch (match.Value.toLower())" and then 'case "%%username%%":' ?

Does 'Body.Replace("%%password%%",' do case-insensitive matching, in case the instance was "%%pASSword%%" ?


\u0025 and % is exactly the same. You could do case "%%username%%" but that's not really the point, string.Replace replaces everything it finds, so you could just call all the Replace things, no need for Regex either. And string.Replace is case-sensitive so it would leave "%%pASSword%%"

And then returning the exception, this is just one big WTF.

Re: Catch and Release

2007-01-25 09:42 • by gram (unregistered)
they also haven't heard about Regex.Replace, which makes life easier (:
but it's true, xslt is the right way

Re: Catch and Release

2007-01-25 09:46 • by Marcin (unregistered)
Consider: the use of exceptions will prevent the mail being sent, preventing erroneous behaviour.

The caller might want to defer exception handling in order to do generic cleanup etc. that would otherwise have to be replicated in each handler. Java/C#/C++ exceptions do impose certain style costs in that regard, and the writer of this code might want to avoid that.

Re: Catch and Release

2007-01-25 09:52 • by gram (unregistered)
if you want to defer exception handling, you just don't need to catch it here...

Re: Catch and Release

2007-01-25 09:53 • by John Cowan (unregistered)
Sure, why not return an exception? It's a first-class object; you can pass it around however you like, pick it apart, whatever. Nothing says you *have* to throw it.

Re: Catch and Release

2007-01-25 10:04 • by abx
If I were to call a method, and I noticed that the return type was an exception, I would consider sending the damn email myself.

Re: Catch and Release

2007-01-25 10:06 • by Smudge (unregistered)
Whenever I see "== true" or "== false", it's always a guarantee that the rest of the code will be awful too. This one didn't disappoint,

Re: Catch and Release

2007-01-25 10:18 • by Frzr (unregistered)
"Catch and Release is more than just a way of hurting fish for fun." -- Dogbert

Captcha: atari, where you had no exceptions.

Re: Catch and Release

2007-01-25 10:18 • by snoofle
113385 in reply to 113370
Odhran:
What a bloody awful piece of code!!

They obviously never heard of XSL transforms either ... for shame!

Yeah, because someone who would write something like this needs to be turned loose with XSLT...

... not saying XSLT is the wrong way, just this seems like the wrong person to attempt to use it!

Re: Catch and Release

2007-01-25 10:27 • by willy (unregistered)
113386 in reply to 113382
Smudge:
Whenever I see "== true" or "== false", it's always a guarantee that the rest of the code will be awful too. This one didn't disappoint,


I dunno, in any language I try to make things as explicit as possible. having '== true' or whatever (although a bit redundant makes it obvious as to what the return type is being evaluated in the conditional without the need to investigate further.

I do a lot of code maintenance, so maybe it's just me, but when I'm skimming through existing code after its been written, little visual queues like this save time and brainpower.

Re: Catch and Release

2007-01-25 10:28 • by Your Name (unregistered)
113387 in reply to 113382
I agree with you on single comparisons, but when there are muliple ands and ors, it doesn't hurt to really say what you mean.

I hope you aren't one of those people who also berates others for using excess parentheses, because they "slow down the compiler" or something.

Re: Catch and Release

2007-01-25 10:31 • by AndrewB (unregistered)
113388 in reply to 113382
Smudge:
Whenever I see "== true" or "== false", it's always a guarantee that the rest of the code will be awful too. This one didn't disappoint,


Ever heard of readability?

Re: Catch and Release

2007-01-25 10:32 • by Fredric (unregistered)
I must say that I had never thought of returning an exception until now.

It's quite a strange way of returning an error code, but essentially it's the same thing.

With the difference that Null means the function succeeded, while usually if a function returns Null you'd expect it to have failed. In this case it fails if it actually returns something...

And the lower/upper case stuff... That's just dumb...

captcha: bathe, is that even a real word?

Re: Catch and Release

2007-01-25 10:32 • by snoofle
113390 in reply to 113387
Your Name:
I agree with you on single comparisons, but when there are muliple ands and ors, it doesn't hurt to really say what you mean.

I hope you aren't one of those people who also berates others for using excess parentheses, because they "slow down the compiler" or something.

A few years ago, I worked with this woman who was a former Lisp programmer, and she used to bury her expressions in lots of layers of parentheses, just because the compiler would effectively ignore them:

if ((( x == 3 ))) { ... }

On short expressions, it's not so bad, but on very complex expressions with multiply nested sub-groups, it was a horror to manually scan. I spent a whole lot of time normalizing her code

Re: Catch and Release

2007-01-25 10:36 • by el jaybird (unregistered)
113391 in reply to 113384
Frzr:
atari, where you had no exceptions.


Shaka, when the walls fell.

Re: Catch and Release

2007-01-25 10:40 • by Anonymous (unregistered)
113392 in reply to 113382
My favorite is the gradual maintenance that eventually results in abominations like this:

if(!(!foo == !false))

... logic parse error, core dumped ...

Re: Catch and Release

2007-01-25 10:43 • by Havic (unregistered)
I must say that I don't see a problem returning an error from the method, though I should think that you would want to catch explicit errors and only return errors you were expecting, any unknown errors could just be rethrown. In this way the calling function could check for null for success, anything else was a minor error probably indicating that the email couldn't be sent. I've done email routines this way many times. I usually just tell the user that there was a problem sending the email and then spit out the error description.

Re: Catch and Release

2007-01-25 10:44 • by Jon (unregistered)
113394 in reply to 113389
Fredric:

captcha: bathe, is that even a real word?


Ask your coworkers if you stink

captcha: paint

Re: Catch and Release

2007-01-25 10:49 • by digdug (unregistered)
113396 in reply to 113372
gram:
they also haven't heard about Regex.Replace, which makes life easier (:
but it's true, xslt is the right way

Nope, the Right Way is a templating language (free template or the like) rather than XML/XSLT.

Re: Catch and Release

2007-01-25 10:55 • by Brian (unregistered)
Am I the only one to think that naming template files with an extension of .tmp is a WTF?

Re: Catch and Release

2007-01-25 10:58 • by n (unregistered)
113399 in reply to 113391
el jaybird:
Frzr:
atari, where you had no exceptions.


Shaka, when the walls fell.



YAYAYAAYY!!!! You made my day!

Temba, his arms wide.

(captcha -- xevious, one of my favorite old video games. I can still hum the theme song.)

Re: Catch and Release

2007-01-25 11:07 • by htg (unregistered)
113404 in reply to 113393
I must say that I don't see a problem returning an error from the method, though I should think that you would want to catch explicit errors and only return errors you were expecting

Define your own exception, e.g., MyMailSendException, with useful fields.

Catch email and I/O exceptions, then use them to throw your own MyMailSendException, thus protecting the code that calls your code from the email/IO/etc exceptions.

But returning exceptions, that's nutty. However it is less CPU overhead than executing code in a try..catch statement (well, historically in Java, I don't know about now, or C#). Also simply catching the highest level Exception is not nice, that's why subclasses exist!

This code does explain spam emails I get with %%username%% in them though.

Catch and Release, yeah, fishy, but Catch and Return is more accurate here...

Re: Catch and Release

2007-01-25 11:10 • by snoofle
113407 in reply to 113399
n:
el jaybird:
Frzr:
atari, where you had no exceptions.


Shaka, when the walls fell.



YAYAYAAYY!!!! You made my day!

Temba, his arms wide.

(captcha -- xevious, one of my favorite old video games. I can still hum the theme song.)


Somewhat off topic, but...

you know you're getting old when you feel old because you get not only both of these references, but the original references that came (way) before them..

Re: Catch and Release

2007-01-25 11:17 • by Nurgle (unregistered)
113410 in reply to 113382
if (true == HateReadableCode)
{
return NoCookie;
} else if (false == HateReadableCode)
{
return ChocolateChip;
} else
{
return FileNotFound;
}

captcha: mmmm... yummy cookies...

Re: Catch and Release

2007-01-25 11:17 • by htg (unregistered)
Also shouldn't the entire regex stuff be the C# equivalent of the single line in Perl:

$template ~= s/%%(.$)%%/$t{"$1"}/g;

although when I've done stuff before (a few years ago, and in Perl which I'm now rusty in) I used to use {{variable}} in my templates (usually HTML, but did have email templates) instead of %%variable%%.

Now I'd use a proper templating engine.

%t = {
captcha => "pirates";
};

Re: Catch and Release

2007-01-25 11:17 • by NoName (unregistered)
113412 in reply to 113382
Smudge:
Whenever I see "== true" or "== false", it's always a guarantee that the rest of the code will be awful too. This one didn't disappoint,


Usually, I'd agree. However, I admit to having used this syntax in weakly typed languages (like Python), because I used a "trinary" variable. That is, it could be true, false, or an object. So the check "foo == true" made perfect sense, because if "foo" is an object, it will cast to "true", but that's different from foo actually being equal to true.

However, in strongly-typed langugaes, there's no good reason for that.

Re: Catch and Release

2007-01-25 11:18 • by Nurgle (unregistered)
113413 in reply to 113410
Oops, forgot to qutoe above...


Smudge:
Whenever I see "== true" or "== false", it's always a guarantee that the rest of the code will be awful too. This one didn't disappoint,

Re: Catch and Release

2007-01-25 11:19 • by Peter Antoine (unregistered)
113414 in reply to 113407
snoofle:
n:
el jaybird:
Frzr:
atari, where you had no exceptions.


Shaka, when the walls fell.



YAYAYAAYY!!!! You made my day!

Temba, his arms wide.

(captcha -- xevious, one of my favorite old video games. I can still hum the theme song.)


Somewhat off topic, but...

you know you're getting old when you feel old because you get not only both of these references, but the original references that came (way) before them..


I apologise for being equally as old but, http://en.wikipedia.org/wiki/Row_of_bombs.

DevPack ST ahhhh..... the good old days. (not quite as good as DevPack on the 800XL --- now that was coding)

Re: Catch and Release

2007-01-25 11:22 • by Andrew Bell (unregistered)
113417 in reply to 113404
Returning exceptions isn't so nutty. Not to say that this is good code, but it could be useful. Maybe some callers will care that things failed, and some callers don't care. Those that care can check to see if an exception is returned. Those that don't care don't have to do ANYTHING. To ME, this is nicer than:

try {
call();
}
catch (Exception ex)
{}

Returning the exception provides everything you want to know IF you want to know it.

Re: Catch and Release

2007-01-25 11:34 • by htg (unregistered)
113425 in reply to 113417
Andrew Bell:
Returning exceptions isn't so nutty. Not to say that this is good code, but it could be useful. Maybe some callers will care that things failed, and some callers don't care. Those that care can check to see if an exception is returned. Those that don't care don't have to do ANYTHING. To ME, this is nicer than:


Or you could use a policy of "Catch And Consume" within the method and simply return a boolean, true for successfully sent, false for failed to send, and filenotfound otherwise.

Regardless returning an Exception is silly, you might as well return any type of object, e.g., in this case a String would suffice with the error message inside.

Or is your optional code, i.e.,

if (exception != null) {
if (exception instanceof FileNotFoundException) { blah }
else if (exception instanceof EmailNotSentException) { blah }
else ....
else { have we tried all possible exceptions? I'm not sure, the javadoc didn't say what the possible exceptions would be, but when we use a try/catch the IDE will let us know }
}

really that good?

captcha: stinky. yup, it certainly is.

Re: Catch and Release

2007-01-25 11:35 • by dwm (unregistered)
113428 in reply to 113411
htg:


%t = {
captcha => "pirates";
};


Your Perl really is rusty. That should be:

%t = (
captcha => "pirates",
);

Re: Catch and Release

2007-01-25 11:38 • by Eam (unregistered)
113431 in reply to 113378
John Cowan:
Sure, why not return an exception? It's a first-class object; you can pass it around however you like, pick it apart, whatever. Nothing says you *have* to throw it.


Sarcasm usually doesn't come across clearly in text, so I'm going to have to assume you're not joking.

Please stay away from all languages that treat exceptions as first-class objects.

Re: Catch and Release

2007-01-25 11:47 • by Smudge (unregistered)
113434 in reply to 113388
AndrewB:
Smudge:
Whenever I see "== true" or "== false", it's always a guarantee that the rest of the code will be awful too. This one didn't disappoint,


Ever heard of readability?


Ever heard of DRY? If you're going to test a boolean against a boolean to get a boolean, why stop there? Why don't you test the result of that against another boolean, just to be sure?

Re: Catch and Release

2007-01-25 11:47 • by So..... (unregistered)
113436 in reply to 113382
Smudge:
Whenever I see "== true" or "== false", it's always a guarantee that the rest of the code will be awful too. This one didn't disappoint,


Very true! One of me pet hates is when people put

<code>if(true==isEnabled){}</code?

I know that it stops you accidentally assigning the value but come on it is just wrong!

Re: Catch and Release

2007-01-25 11:47 • by woohoo (unregistered)
113437 in reply to 113417
Andrew Bell:
Returning exceptions isn't so nutty. Not to say that this is good code, but it could be useful. Maybe some callers will care that things failed, and some callers don't care. Those that care can check to see if an exception is returned. Those that don't care don't have to do ANYTHING. To ME, this is nicer than:

try {
call();
}
catch (Exception ex)
{}

Returning the exception provides everything you want to know IF you want to know it.


sorry, but no.
it *is* nutty to return exceptions. one of the main reasons for exceptions is the fact that you are *required* to handle them (with the notable exception ;o) of runtime/unchecked exceptions, which are nevertheless always handed up the call stack and manifest themselves somewhere), and it is usually not just for the fun of it if any method throws an exception.

if you are just returning exceptions it is even easier to just ignore or swallow them unnoticed.

the fact that very many exceptions are indeed "handled" in the way you are insinuating in your code snippet just shows how many developers do not have the dimmest of ideas about the concepts of "their" languages. code like above should immediately result in a release - of the developer in question, that is ;o)

thankfully modern IDEs (like eclipse e.g.) issue warnings about empty blocks, so it is easier to nail down whick devloper is to be "released"... ;o)

captcha: sanitarium ;o))

Re: Catch and Release

2007-01-25 11:59 • by Smudge (unregistered)
113448 in reply to 113437
it *is* nutty to return exceptions. one of the main reasons for exceptions is the fact that you are *required* to handle them (with the notable exception ;o) of runtime/unchecked exceptions,


...which are all that C# has.

Re: Catch and Release

2007-01-25 12:04 • by Baston (unregistered)
113455 in reply to 113384
Frzr:
"Catch and Release is more than just a way of hurting fish for fun." -- Dogbert


Catch and Throw is far worse :o)

Re: Catch and Release

2007-01-25 12:15 • by Stephen (unregistered)
113462 in reply to 113390
snoofle:
A few years ago, I worked with this woman who was a former Lisp programmer, and she used to bury her expressions in lots of layers of parentheses, just because the compiler would effectively ignore them:

if ((( x == 3 ))) { ... }


I learned C on a machine with 64K RAM. That compiler had a quirk: every time it ran into an open-parenthesis or a left-curly-bracket, something inside it recursed, with neither a recursion limit nor stack bounds-checking. Its behavior under stack overflow usually wasn't to crash. Instead it would generate bad code. (Oh, the second half of my function? I guess I didn't need it anyway... That illegal assembly code the compiler generated? It must be the assembler's problem...)

If your coworker's post-death fate should be the firey place down below, that old compiler is probably waiting for her. :-)

Re: Catch and Release

2007-01-25 12:20 • by Anon (unregistered)
113464 in reply to 113436
I will stand up and defend if(false==something) because, personally, I read code like this:

if (something happens) {
do something;
}

If the statement in the if is required to be false that is *abnormal*, and I don't like to include anomalies in my code without making them highly visible.

Doing if(true==something) is retarded, though.

Re: Catch and Release

2007-01-25 12:26 • by htg (unregistered)
113467 in reply to 113428
dwm:
htg:


%t = {
captcha => "pirates";
};


Your Perl really is rusty. That should be:

%t = (
captcha => "pirates",
);


I saw that after posting, no edit functionality! (I had written $t{'captcha'} = "pirates"; and decided to change it, honest!)

Re: Catch and Release

2007-01-25 12:45 • by webdev101 (unregistered)
113477 in reply to 113385
snoofle:
Odhran:
What a bloody awful piece of code!!

They obviously never heard of XSL transforms either ... for shame!

Yeah, because someone who would write something like this needs to be turned loose with XSLT...

... not saying XSLT is the wrong way, just this seems like the wrong person to attempt to use it!


more like wrong person to attempt to use anything!
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment