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
Isn't this a programming question?
-
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 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."
-
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
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 -
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
-
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:
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.
-
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
-
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.
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 -
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
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 withif
's andswitch
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
-
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 withif
's andswitch
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
-
Isn't this a programming question?
-
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, 1997John Simmons / outlaw programmer wrote:
increasing stack usage
Not if you make it a macro. :-O
-
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
-
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
-
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 -
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[^]
-
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[^]