|
Whilre reviewing the code from some contract developers, I have found a common construct:
<br />
public void SomeMethod()<br />
{<br />
try<br />
{<br />
}<br />
catch (Exception exception)<br />
{<br />
throw exception;
}<br />
}<br />
Why just catch and re-throw an exception? This just adds to the complexity of the execution without doing anything in return.
|
|
|
|
|
I would wrap that in an # if DEBUG ; too many times when debugging I can't figure out where an exception happened, especially when there's a finally involved.
|
|
|
|
|
Yep it can be useful for debugging. Anyway, I'd use just throw; to rethrow exception, as that way I don't lose stack trace IIRC.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
|
|
|
|
|
You know you can get VS to break on exceptions when debugging so even if the code is wrapped in a try - catch / finally block it'll always break when the exception is thrown rather than when it's un-handled.
|
|
|
|
|
I agree with the use of conditional statements for this purpose, but they are not there and this is supposedly debugged code. I can also see re-throwing if there is a finally block cleaning up, regardless of the use of conditionals.
What I didn't show was that there are typically three or four lines of code inside the try block...
|
|
|
|
|
I once worked with a programmer who wrote code like that. He yelled at me (in front of the big boss) for getting rid of it. It turns out he wasn't aware that .Net puts a stack trace in Exception objects! The real lesson that *I* learned is "old habits die hard"; he was still using techniques from back in the C++ / COM days.
|
|
|
|
|
I feel you pain. Old habits do die hard, so the trick is learning the right habbits. Asking yourself "Do we have a backup?" is the best habbit to have. Beyond that is a functional understanding of what the compiler is doing to your source, regardless of the language. Unfortunately too many people try to compare a medium-level language like C or C++ to a high level language like C# based on the syntactical similarities alone.
|
|
|
|
|
Actually, this code has a somewhat subtle behavior, since it is catching by-value, prior to throwing.
If a derived exception is thrown, it will be "clipped" to the base class when it is thrown. This could be potentially
different from invoking throw, which would throw the derived class.
|
|
|
|
|
One of my favorite incidents happened over 10 years ago when computers were much less powerful. An engineer came to me with a financial forecast spread sheet that was taking 4-6 hours to run. After speaking with him about what he was attempting to accomplish, I rewrote his application in FORTRAN (the engineering language of choice at that time) and he was able to do an analysis in 6 seconds! That is not a misprint.
This engineer was intimately familiar with spread sheets, his only tool was a hammer, so everything looked like a nail.
Brian Leach
|
|
|
|
|
Brian Leach wrote: This engineer was intimately familiar with spread sheets,
won't touch it. nope. not gonna.
|
|
|
|
|
I'm going through the same thing now, inherited a bunch of Excel "reports" to maintain. Oh, the horror!
-- modified at 20:29 Monday 5th March, 2007
Oh, and... every tool is a hammer.
|
|
|
|
|
VBA/Excel03 still has the same performance penalties over modern languages. >30min to ~1s (timed from button click to completed dialog appeared, so a decent chunk of that second was form loading)
--
Rules of thumb should not be taken for the whole hand.
|
|
|
|
|
In the last company I worked (about 8 years back), I was asked to fix a bug in some invoice processing module. The main code was in a single function which had about 30,000 (hyperbole, don't remember the xact no but it was enormous) lines which is a WTF in itself. It was a common practice in the whole application. When looking at the issue to solve, I also found that there was a major issue that caused the code database to rollback even when the processing was successful. Believe it or not the issue was that braces did not match properly in that 30000 lines of code. I wish I kept the original code but after struggling for about a day I finally re factored the code and separated into various functions and made it work.
It did not here. Some people working at a client's place found the issue of Rollback and notified the team leader. The team leader compared the code and thought that it was my changes that lead to this issue. I debated with him for lot of time but apparently he did not like the idea of clean code. He (who also happened to like me) had worked with the app for about 5 years and was probably not used to the clean code. Finally, the QA tests revealed that my code was the one which was working properly. However, I still had to remove all the refactorings I did to my code (mostly function extractions) and put back in that one large function.
Needless to say that I left the company in another 2 months.
|
|
|
|
|
Yup. That is one issue that sneaks up on all of us. In the past I've had to "make only those modifications that will fix the described issue" for what were called patch builds. And as you can guess, when the customer received and used the "fixed" code, it broke just as badly as before. That company is now struggling to survive.
Phil
|
|
|
|
|
Wow - that's amazing. I've seen some terribly long functions ( which I then refactor, obviously ), but nothing that bad. What a nightmare.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I can fully understand your reasons for leaving. Makes you thing that no one has ever heard of structured coding.
The worst I have seen was while extending a DLL written in C. Apparently the orignial developer did not understand how to pass parameters, instead using about 2000 global variables.
Fortunately I was able to put my foot down and get rid of all but one or two globals.
|
|
|
|
|
For the last few months, i've been enhancing and maintaining a library written in VB.NET.
I've done a lot of other stupid, miserable things during this time, but this library pretty much takes the cake in terms of filling me with shame and self-loathing at the end of a long day. It's somewhat akin to knowingly feasting on mudpies, all day, for weeks at a time...
----
It appears that everybody is under the impression that I approve of the documentation. You probably also blame Ken Burns for supporting slavery.
--Raymond Chen on MSDN
|
|
|
|
|
Shog9 wrote: For the last few months, i've been enhancing and maintaining a library written in VB.NET.
I've done a lot of other stupid, miserable things during this time, but this library pretty much takes the cake in terms of filling me with shame and self-loathing at the end of a long day.
So why didn't you just rewrite the thing?
|
|
|
|
|
The Grand Negus wrote: So why didn't you just rewrite the thing?
Well, i am, a bit at a time. But technically, i'm not supposed to be spending all that much time on it - the original plan was, i'd take over ownership of it, but any work would be done by outside consultants, thereby freeing me up to work on more interesting projects. So... now i'm slowly learning to program consultants. In the meantime, there's still work that needs to be done...
----
It appears that everybody is under the impression that I approve of the documentation. You probably also blame Ken Burns for supporting slavery.
--Raymond Chen on MSDN
|
|
|
|
|
Shog9 wrote: So... now i'm slowly learning to program consultants.
Big job. What language are you using for that?
|
|
|
|
|
Since i don't know Hindi, i'm trying to use English. It's not a very good fit.
----
It appears that everybody is under the impression that I approve of the documentation. You probably also blame Ken Burns for supporting slavery.
--Raymond Chen on MSDN
|
|
|
|
|
Is there any reason you can't convert it to C# ? Excellent tools exist for this purpose.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Other than the one you illustrated below?
It's not the syntax that really irritates me; it's the little programming techniques that, by now, just scream "VB" to me: insufficient error handling, massive amounts of duplicate code, unnecessary use of late binding, etc. Every time i need to make significant changes to a class or routine, i'll re-write it, to where it reads the same as it would have if i'd written it in C#. Eventually, i'll either have re-written the whole thing to where converting it to C# will be painless... or i'll have pushed the project off onto some other poor sucker, and won't care anymore...
----
It appears that everybody is under the impression that I approve of the documentation. You probably also blame Ken Burns for supporting slavery.
--Raymond Chen on MSDN
|
|
|
|
|
You do have "Option Strict" on?
|
|
|
|
|
It's set up to warn. Eventually, i'll manage to clear up all the warnings...
----
It appears that everybody is under the impression that I approve of the documentation. You probably also blame Ken Burns for supporting slavery.
--Raymond Chen on MSDN
|
|
|
|