Left brace on a single line -- Question

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

    Left brace on a single line -- Question

    I want to know the best practice to write on both C / C++ source codes.
    The best practice is to ensure readable. Should left brace be after
    function or if on the same line or next line.

    For example...

    void Test( void) {
    int a = 5;
    printf( "%d\n", a);
    }

    or...

    void Test( void)
    {
    int a = 5;
    printf( "%d\n", a);
    }

    It applies to if. For example...

    if (a == 5) {
    printf( "%d\n", a);
    printf( "End...\n";
    }

    or...

    if (a == 5)
    {
    printf( "%d\n", a);
    printf( "End...\n";
    }

    --

    Yours Truly,
    Bryan Parkoff


  • Ioannis Vranos

    #2
    Re: Left brace on a single line -- Question

    Bryan Parkoff wrote:
    I want to know the best practice to write on both C / C++ source codes.
    The best practice is to ensure readable. Should left brace be after
    function or if on the same line or next line.
    LOL.

    Comment

    • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

      #3
      Re: Left brace on a single line -- Question

      On 2008-04-05 22:11, Bryan Parkoff wrote:
      I want to know the best practice to write on both C / C++ source codes.
      The best practice is to ensure readable. Should left brace be after
      function or if on the same line or next line.
      The best style is the one that is used in the code you are working in.
      If you are starting from scratch use whatever your IDE uses. Don't spend
      a lot of time on indentation and such, for most people it really does
      not matter once they get used to a style.

      I'll say this though, monitors are getting quite large these days and
      whitespace is cheep, your goal should always be to keep your code as
      readable as possible.

      --
      Erik Wikström

      Comment

      • Michael.Boehnisch@gmail.com

        #4
        Re: Left brace on a single line -- Question

        On 5 Apr., 22:11, "Bryan Parkoff" <nos...@nospam. comwrote:
        I want to know the best practice to write on both C / C++ source codes.
        The best practice is to ensure readable. Should left brace be after
        function or if on the same line or next line.
        This is a matter of personal preference in study programs or homebrew
        code. In commercial projects there is usually a company coding guide
        that defines this kind of stuff. It is not so important what style you
        decide on, it is important that every developer working on one code
        base uses the same style.

        best,

        Michael

        Comment

        • Jim Langston

          #5
          Re: Left brace on a single line -- Question

          Bryan Parkoff wrote:
          I want to know the best practice to write on both C / C++ source
          codes. The best practice is to ensure readable. Should left brace be
          after function or if on the same line or next line.
          >
          For example...
          >
          void Test( void) {
          int a = 5;
          printf( "%d\n", a);
          }
          >
          or...
          >
          void Test( void)
          {
          int a = 5;
          printf( "%d\n", a);
          }
          >
          It applies to if. For example...
          >
          if (a == 5) {
          printf( "%d\n", a);
          printf( "End...\n";
          }
          >
          or...
          >
          if (a == 5)
          {
          printf( "%d\n", a);
          printf( "End...\n";
          }
          Personal choice. Whatever you are comfortable with. When working with
          existing code it's probably a good idea to stick with the formatting it
          uses.

          Personally, I use the 2nd format of { on it's own line. My IDE prefers that
          format too and some auto formatting gets screwed up if it isn't in that
          format.


          --
          Jim Langston
          tazmaster@rocke tmail.com


          Comment

          • James Kanze

            #6
            Re: Left brace on a single line -- Question

            On 5 avr, 23:36, Erik Wikström <Erik-wikst...@telia. comwrote:
            On 2008-04-05 22:11, Bryan Parkoff wrote:
            I want to know the best practice to write on both C / C++
            source codes. The best practice is to ensure readable.
            Should left brace be after function or if on the same line
            or next line.
            The best style is the one that is used in the code you are
            working in. If you are starting from scratch use whatever
            your IDE uses.
            Do IDE's impose a style? Or even default to one? The editors I
            use don't normally break a line just because you enter a brace;
            if the brace is to be on a line by itself, you have to enter the
            newline manually. (The editors do indent the next line
            according to their idea of where it should be indented. I find
            this mainly useful because if their idea of where it should be
            isn't the same as mine, it often means I've forgotten to close a
            parentheses.)
            Don't spend a lot of time on indentation and such, for most
            people it really does not matter once they get used to a
            style.
            Within limits, and as long as it is consistent. (I'm old enough
            to remember when no indentation was the most common style, every
            line starting in column 1. Or column 7. No thank you.)
            I'll say this though, monitors are getting quite large these
            days and whitespace is cheep, your goal should always be to
            keep your code as readable as possible.
            I don't understand the issue about white space. If you have to
            indent more than three levels, for whatever reason, there's
            something wrong. The monitor width is potentially valid with
            regards to symbol length, but you shouldn't neglect the fact
            that you might want to have a listing; I'd argue that the lines
            should be short enough to fit horizontally on a page of A4
            paper, in portrait mode, with a readable sized font.

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            • stan

              #7
              Re: Left brace on a single line -- Question

              James Kanze wrote:
              On Apr 10, 5:19 am, stan <smo...@exis.ne twrote:
              >James Kanze wrote:
              >
              [...]
              >I've never found the vim help files terribly intuitive, what
              >does the actual vi help look like?
              >
              What help? There is no on line help.
              What was I thinking?
              >
              >AFAIK there's no actual open vi, is that correct?
              >
              I think there was, at one time. But why bother? If you're
              going to compile and install your own, you might as well make it
              vim (or emacs). (Our production machines are Sun Sparcs,
              running Solaris, and the only installed editors are those that
              are bundled with Solaris. Which means the pure Berkley vi, just
              as Bill Joy wrote it, when he was still a grad student, and
              hadn't helped found Sun.)
              I have vim. If available I might have loaded up vi to see how my hands
              reacted. Actually my hands are confused as I jump between vim and emacs.
              I might be risking permanent damage even trying a pure vi :)

              <snip>
              >I'm pretty comfortable with yacc/bison and flex and I've done
              >many little language things so I have some feeling for working
              >on front ends. However the urge to simply write a c or c++
              >front end never really bubbled up to the top of my todo list.
              >
              A C front end wouldn't be too difficult, despite the ambiguities
              in the declaration syntax. C++ probably requires backtracking.
              Still, I don't think it would be that difficult to get to the
              stage of building a parse tree. The real fun would start when
              you start to annotate it. Then comes name look up, function
              overload resolution, and templates.
              As I said, I think I might just give it a try.

              Comment

              Working...