matching { } syntax style

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Clive

    #16
    Re: matching { } syntax style

    "J. Campbell" <mango_maniac@y ahoo.com> wrote in message
    news:b97c86e1.0 310101604.79cdf af5@posting.goo gle.com...
    <snip>
    < However, I also sometimes do single line if else if it fits nicely
    < onto one line.
    <
    < function(){
    < if(expression) statement; else statement;
    < }

    I don't meant to be fickle (I think this is the first occasion of me using
    that word :)
    but if your expression and statements fit nicely on a single line, wouldn't
    you be better off writing

    expression ? true_statement : false_statement ;

    That way, you dont have to write if, any brackets, an extra ; and the else.


    Comment

    • lilburne

      #17
      Re: matching { } syntax style

      Clive wrote:
      [color=blue]
      > "J. Campbell" <mango_maniac@y ahoo.com> wrote in message
      > news:b97c86e1.0 310101604.79cdf af5@posting.goo gle.com...
      > <snip>
      > < However, I also sometimes do single line if else if it fits nicely
      > < onto one line.
      > <
      > < function(){
      > < if(expression) statement; else statement;
      > < }
      >
      > I don't meant to be fickle (I think this is the first occasion of me using
      > that word :)
      > but if your expression and statements fit nicely on a single line, wouldn't
      > you be better off writing
      >
      > expression ? true_statement : false_statement ;
      >
      > That way, you dont have to write if, any brackets, an extra ; and the else.
      >
      >[/color]

      The problem with both of these 'styles' is that you can't
      put a simple breakpoint on any of the statements. The best
      you can do is put a conditional breakpoint (based on
      expression) on the line and then single step the expression.

      With the tertiary expression both true_statement and
      false_statement also have to be type compatible.

      Comment

      • Tim Clacy

        #18
        Re: matching { } syntax style

        Phlip wrote:[color=blue]
        > Tim Clacy wrote:
        >[color=green]
        >> ...and just to set the cat amongst the pigeons, I'm quite fond of
        >> this:
        >>
        >> void fn() {
        >> while (...) {
        >> switch (...) {
        >> case :
        >> case :
        >> default : } } }
        >>
        >> :-)[/color]
        >
        > I'm fond of very short functions, and replacing switch statements with
        > virtual messages, so my bad-ass pidgeon is not alarmed by your silly
        > little cat.[/color]

        The shorter the function, the more the relative waste of space the braces
        become; so where so you put your braces in your 'very short functions'? How
        about:

        struct A {
        virtual void fn0() { ... }
        virtual void fn1() { ... }
        virtual void fn2() { ... } };


        Comment

        • Risto Lankinen

          #19
          Re: matching { } syntax style


          "Clive" <clive@clive.cl ive> wrote in message
          news:bm9ehs$njq $1@enyo.uwa.edu .au...[color=blue]
          >
          > I don't meant to be fickle (I think this is the first occasion of me using
          > that word :)
          > but if your expression and statements fit nicely on a single line,[/color]
          wouldn't[color=blue]
          > you be better off writing
          >
          > expression ? true_statement : false_statement ;[/color]

          Statements are not allowed in expressions. You cannot
          write, for instance ...

          someFlag ? continue : break;

          Also, if you used the syntactically correct form of...

          expression ? true_expression : false_expressio n;

          .... instead, it still must also be semantically correct so
          that the types returned by the sub-expressions are
          assignable to a common type (which then becomes the
          type of the entire expression). This, for instance,
          would be syntactically correct, but semantically illegal:

          someFlag ? 123 : "";

          IMO, it is always better to use the if(){}else{} instead
          of the ?: operator in all contexts that allow a statement,
          and use the conditional operator only when the context
          *requires* (rather than just *allows*) an expression.

          Cheers!

          - Risto -



          Comment

          • Stewart Gordon

            #20
            Re: matching { } syntax style

            While it was 13/10/03 10:15 am throughout the UK, Risto Lankinen
            sprinkled little black dots on a white screen, and they fell thus:
            <snip>[color=blue]
            > This, for instance, would be syntactically correct, but semantically illegal:
            >
            > someFlag ? 123 : "";[/color]

            But I've just tried

            someFlag ? (void) 123 : (void) "";

            and gcc/g++ accepts it.
            [color=blue]
            > IMO, it is always better to use the if(){}else{} instead
            > of the ?: operator in all contexts that allow a statement,
            > and use the conditional operator only when the context
            > *requires* (rather than just *allows*) an expression.[/color]

            Guess you're kind of right there.

            But, which do most people prefer:

            if(qwert > 100) {
            yuiop = 50;
            } else {
            yuiop = 25;
            }

            or

            yuiop = (qwert > 100) ? 50 : 25;

            or even

            yuiop = ((qwert > 100) + 1) * 25;

            ....?

            Stewart.

            --
            My e-mail is valid but not my primary mailbox. Please keep replies on
            on the 'group where everyone may benefit.

            Comment

            • Marc Ferry

              #21
              Re: matching { } syntax style

              In order to avoid these kind of "religious" matters, I was looking for a
              tool enabling each developper to have his own style.

              Found "C++ SourceStyler" from OchreSoftware
              (http://www.sourcestyler.com/) but it is expensive and not available on
              Unix/Linux.

              Does anybody know of a FREE "C++ styler" available on Unix/Linux ?
              Please don't answer with "indent".

              Marc

              Comment

              • Phlip

                #22
                Re: matching { } syntax style

                Marc Ferry wrote:
                [color=blue]
                > In order to avoid these kind of "religious" matters, I was looking for a
                > tool enabling each developper to have his own style.[/color]

                Do you mean each developer reformats everything at check-out time?

                That will fill the source control system up with irrelevant changes. I have
                heard this suggested before; has anyone ever actually done it?

                Developers should share a team style, and all code should match it. One can
                support the style electronically, but the common style should support team
                bonding. Developers can find more productive ways to be individualistic .

                --
                Phlip


                Comment

                • Phlip

                  #23
                  Re: matching { } syntax style

                  > The shorter the function, the more the relative waste of space the braces[color=blue]
                  > become; so where so you put your braces in your 'very short functions'?[/color]
                  How[color=blue]
                  > about:[/color]

                  struct
                  A
                  {
                  virtual void fn0() { ... }
                  virtual void fn1() { ... }
                  virtual void fn2() { ... }
                  };

                  There. A is in the first column. 'struct', being less important, is out of
                  the first column, and the braces parallel each other.

                  The functions are so short that the next stiff breeze will make them
                  collapse into something using even less lines of code.

                  --
                  Phlip



                  Comment

                  • Marc Ferry

                    #24
                    Re: matching { } syntax style

                    Phlip wrote:
                    [color=blue]
                    > Marc Ferry wrote:
                    >
                    >[color=green]
                    >>In order to avoid these kind of "religious" matters, I was looking for a
                    >>tool enabling each developper to have his own style.[/color]
                    >
                    >
                    > Do you mean each developer reformats everything at check-out time?
                    >
                    > That will fill the source control system up with irrelevant changes. I have
                    > heard this suggested before; has anyone ever actually done it?[/color]

                    Have a look at "SourceStyl er C++" at http://www.sourcestyler.com/
                    Source file doesn't seem to be reformated.
                    Marc

                    Comment

                    • lilburne

                      #25
                      Re: matching { } syntax style

                      Marc Ferry wrote:[color=blue]
                      > Phlip wrote:
                      >[color=green]
                      >> Marc Ferry wrote:
                      >>
                      >>[color=darkred]
                      >>> In order to avoid these kind of "religious" matters, I was looking for a
                      >>> tool enabling each developper to have his own style.[/color]
                      >>
                      >>
                      >>
                      >> Do you mean each developer reformats everything at check-out time?
                      >>
                      >> That will fill the source control system up with irrelevant changes. I
                      >> have
                      >> heard this suggested before; has anyone ever actually done it?[/color]
                      >
                      >
                      > Have a look at "SourceStyl er C++" at http://www.sourcestyler.com/
                      > Source file doesn't seem to be reformated.
                      > Marc
                      >[/color]

                      Actually it is reformatted:


                      I emailed the link to colleagues and you could tell when
                      they had opened it by the laughter.

                      A coding style contains much more than where you place a
                      brace or how many spaces you indent. It will have rulings on
                      language constructs to be avoided, naming schemes for
                      variables, classes, and functions etc, lots of stuff that
                      reformatting just doesn't address.

                      If you are going to adopt a coding style you need to pick
                      one have a meeting to discuss any contentious issues, and
                      have someone who is in a position to make a final ruling.
                      Then adopt and enforce the scheme, no one should be exempt
                      from the adopted style.

                      Comment

                      • Phlip

                        #26
                        Re: matching { } syntax style

                        lilburne wrote:
                        [color=blue]
                        > Actually it is reformatted:
                        > http://www.ochresoftware.com/sourcestyler-gui.html
                        >
                        > I emailed the link to colleagues and you could tell when
                        > they had opened it by the laughter.
                        >
                        > A coding style contains much more than where you place a
                        > brace or how many spaces you indent. It will have rulings on
                        > language constructs to be avoided, naming schemes for
                        > variables, classes, and functions etc, lots of stuff that
                        > reformatting just doesn't address.
                        >
                        > If you are going to adopt a coding style you need to pick
                        > one have a meeting to discuss any contentious issues, and
                        > have someone who is in a position to make a final ruling.
                        > Then adopt and enforce the scheme, no one should be exempt
                        > from the adopted style.[/color]

                        If you are going to adopt coding styles, start by defining your terms.
                        There are two kinds - esthetic and technical. The OP asked about an
                        esthetic guideline.

                        You list technical things. The two guidelines should be democratic and
                        meritocratic, respectively. If we declare a technical guideline, and
                        you find a way to exceed it, you may simply tell us what the actual
                        technology is. (Tip: Fighting on USENET helps teach us how to defend
                        our principles ;)

                        For example, suppose we agree on camelCase for identifiers, and on
                        "complete words without excess abbreviations". But if you want to
                        write a big identifier, like camelCaseWithFu llPack, you are allowed to
                        write and defend "camelCase_with FullPack". You added a _ for major
                        breaks in identifiers, and this has merit.

                        --
                        Phlip

                        Comment

                        • lilburne

                          #27
                          Re: matching { } syntax style

                          Phlip wrote:[color=blue]
                          > lilburne wrote:
                          >[color=green]
                          >>
                          >>If you are going to adopt a coding style you need to pick
                          >>one have a meeting to discuss any contentious issues, and
                          >>have someone who is in a position to make a final ruling.
                          >>Then adopt and enforce the scheme, no one should be exempt
                          >>from the adopted style.[/color]
                          >
                          >
                          > If you are going to adopt coding styles, start by defining your terms.
                          > There are two kinds - esthetic and technical. The OP asked about an
                          > esthetic guideline.[/color]


                          The poster to this sub-thread was talking about a standard
                          layout that wasn't a standard layout, something about not
                          having a religious war over brace style and such. I've yet
                          to see an in-house style that didn't combine the aesthetic
                          and the technical.

                          But consider for the moment what was being proposed:

                          "In order to avoid these kind of "religious" matters,
                          I was looking for a tool enabling each developper to
                          have his own style."

                          the idea is that when the file is extracted from the
                          repository it is formatted into the devvies prefered style,
                          then when it is returned to the repository it is converted
                          into house style. This presupposes there is a "house style"
                          or common layout, but the motivating factor for doing this
                          is that there isn't "each developer to have his own style".
                          If this group of developers can agree a common layout format
                          why can't they adopt it as *the* style?

                          Now think about what happens when two devvies are working
                          together, joint debugging, or extreme programming session,
                          or whatever. Whose layout style are they going to use?

                          I hope the company that produces this software makes a lot
                          of money from it, I pray that all my industry competitors
                          adopt it too.

                          [color=blue]
                          > You list technical things. The two guidelines should be democratic and
                          > meritocratic, respectively. If we declare a technical guideline, and
                          > you find a way to exceed it, you may simply tell us what the actual
                          > technology is. (Tip: Fighting on USENET helps teach us how to defend
                          > our principles ;)
                          >
                          > For example, suppose we agree on camelCase for identifiers, and on
                          > "complete words without excess abbreviations". But if you want to
                          > write a big identifier, like camelCaseWithFu llPack, you are allowed to
                          > write and defend "camelCase_with FullPack". You added a _ for major
                          > breaks in identifiers, and this has merit.
                          >[/color]

                          Of course, the standard will be revised from time to time
                          and there ought to be a way of managing that. What you don't
                          want is ad hoc changes or worse yet someone taking the
                          attitude that they are not bound by it. Practically all of
                          the in-house style will have some technical basis even the
                          aesthetic ones.

                          Comment

                          • Roxann Higuera

                            #28
                            Re: matching { } syntax style

                            "Phlip" <phlip_cpp@yaho o.com> wrote in message news:<Abyib.790 2$xL6.6911@news svr16.news.prod igy.com>...[color=blue]
                            > Marc Ferry wrote:
                            >[color=green]
                            > > In order to avoid these kind of "religious" matters, I was looking for a
                            > > tool enabling each developper to have his own style.[/color]
                            >
                            > Do you mean each developer reformats everything at check-out time?
                            >
                            > That will fill the source control system up with irrelevant changes. I have
                            > heard this suggested before; has anyone ever actually done it?
                            >
                            > Developers should share a team style, and all code should match it. One can
                            > support the style electronically, but the common style should support team
                            > bonding. Developers can find more productive ways to be individualistic .[/color]

                            I've done that. But I didn't check in that reformatted version.
                            Instead, I would run vdiff (a function from an old version of PVCS) to
                            determine what really changed and then make those same changes in a
                            fresh copy of the code. I'd then vdiff the two modified versions
                            again to make sure I'd copied in all of my changes before checking the
                            code back in. That way I could format the code in whatever way best
                            suited my understanding, and the resulting code didn't overburden the
                            source control system.

                            Comment

                            • E. Robert Tisdale

                              #29
                              Tisdale's C and C++ Style Guide

                              bart wrote:
                              [color=blue]
                              > Why is it that everybody likes the following curly braces syntax
                              >
                              > void function(void) {
                              > implementations ;
                              > }[/color]

                              Style is a very subjective and personal consideration.
                              C and C++ programmers develop or adopt a style
                              in order to make their code easier for themselves
                              and other programmers to read, understand and maintain.
                              If you are developing your own style, there are no rules
                              except that you should try to be consistent.
                              Otherwise, you should try to adopt a style
                              with which other C and C++ programmers are comfortable,
                              familiar or that they will at least recognize.
                              Personally, I try to use the same punctuation rules
                              that are used for ordinary (mathematical) typesetting.
                              Here are my recommendations :

                              Terminators always follow immediately after an expression

                              x@ for all @ in {?, :, ,, ;}

                              and are followed by at least one white space.
                              Write

                              x? y: z

                              instead of

                              x ? y : z

                              or

                              x?y:z

                              and write

                              void f(int, int, int); void g(double);

                              instead of

                              void f(int,int,int); void g(double);

                              for example.

                              There is no space
                              between some binary operators and their operands

                              x@y for all @ in {::, ., ->, .*, ->*, *, /, %, &, ^, |}

                              but there is always a space
                              between other binary operators and their operands

                              x @ y for all @ in {+, -, <<, >>;, <, <=, >, >=, ==, !=,
                              &&, ||, =, *=, /=, %=, +=, -=, <<=, >>=, &=, |=, ^=}

                              except when expressions appear as subscripts.
                              Write

                              x + y

                              instead of

                              x+y
                              and

                              x*y

                              instead of

                              x * y

                              for example.
                              But you may wish to write

                              A[i+1][j-1]

                              instead of

                              A[i + 1][j - 1]

                              for example to subscript array A.


                              Most unary prefix operators never have any whitespace
                              between themselves and their operands

                              @x for all @ in {::, ++, --, ~, !, -, +, &, *}

                              but others do

                              @ x for all @ in {sizeof, new, delete, delete [], throw}

                              No unary postfix operators

                              x@ for all @ in {[], (), ++, --}

                              ever have any whitespace between themselves and their operands.

                              Use the normal typesetting rules for parentheses (),
                              square brackets [], angle brackets <> and curly brackets {}.
                              No space after (, [, < or { and no space before ), ], > or }.
                              Write

                              (x)

                              instead of

                              ( x )

                              or

                              (x )

                              or

                              ( x)

                              and write

                              [x]

                              instead of

                              [ x ]

                              or

                              [x ]

                              or

                              [ x]

                              for example.
                              There are, of course, exceptions
                              where extra white space helps to make your code more readable:

                              double A[2][3] = {{ 1, -1, 0},
                              {-10, 11, -21}};


                              Don't give identifiers cryptic, mangled names.
                              Use ordinary, meaningful words, conventional symbols
                              or abbreviations with annotations.
                              Write

                              double distance, velocity, acceleration, mass, Force;

                              Force = mass*accelerati on;

                              or

                              double x; // distance
                              double v; // velocity
                              double a; // acceleration
                              double m; // mass
                              double F; // force

                              F = m*a;

                              for example.

                              Don't rely on defaults. Make declarations explicit.
                              Write

                              int i = 1;

                              instead of

                              i = 1;

                              to declare and initialize integer i and write

                              class X {
                              private:
                              // Representation
                              int I;
                              public:
                              // Constructors
                              // ...
                              };

                              instead of

                              class X {
                              // Representation
                              int I;
                              public:
                              // Constructors
                              // ...
                              };

                              to define the private data members of class X for example.


                              Use indentation to emphasize scope.
                              Everybody is comfortable with standard indentation:

                              void f()
                              {
                              // indent
                              }

                              But I indent curly brackets to the scope of the function body:

                              void f()
                              {
                              // indent
                              }

                              And I include the open curly bracket with the function heading:

                              void f() {
                              // indent
                              }

                              to save a line of code.

                              I always indent just two spaces at a time and
                              I place just one statement on each line so that
                              there is usually room for a comment at the end of each line
                              beginning in column 33 or 41.

                              Write

                              if (condition) {
                              // statements
                              }

                              instead of

                              if(condition) {
                              // statements
                              }

                              and

                              while (condition) {
                              // statements
                              }

                              instead of

                              while(condition ) {
                              // statements
                              }

                              to distinguish flow control structures from function calls.

                              I use

                              // comment

                              for comments in C++ and I reserve

                              /*
                              a = b;
                              // comment
                              b = c;
                              */

                              to comment out code which may include comments.


                              If you find yourself in an environment
                              that requires you to conform to style rules with which you are not
                              comfortable,
                              consider investing a little time and effort in a program like astyle

                              Artistic Style


                              which changes the appearance of C or C++ programs
                              by inserting or deleting whitespace.

                              Write

                              constant == variable

                              instead of

                              variable == constant

                              when comparing a variable to a constant for equality
                              so that if you write

                              constant = variable

                              by mistake, the compiler will detect the error.

                              I always write

                              x < y

                              or

                              x <= y

                              instead of

                              y > x

                              or

                              y >= x

                              when comparing two values so that the expression is true
                              when the left hand side is to the left of the right hand side
                              on the real number line.

                              Comment

                              • Keith A. Lewis

                                #30
                                Re: Tisdale's C and C++ Style Guide

                                In article <3F8ECB0A.50300 08@jpl.nasa.gov >,
                                E. Robert Tisdale <E.Robert.Tisda le@jpl.nasa.gov > wrote:

                                Is there a hyphen in anal(-?)retentive?

                                Comment

                                Working...