Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. A question of indentation!

A question of indentation!

Scheduled Pinned Locked Moved The Lounge
questionc++helptutorial
81 Posts 23 Posters 32 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Tomasz Sowinski

    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?

    E Offline
    E Offline
    Eddie Velasquez
    wrote on last edited by
    #66

    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

    T 1 Reply Last reply
    0
    • N Nish Nishant

      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.

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #67

      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

      A 1 Reply Last reply
      0
      • E Eddie Velasquez

        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

        T Offline
        T Offline
        Tomasz Sowinski
        wrote on last edited by
        #68

        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?

        E 1 Reply Last reply
        0
        • T Tomasz Sowinski

          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?

          E Offline
          E Offline
          Eddie Velasquez
          wrote on last edited by
          #69

          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

          T 1 Reply Last reply
          0
          • E Eddie Velasquez

            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

            T Offline
            T Offline
            Tomasz Sowinski
            wrote on last edited by
            #70

            Eddie 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?

            1 Reply Last reply
            0
            • N Nish Nishant

              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.

              T Offline
              T Offline
              Tomasz Sowinski
              wrote on last edited by
              #71

              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?

              1 Reply Last reply
              0
              • N Nish Nishant

                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.

                H Offline
                H Offline
                Hans Dietrich
                wrote on last edited by
                #72

                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

                T N 2 Replies Last reply
                0
                • N Nish Nishant

                  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.

                  B Offline
                  B Offline
                  Brit
                  wrote on last edited by
                  #73

                  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;
                  }

                  1 Reply Last reply
                  0
                  • R Ravi Bhavnani

                    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

                    A Offline
                    A Offline
                    Alexandru Savescu
                    wrote on last edited by
                    #74

                    :cool: Best regards, Alexandru Savescu

                    1 Reply Last reply
                    0
                    • H Hans Dietrich

                      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

                      T Offline
                      T Offline
                      Tomasz Sowinski
                      wrote on last edited by
                      #75

                      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?

                      1 Reply Last reply
                      0
                      • J Jason Henderson

                        I never thought of it this way but try/catch would be a good "control structure" to use instead of nested if's. Its also 100x easier to read. Is Stroustrup trying to say it should only be used as a return mechanism if it isn't used in error handling? Like it or not, I'm right.

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #76

                        Jason Henderson wrote: Is Stroustrup trying to say it should only be used as a return mechanism if it isn't used in error handling? He's saying it's a valid way to control program flow, although it will most often be used to catch errors. He's suggesting that we not limit our thinking to error handling, because it can be used in other ways as well, which really are up to us as developers. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002

                        1 Reply Last reply
                        0
                        • E Eddie Velasquez

                          Christian Graus wrote: There are therefore legitimate uses of exceptions that have nothing to do with errors. Well, I can't pretend to teach C++ to Bjarne! :-O But as quoted, these "legitimate uses that have nothing to do with errors" are the exception, not the rule. (pun intended!) I'm not really thinking about this too hard, but I fail to come up with a case where I would raise an exception that doesn't somehow indicate an error condition. :confused:


                          Eddie Velasquez: A Squeezed Devil
                          Checkout General Guidelines for C# Class Implementation

                          C Offline
                          C Offline
                          Christian Graus
                          wrote on last edited by
                          #77

                          Eddie Velasquez wrote: I'm not really thinking about this too hard, but I fail to come up with a case where I would raise an exception that doesn't somehow indicate an error condition. Most arguments people put forth for the use of goto are better controlled with try/catch, of the type where multiple points in the code can branch to the same block at the bottom. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002

                          1 Reply Last reply
                          0
                          • N Nish Nishant

                            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.

                            J Offline
                            J Offline
                            Jonathan Gilligan
                            wrote on last edited by
                            #78

                            This kind of thing does indeed get ugly, but there's more to solving it than indentation. It's hard to keep straight how deep you are into the nested if statements. Better to break out and write something like this:

                            bool ok(true);

                            ok = call1();
                            if (ok)
                            ok = call2();
                            if (ok)
                            ok = call3();
                            ...

                            The bool variable doesn't hurt performance noticeably (the compiler will optimize it out anyway, when you compile for release), and it makes the whole thing much more readable.

                            1 Reply Last reply
                            0
                            • H Hans Dietrich

                              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

                              N Offline
                              N Offline
                              Nish Nishant
                              wrote on last edited by
                              #79

                              Nice and simple solution. Thanks :-) Nish


                              Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.

                              1 Reply Last reply
                              0
                              • N Nish Nishant

                                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.

                                O Offline
                                O Offline
                                Oliver Anhuth
                                wrote on last edited by
                                #80

                                Hello Nish, in such cases I prefer the following indention style:

                                int result;

                                result = call1();
                                if (result)
                                {
                                result = call2();
                                }

                                if (result)
                                {
                                result = call3();
                                }

                                if (result)
                                {
                                result = call4();
                                }

                                and so on. This may take getting used to, but it keeps the indention level constant. regards Oliver

                                1 Reply Last reply
                                0
                                • N Nish Nishant

                                  Len Holgate wrote: The indentation isn't the problem, you're just doing too much in one function The problem is that each of these functions require something from the previous function. For example if fn_1 allocs something, then fn_2 uses that and allocs something else used by fn_3 and so on. On failure at any point I also have to call the respective deallocing PGP functions in reverse order Nish


                                  Regards, Nish Native CPian. Born and brought up on CP. With the CP blood in him.

                                  L Offline
                                  L Offline
                                  Len Holgate
                                  wrote on last edited by
                                  #81

                                  Nish - Native CPian wrote: The problem is that each of these functions require something from the previous function. For example if fn_1 allocs something, then fn_2 uses that and allocs something else used by fn_3 and so on. On failure at any point I also have to call the respective deallocing PGP functions in reverse order That's the point. If you need to allocate a resource you do so in the constructor, you then use the resource, perhaps to allocate another resource, and release it in the destructor, hence you end up with code like:

                                  CUsesPGP usesPGP(initStuff);

                                  CPGPProvider provider("MyProvider");

                                  CPGPKey key provider.GetKey("Blah");

                                  CPGPData data(pData, length);

                                  data.Encrypt(key);

                                  The c++ class construction order makes sure that the resources are released correctly even if an exception is thrown. Methods throw exceptions so that the user cant ignore errors by default. The code is pretty much self documenting so you dont need huge comment blocks that get out of date. Oh, and it fixes your indentation problem... Len Holgate www.jetbyte.com The right code, right now.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups