A question of indentation!
-
What about this? :)
switch (1)
{
case 1: if (!call1()) break;
case 2: if (!call2()) break;
case 3: if (!call3()) break;
...
}Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans?That was indeed innovative Tomasz :-) Nish
Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.
-
I don't buy performance-related arguments, at least not in 100%. While there are the situations in which this could matter (thight loops etc), in my example you'd access disk. For sure this operation would be orders of magnitude slower than throwing/catching exceptions. The same applies to displaying anything on the screen or accepting user input. In short, the cost associated with exception handling would be totally invisible. Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans?Tomasz Sowinski wrote: While there are the situations in which this could matter (thight loops etc), in my example you'd access disk As everything in software development there's no such thing as an absolute certainty. That's why I used the word "usually". And I still believe it's a bad idea to getting used to use exceptions for other things besides error handling. Tomasz Sowinski wrote: In short, the cost associated with exception handling would be totally invisible. Remember the cost of exceptions isn't only related to speed, size matters too! (Doesn't matter what some women say :-O ;))
Eddie Velasquez: A Squeezed Devil
Checkout General Guidelines for C# Class Implementation -
Post more code - especially, how PGPError is returned and when it means an error. Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans?Tomasz Sowinski wrote: Post more code Tomasz Sowinski said that on the Lounge ;-) ;-) Nish
Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.
-
Nish - Native CPian wrote: Shucks! It always comes down to the basics and theory. If my OOP theory was strong enough I'd not have run into this situation Nish, don't get discouraged. The prime motivation for most of us to learn OOP was because of these things. Don't shy away from OOP. In a few months you will be the one telling others to wrap their functions into objects. C++ Programmers do it in class Drinking In The Sun Forgot Password?
[James Pullicino] wrote: Nish, don't get discouraged. The prime motivation for most of us to learn OOP was because of these things. Don't shy away from OOP. In a few months you will be the one telling others to wrap their functions into objects. Thanks James! :-) Nish
Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.
-
Nish - Native CPian wrote: if (call1() && call2() && call3() ....) That won;'t work. I am not supposed to call call-n unless call-n-1 returns true! :confused: Why won't that work then? With C++, you have "short-circuit" evaluation - that is, if the first item in an If statement fails, the rest won't even get evaluated. For instance, I have code like this all the time (well, when I'm not using CStrings... :-D) : if(pStr != NULL && strlen(pStr) > 0) { ... } If pStr is NULL, the first condition fails, and the second condition (strlen) won't even be evaluated. No generalization is 100% true. Not even this one.
Navin wrote: If pStr is NULL, the first condition fails, and the second condition (strlen) won't even be evaluated. PGP calls don't return true/false/1/0. They return a PGPError type. We gotta call IsPGPError() on the return value to figure out if it's an error. On some occasions an error is permissible. Like if we have a call that works only with DSS keys, then if it fails for an RSA key it's not really an error. Nish
Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.
-
Eddie Velasquez wrote: if(!call3()) return; I can't do that. Since I need to do some cleaning up as well :( Nish
Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.
Some people use goto :~ for this kind of stuff. Basically it works something like this:
if(someCondition) { // acquire some resources... // some error is detected - bail out goto cleanup\_1; } if(someOtherCondition) { // acquire some more resources... // some error is detected - bail out goto cleanup\_2; } // perhaps some more code return true; // for success
cleanup_2:
// cleanup resources acquired up to "goto cleanup_2"
cleanup_1:
// cleanup resources acquired up to "goto cleanup_1"
return false; // for failureThis is the only instance where I'm prepared to defend the usage of goto's. It's fast (no exception handling overhead), it's not spaghetti code and it's quite readable! Sonorked as well: 100.13197 jorgen FreeBSD is sexy.
-
Tomasz Sowinski wrote: While there are the situations in which this could matter (thight loops etc), in my example you'd access disk As everything in software development there's no such thing as an absolute certainty. That's why I used the word "usually". And I still believe it's a bad idea to getting used to use exceptions for other things besides error handling. Tomasz Sowinski wrote: In short, the cost associated with exception handling would be totally invisible. Remember the cost of exceptions isn't only related to speed, size matters too! (Doesn't matter what some women say :-O ;))
Eddie Velasquez: A Squeezed Devil
Checkout General Guidelines for C# Class ImplementationEddie Velasquez wrote: Remember the cost of exceptions isn't only related to speed, size matters too! So how much bigger my program will be if I replace multiple nested ifs with try/throw/catch? Assuming that you're using exceptions to catch 'real' errors the difference is zero. Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans? -
peterchen wrote: do { // while(0) ok = hamlet(); if (!ok) break; ok = ophelia(); if (!ok) break; ....} while(0); Cool! I like your do while(0) idea. That's what I am gonna use I think :-) Nish
Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.
Nish - Native CPian wrote: I like your do while(0) idea do { } while(0) is a really cool technique, especially with macros. It is easy to mistype macros in such ways that they look syntactically correct, but expanded they wreak havoc. Consider this:
#define RET_IF_FAIL(x) if(FAILED(x)) return
if(someCondition)
RET_IF_FAIL(m_pObject->DoTheChokaChoka());
else
MessageBox(NULL, "someCondition not met", "Status report", MB_OK);The else will be connected to your "hidden if" inside RET_IF_FAIL which is clearly not the intent. The expanded code will look like:
if(someCondition)
if(FAILED(m_pObject->DoTheChokaChoka())) return;
else
MessageBox(NULL, "someCondition not met", "Status report", MB_OK);Scary stuff! I've had my fair share of these bloody mistakes! Solution? Keep on reading:
#define RET_IF_FAIL(x) do { if(FAILED(hr)) return; } while(0)
if(someCondition)
RET_IF_FAIL(pObject->DoTheChokaChoka());
else
MessageBox(NULL, "someCondition not met", "Status report", MB_OK);This is much better since the "hidden if" is wrapped inside a block which has an associated language construct - the do while. Thus the if nor the block cannot be "misconnected". The expanded code will be:
if(someCondition)
do { if(FAILED(m_pObject->DoTheChokaChoka())) return; } while(0);
else
MessageBox(NULL, "someCondition not met", "Status report", MB_OK);And the best part is that a do { } while(0) is totally costless! The compiler will optimize away the loop expression/jump so that it will not add any extra garbage to your code. Sonorked as well: 100.13197 jorgen FreeBSD is sexy.
-
Eddie Velasquez wrote: Remember the cost of exceptions isn't only related to speed, size matters too! So how much bigger my program will be if I replace multiple nested ifs with try/throw/catch? Assuming that you're using exceptions to catch 'real' errors the difference is zero. Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans?Tomasz Sowinski wrote: So how much bigger my program will be if I replace multiple nested ifs with try/throw/catch? Assuming that you're using exceptions to catch 'real' errors the difference is zero. It depends. A lot of factors come into play, obvious ones: if you define variables with ctors and dtors between the ifs and if the code is templated. As I said before: it all depends on the particular case. In some scenarios even the use of exceptions for error reporting is overkill. For most developers getting into the habit of misusing exceptions (or templates or whatever relatively obscure language feature) makes them produce bad code. I'm not saying that you or me will produce buggy or bad code (we don't write buggy code, do we? ;) ) But the average developer is so lame that (s)he acts like a robot without really thinking about the code (s)he is writing and the consecuences of the design decisions made.
Eddie Velasquez: A Squeezed Devil
Checkout General Guidelines for C# Class Implementation -
Tomasz Sowinski wrote: So how much bigger my program will be if I replace multiple nested ifs with try/throw/catch? Assuming that you're using exceptions to catch 'real' errors the difference is zero. It depends. A lot of factors come into play, obvious ones: if you define variables with ctors and dtors between the ifs and if the code is templated. As I said before: it all depends on the particular case. In some scenarios even the use of exceptions for error reporting is overkill. For most developers getting into the habit of misusing exceptions (or templates or whatever relatively obscure language feature) makes them produce bad code. I'm not saying that you or me will produce buggy or bad code (we don't write buggy code, do we? ;) ) But the average developer is so lame that (s)he acts like a robot without really thinking about the code (s)he is writing and the consecuences of the design decisions made.
Eddie Velasquez: A Squeezed Devil
Checkout General Guidelines for C# Class ImplementationEddie Velasquez wrote: In some scenarios even the use of exceptions for error reporting is overkill. Hard real time systems controlling nuclear reactors, yes. But not for majority of non-embedded stuff. Anyway, Standard C++ uses exceptions for basic functionality, like reporting failures from new operator. Your code already has exception frames set. Eddie Velasquez wrote: But the average developer is so lame that (s)he acts like a robot without really thinking about the code (s)he is writing and the consecuences of the design decisions made. So it's better to have dozen of nested ifs and 'manual' cleanup code? C'mon :) Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans? -
Eddie Velasquez wrote: In some scenarios even the use of exceptions for error reporting is overkill. Hard real time systems controlling nuclear reactors, yes. But not for majority of non-embedded stuff. Anyway, Standard C++ uses exceptions for basic functionality, like reporting failures from new operator. Your code already has exception frames set. Eddie Velasquez wrote: But the average developer is so lame that (s)he acts like a robot without really thinking about the code (s)he is writing and the consecuences of the design decisions made. So it's better to have dozen of nested ifs and 'manual' cleanup code? C'mon :) Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans?Tomasz Sowinski wrote: Hard real time systems controlling nuclear reactors, yes. No need for exageration. Tomasz Sowinski wrote: Standard C++ uses exceptions for basic functionality, like reporting failures from new operator. Not Visual Studio. At least version 6.0 Tomasz Sowinski wrote: So it's better to have dozen of nested ifs and 'manual' cleanup code? C'mon More exageration. When a function is overly complex you split it in managable units. Destructors were invented for cleanup. Why did you quote the word 'manual'? I don't recall mentioning manual cleanup code. :confused: I've never advocated writing spagetti code or not using exceptions or templates or whatever in my code. Just use 'em where they should be used and try hard not to grow bad habits.
Eddie Velasquez: A Squeezed Devil
Checkout General Guidelines for C# Class Implementation -
Indentation is nice. In fact code that is not indented is an absolute pain to even look at. But then sometimes you get into absurd levels of indentation. Right now I am working with the PGP SDK. For certain operations I need to call about 7-10 functions sequentially. The problem is that each of these functions can be called ONLY if all the previous functions are successful. Thus I have something like this.
if(call1())
{
if(call2())
{
if(call3())
{
if(call4())
{
if((call5())
{
if(call6())
{That's just a sample, just the tip of the large iceberg. Often it get's a LOT more deeply nested than I have shown above! In such situations can we actually do away with indentation at least partially? For example would it be considered okay to do this.
if(call1())
{
if(call2())
{
if(call3())
{
if(call4())
{
if((call5())
{
if(call6())
{I have maintained a little indentation, but it's not perfectly done! Your comments are welcome
Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.
How about:
if (call1() && call2() && call3() &&
call4() && call5() && call6()) {
// Success
} else {
// Error
}/ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com
-
Tomasz Sowinski wrote: Hard real time systems controlling nuclear reactors, yes. No need for exageration. Tomasz Sowinski wrote: Standard C++ uses exceptions for basic functionality, like reporting failures from new operator. Not Visual Studio. At least version 6.0 Tomasz Sowinski wrote: So it's better to have dozen of nested ifs and 'manual' cleanup code? C'mon More exageration. When a function is overly complex you split it in managable units. Destructors were invented for cleanup. Why did you quote the word 'manual'? I don't recall mentioning manual cleanup code. :confused: I've never advocated writing spagetti code or not using exceptions or templates or whatever in my code. Just use 'em where they should be used and try hard not to grow bad habits.
Eddie Velasquez: A Squeezed Devil
Checkout General Guidelines for C# Class ImplementationEddie Velasquez wrote: Not Visual Studio. At least version 6.0 What's the importance of this? :-D Eddie Velasquez wrote: More exageration. Just a little bit :) Anyway, we're not going to agree. Let's finish this discussion. Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans? -
Eddie Velasquez wrote: Not Visual Studio. At least version 6.0 What's the importance of this? :-D Eddie Velasquez wrote: More exageration. Just a little bit :) Anyway, we're not going to agree. Let's finish this discussion. Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans?Tomasz Sowinski wrote: What's the importance of this? Well, a lot of developers use it as it's main compiler? And the some big chunks of the standard aren't correctly handled by VC6 (and VC7). And that you implied (or so I interpreted) that because standard C++ uses exceptions all over the place then everybody is using exceptions unknowingly, so the exception overhead has already been payed for. And I said that VC6 doesn't conform very well so the exception overhead isn't automatically included. Tomasz Sowinski wrote: Anyway, we're not going to agree. Let's finish this discussion. That's ok with me.
Eddie Velasquez: A Squeezed Devil
Checkout General Guidelines for C# Class Implementation -
Tomasz Sowinski wrote: What's the importance of this? Well, a lot of developers use it as it's main compiler? And the some big chunks of the standard aren't correctly handled by VC6 (and VC7). And that you implied (or so I interpreted) that because standard C++ uses exceptions all over the place then everybody is using exceptions unknowingly, so the exception overhead has already been payed for. And I said that VC6 doesn't conform very well so the exception overhead isn't automatically included. Tomasz Sowinski wrote: Anyway, we're not going to agree. Let's finish this discussion. That's ok with me.
Eddie Velasquez: A Squeezed Devil
Checkout General Guidelines for C# Class ImplementationEddie Velasquez wrote: And I said that VC6 doesn't conform very well so the exception overhead isn't automatically included It's included by default. Projects you create with wizard have exception handling turned on. MFC uses exceptions. Built-in COM support uses exceptions... Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans? -
Tomasz Sowinski wrote: Post more code Tomasz Sowinski said that on the Lounge ;-) ;-) Nish
Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.
Ok, ok, you've got me. But rules are made to break them, right? :-D Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans? -
Indentation is nice. In fact code that is not indented is an absolute pain to even look at. But then sometimes you get into absurd levels of indentation. Right now I am working with the PGP SDK. For certain operations I need to call about 7-10 functions sequentially. The problem is that each of these functions can be called ONLY if all the previous functions are successful. Thus I have something like this.
if(call1())
{
if(call2())
{
if(call3())
{
if(call4())
{
if((call5())
{
if(call6())
{That's just a sample, just the tip of the large iceberg. Often it get's a LOT more deeply nested than I have shown above! In such situations can we actually do away with indentation at least partially? For example would it be considered okay to do this.
if(call1())
{
if(call2())
{
if(call3())
{
if(call4())
{
if((call5())
{
if(call6())
{I have maintained a little indentation, but it's not perfectly done! Your comments are welcome
Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.
Basically, what you want to do is call bunch of functions one after the other, but stop calling them once you get error. Here is code that will do that: BOOL rc = TRUE; if (rc) rc = rc && call1(); if (rc) rc = rc && call2(); if (rc) rc = rc && call3(); ... if (!rc) { // error handling code } else { // continue processing } You can use for any kind of sequential processing of functions, testing flags, etc. Best wishes, Hans
-
Eddie Velasquez wrote: if(!call3()) return; I can't do that. Since I need to do some cleaning up as well :( Nish
Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.
Then why not this?
if(!call1())
{
// cleanup
return;
}
if(!call2())
{
// cleanup
return;
}
if(!call3())
{
// cleanup
return;
}Or:
if(!call1())
{
cleanup1();
return;
}
if(!call2())
{
cleanup2();
cleanup1();
return;
}
if(!call3())
{
cleanup3();
cleanup2();
cleanup1();
return;
} -
How about:
if (call1() && call2() && call3() &&
call4() && call5() && call6()) {
// Success
} else {
// Error
}/ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com
:cool: Best regards, Alexandru Savescu
-
Basically, what you want to do is call bunch of functions one after the other, but stop calling them once you get error. Here is code that will do that: BOOL rc = TRUE; if (rc) rc = rc && call1(); if (rc) rc = rc && call2(); if (rc) rc = rc && call3(); ... if (!rc) { // error handling code } else { // continue processing } You can use for any kind of sequential processing of functions, testing flags, etc. Best wishes, Hans
You can remove && from expressions and use rc = callN() only. After all, this is executed only if rc is true - you're testing this condition few cycles before rc = rc && callN(). Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans?