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. How do you write fewer lines of code?

How do you write fewer lines of code?

Scheduled Pinned Locked Moved The Lounge
questioncsharpphpcsslinq
72 Posts 38 Posters 3 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.
  • realJSOPR realJSOP

    Close the IDE after the next semi-colon.

    Marc Clifton wrote:

    One obvious answer is, replace redundant code with a function.

    But that's just writing the code somewhere else, so that doesn't count. There are no "good practices. You can eliminate error checking (bad practice), elminate line breaks (bad practice), or according to some start writing in VB (bad practice). This bizarre search for "less code" leads to the crap we get from Microsoft.

    ".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

    R Offline
    R Offline
    Reiss
    wrote on last edited by
    #6

    John Simmons / outlaw programmer wrote:

    Marc Clifton wrote:

    One obvious answer is, replace redundant code with a function.

    But that's just writing the code somewhere else, so that doesn't count.

    I think Marc meant replacing duplicated code with a function call.

    realJSOPR 1 Reply Last reply
    0
    • M Marc Clifton

      From Mehdi's question about "is less lines of code better" -- Question: How do you go about writing fewer lines of code (omitting removing line breaks as an answer) ? One obvious answer is, replace redundant code with a function. Another possible answer is, using Linq to replace for-next loops (funny how we [well, I do] still call them for-next loops) One other answer to that comes to mind is using OOP to eliminate "if" statements regarding type. Anyways, that's my question--if you really want to achieve fewer lines of code but the same behavior, what really are good practices? Marc

      My Blog

      R Offline
      R Offline
      Reiss
      wrote on last edited by
      #7

      Most importantly get your design right and documented and logically trace all code paths - more often than not you will find duplicated/impossible equality checks etc as you move through various function calls.

      1 Reply Last reply
      0
      • R Reiss

        John Simmons / outlaw programmer wrote:

        Marc Clifton wrote:

        One obvious answer is, replace redundant code with a function.

        But that's just writing the code somewhere else, so that doesn't count.

        I think Marc meant replacing duplicated code with a function call.

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #8

        Ahh yes. For me, there has to be more than two lines of code to make it worth it, and then you have to consider th4 amount of stack and heap manipulation involved in making the function call (if it's "redundant", it probably requires some sort of poarameter for the function, thus increasing stack usage) versus just leaving the code where it is. Like everything else in coding, there are trade-offs.

        ".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

        P L T 3 Replies Last reply
        0
        • M Marc Clifton

          From Mehdi's question about "is less lines of code better" -- Question: How do you go about writing fewer lines of code (omitting removing line breaks as an answer) ? One obvious answer is, replace redundant code with a function. Another possible answer is, using Linq to replace for-next loops (funny how we [well, I do] still call them for-next loops) One other answer to that comes to mind is using OOP to eliminate "if" statements regarding type. Anyways, that's my question--if you really want to achieve fewer lines of code but the same behavior, what really are good practices? Marc

          My Blog

          N Offline
          N Offline
          Nagy Vilmos
          wrote on last edited by
          #9

          Marc Clifton wrote:

          One other answer to that comes to mind is using OOP to eliminate "if" statements regarding type.

          I'm not sure it reduces TLOC, but sub-classing by type does reduce complexity. A year or two back, we were putting together a rule engine. Each rule consisted of a field, an operator and a value. The numpty who did the dev work lumped it all into a single Rule class which, as you can imagine was packed from armpits to breaskfast with if's and switch cases. It twas an afternoon's work to prototype splitting it all out and then another couple of days for numpty to implement. As an example the check function went from something like this:

          public boolean check (Field field) {
          switch (comparison) {
          case Comparison.EQUALS {
          return field.getValue().equals(value);
          }
          case Comparison.LESS_THAN {
          return (field.getValue().compare(value) < 0);
          }
          case Comparison.GREATER_THAN {
          return (field.getValue().compare(value) < 0);
          }
          // ten more like this ...
          default : {
          return false;
          }
          }
          }

          to:

          public boolean check (Field field) {
          return comparison.check(field.getValue(), value);
          }

          The comparison was then done specifically by the different types.


          Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

          B 1 Reply Last reply
          0
          • N Nagy Vilmos

            Marc Clifton wrote:

            One other answer to that comes to mind is using OOP to eliminate "if" statements regarding type.

            I'm not sure it reduces TLOC, but sub-classing by type does reduce complexity. A year or two back, we were putting together a rule engine. Each rule consisted of a field, an operator and a value. The numpty who did the dev work lumped it all into a single Rule class which, as you can imagine was packed from armpits to breaskfast with if's and switch cases. It twas an afternoon's work to prototype splitting it all out and then another couple of days for numpty to implement. As an example the check function went from something like this:

            public boolean check (Field field) {
            switch (comparison) {
            case Comparison.EQUALS {
            return field.getValue().equals(value);
            }
            case Comparison.LESS_THAN {
            return (field.getValue().compare(value) < 0);
            }
            case Comparison.GREATER_THAN {
            return (field.getValue().compare(value) < 0);
            }
            // ten more like this ...
            default : {
            return false;
            }
            }
            }

            to:

            public boolean check (Field field) {
            return comparison.check(field.getValue(), value);
            }

            The comparison was then done specifically by the different types.


            Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

            B Offline
            B Offline
            Bassam Abdul Baki
            wrote on last edited by
            #10

            Nagy Vilmos wrote:

            case Comparison.GREATER_THAN { return (field.getValue().compare(value) < 0); }

            Shouldn't this be a >?

            Web - BM - RSS - Math - LinkedIn

            N 1 Reply Last reply
            0
            • C Corporal Agarn

              Isn't this a programming question?

              R Offline
              R Offline
              Reiss
              wrote on last edited by
              #11

              djj55 wrote:

              Isn't this a programming question?

              programming question != question about programming :-D

              X 1 Reply Last reply
              0
              • realJSOPR realJSOP

                Ahh yes. For me, there has to be more than two lines of code to make it worth it, and then you have to consider th4 amount of stack and heap manipulation involved in making the function call (if it's "redundant", it probably requires some sort of poarameter for the function, thus increasing stack usage) versus just leaving the code where it is. Like everything else in coding, there are trade-offs.

                ".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

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

                John Simmons / outlaw programmer wrote:

                increasing stack usage

                Not if you make it a macro. :-O

                1 Reply Last reply
                0
                • B Bassam Abdul Baki

                  Nagy Vilmos wrote:

                  case Comparison.GREATER_THAN { return (field.getValue().compare(value) < 0); }

                  Shouldn't this be a >?

                  Web - BM - RSS - Math - LinkedIn

                  N Offline
                  N Offline
                  Nagy Vilmos
                  wrote on last edited by
                  #13

                  It's just an example! :sigh:


                  Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                  B 1 Reply Last reply
                  0
                  • M Marc Clifton

                    From Mehdi's question about "is less lines of code better" -- Question: How do you go about writing fewer lines of code (omitting removing line breaks as an answer) ? One obvious answer is, replace redundant code with a function. Another possible answer is, using Linq to replace for-next loops (funny how we [well, I do] still call them for-next loops) One other answer to that comes to mind is using OOP to eliminate "if" statements regarding type. Anyways, that's my question--if you really want to achieve fewer lines of code but the same behavior, what really are good practices? Marc

                    My Blog

                    N Offline
                    N Offline
                    NormDroid
                    wrote on last edited by
                    #14

                    How do you write fewer lines of code? Easy; become a burger flipper.

                    Software Kinetics Wear a hard hat it's under construction
                    Metro RSS

                    L 1 Reply Last reply
                    0
                    • N NormDroid

                      How do you write fewer lines of code? Easy; become a burger flipper.

                      Software Kinetics Wear a hard hat it's under construction
                      Metro RSS

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

                      Or if that is a bit too difficult, a manager.

                      Every man can tell how many goats or sheep he possesses, but not how many friends.

                      1 Reply Last reply
                      0
                      • M Marc Clifton

                        From Mehdi's question about "is less lines of code better" -- Question: How do you go about writing fewer lines of code (omitting removing line breaks as an answer) ? One obvious answer is, replace redundant code with a function. Another possible answer is, using Linq to replace for-next loops (funny how we [well, I do] still call them for-next loops) One other answer to that comes to mind is using OOP to eliminate "if" statements regarding type. Anyways, that's my question--if you really want to achieve fewer lines of code but the same behavior, what really are good practices? Marc

                        My Blog

                        D Offline
                        D Offline
                        Dalek Dave
                        wrote on last edited by
                        #16

                        If you want shorter code, limit memory. Back when dinosaurs ruled the Earth we had 16K, and we did a lot with that because we were efficient and didn't fill stuff with unnecessary rubbish. I even used an NCR mainframe which had a massive 128K RAM and twin magnetic tape drives! Programs were 'Terse' to say the least.

                        ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC Link[^] Trolls[^]

                        L 1 Reply Last reply
                        0
                        • B Bassam Abdul Baki

                          Marc Clifton wrote:

                          Another possible answer is, using Linq to replace for-next loops (funny how we [well, I do] still call them for-next loops)

                          Using someone else's code is not technically reducing it.

                          Web - BM - RSS - Math - LinkedIn

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

                          Bassam Abdul-Baki wrote:

                          Using someone else's code is not technically reducing it.

                          I was wondering if someone would say that. Well, technically, it reduces the lines of "my" code. Otherwise, you would have to take this to the logical extremes of 1) any framework code + calls to Win API and 2) might as well reduce it to lines of code in the MSIL/assembly language. That doesn't make a lot of sense to me. Marc

                          My Blog

                          D B 2 Replies Last reply
                          0
                          • realJSOPR realJSOP

                            Ahh yes. For me, there has to be more than two lines of code to make it worth it, and then you have to consider th4 amount of stack and heap manipulation involved in making the function call (if it's "redundant", it probably requires some sort of poarameter for the function, thus increasing stack usage) versus just leaving the code where it is. Like everything else in coding, there are trade-offs.

                            ".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

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

                            Don't forget that the compiler inlines small functions and so eliminates the entire effort for calling. But it's great that there still are people around who give such things a thought.

                            "Dark the dark side is. Very dark..." - Yoda ---
                            "Shut up, Yoda, and just make yourself another toast." - Obi Wan Kenobi

                            1 Reply Last reply
                            0
                            • M Marc Clifton

                              Bassam Abdul-Baki wrote:

                              Using someone else's code is not technically reducing it.

                              I was wondering if someone would say that. Well, technically, it reduces the lines of "my" code. Otherwise, you would have to take this to the logical extremes of 1) any framework code + calls to Win API and 2) might as well reduce it to lines of code in the MSIL/assembly language. That doesn't make a lot of sense to me. Marc

                              My Blog

                              D Offline
                              D Offline
                              Dalek Dave
                              wrote on last edited by
                              #19

                              Marc Clifton wrote:

                              I was wondering if someone would say that. Well, technically, it reduces the lines of "my" code. Otherwise, you would have to take this to the logical extremes of 1) any framework code + calls to Win API and 2) might as well reduce it to lines of code in the MSIL/assembly language.
                               
                              That doesn't make a lot of sense to me.

                              Let me reduce that... I thunk other may ?"that". Reduction.true < MyCode Else U forced object +/- ∞ Case 1: != code.null +API Case 2: < Code.Lines lower language Sense tends to zero.

                              ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC Link[^] Trolls[^]

                              1 Reply Last reply
                              0
                              • D Dalek Dave

                                If you want shorter code, limit memory. Back when dinosaurs ruled the Earth we had 16K, and we did a lot with that because we were efficient and didn't fill stuff with unnecessary rubbish. I even used an NCR mainframe which had a massive 128K RAM and twin magnetic tape drives! Programs were 'Terse' to say the least.

                                ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC Link[^] Trolls[^]

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

                                Ha, beat you! My old computer first had 256 bytes RAM and then was genrously expanded to 4k :)

                                "Dark the dark side is. Very dark..." - Yoda ---
                                "Shut up, Yoda, and just make yourself another toast." - Obi Wan Kenobi

                                D 1 Reply Last reply
                                0
                                • realJSOPR realJSOP

                                  Close the IDE after the next semi-colon.

                                  Marc Clifton wrote:

                                  One obvious answer is, replace redundant code with a function.

                                  But that's just writing the code somewhere else, so that doesn't count. There are no "good practices. You can eliminate error checking (bad practice), elminate line breaks (bad practice), or according to some start writing in VB (bad practice). This bizarre search for "less code" leads to the crap we get from Microsoft.

                                  ".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

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

                                  John Simmons / outlaw programmer wrote:

                                  But that's just writing the code somewhere else, so that doesn't count.

                                  Well, no, if I've copied and pasted the same code into two different methods, I've got x lines of code, but if I make that code into a function, I've got x/2 lines of code (where x is the line count of the code I pasted twice, for those who like arguing. :) )

                                  John Simmons / outlaw programmer wrote:

                                  There are no "good practices.

                                  You know, I like that philosophy. "There are only bad practices." :) Marc

                                  My Blog

                                  1 Reply Last reply
                                  0
                                  • L Lost User

                                    Ha, beat you! My old computer first had 256 bytes RAM and then was genrously expanded to 4k :)

                                    "Dark the dark side is. Very dark..." - Yoda ---
                                    "Shut up, Yoda, and just make yourself another toast." - Obi Wan Kenobi

                                    D Offline
                                    D Offline
                                    Dalek Dave
                                    wrote on last edited by
                                    #22

                                    256 Bytes! That is not far up from an abacus. :)

                                    ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC Link[^] Trolls[^]

                                    L 1 Reply Last reply
                                    0
                                    • OriginalGriffO OriginalGriff

                                      If you really want to write fewer lines of code: get a kitten.

                                      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

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

                                      OriginalGriff wrote:

                                      get a kitten.

                                      Amen to that. My cat definitely reduces the number of lines of code I write! Then again, he's been known to write some extremely verbose, monotonic code. I've been well trained - Win-L to lock the keyboard when I walk away from the desk! Marc

                                      My Blog

                                      OriginalGriffO 1 Reply Last reply
                                      0
                                      • M Marc Clifton

                                        From Mehdi's question about "is less lines of code better" -- Question: How do you go about writing fewer lines of code (omitting removing line breaks as an answer) ? One obvious answer is, replace redundant code with a function. Another possible answer is, using Linq to replace for-next loops (funny how we [well, I do] still call them for-next loops) One other answer to that comes to mind is using OOP to eliminate "if" statements regarding type. Anyways, that's my question--if you really want to achieve fewer lines of code but the same behavior, what really are good practices? Marc

                                        My Blog

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

                                        What's a line of code ? OOP, generics, code generation, code reuse, implants[^]...

                                        M L T 3 Replies Last reply
                                        0
                                        • D Dalek Dave

                                          256 Bytes! That is not far up from an abacus. :)

                                          ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC Link[^] Trolls[^]

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

                                          Typical static RAMs available around 1976. Something like 256 words x 4 bits. You needed two of them to get your 256 bytes. The computer was a kit and you had to solder it together yourself. Back then a small but working computer for 100$ was sensational, but you could not expect it to have plenty of anything. But it still works and sits on a separate desk at home :)

                                          "Dark the dark side is. Very dark..." - Yoda ---
                                          "Shut up, Yoda, and just make yourself another toast." - Obi Wan Kenobi

                                          D 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