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. Javascript brace style - THAT discussion probably again

Javascript brace style - THAT discussion probably again

Scheduled Pinned Locked Moved The Lounge
csharpquestionjavascriptpythonhtml
31 Posts 21 Posters 25 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.
  • M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #1

    So, I force myself to write Javascript with this brace style, since it seems that that's what is the "correct" style (example):

    function ajaxError(data) {
    alertBad(data);
    }

    But then I see this [HTML5 WebSockets](https://www.tutorialspoint.com/html5/html5\_websocket.htm) and their example uses the style I'm used to in C#. So which is the "approved / standard / whatever" style? What style do you use: 1: Javascript style as per example? 2: Braces on separate lines style?

    Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

    M P W R M 14 Replies Last reply
    0
    • M Marc Clifton

      So, I force myself to write Javascript with this brace style, since it seems that that's what is the "correct" style (example):

      function ajaxError(data) {
      alertBad(data);
      }

      But then I see this [HTML5 WebSockets](https://www.tutorialspoint.com/html5/html5\_websocket.htm) and their example uses the style I'm used to in C#. So which is the "approved / standard / whatever" style? What style do you use: 1: Javascript style as per example? 2: Braces on separate lines style?

      Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      I don't have a definitive answer on such a religious question. Use the coding style you are familiar with. OR Use the coding style already in place ( follow current style).

      I'd rather be phishing!

      1 Reply Last reply
      0
      • M Marc Clifton

        So, I force myself to write Javascript with this brace style, since it seems that that's what is the "correct" style (example):

        function ajaxError(data) {
        alertBad(data);
        }

        But then I see this [HTML5 WebSockets](https://www.tutorialspoint.com/html5/html5\_websocket.htm) and their example uses the style I'm used to in C#. So which is the "approved / standard / whatever" style? What style do you use: 1: Javascript style as per example? 2: Braces on separate lines style?

        Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

        P Offline
        P Offline
        phil o
        wrote on last edited by
        #3

        Whatever the style, most important is the consistency of the style. I have my own style choices for my personal projects, but then when I have to dive into shared code, I adopt whatever the style which has been chosen. It doesn't prevent me from quickly getting a method or class functionality, provided there is some consistency across the codebase. Everything else are just opinions, and, well, you know about opinions, don't you?... :)

        "I'm neither for nor against, on the contrary." John Middle

        1 Reply Last reply
        0
        • M Marc Clifton

          So, I force myself to write Javascript with this brace style, since it seems that that's what is the "correct" style (example):

          function ajaxError(data) {
          alertBad(data);
          }

          But then I see this [HTML5 WebSockets](https://www.tutorialspoint.com/html5/html5\_websocket.htm) and their example uses the style I'm used to in C#. So which is the "approved / standard / whatever" style? What style do you use: 1: Javascript style as per example? 2: Braces on separate lines style?

          Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

          W Offline
          W Offline
          W Balboos GHB
          wrote on last edited by
          #4

          Use what pleases your eyes:   you are the one looking at it ! I, personally (in all brace-using languages) would use a style similar to what you call javaScript, except I would put the closing brace aligned with the beginning of the block (i.e., under the 'f' in function. Similarly, under the 'i' in an if-block. My comfort zone for visual organization.

          Ravings en masse^

          "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

          "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

          1 Reply Last reply
          0
          • M Marc Clifton

            So, I force myself to write Javascript with this brace style, since it seems that that's what is the "correct" style (example):

            function ajaxError(data) {
            alertBad(data);
            }

            But then I see this [HTML5 WebSockets](https://www.tutorialspoint.com/html5/html5\_websocket.htm) and their example uses the style I'm used to in C#. So which is the "approved / standard / whatever" style? What style do you use: 1: Javascript style as per example? 2: Braces on separate lines style?

            Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

            R Offline
            R Offline
            R Giskard Reventlov
            wrote on last edited by
            #5

            Same as you; mostly because I use the format document/selection menu option in Visual Studio to tidy up code and it's left at the defaults.

            M T 2 Replies Last reply
            0
            • R R Giskard Reventlov

              Same as you; mostly because I use the format document/selection menu option in Visual Studio to tidy up code and it's left at the defaults.

              M Offline
              M Offline
              Marc Clifton
              wrote on last edited by
              #6

              R. Giskard Reventlov wrote:

              Same as you; mostly because I use the format document/selection menu option in Visual Studio to tidy up code and it's left at the defaults.

              Yeah, I just tried that -- VS formatted it back to option 1. :doh: And it's such a useful feature with the html, I guess I'll have to either figure out if it's possible to tell the formatter to use C# style, or live with it, which I've pretty much gotten used to at this point anyways.

              Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

              S 1 Reply Last reply
              0
              • M Marc Clifton

                R. Giskard Reventlov wrote:

                Same as you; mostly because I use the format document/selection menu option in Visual Studio to tidy up code and it's left at the defaults.

                Yeah, I just tried that -- VS formatted it back to option 1. :doh: And it's such a useful feature with the html, I guess I'll have to either figure out if it's possible to tell the formatter to use C# style, or live with it, which I've pretty much gotten used to at this point anyways.

                Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                S Offline
                S Offline
                Slacker007
                wrote on last edited by
                #7

                Marc Clifton wrote:

                VS formatted it back to option 1.

                you can change this in VS options Options --> text editor --> javascript/typescript

                R M 2 Replies Last reply
                0
                • M Marc Clifton

                  So, I force myself to write Javascript with this brace style, since it seems that that's what is the "correct" style (example):

                  function ajaxError(data) {
                  alertBad(data);
                  }

                  But then I see this [HTML5 WebSockets](https://www.tutorialspoint.com/html5/html5\_websocket.htm) and their example uses the style I'm used to in C#. So which is the "approved / standard / whatever" style? What style do you use: 1: Javascript style as per example? 2: Braces on separate lines style?

                  Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                  M Offline
                  M Offline
                  Mario Z
                  wrote on last edited by
                  #8

                  I agree with others that it's a matter of personal taste or it's defined by company's coding style / culture. Whatever the case is, I think the only important thing is to stay consistent. However, that example actually uses both styles so I don't think it's good idea to follow their styling...

                  1 Reply Last reply
                  0
                  • S Slacker007

                    Marc Clifton wrote:

                    VS formatted it back to option 1.

                    you can change this in VS options Options --> text editor --> javascript/typescript

                    R Offline
                    R Offline
                    R Giskard Reventlov
                    wrote on last edited by
                    #9

                    :thumbsup:

                    1 Reply Last reply
                    0
                    • M Marc Clifton

                      So, I force myself to write Javascript with this brace style, since it seems that that's what is the "correct" style (example):

                      function ajaxError(data) {
                      alertBad(data);
                      }

                      But then I see this [HTML5 WebSockets](https://www.tutorialspoint.com/html5/html5\_websocket.htm) and their example uses the style I'm used to in C#. So which is the "approved / standard / whatever" style? What style do you use: 1: Javascript style as per example? 2: Braces on separate lines style?

                      Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                      Mike HankeyM Offline
                      Mike HankeyM Offline
                      Mike Hankey
                      wrote on last edited by
                      #10

                      I actually use both. Since they both have accepted styles I've adapted and just go with the flow.

                      Someone's therapist knows all about you!

                      1 Reply Last reply
                      0
                      • R R Giskard Reventlov

                        Same as you; mostly because I use the format document/selection menu option in Visual Studio to tidy up code and it's left at the defaults.

                        T Offline
                        T Offline
                        TNCaver
                        wrote on last edited by
                        #11

                        I used to use the format selection option all the time, until it mysteriously disappeared from the menu. Can't figure out how to get it back.

                        If you think 'goto' is evil, try writing an Assembly program without JMP.

                        1 Reply Last reply
                        0
                        • M Marc Clifton

                          So, I force myself to write Javascript with this brace style, since it seems that that's what is the "correct" style (example):

                          function ajaxError(data) {
                          alertBad(data);
                          }

                          But then I see this [HTML5 WebSockets](https://www.tutorialspoint.com/html5/html5\_websocket.htm) and their example uses the style I'm used to in C#. So which is the "approved / standard / whatever" style? What style do you use: 1: Javascript style as per example? 2: Braces on separate lines style?

                          Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                          N Offline
                          N Offline
                          Nemanja Trifunovic
                          wrote on last edited by
                          #12

                          Be careful! With JavaScript, it is not just a matter of style[^]. Just use the K&R style or you may introduce subtle bugs. Plus, K&R is the only true style :-D

                          utf8-cpp

                          M G 2 Replies Last reply
                          0
                          • M Marc Clifton

                            So, I force myself to write Javascript with this brace style, since it seems that that's what is the "correct" style (example):

                            function ajaxError(data) {
                            alertBad(data);
                            }

                            But then I see this [HTML5 WebSockets](https://www.tutorialspoint.com/html5/html5\_websocket.htm) and their example uses the style I'm used to in C#. So which is the "approved / standard / whatever" style? What style do you use: 1: Javascript style as per example? 2: Braces on separate lines style?

                            Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                            J Offline
                            J Offline
                            Jeremy Falcon
                            wrote on last edited by
                            #13

                            I like you, refused to use 1TBS style braces (it's based on K&R and what JavaScript uses). I came from a C/C++ and PHP world after all. I was a real programmer. Besides, the style sucked. It's evil and ugly. Somewhere ponies will die if I used them. However, there is a valid reason why 1TBS became so popular with JavaScript. That code on the site was simply written by a rookie to JavaScript. Check out section 3 in the following link... Brace styles and JavaScript[^] That one little tidbit is the only valid reason for it and it started it all. In other words, it's only way to return an object literal like that in a function. So the community just adapted this style.

                            Jeremy Falcon

                            M J 2 Replies Last reply
                            0
                            • S Slacker007

                              Marc Clifton wrote:

                              VS formatted it back to option 1.

                              you can change this in VS options Options --> text editor --> javascript/typescript

                              M Offline
                              M Offline
                              Marc Clifton
                              wrote on last edited by
                              #14

                              Slacker007 wrote:

                              you can change this in VS options

                              Woohoo! Saved me a google search. :jig:

                              Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                              S 1 Reply Last reply
                              0
                              • M Marc Clifton

                                Slacker007 wrote:

                                you can change this in VS options

                                Woohoo! Saved me a google search. :jig:

                                Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                                S Offline
                                S Offline
                                Slacker007
                                wrote on last edited by
                                #15

                                :)

                                1 Reply Last reply
                                0
                                • N Nemanja Trifunovic

                                  Be careful! With JavaScript, it is not just a matter of style[^]. Just use the K&R style or you may introduce subtle bugs. Plus, K&R is the only true style :-D

                                  utf8-cpp

                                  M Offline
                                  M Offline
                                  Marc Clifton
                                  wrote on last edited by
                                  #16

                                  Nemanja Trifunovic wrote:

                                  Be careful! With JavaScript

                                  Eep! That was quite informative!

                                  Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                                  1 Reply Last reply
                                  0
                                  • J Jeremy Falcon

                                    I like you, refused to use 1TBS style braces (it's based on K&R and what JavaScript uses). I came from a C/C++ and PHP world after all. I was a real programmer. Besides, the style sucked. It's evil and ugly. Somewhere ponies will die if I used them. However, there is a valid reason why 1TBS became so popular with JavaScript. That code on the site was simply written by a rookie to JavaScript. Check out section 3 in the following link... Brace styles and JavaScript[^] That one little tidbit is the only valid reason for it and it started it all. In other words, it's only way to return an object literal like that in a function. So the community just adapted this style.

                                    Jeremy Falcon

                                    M Offline
                                    M Offline
                                    Marc Clifton
                                    wrote on last edited by
                                    #17

                                    Jeremy Falcon wrote:

                                    That one little tidbit is the only valid reason for it and it started it all. In other words, it's only way to return an object literal like that in a function. So the community just adapted this style.

                                    Yup - just read about that from Nemanja Trifunovic's post. That was a learning moment for me!

                                    Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                                    J 1 Reply Last reply
                                    0
                                    • M Marc Clifton

                                      Jeremy Falcon wrote:

                                      That one little tidbit is the only valid reason for it and it started it all. In other words, it's only way to return an object literal like that in a function. So the community just adapted this style.

                                      Yup - just read about that from Nemanja Trifunovic's post. That was a learning moment for me!

                                      Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                                      J Offline
                                      J Offline
                                      Jeremy Falcon
                                      wrote on last edited by
                                      #18

                                      Oh snap, he beat me to it. It was a moment for me too man. Had it not been for that one point I'd not be using that style either. The only good news is at least you get used to it... at some point. :laugh:

                                      Jeremy Falcon

                                      1 Reply Last reply
                                      0
                                      • M Marc Clifton

                                        So, I force myself to write Javascript with this brace style, since it seems that that's what is the "correct" style (example):

                                        function ajaxError(data) {
                                        alertBad(data);
                                        }

                                        But then I see this [HTML5 WebSockets](https://www.tutorialspoint.com/html5/html5\_websocket.htm) and their example uses the style I'm used to in C#. So which is the "approved / standard / whatever" style? What style do you use: 1: Javascript style as per example? 2: Braces on separate lines style?

                                        Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #19

                                        The only language I know of that gives a crap is tcl. Fortunately I don't have to write it, I only have to parse it.

                                        1 Reply Last reply
                                        0
                                        • J Jeremy Falcon

                                          I like you, refused to use 1TBS style braces (it's based on K&R and what JavaScript uses). I came from a C/C++ and PHP world after all. I was a real programmer. Besides, the style sucked. It's evil and ugly. Somewhere ponies will die if I used them. However, there is a valid reason why 1TBS became so popular with JavaScript. That code on the site was simply written by a rookie to JavaScript. Check out section 3 in the following link... Brace styles and JavaScript[^] That one little tidbit is the only valid reason for it and it started it all. In other words, it's only way to return an object literal like that in a function. So the community just adapted this style.

                                          Jeremy Falcon

                                          J Offline
                                          J Offline
                                          jsc42
                                          wrote on last edited by
                                          #20

                                          TOOTBS (aka K&R) is only about layout of statements. It gets confused with object literals purely because they use braces but for an entirely different purpose; same as '(' and ')' have multiple uses, as does the ','. That, combined with the lazy end of statement convention, is the only rationale for using TOOTBS. But you can use logical aligned braces even when returning objects either by making them subexpressions (using one of the alternative uses of '(' and ')') or by converting the object to a named object. Viz:

                                          return (
                                          {
                                          p1: 1,
                                          p2: 2
                                          });
                                          // or (my preference)
                                          var result =
                                          {
                                          p1: 1,
                                          p2: 2
                                          };

                                          return result;

                                          J 2 Replies 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