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 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.
  • P PIEBALDconsult

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

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

    That's a great question! Marc

    My Blog

    1 Reply Last reply
    0
    • L Lost User

      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 Offline
      D Offline
      Dalek Dave
      wrote on last edited by
      #27

      I bet word processing was a bugger! :)

      ------------------------------------ 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
      • P PIEBALDconsult

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

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

        About 3 quid I think, depending on who you buy it from. Oh sorry, it was a 'd'.

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

        1 Reply Last reply
        0
        • M Marc Clifton

          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 Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #29

          Mine has discovered the delete key, the space bar (good for dialogs, it acts as OK on most of them, whether I want it to or not) and his particular favourite: F11. Holding that down gives him hours of fun (and me a headache).

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

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          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
            Ravi Bhavnani
            wrote on last edited by
            #30

            IMHO, probably the most efficient way to realize this goal is by refactoring and using existing frameworks where possible.  Some MS technology shops exhibit the "not invented here" syndrome and prefer to shy away from non-Microsoft, non-commercial libraries, sometimes for legal reasons (putting a company's IP for sale is easier when you reduce dependencies on open source and other non-MS commercial libraries). /ravi

            My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

            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

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

              Inheritance. Then you have to look up all that huge class hierarchy to see what the concise fu#*in' code is supposed to do. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              1 Reply Last reply
              0
              • D Dalek Dave

                I bet word processing was a bugger! :)

                ------------------------------------ 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
                #32

                With 256 bytes? No chance :) But this is my first interpreter in action (using the 64 x 32 pixel graphics): Tiny BASIC[^]. Please note the elegant wrapping at the end of the line. And here[^] you have a replica of the original version of the computer, which was a project in a magazine. On the screen you see the same 256 byte test program which I also typed in into mine after building it. The graphics need exactly 256 bytes as video buffer, so the seemingly random pixels at the top are the program code.

                "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
                • R Reiss

                  djj55 wrote:

                  Isn't this a programming question?

                  programming question != question about programming :-D

                  X Offline
                  X Offline
                  Xiangyang Liu
                  wrote on last edited by
                  #33

                  Reiss wrote:

                  programming question != question about programming

                  And also

                  question about programming != Mark's Question About Programming
                  

                  --- Mark does not ask programming questions, the questions ask him. :)

                  My Younger Son & His "PET"

                  D 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
                    Nicholas Butler
                    wrote on last edited by
                    #34

                    One way is to get the computer to write the tedious, repetitive code for you. It's relatively cheap to ensure that generated code is correct and so I don't count those lines of code in the total. I'm more interested in counting the lines of code I have to type, test and debug - those are the ones that cost! As a quantitative example, I wrote a code generator ( affectionately called Jenny ) that created wrappers for CRUD operations over the Entity Framework. Jenny is about 1,000 LOC, but for about 100 database tables it generates 8,000 LOC in the DTO assembly, 20,000 in the DAL and 7,000 in the BLL. That's 35,000 lines of copy/paste/edit code I didn't have to write. Plus, if I change my mind about my implementation, I can edit all that code at once by running Jenny again. Nick

                    M 1 Reply Last reply
                    0
                    • X Xiangyang Liu

                      Reiss wrote:

                      programming question != question about programming

                      And also

                      question about programming != Mark's Question About Programming
                      

                      --- Mark does not ask programming questions, the questions ask him. :)

                      My Younger Son & His "PET"

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

                      There is no Spoon.

                      ------------------------------------ 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
                      • 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

                        M Offline
                        M Offline
                        Mark_Wallace
                        wrote on last edited by
                        #36

                        Including the title of your posting, you asked the same question three times. There's gotta be a moral in there somewhere.

                        I wanna be a eunuchs developer! Pass me a bread knife!

                        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

                          M Offline
                          M Offline
                          Mehdi Gholam
                          wrote on last edited by
                          #37

                          While you were pondering the question me and OG were down at the pub, that's one way of writing less code. I think someone mentioned getting someone else to write the code is another option... But seriously, I believe you write less code by better design and better algorithms.

                          Its the man, not the machine - Chuck Yeager If at first you don't succeed... get a better publicist If the final destination is death, then we should enjoy every second of the journey.

                          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
                            Paul M Watt
                            wrote on last edited by
                            #38

                            I have started using Test Driven Development (TDD). I find that my class interfaces are much more usable, because I am using them while developing. The code is tested as I develop. I don't waste that extra time writing for features that I think I am going to use, I only write when I need. Including the test code, I find I write less code overall.

                            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

                              S Offline
                              S Offline
                              swjam
                              wrote on last edited by
                              #39

                              ...or a Project Manager.

                              ---------------------------------------------------------- Lorem ipsum dolor sit amet.

                              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

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

                                True, but then call your app from a script thereby reducing it to just the call. :) There's no right answer for this. Generally speaking, we reduce, improve, and optimize functions since that is where "our" code usually is.

                                Web - BM - RSS - Math - LinkedIn

                                T 1 Reply Last reply
                                0
                                • N Nagy Vilmos

                                  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 Offline
                                  B Offline
                                  Bassam Abdul Baki
                                  wrote on last edited by
                                  #41

                                  Hey, if the grammar police can write up tickets in a programming website, then the IT police must have a wider jurisdiction. :)

                                  Web - BM - RSS - Math - LinkedIn

                                  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

                                    S Offline
                                    S Offline
                                    Single Step Debugger
                                    wrote on last edited by
                                    #42

                                    Easy – you need to get smarter. This highly reduces LOC.

                                    There is only one Vera Farmiga and Salma Hayek is her prophet! Advertise here – minimum three posts per day are guaranteed.

                                    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

                                      A Offline
                                      A Offline
                                      Andy Brummer
                                      wrote on last edited by
                                      #43

                                      Good design and reducing plumbing code more than anything. Nothing complicates an app and drives up the line count than fighting with an object model that doesn't match what you are trying to do with it. If you have to routinely make changes to multiple parts of your model that require complicated operations to connect, then you are going to write a bunch of extra code.

                                      Curvature of the Mind now with 3D

                                      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

                                        J Offline
                                        J Offline
                                        Judah Gabriel Himango
                                        wrote on last edited by
                                        #44

                                        I tend to collapse if/else statements. If there's just a single if/else, I'll use the (what's the name of this?) operator:

                                        return foo ? "it was foo" : "it was not!";

                                        For big if/elses, or switch statements, I use a custom extension method and type, loosely inspired by F#'s match expressions:

                                        return answer
                                        .Match(42, "It was 42!")
                                        .Match(7, "Perfecttion!")
                                        .Match(-1, () => throw new ArgumentException(...))
                                        .DefaultTo("unknown!");

                                        I find that cleaner and more concise than big if/else or switch blocks.

                                        My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

                                        M A T 3 Replies Last reply
                                        0
                                        • J Judah Gabriel Himango

                                          I tend to collapse if/else statements. If there's just a single if/else, I'll use the (what's the name of this?) operator:

                                          return foo ? "it was foo" : "it was not!";

                                          For big if/elses, or switch statements, I use a custom extension method and type, loosely inspired by F#'s match expressions:

                                          return answer
                                          .Match(42, "It was 42!")
                                          .Match(7, "Perfecttion!")
                                          .Match(-1, () => throw new ArgumentException(...))
                                          .DefaultTo("unknown!");

                                          I find that cleaner and more concise than big if/else or switch blocks.

                                          My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

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

                                          Judah Himango wrote:

                                          I use a custom extension method and type, inspired by F#'s match expressions:

                                          Ooh, that's sweet! I remember encountering this a while ago, but I'm glad you reminded me. Marc

                                          My Blog

                                          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