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 good code?

What is good code?

Scheduled Pinned Locked Moved The Lounge
questiondiscussion
104 Posts 39 Posters 1 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.
  • R realJSOP

    If the code works as designed, it's good.

    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
    -----
    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
    -----
    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

    S Offline
    S Offline
    SSEAR
    wrote on last edited by
    #22

    public int Add(int num1, int num2)
    {
    return num1 + num2;
    }

    public int Add(int num1, int num2)
    {
    return num1 + num2 - 0;
    }

    Both code will work as it designed for. But which one is the best code?

    S C L 3 Replies Last reply
    0
    • S SSEAR

      public int Add(int num1, int num2)
      {
      return num1 + num2;
      }

      public int Add(int num1, int num2)
      {
      return num1 + num2 - 0;
      }

      Both code will work as it designed for. But which one is the best code?

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

      SSEAR wrote:

      Both code will work as it designed for. But which one is the best code?

      Ah! There in lies the question. If both work as designed, does it really matter, and whom does it matter too? ;)

      "the meat from that butcher is just the dogs danglies, absolutely amazing cuts of beef." - DaveAuld (2011)
      "No, that is just the earthly manifestation of the Great God Retardon." - Nagy Vilmos (2011) "It is the celestial scrotum of good luck!" - Nagy Vilmos (2011) "But you probably have the smoothest scrotum of any grown man" - Pete O'Hanlon (2012)

      S 1 Reply Last reply
      0
      • S SSEAR

        public int Add(int num1, int num2)
        {
        return num1 + num2;
        }

        public int Add(int num1, int num2)
        {
        return num1 + num2 - 0;
        }

        Both code will work as it designed for. But which one is the best code?

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #24

        I like the second one: good for teaching the kids about the addition neutral element.

        Veni, vidi, vici.

        1 Reply Last reply
        0
        • S Slacker007

          Who determines if your code is good? What is good code and what are the precise criteria for giving it a status of "good"? Since most of your code will never be reviewed by your peers (outside of work) then how do you know it is worth a damn, really? If I think your code sucks but the next guy thinks it is good, then who is right? This kind of goes in line with my other thoughts about the "greatness" of an individual programmer. Who determines that a person is a great programmer and that person writes some damn good code? You? Me? None of the above? We obviously cannot make this determination about ourselves or our code. So who does?

          "the meat from that butcher is just the dogs danglies, absolutely amazing cuts of beef." - DaveAuld (2011)
          "No, that is just the earthly manifestation of the Great God Retardon." - Nagy Vilmos (2011) "It is the celestial scrotum of good luck!" - Nagy Vilmos (2011) "But you probably have the smoothest scrotum of any grown man" - Pete O'Hanlon (2012)

          E Offline
          E Offline
          Espen Harlinn
          wrote on last edited by
          #25

          Slacker007 wrote:

          What is good code and what are the precise criteria for giving it a status of "good"?

          There will never be any 'precise criteria' that will allow you to determine this.

          Slacker007 wrote:

          how do you know it is worth a damn, really?

          Does it work? if not, it's worthless ... Is it readable, consistent and does it conform to common conventions? If not it will be a nightmare to pick up in a couple of years. How much time have you spent debugging your code? I think this is a good indicator, the less time you spend in the debugger the better the quality of the code. Is the important stuff testable, and do you have a good suite of tests for that? I think 80% of the tests I've seen does little more than test that the developer is not schizofrenic. Writing good tests is at least as hard as writing the other stuff. I prefer quality over quantity. Is it easy to debug? Complex expressions are harder to debug. By splitting them up into multiple expressions your code will be easier to debug.

          Slacker007 wrote:

          So who does?

          Call in an external expert - preferably one who is well known for the quality of her/his work, and make it clear that you will not be offended when she/he has something less than favourable to say about your work. Done right this can be a great learning experience.

          Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS My LinkedIn Profile

          S 1 Reply Last reply
          0
          • V V 0

            Slacker007 wrote:

            someone who thinks they know what good code is

            That's the key point. We all think we write good code. I can't imagine any professional programmer that would write bad code on purpose. There are people we use different naming conventions, different patterns or just plain old spaghetti code, but in the end, if the client is happy and the application works, it is not bad code. Could it be better? Probably and mostly, but in the end all these things are the programmers personal opinions. (eg. I can fix bugs in a matter of minutes in my own applications, but if you ask someone else to look at it, even if he knows the code very well, it will take longer, just because my mindset is different)

            V.

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #26

            V. wrote:

            but in the end, if the client is happy and the application works, it is not bad code

            It may be ugly, though.

            Veni, vidi, vici.

            V L 2 Replies Last reply
            0
            • E Espen Harlinn

              Slacker007 wrote:

              What is good code and what are the precise criteria for giving it a status of "good"?

              There will never be any 'precise criteria' that will allow you to determine this.

              Slacker007 wrote:

              how do you know it is worth a damn, really?

              Does it work? if not, it's worthless ... Is it readable, consistent and does it conform to common conventions? If not it will be a nightmare to pick up in a couple of years. How much time have you spent debugging your code? I think this is a good indicator, the less time you spend in the debugger the better the quality of the code. Is the important stuff testable, and do you have a good suite of tests for that? I think 80% of the tests I've seen does little more than test that the developer is not schizofrenic. Writing good tests is at least as hard as writing the other stuff. I prefer quality over quantity. Is it easy to debug? Complex expressions are harder to debug. By splitting them up into multiple expressions your code will be easier to debug.

              Slacker007 wrote:

              So who does?

              Call in an external expert - preferably one who is well known for the quality of her/his work, and make it clear that you will not be offended when she/he has something less than favourable to say about your work. Done right this can be a great learning experience.

              Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS My LinkedIn Profile

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

              :thumbsup:

              "the meat from that butcher is just the dogs danglies, absolutely amazing cuts of beef." - DaveAuld (2011)
              "No, that is just the earthly manifestation of the Great God Retardon." - Nagy Vilmos (2011) "It is the celestial scrotum of good luck!" - Nagy Vilmos (2011) "But you probably have the smoothest scrotum of any grown man" - Pete O'Hanlon (2012)

              1 Reply Last reply
              0
              • C CPallini

                V. wrote:

                but in the end, if the client is happy and the application works, it is not bad code

                It may be ugly, though.

                Veni, vidi, vici.

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #28

                CPallini wrote:

                It may be ugly, though.

                To you and/or to me, but is it bad then? You'd probably think I wrote bad code as well. The more two programmers 'think/write' alike, the more they think of each other as good programmers. :)

                V.

                C 1 Reply Last reply
                0
                • S Slacker007

                  Who determines if your code is good? What is good code and what are the precise criteria for giving it a status of "good"? Since most of your code will never be reviewed by your peers (outside of work) then how do you know it is worth a damn, really? If I think your code sucks but the next guy thinks it is good, then who is right? This kind of goes in line with my other thoughts about the "greatness" of an individual programmer. Who determines that a person is a great programmer and that person writes some damn good code? You? Me? None of the above? We obviously cannot make this determination about ourselves or our code. So who does?

                  "the meat from that butcher is just the dogs danglies, absolutely amazing cuts of beef." - DaveAuld (2011)
                  "No, that is just the earthly manifestation of the Great God Retardon." - Nagy Vilmos (2011) "It is the celestial scrotum of good luck!" - Nagy Vilmos (2011) "But you probably have the smoothest scrotum of any grown man" - Pete O'Hanlon (2012)

                  S Offline
                  S Offline
                  S Houghtelin
                  wrote on last edited by
                  #29

                  Good code above all, as several of the posters have already pointed to, is code that works as intended. I find the idea of people comparing their code masterpieces to paintings by great artists interesting. And indeed there is some beautiful code out there. However, if I hire painters to paint my house white, I don’t want a masterpiece that belongs in a museum, I want the white I paid the guy to paint. It should take at most a day or two to finish the job. I think the same holds true for a musical ensemble, all the parts need to work together and many of those parts are simple sequences. As a whole they make beautiful music. The band I’m in we’ve been auditioning for a fourth member, every one of them so far that have come have been very talented players, but having someone going “beadily-beadily- beadily-kerang!” over everything we play isn’t what we are looking for. We want a good utility player who can shine but not overpower or outshine everything we do. It makes for unlistenable music. Ultimately, the determination of good code is how the audience or customer perceives it. Is it useful? Does it do what they paid you to make it do? Do they want to come back for more?

                  It was broke, so I fixed it.

                  E V 2 Replies Last reply
                  0
                  • S S Houghtelin

                    Good code above all, as several of the posters have already pointed to, is code that works as intended. I find the idea of people comparing their code masterpieces to paintings by great artists interesting. And indeed there is some beautiful code out there. However, if I hire painters to paint my house white, I don’t want a masterpiece that belongs in a museum, I want the white I paid the guy to paint. It should take at most a day or two to finish the job. I think the same holds true for a musical ensemble, all the parts need to work together and many of those parts are simple sequences. As a whole they make beautiful music. The band I’m in we’ve been auditioning for a fourth member, every one of them so far that have come have been very talented players, but having someone going “beadily-beadily- beadily-kerang!” over everything we play isn’t what we are looking for. We want a good utility player who can shine but not overpower or outshine everything we do. It makes for unlistenable music. Ultimately, the determination of good code is how the audience or customer perceives it. Is it useful? Does it do what they paid you to make it do? Do they want to come back for more?

                    It was broke, so I fixed it.

                    E Offline
                    E Offline
                    Espen Harlinn
                    wrote on last edited by
                    #30

                    S Houghtelin wrote:

                    I think the same holds true for a musical ensemble

                    A musical ensemble spends a lot of time practising, how many developers does that at all?

                    Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS My LinkedIn Profile

                    S P 2 Replies Last reply
                    0
                    • S S Houghtelin

                      Good code above all, as several of the posters have already pointed to, is code that works as intended. I find the idea of people comparing their code masterpieces to paintings by great artists interesting. And indeed there is some beautiful code out there. However, if I hire painters to paint my house white, I don’t want a masterpiece that belongs in a museum, I want the white I paid the guy to paint. It should take at most a day or two to finish the job. I think the same holds true for a musical ensemble, all the parts need to work together and many of those parts are simple sequences. As a whole they make beautiful music. The band I’m in we’ve been auditioning for a fourth member, every one of them so far that have come have been very talented players, but having someone going “beadily-beadily- beadily-kerang!” over everything we play isn’t what we are looking for. We want a good utility player who can shine but not overpower or outshine everything we do. It makes for unlistenable music. Ultimately, the determination of good code is how the audience or customer perceives it. Is it useful? Does it do what they paid you to make it do? Do they want to come back for more?

                      It was broke, so I fixed it.

                      V Offline
                      V Offline
                      V 0
                      wrote on last edited by
                      #31

                      S Houghtelin wrote:

                      we’ve been auditioning for a fourth member

                      Damn, too far away. sorry :-D

                      V.

                      S 1 Reply Last reply
                      0
                      • E Espen Harlinn

                        S Houghtelin wrote:

                        I think the same holds true for a musical ensemble

                        A musical ensemble spends a lot of time practising, how many developers does that at all?

                        Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS My LinkedIn Profile

                        S Offline
                        S Offline
                        S Houghtelin
                        wrote on last edited by
                        #32

                        Espen Harlinn wrote:

                        A musical ensemble spends a lot of time practising, how many developers does that at all?

                        If you code for a living, the practice is from the experience you gain from coding every day. You may be right, I've seen some examples of those who don't appear to code very often... ;)

                        It was broke, so I fixed it.

                        E 1 Reply Last reply
                        0
                        • V V 0

                          CPallini wrote:

                          It may be ugly, though.

                          To you and/or to me, but is it bad then? You'd probably think I wrote bad code as well. The more two programmers 'think/write' alike, the more they think of each other as good programmers. :)

                          V.

                          C Offline
                          C Offline
                          CPallini
                          wrote on last edited by
                          #33

                          Could be ugly to common sense. For instance, 'copy & paste' as 'code reusing' may look ugly to most developers.

                          Veni, vidi, vici.

                          1 Reply Last reply
                          0
                          • S S Houghtelin

                            Espen Harlinn wrote:

                            A musical ensemble spends a lot of time practising, how many developers does that at all?

                            If you code for a living, the practice is from the experience you gain from coding every day. You may be right, I've seen some examples of those who don't appear to code very often... ;)

                            It was broke, so I fixed it.

                            E Offline
                            E Offline
                            Espen Harlinn
                            wrote on last edited by
                            #34

                            S Houghtelin wrote:

                            the practice is from the experience you gain from coding every day

                            This works well up to a certain limit, to go beyond that I think you have to start practicing ...

                            Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS My LinkedIn Profile

                            1 Reply Last reply
                            0
                            • R realJSOP

                              If the code works as designed, it's good.

                              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                              -----
                              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                              -----
                              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                              E Offline
                              E Offline
                              Espen Harlinn
                              wrote on last edited by
                              #35

                              John Simmons / outlaw programmer wrote:

                              If the code works as designed

                              Have you seen code that doesn't work as designed?

                              Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS My LinkedIn Profile

                              C 1 Reply Last reply
                              0
                              • S Slacker007

                                SSEAR wrote:

                                Both code will work as it designed for. But which one is the best code?

                                Ah! There in lies the question. If both work as designed, does it really matter, and whom does it matter too? ;)

                                "the meat from that butcher is just the dogs danglies, absolutely amazing cuts of beef." - DaveAuld (2011)
                                "No, that is just the earthly manifestation of the Great God Retardon." - Nagy Vilmos (2011) "It is the celestial scrotum of good luck!" - Nagy Vilmos (2011) "But you probably have the smoothest scrotum of any grown man" - Pete O'Hanlon (2012)

                                S Offline
                                S Offline
                                SSEAR
                                wrote on last edited by
                                #36

                                There are a number of aspects we should consider to judge a good code. Unfortunately these are really vague and relative to the situation

                                1 Reply Last reply
                                0
                                • S Slacker007

                                  Who determines if your code is good? What is good code and what are the precise criteria for giving it a status of "good"? Since most of your code will never be reviewed by your peers (outside of work) then how do you know it is worth a damn, really? If I think your code sucks but the next guy thinks it is good, then who is right? This kind of goes in line with my other thoughts about the "greatness" of an individual programmer. Who determines that a person is a great programmer and that person writes some damn good code? You? Me? None of the above? We obviously cannot make this determination about ourselves or our code. So who does?

                                  "the meat from that butcher is just the dogs danglies, absolutely amazing cuts of beef." - DaveAuld (2011)
                                  "No, that is just the earthly manifestation of the Great God Retardon." - Nagy Vilmos (2011) "It is the celestial scrotum of good luck!" - Nagy Vilmos (2011) "But you probably have the smoothest scrotum of any grown man" - Pete O'Hanlon (2012)

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #37

                                  Good code is fit for purpose. Of course the first idea of fit for purpose is that it fulfills the functional requirements and doesn't break the nonfunctional criteria. But as, or more, important might be maintainability. Commenting, clean object orientation and layering of library code might be more important than execution efficiency. Consider whether your project might live long, and might get maintained by several people, and whether those people will be with the project long enough to learn all its pitfalls or whether they will be temporary hires to fix up and add functionality. These factors affect how important documentation, naming conventions and avoiding optimizations that make the code hard to read. In the project I'm working on,good code is code that is tested, where the tests are marked up with requirements tracing so we can see who asked for the functionality and in what context. Then we can retire unwanted functionality which allows refactoring. Without that, we can't touch code in case we break something that some user relies on.

                                  1 Reply Last reply
                                  0
                                  • S Slacker007

                                    Who determines if your code is good? What is good code and what are the precise criteria for giving it a status of "good"? Since most of your code will never be reviewed by your peers (outside of work) then how do you know it is worth a damn, really? If I think your code sucks but the next guy thinks it is good, then who is right? This kind of goes in line with my other thoughts about the "greatness" of an individual programmer. Who determines that a person is a great programmer and that person writes some damn good code? You? Me? None of the above? We obviously cannot make this determination about ourselves or our code. So who does?

                                    "the meat from that butcher is just the dogs danglies, absolutely amazing cuts of beef." - DaveAuld (2011)
                                    "No, that is just the earthly manifestation of the Great God Retardon." - Nagy Vilmos (2011) "It is the celestial scrotum of good luck!" - Nagy Vilmos (2011) "But you probably have the smoothest scrotum of any grown man" - Pete O'Hanlon (2012)

                                    M Offline
                                    M Offline
                                    Mycroft Holmes
                                    wrote on last edited by
                                    #38

                                    I go the other way, I know when code is crappy. Mind you crappy code tends to appear whenever I look at old applications, the more you learn the better your code should be. If it does the job and I like the way it does the job then AFAIAC it is good code.

                                    Never underestimate the power of human stupidity RAH

                                    S 1 Reply Last reply
                                    0
                                    • E Espen Harlinn

                                      John Simmons / outlaw programmer wrote:

                                      If the code works as designed

                                      Have you seen code that doesn't work as designed?

                                      Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS My LinkedIn Profile

                                      C Offline
                                      C Offline
                                      CPallini
                                      wrote on last edited by
                                      #39

                                      Espen Harlinn wrote:

                                      Have you seen code that doesn't work as designed?

                                      All the time.

                                      Veni, vidi, vici.

                                      E 1 Reply Last reply
                                      0
                                      • R realJSOP

                                        If the code works as designed, it's good.

                                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                        -----
                                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                        -----
                                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                                        Z Offline
                                        Z Offline
                                        ZurdoDev
                                        wrote on last edited by
                                        #40

                                        Just because code works, does not mean it is good.

                                        There are only 10 types of people in the world, those who understand binary and those who don't.

                                        R 1 Reply Last reply
                                        0
                                        • S Slacker007

                                          Who determines if your code is good? What is good code and what are the precise criteria for giving it a status of "good"? Since most of your code will never be reviewed by your peers (outside of work) then how do you know it is worth a damn, really? If I think your code sucks but the next guy thinks it is good, then who is right? This kind of goes in line with my other thoughts about the "greatness" of an individual programmer. Who determines that a person is a great programmer and that person writes some damn good code? You? Me? None of the above? We obviously cannot make this determination about ourselves or our code. So who does?

                                          "the meat from that butcher is just the dogs danglies, absolutely amazing cuts of beef." - DaveAuld (2011)
                                          "No, that is just the earthly manifestation of the Great God Retardon." - Nagy Vilmos (2011) "It is the celestial scrotum of good luck!" - Nagy Vilmos (2011) "But you probably have the smoothest scrotum of any grown man" - Pete O'Hanlon (2012)

                                          Z Offline
                                          Z Offline
                                          ZurdoDev
                                          wrote on last edited by
                                          #41

                                          Good code can be understood and modified by other developers. Good code is modular enough, but not too much, such that when change requests come it is usually not a major rewrite.

                                          There are only 10 types of people in the world, those who understand binary and those who don't.

                                          L 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