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.
  • M Matt Gerrans

    No, it is an aesthetics question! What programmer has style? ;)

    Matt Gerrans

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

    OK my bad. What about

          bstrValue
         =
    

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

    :rolleyes:


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

    M 1 Reply Last reply
    0
    • M Matt Gerrans

      That's just plain wrong (and I'm not just talking about the superfluous curlies) -- I already pointed out that this is not about programming, per se. I guess some people can't tell the difference between a question and a metaquestion. ;P

      Matt Gerrans

      C Offline
      C Offline
      Clickok
      wrote on last edited by
      #23

      Matt Gerrans wrote:

      some people can't tell the difference between a question and a metaquestion.

      Ha! metaquestion is very good! Do you accept metareplies too? :laugh::laugh::laugh:


      For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:

      P 1 Reply Last reply
      0
      • M Matt Gerrans

        That's just plain wrong (and I'm not just talking about the superfluous curlies) -- I already pointed out that this is not about programming, per se. I guess some people can't tell the difference between a question and a metaquestion. ;P

        Matt Gerrans

        J Offline
        J Offline
        Jerry Hammond
        wrote on last edited by
        #24

        Rules: The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. Do not post programming questions (use the programming forums for that) and please don't post ads. So sorry, you get it, no.

        A room without books is like a body without a soul. - Cicero (106 BC - 43 AD)

        M 1 Reply Last reply
        0
        • J Jerry Hammond

          What happened to if(IsLounge) { Label1.Text="No Programming Questions"; } elseif { Label1.Text="Ask in appropriate forum"; }

          A room without books is like a body without a soul. - Cicero (106 BC - 43 AD)

          B Offline
          B Offline
          Bradml
          wrote on last edited by
          #25

          Jerry Hammond wrote:

          elseif

          Should this not be "else"?


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

          D 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

            J Offline
            J Offline
            Jasmine2501
            wrote on last edited by
            #26

            They are both the same. The first one has extraneous comments though. The code clearly says it's being set to "OFF or "ON"... why say the same thing in a comment?

            "Quality Software since 1983!"
            http://www.smoothjazzy.com/ - see the "Programming" section for (freeware) JazzySiteMaps, a simple application to generate .Net and Google-style sitemaps!

            1 Reply Last reply
            0
            • D DavidNohejl

              OK my bad. What about

                    bstrValue
                   =
              

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

              :rolleyes:


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

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

              I've seen "production code" that looks a lot like that. X|

              Matt Gerrans

              1 Reply Last reply
              0
              • J Jerry Hammond

                Rules: The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. Do not post programming questions (use the programming forums for that) and please don't post ads. So sorry, you get it, no.

                A room without books is like a body without a soul. - Cicero (106 BC - 43 AD)

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

                I'm not sure I understand your broken sentence rightly, but it sounds like you don't understand the difference between "a programming question" and "a question about programming practice." A programming question would be something like "how do you code the travelling salesman algorithm in Plain English?" * * and the answer would be a lot of dodging about without actually answering the question! :)

                Matt Gerrans

                J W 2 Replies 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
                  Andy Brummer
                  wrote on last edited by
                  #29

                  For a simple condition like that the later. The only bad part of the first approach are the comments, though I would type the second if I was doing it myself, I wouldn't really care when reading it. I hope that is a CComBSTR or some wrapper like that, or the style isn't going to make much difference.

                  The pig go. Go is to the fountain. The pig put foot. Grunt. Foot in what? ketchup. The dove fly. Fly is in sky. The dove drop something. The something on the pig. The pig disgusting. The pig rattle. Rattle with dove. The dove angry. The pig leave. The dove produce. Produce is chicken wing. With wing bark. No Quack. - Thedailywtf.com

                  1 Reply Last reply
                  0
                  • C Clickok

                    I started to think about, but I have noticed what is a programming question in the Lounge, then my opinion goes to soapbox... Ok, ok I'm lazy about select another forum... Here goes: The first option is better. Why? The code is easy to maintain. If I wish add some task if is true (by example), I can do this:

                    //If the flag is on then
                    if(READBIT(*pGlobalFlags, uFlag))
                    {
                    //Value is set to on
                    bstrValue = "ON";
                    // another task here
                    MyAnotherTaskFunction();

                    }
                    else
                    {
                    //Value is set to off
                    bstrValue = "OFF";
                    }

                    Notice what I just added 1 line of code, and in your another style I will need create the supressed if construct. Just my 2 cents Regards


                    For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:

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

                    Well I think that's a valid point, and sort of why I also always put in the {} even around a single statement (and I think they should be required, and the IDE should insert them automatically, with the else as well). The opposite of this situation would when there are multiple statements and then all but one are removed, would you then remove the {}? Would you rearrange it to the ternary operator? However, I really only use the ternary operator when dealing with parameters (and returns): System.Console.WriteLine ( "I want {0} piece{1} of candy." , x , x==1?"":"s" ) ; return ( x==-1?0:x ) ; The main problem with the ternary operator is that (like macroes) some times things can get confused, I don't have an example in front of me, but at times you need to use "extra" parentheses: (int)x==1?y:z // will likely cause trouble; what does the cast apply to? ((int)x==1)?y:z // do you mean this? (int)(x==1?y:z) // or this?

                    1 Reply Last reply
                    0
                    • C Clickok

                      Matt Gerrans wrote:

                      some people can't tell the difference between a question and a metaquestion.

                      Ha! metaquestion is very good! Do you accept metareplies too? :laugh::laugh::laugh:


                      For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:

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

                      Hmmm, I may have this backward.... Dicky Smothers: "I asked a rhetorical question." Tommy Smothers: "I gave a rhetorical answer."

                      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

                        B Offline
                        B Offline
                        brianwelsch
                        wrote on last edited by
                        #32

                        I'd go for the if..else construct any time I can. I can't stand the ternary operator. Irrational I suppose, but there it is.

                        BW


                        If you're not part of the solution, you're part of the precipitate.
                        -- Steven Wright

                        1 Reply Last reply
                        0
                        • M Matt Gerrans

                          I'm not sure I understand your broken sentence rightly, but it sounds like you don't understand the difference between "a programming question" and "a question about programming practice." A programming question would be something like "how do you code the travelling salesman algorithm in Plain English?" * * and the answer would be a lot of dodging about without actually answering the question! :)

                          Matt Gerrans

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

                          Matt Gerrans wrote:

                          A programming question would be something like "how do you code the travelling salesman algorithm in Plain English?" *

                          You have to give Jerry credit though, it is a fine line being walked. And, for reasons of people searching for this stuff (let's assume the CP site search works :rolleyes:) it would be better off belonging in the VC++ forum. Personally, I'm indifferent on this one, but I do see where he's coming from.

                          Jeremy Falcon A multithreaded, OpenGL-enabled application.[^]

                          1 Reply Last reply
                          0
                          • J Jerry Hammond

                            What happened to if(IsLounge) { Label1.Text="No Programming Questions"; } elseif { Label1.Text="Ask in appropriate forum"; }

                            A room without books is like a body without a soul. - Cicero (106 BC - 43 AD)

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

                            My thoughts: I find it totally appropriate that we can talk about programming in a casual way in the lounge. After all, that's what on our minds. I just don't want to see the lounge flooded with "help with my homework" requests, and "I know the VC++ forum, but I hope to get a quicker and more detailed answer here". If, to enforce this, we have to ban all programming talk from the lounge, I can live with this. But I rather have people show good manners and good taste, and don't crash into a doctors apres-ski party to ask about their furuncles. Above question is fine with me (as are the XDay programming quizzes)


                            Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
                            We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                            Linkify!|Fold With Us!

                            J 1 Reply Last reply
                            0
                            • P peterchen

                              My thoughts: I find it totally appropriate that we can talk about programming in a casual way in the lounge. After all, that's what on our minds. I just don't want to see the lounge flooded with "help with my homework" requests, and "I know the VC++ forum, but I hope to get a quicker and more detailed answer here". If, to enforce this, we have to ban all programming talk from the lounge, I can live with this. But I rather have people show good manners and good taste, and don't crash into a doctors apres-ski party to ask about their furuncles. Above question is fine with me (as are the XDay programming quizzes)


                              Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
                              We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                              Linkify!|Fold With Us!

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

                              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 1 Reply Last reply
                              0
                              • M Matt Gerrans

                                I'm not sure I understand your broken sentence rightly, but it sounds like you don't understand the difference between "a programming question" and "a question about programming practice." A programming question would be something like "how do you code the travelling salesman algorithm in Plain English?" * * and the answer would be a lot of dodging about without actually answering the question! :)

                                Matt Gerrans

                                W Offline
                                W Offline
                                WillemM
                                wrote on last edited by
                                #36

                                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 1 Reply Last reply
                                0
                                • A Anton Afanasyev

                                  You're all wrong. The correct answer depends on how you're getting paid - by the number of lines written(then the first), or by the functionality implemented (then the second). Yup, thats it;)


                                  :badger:

                                  W Offline
                                  W Offline
                                  WillemM
                                  wrote on last edited by
                                  #37

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