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 36 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.
  • N Offline
    N Offline
    Nish Nishant
    wrote on last edited by
    #1

    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.

    M D T E P 13 Replies 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.

      M Offline
      M Offline
      Michael A Barnhart
      wrote on last edited by
      #2

      I typically just use a singe character space vs none and then 4 or 5 in these cases. It is still readable, just does not stand out as much. To be conscious that you are ignorant of the facts is a great step towards Knowledge. Benjamin Disraeli

      N 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.

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        That's why exception handling was invented. Try throwing exceptions instead of nesting if's. Two ways: encapsulating each call in a function or method, or:

        try
        {
        if (!call1)
        throw PGPException();

        if (!call2)
          throw PGPException();
        
        if (!call3)
          throw PGPException();
        

        }
        catch (PGPException)
        {
        }

        Crivo Automated Credit Assessment

        E N 2 Replies 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.

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

          if (call1() && call2() && call3() ....)
          {
          }

          Of course, you can insert line breaks between call[N]() and call[N+1](). Tomasz Sowinski -- http://www.shooltz.com

          - It's for protection
          - Protection from what? Zee Germans?

          N 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.

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

            Sometimes the following code structure:

            if(call1())
            {
            if(call2())
            {
            if(call3())
            {
            ...
            }
            }
            }

            Can be safely replaced by:

            if(!call1())
            return;
            if(!call2())
            return;
            if(!call3())
            return;

            If the semantics of the API I'm using is too C-ish, I'll wrap it up in C++ classes and this usually takes care of the excesive indentation and allows the use of the second code structure.


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

            N 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.

              P Offline
              P Offline
              peterchen
              wrote on last edited by
              #6

              2 spaces for indentation, and if I get that deep I wonder if I'm wrong with my design, should break this down to some helper functions, etc. I wouldn't recommend your 2nd indentation style, as it suggests to be "indented correctly", but isn't. trick: instead of

              ok = hamlet();
              if (ok) {
              ok = ophelia();
              if (ok) {
              ok = polonius();
              if (ok) {
              ok = laertes();
              }
              }
              }

              I use

              do { // while(0)
              ok = hamlet();
              if (!ok) break;
              ok = ophelia();
              if (!ok) break;
              ....
              } while(0);

              not from the source book of clean and accepted code, but it helps a lot (esp. with a #define HBRK if (FAILED(hr)) break ) Peter


              The earth is not dying. It is being killed.

              N 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.

                M Offline
                M Offline
                Mauricio Ritter
                wrote on last edited by
                #7

                I use the first one with a comment at the closing bracket (so that I know from which call that closing bracket belongs.

                if(call1())
                {
                if(call2())
                {
                if(call3())
                {
                if(call4())
                {
                if((call5())
                {
                ...
                } /* Call5 */
                } /* Call4 */
                } /* Call3 */
                } /* Call2 */
                } /* Call1 */

                Mauricio Ritter - Brazil Sonorking now: 100.13560 Trank :beer: The alcohol is one of the greatest enemys of man, but a man who flee from his enemys is a coward. :beer:

                N 1 Reply Last reply
                0
                • D Daniel Turini

                  That's why exception handling was invented. Try throwing exceptions instead of nesting if's. Two ways: encapsulating each call in a function or method, or:

                  try
                  {
                  if (!call1)
                  throw PGPException();

                  if (!call2)
                    throw PGPException();
                  
                  if (!call3)
                    throw PGPException();
                  

                  }
                  catch (PGPException)
                  {
                  }

                  Crivo Automated Credit Assessment

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

                  Daniel Turini wrote: That's why exception handling was invented. Try throwing exceptions instead of nesting if's. Not necessarilly. Exceptions should only be used for reporting "exceptional" conditions and not all function returns mean and exceptional condition. e.g. In some cases an end-of-file is a normal situation, but if reading a header of a structured file it means that the file is corrupt and should raise an exception.


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

                  D C N 3 Replies 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.

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

                    The indentation isn't the problem, you're just doing too much in one function ;) Personally I'd try and wrap the api in some classes and make sure that each method does one clearly defined thing. I'd be inclided to make these classes report errors via exceptions, so at the user code level you just have straight line code. Len Holgate www.jetbyte.com The right code, right now.

                    N 1 Reply Last reply
                    0
                    • M Michael A Barnhart

                      I typically just use a singe character space vs none and then 4 or 5 in these cases. It is still readable, just does not stand out as much. To be conscious that you are ignorant of the facts is a great step towards Knowledge. Benjamin Disraeli

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

                      Michael A. Barnhart wrote: typically just use a singe character space vs none and then 4 or 5 in these cases. It is still readable, just does not stand out as much. That'll lessen the issue but won't eradicate it. Nish


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

                      1 Reply Last reply
                      0
                      • E Eddie Velasquez

                        Sometimes the following code structure:

                        if(call1())
                        {
                        if(call2())
                        {
                        if(call3())
                        {
                        ...
                        }
                        }
                        }

                        Can be safely replaced by:

                        if(!call1())
                        return;
                        if(!call2())
                        return;
                        if(!call3())
                        return;

                        If the semantics of the API I'm using is too C-ish, I'll wrap it up in C++ classes and this usually takes care of the excesive indentation and allows the use of the second code structure.


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

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

                        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.

                        E C J B 4 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.

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

                          Nish - Native CPian wrote: I can't do that. Since I need to do some cleaning up as well That's why you should wrap up the API in C++ classes and let destructors take care of the cleanup.


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

                          N 1 Reply Last reply
                          0
                          • E Eddie Velasquez

                            Daniel Turini wrote: That's why exception handling was invented. Try throwing exceptions instead of nesting if's. Not necessarilly. Exceptions should only be used for reporting "exceptional" conditions and not all function returns mean and exceptional condition. e.g. In some cases an end-of-file is a normal situation, but if reading a header of a structured file it means that the file is corrupt and should raise an exception.


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

                            D Offline
                            D Offline
                            Daniel Turini
                            wrote on last edited by
                            #13

                            I thought that in PGP SDK a return value of false indicates an error condition. Crivo Automated Credit Assessment

                            E 1 Reply Last reply
                            0
                            • D Daniel Turini

                              That's why exception handling was invented. Try throwing exceptions instead of nesting if's. Two ways: encapsulating each call in a function or method, or:

                              try
                              {
                              if (!call1)
                              throw PGPException();

                              if (!call2)
                                throw PGPException();
                              
                              if (!call3)
                                throw PGPException();
                              

                              }
                              catch (PGPException)
                              {
                              }

                              Crivo Automated Credit Assessment

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

                              Daniel Turini wrote: That's why exception handling was invented. Try throwing exceptions instead of nesting if's. Cool! I never thought of that! I just don't know much about exception handling though. What's this PGPException thing? Nish


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

                              C D 2 Replies Last reply
                              0
                              • T Tomasz Sowinski

                                if (call1() && call2() && call3() ....)
                                {
                                }

                                Of course, you can insert line breaks between call[N]() and call[N+1](). Tomasz Sowinski -- http://www.shooltz.com

                                - It's for protection
                                - Protection from what? Zee Germans?

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

                                Tomasz Sowinski wrote: if (call1() && call2() && call3() ....) That won;'t work. I am not supposed to call call-n unless call-n-1 returns true!


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

                                T N 2 Replies Last reply
                                0
                                • P peterchen

                                  2 spaces for indentation, and if I get that deep I wonder if I'm wrong with my design, should break this down to some helper functions, etc. I wouldn't recommend your 2nd indentation style, as it suggests to be "indented correctly", but isn't. trick: instead of

                                  ok = hamlet();
                                  if (ok) {
                                  ok = ophelia();
                                  if (ok) {
                                  ok = polonius();
                                  if (ok) {
                                  ok = laertes();
                                  }
                                  }
                                  }

                                  I use

                                  do { // while(0)
                                  ok = hamlet();
                                  if (!ok) break;
                                  ok = ophelia();
                                  if (!ok) break;
                                  ....
                                  } while(0);

                                  not from the source book of clean and accepted code, but it helps a lot (esp. with a #define HBRK if (FAILED(hr)) break ) Peter


                                  The earth is not dying. It is being killed.

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

                                  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.

                                  J 1 Reply Last reply
                                  0
                                  • M Mauricio Ritter

                                    I use the first one with a comment at the closing bracket (so that I know from which call that closing bracket belongs.

                                    if(call1())
                                    {
                                    if(call2())
                                    {
                                    if(call3())
                                    {
                                    if(call4())
                                    {
                                    if((call5())
                                    {
                                    ...
                                    } /* Call5 */
                                    } /* Call4 */
                                    } /* Call3 */
                                    } /* Call2 */
                                    } /* Call1 */

                                    Mauricio Ritter - Brazil Sonorking now: 100.13560 Trank :beer: The alcohol is one of the greatest enemys of man, but a man who flee from his enemys is a coward. :beer:

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

                                    Mauricio Ritter wrote: I use the first one with a comment at the closing bracket (so that I know from which call that closing bracket belongs. Good idea Mauricio! But my problem was with the indentation level going too deep! 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

                                      Tomasz Sowinski wrote: if (call1() && call2() && call3() ....) That won;'t work. I am not supposed to call call-n unless call-n-1 returns true!


                                      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
                                      #18

                                      Nish - Native CPian wrote: I am not supposed to call call-n unless call-n-1 returns true! Geez, Nish, you're disappointing me again :-D How can you think about earning almost $90K without basic C++ knowledge? Don't you know how logical operators work? call-n isn't going to be called when call-n-1 returns false. Tomasz Sowinski -- http://www.shooltz.com

                                      - It's for protection
                                      - Protection from what? Zee Germans?

                                      N 1 Reply Last reply
                                      0
                                      • L Len Holgate

                                        The indentation isn't the problem, you're just doing too much in one function ;) Personally I'd try and wrap the api in some classes and make sure that each method does one clearly defined thing. I'd be inclided to make these classes report errors via exceptions, so at the user code level you just have straight line code. Len Holgate www.jetbyte.com The right code, right now.

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

                                        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 1 Reply Last reply
                                        0
                                        • E Eddie Velasquez

                                          Daniel Turini wrote: That's why exception handling was invented. Try throwing exceptions instead of nesting if's. Not necessarilly. Exceptions should only be used for reporting "exceptional" conditions and not all function returns mean and exceptional condition. e.g. In some cases an end-of-file is a normal situation, but if reading a header of a structured file it means that the file is corrupt and should raise an exception.


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

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

                                          An exception does not necessarily mean an error. 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

                                          E 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