|
Reminds me of Resume Next from a particularly dark past!
Somewhat, but it's easier to limit the effect of catch-and-ignore to a single statement. Realistically, there are quite a few circumstances in which if an error happens very often the developer should be made aware of it, but beyond that there's no particularly useful action the code can take in response to an error.
For example, if a field that affects the display of a control is changed outside the UI thread, it's necessary to use BeginInvoke to let the control update itself. If the BeginInvoke works, great. If it fails because someone closed a window at just the "right" time, so what? It's important to ensure that the code doesn't waste time repeatedly trying to BeginInvoke a control after it's disposed, but otherwise occasional invocation failures should be expected and I really don't see anything useful to be done with them other than ignore them.
|
|
|
|
|
supercat9 wrote: Somewhat, but it's easier to limit the effect of catch-and-ignore to a single statement. Realistically, there are quite a few circumstances in which if an error happens very often the developer should be made aware of it, but beyond that there's no particularly useful action the code can take in response to an error.
In my app, this is the rare case. I think I have one or two and I commented like this
catch(Exception e)
{
// nothing we can do about an error here
}
Unfortunately, for a lot of people, exception handling consists of
catch(...)
{
}
Somewhere they learned this is the proper way to handle exceptions. If it prevents the app from crashing (at this point anyway), that's all you need to do. If you blow up a buffer, hide it and let the next guy deal with it.
|
|
|
|
|
If something like an empty try/catch is discovered in code that I am in charge of (and believe me, I will discover it, thanks to FxCop), I will refuse to accept it for production code.
If an error happens and absolutely no action can or should be taken (a very rare case), then at least the exception has to be logged and there has to be a comment in the code that explains why it's OK to just "swallow" the exception in this special case. This is the very least for good code...
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Thomas Weller wrote: If something like an empty try/catch is discovered in code that I am in charge of (and believe me, I will discover it, thanks to FxCop), I will refuse to accept it for production code.
If an error happens and absolutely no action can or should be taken (a very rare case), then at least the exception has to be logged and there has to be a comment in the code that explains why it's OK to just "swallow" the exception in this special case. This is the very least for good code...
Regards
Thomas
All I can add is AMEN
I will confess that in my code above where I "swallowed" the exception was when a thread was ending and his parent had already disappeared. Which in practice, has never happened. I believe that is the only time I have done this.
I sent out an email a few years ago telling people to add exception handling (since I throw exceptions) and their response was to add catch (...) {} all over the place. Fortunatley, I log all exceptions at the source.
|
|
|
|
|
I agree.
There are some rare cases where doing nothing is the correct thing to do. A third-party library we use throws an exception if you call Init() more than once, but doesn't actually mess up the state of the library. Our app is multithreaded, so we have an empty catch block there.
Mind you, these edge cases are well documented with comments.
|
|
|
|
|
Yes, sometimes is a good reason to use an empty catch. It still only acceptable if it is well documented as to why it was done so that some poor programmer a few years down the road doesn't have to waste time trying to figure out how the exception should really be handled.
Just because the code works, it doesn't mean that it is good code.
|
|
|
|
|
LMAO
|
|
|
|
|
I saw this at work. I guess the coder wanted to do a series of validations and return an error when the first failure condition was found. I don't know if you'll consider this a horror or not, but it is definitely a backwards way of doing this.
Select Case (true)
Case [failure condition 1]
Throw New Exception("Condition 1 failed.")
Case [failure condition 2]
Throw New Exception("Condition 2 failed.")
'etc...
Else
'Success!
End Select
|
|
|
|
|
Looks ok for me. Why a backwards way?
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Why? Normally the test expression is evaluated at runtime, and the result is compared to each case until a match is found. In my experience, the case expressions are usually constant, and they are usually unique values (in C# & C++ these behaviors are enforced).
Here, the test expression is the constant, and the case expressions are calculated at runtime. And, since in the actual example there were several expressions that were returning boolean results, there were several cases that would have the same values.
That is what I mean by backward. It certainly works, and maybe it just struck me as odd because my background is predominantly C++ & C#. But, the company I'm at has a large body of VB.NET code and this is the only instance I've seen of a case statement done this way. And, it is backward compared to the example in MSDN as well.
It seems like if you want to evaluate a series of expressions until you find one that is true, using an if-then-elseif would be more appropriate. Just my opinion.
|
|
|
|
|
Of course it wouldn't make sense in neither C++ nor C# code. But since VB doesn't require case expressions to be constant, the syntax above is acceptable. Anyway it's not just your opinion -- I find this odd and confusing too. On the other side, I am not a VB guy and maybe it's normal to them and you have to get used to Switch (True) mess.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Perhaps more interesting would be a use I've seen suggested for "Catch ... When" in vb.net: have the "When" clause invoke a function which logs the exception and always returns true (if the goal is to catch the exception, but use a centralized logging facility) or always returns false (if the goal is to log the exception and related information without having to catch and rethrow the exception).
|
|
|
|
|
Think of it as If ElseIf statements with uniform indentation of the conditionals.
Of course in this case it could be reduced to:
If [failure condition 1] Then _
Throw New Exception("Condition 1 failed.")
If [failure condition 2] Then _
Throw New Exception("Condition 2 failed.")
'etc...
'Success!
But in defence of Select Case True , it used to be the easiest way to get short-circuiting in VB6. Could this code have been around long enough to have been converted?
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)
|
|
|
|
|
So, I'm doing a C++ class.
As I taught myself a few things in C++ and am a prper C# programmer, the first of three months is nothing but child's play.
But the problem is the teacher.
Whenever I try to do something like it's donw right, using good practices, he tell me it's not right in his course and after a short discussion where I try to tell him how it's done properly, he always says "but not in my class".
Every time I ask him something a tad more advance then what he's currently teaching, he complains about me using such advanced techniques and makes me use some beginner's stuff that is plain wrong.
The best is that he always complains and argues about my data encapsulation.
It started when he told me get and set methods were bad and I should always make every meber public, except the rare case that I have actual checking done in these methods.
And now, I created a program for the current project with a 3 tier architecture, data, console in/output and file in/output.
Yet he tells me I shouldn't and it's wrong and made me dump all methods into the data classes.
I tried to explain him that this is the right way, but as always he only tells me what he teaches is right, of course he doesn't have experience working as programmer at all but always only taught it.
I abide, because it takes only a handful minutes for me and I don't want to risk getting a lower grade because I didn't do what he said.
Did I mention that he doesn't even try to understand when don't do things 100% exactly as he says?
He just says "sorry, I don't understand" and leaves, without even looking once at the code.
But, the good part is that it's only one more week.
Monday after next I'll have a new teacher and start with C++ windows programming, I really hope that new teacher isn't such a blockhead.
modified on Friday, March 13, 2009 10:15 AM
|
|
|
|
|
That's not a coding horror; that's a teaching horror. Show some examples of what he thinks is good code.
|
|
|
|
|
Megidolaon wrote: The best is that he always complains and argues about my data encapsulation.
It started when he told me get and set methods were bad and I should always make every meber public, except the rare case that I have actual checking done in these methods. Sigh
And now, I created a program for the current project with a 3 tier architecture, data, console in/output and file in/output.
Yet he tells me I shouldn't and it's wrong and made me dump all methods into the data classes.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
The code itself is okay, but he disallows all good practices I try to use.
|
|
|
|
|
I had the same thing on my university. However I just did things 100% exactly as they wanted. Senior teachers are not a material which can be reformed, so just botch all these homeworks up, pass a damn exam and go for a beer.
And yes, I was supposed to put "Write" and "Save" methods in data containers...
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Yeah, I do as he says, no use in fighting back.
That's why I made this topic, to vent my anger...
|
|
|
|
|
He's actually a very good teacher.
The lesson here is to prepare you for dealing with your future pointy haired boss[^] that will be telling you to do far more incomprehensible things on a daily basis.
|
|
|
|
|
If I ever encounter such a boss, I'll get a new job.
|
|
|
|
|
a wise man once said
"those who can DO... those who cant TEACH"
very true sometimes... although thankfully there are exceptions to this rule
|
|
|
|
|
But the flip-side is, "you haven't truly mastered a subject until you've taught it".
|
|
|
|
|
I disagree with your attitude about the class.
There are situations where it's appropriate to use a 3-tier architecture, or to wrap public members into accessor methods. But you need to know how to do things the simple way before you can start thinking about these things.
And there are plenty of situations outside class where the "right" way is actually wrong. There are still many environments targetted by various C / C++ compilers where space is still quite limited, and in those cases we must not introduce architectural features that don't add new functionality.
As for the 3-tier architecture, this has no place whatsoever in a class assignment or in any other small program. If you're designing a system which is reasonably complex, and promises to have a long lifetime, and has idenitifiable layers, then there may be economic benefit to thinking in terms of intechangeable tiers. But for a class assignment, it's pure overkill... do you really plan on going back to your old homework code and, say, rewriting the GUI using a different architecture? If not, what possible rationale could you have for using a three-tier approach?
I run into this kind of thinking a lot professionally. Generally, I can sort through the quasi-architectural clutter that results, but this three-tier stuff really mucks things up in my experience.
I'm not saying that it's a bad pattern, simply that it's overapplied. For example, if you ask one novice programmer to write a client module and another to write a server module you're quite likely to get some sort of three-tier client running against a three-tier server. Now the nature of the assignment ("write a client" or "write a server") seems to dictate a client / server architecture; but these meticulous fresh graduates simply cannot live with themselves if they don't "do things right" and use three tiers. As as result, the vast majority of the three-tier implementations out there are pure rubbish.
My general advice is to remember that code is just a means to an end. Beautiful code does nothing in and of itself. By overdoing one particular assignment you're only distracting yourself from the other assignments and challenges you're bound to face down the road.
This is the sort of thing that is taught in Economics class, which is a great thing for a programmer to take in my opinion.
|
|
|
|
|
Reminds me of a database course I had to take once.
The teacher insisted that each table had to be placed in a separate database.
She gave us a simple accounting example that ended up with 10 databases. Account database, Client database, Invoice database, InvoiceItem database etc. Attempts to show how all the tables could be housed in one database were marked wrong & returned for reworking.
She even refused to accept it was bad purely on performance grounds, which we could easily demonstrate (it was glacially slow)!
|
|
|
|
|