Oh my %$%@ god, I can't believe I saw what I saw on that article!!! And the author even has a screenshot with a unhandled application exception O_O.
Microsoft should make FxCop and StyleCop mandatory and include it in every VS version including Express, they would do a great favor to humanity, by eliminating whole classes of "stupid" coding mistakes. Not that will be enough ... but ... still ...
Pop, The exception dialog make sense, he is showing that the concurrency strategy works.
Yes, but what I was finding funny in this, is that he has catch blocks all over, without doing anything (except eating stacktrace info), and the only place where he should have cough a expected exception, (the point of the article) he didn't.
First, it seems to me as if it is Not a Copy at all - Neither a shallow Copy nor a deep copy. The method just returns the same object, as objects are passed by ref. There is no use of returning an object when it is passed by reference. There would be a warning that "Not all Code paths return value". No need of Try catch around a return. No need to enclose an Exception object in catch construct when the exception is rethrown.
I'm sure we all write crap code sometimes. At least the guy could accept the criticism of the comment telling him why his exception "handling" is wrong rather than replying
"It was just a DEMO. I didn't intent to show HOW TO USE "TRY ... CATCH" in C#."
Things like this I've seen at lots of project I have been working with. And always the coder of the ugly code defends himself like the auther does here too.
There's some things wrong with this code, yes, and it was clearly written by someone with little understanding of what's going on 'under the bonnet/hood'. But, here, there's a lot of Dunning-Kruger effect. There are some good reasons to be doing what this code is attempting, and yet no one here wants to mention any of it. Shameful and misguided elitism, IMHO.
Why did you leave out the Disassemble method? It really ties the whole thing together. I actually feel kind of bad for him, but worse for the people reading this as an expert example.
@Grunties, I don' claim to be a smart fellow or an enlightened .Net developer, but what are the reasons you are talking about?
This is not about elitism, this is about spreading bad coding practices, in a community where thousands of developers passionate about their framework and language invest huge amounts of time and dedication to write blog posts, articles and books about good coding practices as a community effort to raise the quality bar of the code .Net developers write in general.
We are all better of when bad coding isn't spread around in articles like this one.
Yes, this is just bad code. Ayende has rightfully surfaced it as such. But I really don't see the value of hosing the author as a useless developer. Have you asked yourself "Why am I laughing at this - publicly, and loudly?".
I watched the tail-end of Romy and Michelle's High School Reunion on TV last night. The laugh comments here reminds me of Christine and her girlfriends ridiculing Romy and Michele at the reunion.
I think we are all capable of being better people than this.
I do not know what disturbs me more, the fact that this article was accepted by CodeProject or the fact that someone out there will use this article in production code.
I've googled some of the code in my company's codebase and have found some CodeProject articles come up.
There is nothing wrong with the code, that is just it, there is nothing. It makes me feel almost zen like... There is nothing wrong... There is no spoon... There is no special ingredient... There is just nothing....
@Aslam : If you are going to compare with something, try something that people have heard of :-)
Seriously though, I thought this post was a bit pathetic until I saw the author's apparent inability to accept his mistakes and fix the code in the article. Then I thought the article was worth mocking.
It's counts not only how good you are when things go right, but also how good you are when things go wrong!
If my code is shit I will be glad for the criticism as I learn from changing it.
I am one of those junior developers who can't see what is so wrong with the code. How would it be different if one would do
Catch(Exception ex)
{ throw; }
In the case of the author he is still throwing eventually. I miss the point.
Also I am not sure what are the inherent problems with accepting a parameter as an object and then returning it? Let me go further and say I don't even understand how does that mean deep copy? Just accepting an object by reference and returning the reference back.
The method actually returns a copy of the reference. .NET uses a copy of the original reference inside a method unless the "ref" keyword is used in the parameter declaration. This is why setting the reference to null inside the method will not change the outside reference.
I have an idea. Let me throw up lousy code, lay back and watch herds of women-deprived geeks laugh about it and throw up more code. Oh, no need to. It has already been done here.
> Email-style angle brackets
> are used for blockquotes.
> > And, they can be nested.
> #### Headers in blockquotes
>
> * You can quote a list.
> * Etc.
Horizontal Rules
Three or more dashes or asterisks:
---
* * *
- - - -
Manual Line Breaks
End a line with two or more spaces:
Roses are red,
Violets are blue.
Fenced Code Blocks
Code blocks delimited by 3 or more backticks or tildas:
```
This is a preformatted
code block
```
Header IDs
Set the id of headings with {#<id>} at end of heading line:
## My Heading {#myheading}
Tables
Fruit |Color
---------|----------
Apples |Red
Pears |Green
Bananas |Yellow
Definition Lists
Term 1
: Definition 1
Term 2
: Definition 2
Footnotes
Body text with a footnote [^1]
[^1]: Footnote text here
Abbreviations
MDD <- will have title
*[MDD]: MarkdownDeep
FUTURE POSTS
RavenDB 7.1: Next-Gen Pagers - 5 days from now
RavenDB 7.1: Write modes - 7 days from now
RavenDB 7.1: Reclaiming disk space - 9 days from now
RavenDB 7.1: Shared Journals - 12 days from now
There are posts all the way to Feb 17, 2025
RECENT SERIES
Challenge
(77): 03 Feb 2025 - Giving file system developer ulcer
Answer
(13): 22 Jan 2025 - What does this code do?
Comments
lol that's hilarious
Lol thats too funny, or to put it into terms the original author would recognize.
try
{
}
catch (AuthorIsAnIdiotException ex)
{
}
lol
At least the exception is being rethrown! ;-)
Beautiful!
Is it wrong that I started laughing out loud when I read this?
OH WAIT. NO. This person's an idiot.
first off... i am surprised that this isn't a generic function (just think of the potential boxing and unboxing performance issues)
as far as i am concerned... you can't get any deeper than this type of "deep copy"
at least the author is using the Single Responsibility principle by calling this method from the Assemble and Disassemble methods
No, the exception is NOT being REthrown.
It is being _thrown_.
Seeing that is a BIG WTF sign in most cases.
Oh my %$%@ god, I can't believe I saw what I saw on that article!!! And the author even has a screenshot with a unhandled application exception O_O.
Microsoft should make FxCop and StyleCop mandatory and include it in every VS version including Express, they would do a great favor to humanity, by eliminating whole classes of "stupid" coding mistakes. Not that will be enough ... but ... still ...
This is enough to make a grown man cry ...
Wow! Everything is wrong with this code starting from the name and ending with exception handling.
Pop,
The exception dialog make sense, he is showing that the concurrency strategy works.
haha, the proper "rethrow" should be:
catch(Exception ex){
throw;
}
This is reassuring! If this is the quality of the code base coming out of East Asia I think we're gonna have job security for quite some time.
@Vince,
I would not call you example proper as well. A proper re-throw could be:
catch (SqlException)
{ throw; }
catch (Exception ex)
{
// handle ex
}
Rethrowing a general System.Exception is pointless.
Something like this would be valid as well:
catch (Exception ex)
{
// handle ex
throw;
}
Really great article!!! Every time when I'll be in a sad mood I'll read it!
Yes, but what I was finding funny in this, is that he has catch blocks all over, without doing anything (except eating stacktrace info), and the only place where he should have cough a expected exception, (the point of the article) he didn't.
LOL, that is fantastic. I haven't laughed at code that quickly in ages (it's normally crying).
So I guess it's a deep copy of a memory pointer. Maybe that's what the author intended.... :P
@Ayende, Vince, Dmitry:
Thrown or rethrown... does it even matter? It is dead code anyways, as "return value" cannot possibly throw an exception.
Or am I missing something?
@Igor,
Click the article link. He uses try/catch blocks like this in every method.
This looks like an entry in a contest to see who can write the highest density of bad code.
First, it seems to me as if it is Not a Copy at all - Neither a shallow Copy nor a deep copy. The method just returns the same object, as objects are passed by ref. There is no use of returning an object when it is passed by reference. There would be a warning that "Not all Code paths return value". No need of Try catch around a return. No need to enclose an Exception object in catch construct when the exception is rethrown.
At least he could made it static LOL :)
Shameful public flogging. Aren't you big men!
I can't believe it! There are people who actually vote 5 on this article!
I'm sure we all write crap code sometimes. At least the guy could accept the criticism of the comment telling him why his exception "handling" is wrong rather than replying
"It was just a DEMO. I didn't intent to show HOW TO USE "TRY ... CATCH" in C#."
I'd fix it out of shame, not defend it!
Good programmers can make mistakes, even as bad as this one. Then there are bad programmers that will always do something like this.
You can tell who's who by their response when you point it out.
Things like this I've seen at lots of project I have been working with. And always the coder of the ugly code defends himself like the auther does here too.
Sometimes is good to cry.
PERFECT :)
BRILLANT
There's some things wrong with this code, yes, and it was clearly written by someone with little understanding of what's going on 'under the bonnet/hood'. But, here, there's a lot of Dunning-Kruger effect. There are some good reasons to be doing what this code is attempting, and yet no one here wants to mention any of it. Shameful and misguided elitism, IMHO.
// tl;dr
duh, everyone knows that this is just a hook for an out-of-VM exception handler.
Why did you leave out the Disassemble method? It really ties the whole thing together. I actually feel kind of bad for him, but worse for the people reading this as an expert example.
public object DeepCopy(object value)
@Grunties, I don' claim to be a smart fellow or an enlightened .Net developer, but what are the reasons you are talking about?
This is not about elitism, this is about spreading bad coding practices, in a community where thousands of developers passionate about their framework and language invest huge amounts of time and dedication to write blog posts, articles and books about good coding practices as a community effort to raise the quality bar of the code .Net developers write in general.
We are all better of when bad coding isn't spread around in articles like this one.
Yes, this is just bad code. Ayende has rightfully surfaced it as such. But I really don't see the value of hosing the author as a useless developer. Have you asked yourself "Why am I laughing at this - publicly, and loudly?".
I watched the tail-end of Romy and Michelle's High School Reunion on TV last night. The laugh comments here reminds me of Christine and her girlfriends ridiculing Romy and Michele at the reunion.
I think we are all capable of being better people than this.
maybe the code was poorly ported from C++ where the object was returned by value and the exception was caught in case the copy constructor failed
Wow...
I do not know what disturbs me more, the fact that this article was accepted by CodeProject or the fact that someone out there will use this article in production code.
I've googled some of the code in my company's codebase and have found some CodeProject articles come up.
Probably you don't have realized that the NH's users generation are changing; this is not a minor problem and sure it is a problem.
There is nothing wrong with the code, that is just it, there is nothing. It makes me feel almost zen like... There is nothing wrong... There is no spoon... There is no special ingredient... There is just nothing....
i haven't laughed so hard in ages. thank you for brightening my day.
It looks like a stub. Move along...
Shame on CodeProject.com.
I'll be extra skeptical next time I see a CodeProject article from an unknown author.
@Grunties : What good reasons are those?
@Aslam : If you are going to compare with something, try something that people have heard of :-)
Seriously though, I thought this post was a bit pathetic until I saw the author's apparent inability to accept his mistakes and fix the code in the article. Then I thought the article was worth mocking.
It's counts not only how good you are when things go right, but also how good you are when things go wrong!
If my code is shit I will be glad for the criticism as I learn from changing it.
Wow, that's something else right there. The creator of that code should give this new fangled learning technique called "reading books" a try.
I call fizzbuzz.
guys.,
I am one of those junior developers who can't see what is so wrong with the code. How would it be different if one would do
Catch(Exception ex)
{ throw; }
In the case of the author he is still throwing eventually. I miss the point.
Also I am not sure what are the inherent problems with accepting a parameter as an object and then returning it? Let me go further and say I don't even understand how does that mean deep copy? Just accepting an object by reference and returning the reference back.
I would be glad for any pointers.
throw; // keep the stack trace
throw ex; // lose the stack trace
Losing the stack trace is _bad_.
Putting a try catch block without doing any error handling is wrong.
DeepCopy should perform a DEEP copy, not a ref copy.
What this method does is return the same reference, so it lies about its impl.
Thanks Ayende. That was quick. I am glad you restored my sanity.
The method actually returns a copy of the reference. .NET uses a copy of the original reference inside a method unless the "ref" keyword is used in the parameter declaration. This is why setting the reference to null inside the method will not change the outside reference.
Well it seems that your criticism of the author has now caused the article to be removed..
I've seen much worse:
try { ... }
catch(Exception ex)
{
Response.Write(ex.Message + "
" + ex.StackTrace);
}
I have an idea. Let me throw up lousy code, lay back and watch herds of women-deprived geeks laugh about it and throw up more code. Oh, no need to. It has already been done here.
It's already been voted into oblivion by the community. Hooray. Thanks for reading The Code Project, though. Hope you voted it down!
It's a shame, that the criticism didn't make the author rewrite it ...
Aslam,
Several reasons:
a) bad code need to be made public, to make sure that people _recognize it as bad code
b) this is actually fairly high on the search for a few important nhibernate concurrency searches
It has now been deleted... I can't get myself into context any more.
How to make this code throw an exception? Would like to see an unit test that demonstrates. Thx.
Brian,
Sort of my point
Comment preview