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. What is Better Code in Your Opinion?

What is Better Code in Your Opinion?

Scheduled Pinned Locked Moved The Lounge
questioncom
50 Posts 25 Posters 0 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.
  • B Bradml

    Jerry Hammond wrote:

    elseif

    Should this not be "else"?


    Brad Australian By contacting your lawyer you negate the right to sue me.

    D Offline
    D Offline
    DavidNohejl
    wrote on last edited by
    #38

    yes, see title of my reply to him. :)


    "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

    1 Reply Last reply
    0
    • M Marc Clifton

      If I really wanted to do it right, I'd do something like:

      bstrValue = GetBitStateAsStateString(READBIT(*pGlobalFlags, uFlag));

      or, if you want to go for debuggable/more readable:

      byte bit=READBIT(*pGlobalFlags, uFlag);
      bstrValue = GetBitStateAsStateString(bit);

      This gives you at least a fighting chance to handle internationalization and centralizes the meaning of a bit when converted to a string. After all, "True" and "False" might be equally valid, so you might have:

      bstrValue = GetBitStateAsTruthString(READBIT(*pGlobalFlags, uFlag));

      And no, I don't write code like that usually myself! :) Marc

      Thyme In The Country

      People are just notoriously impossible. --DavidCrow
      There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
      People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith

      T Offline
      T Offline
      Todd Smith
      wrote on last edited by
      #39

      I was going to say neither but I didn't feel like typing why :D So if those are your only two options then #2 (no pun intended) but I also agree with Marc on the other points.

      Todd Smith

      1 Reply Last reply
      0
      • D DavidNohejl

        Personally, I would do it like this, kinda taking best of both options (IMO).

        // getting string value from flag
        strValue = (READBIT(*pGlobalFlags, uFlag))?"ON":"OFF";

        // do different stuff depending on value of strValue
        if(strValue == "ON")
        {
        MyTaskFunction();
        }
        else
        {
        MyAnotherTaskFunction();
        }


        "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

        T Offline
        T Offline
        Todd Smith
        wrote on last edited by
        #40

        What if you need to change the representation for "ON" and "OFF"?

        Todd Smith

        D 1 Reply Last reply
        0
        • T Todd Smith

          What if you need to change the representation for "ON" and "OFF"?

          Todd Smith

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #41

          How does it relate to original poster's question, which I believe was about if...else construct vs ternary operator? Oh well at least I now know why my style got voted down. :doh:


          "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

          1 Reply Last reply
          0
          • D DavidNohejl

            :P It's style question. People usually get away with it. IMO its equal to "Do you like code red or pitr-cola more?" kind of question.


            "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

            E Offline
            E Offline
            El Corazon
            wrote on last edited by
            #42

            dnh wrote:

            Do you like code red or pitr-cola more

            Code red! ;P though I think I miss Jolt Cola the most. :)

            _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

            D 1 Reply Last reply
            0
            • J Jeremy Falcon

              That's pretty much my thoughts on the matter too, just in case anyone actually cared to know that. :laugh:

              Jeremy Falcon "It's a good thing to do and a tasty way to do it." - Wilford Brimley[^]

              T Offline
              T Offline
              Todd Smith
              wrote on last edited by
              #43

              How about a reward system? Once you've submitted X articles of Y quality you can post anything, anywhere you want :D

              Todd Smith

              1 Reply Last reply
              0
              • E El Corazon

                dnh wrote:

                Do you like code red or pitr-cola more

                Code red! ;P though I think I miss Jolt Cola the most. :)

                _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

                D Offline
                D Offline
                DavidNohejl
                wrote on last edited by
                #44

                http://ars.userfriendly.org/cartoons/?id=20020520[^] :)


                "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

                1 Reply Last reply
                0
                • W WillemM

                  Ooh c'mon, I wanted to ask that for plain mandarin ;P

                  WM. What about weapons of mass-construction? "You can always try to smash it with a wrench to fix that. It might actually work" - WillemM

                  M Offline
                  M Offline
                  Matt Gerrans
                  wrote on last edited by
                  #45

                  WillemM wrote:

                  Ooh c'mon, I wanted to ask that for plain mandarin

                  :doh: I had to read that at least three times before I realized you were talking about the language not the fruit! :) Maybe Jeremy can present the 3D Plain Oatmeal implementation as a demo of his API. :)

                  Matt Gerrans

                  1 Reply Last reply
                  0
                  • W WillemM

                    I wonder why a company would pay you by line, because that first construct could really use some more comments if you get payed by the line ;P

                    WM. What about weapons of mass-construction? "You can always try to smash it with a wrench to fix that. It might actually work" - WillemM

                    A Offline
                    A Offline
                    Anton Afanasyev
                    wrote on last edited by
                    #46

                    I dunno, there just seems to be an explosion of people who write so many unnecessary lines that it seems they're geting paid by the line count.


                    :badger:

                    1 Reply Last reply
                    0
                    • M Matt Gerrans

                      This:

                          //If the flag is on then
                          if(READBIT(\*pGlobalFlags, uFlag))
                          {
                                  //Value is set to on
                                  bstrValue = "ON";
                          }
                          else
                          {
                                  //Value is set to off
                                  bstrValue = "OFF";
                          }
                      

                      Or this:

                      bstrValue = READBIT(*pGlobalFlags, uFlag) ? "ON" : "OFF";

                      :confused:

                      Matt Gerrans

                      S Offline
                      S Offline
                      Stuart Dootson
                      wrote on last edited by
                      #47

                      The second, so you can do:

                      const BSTR bstrValue = READBIT(*pGlobalFlags, uFlag) ? "ON" : "OFF";
                      

                      I tend to write code with lots of consts (kind of an emulation of a functional style), so I use the conditional operator a lot.

                      1 Reply Last reply
                      0
                      • M Matt Gerrans

                        This:

                            //If the flag is on then
                            if(READBIT(\*pGlobalFlags, uFlag))
                            {
                                    //Value is set to on
                                    bstrValue = "ON";
                            }
                            else
                            {
                                    //Value is set to off
                                    bstrValue = "OFF";
                            }
                        

                        Or this:

                        bstrValue = READBIT(*pGlobalFlags, uFlag) ? "ON" : "OFF";

                        :confused:

                        Matt Gerrans

                        A Offline
                        A Offline
                        Aryo Handono
                        wrote on last edited by
                        #48

                        Matt Gerrans wrote:

                        bstrValue = READBIT(*pGlobalFlags, uFlag) ? "ON" : "OFF";

                        I prefer this one, it looks like "advance source code" :-D:-D

                        "Courage choose who will follow, Fate choose who will lead" - Lord Gunner, Septerra Core "Press any key to continue, where's the ANY key ?" - Homer Simpsons Drinking gives me amazing powers of insight. I can solve all the worlds problems when drunk, but can never remember the solutions in the morning. - Michael P Butler to Paul Watson on 12/08/03

                        1 Reply Last reply
                        0
                        • S Shog9 0

                          Why? No redundant comments to infuriate me, that's why.

                          O Offline
                          O Offline
                          Obliterator
                          wrote on last edited by
                          #49

                          Generally the latter, but in this instance I would prefer the former. The comments document the fact the message refers to the state of the 'value'. Without the comments, I may have thought the function simply returned a success flag. I might expect it place the actual value in one of the parameters. I would then need to look more closely at the definition/implementation/comments of the Read function to determine more about how it worked.

                          -- The Obliterator

                          S 1 Reply Last reply
                          0
                          • O Obliterator

                            Generally the latter, but in this instance I would prefer the former. The comments document the fact the message refers to the state of the 'value'. Without the comments, I may have thought the function simply returned a success flag. I might expect it place the actual value in one of the parameters. I would then need to look more closely at the definition/implementation/comments of the Read function to determine more about how it worked.

                            -- The Obliterator

                            S Offline
                            S Offline
                            Shog9 0
                            wrote on last edited by
                            #50

                            Obliterator wrote:

                            I would then need to look more closely at the definition/implementation/comments of the Read function to determine more about how it worked.

                            If there were side-effects, this is something i would expect to be indicated, either in code or in comments. Neither block indicated such a thing, the comments in the first merely describing what was implicit in the code.

                            ---- Do you see what i see? Why do we live like this? Is it because it's true... ...That ignorance is bliss?

                            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