How do you write fewer lines of code?
-
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
-
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
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[^]
-
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 -
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
-
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, 1997Don'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 -
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
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[^]
-
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[^]
-
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, 1997John 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
-
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 -
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."
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
-
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 -
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
-
That's a great question! Marc
-
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 -
-
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
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."
-
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
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
-
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
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] -
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 -
djj55 wrote:
Isn't this a programming question?
programming question != question about programming
:-DReiss 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. :)