Make it "cut and paste" friendly, and as small as possible.
That's a really bad idea. Cut and paste causes code cloning, which is among the most difficult maintenance problems.
Code should be designed, when possible, in small chunks (methods, functions, etc.). This keeps the need to think about refactoring to a minimum, since the code is already factored. Well factored code has many other benefits, including easier-to-write unit tests and better understandability.
I maintain software that was originally written by someone as a prototype and eventually given pro
Make it "cut and paste" friendly, and as small as possible.
Cut and paste causes code cloning, which is among the most difficult maintenance problems. Code should be designed, when possible, in small chunks (methods, functions, etc.).
Wait.. are you trying to say that copying the same lines of code over and over again must be avoided? So tell me genius, how else would you implement such a function without copying?
int multiply(int a, int b) { int x=0;
if (a==1) { x+=b; }
if (a==2) { x+=b; x+=b; }
if (a==3) { x+=b; x+=b; x+=b; }
// Damn lameness filter, wouldn't let me paste my code in the entirety of its 132,356 lines
I'm sorry, but that code goes against our coding standard by having non-const parameters and a goto. I suggest the following before submitting your changes:
int multiply(int const a, int const b) {
assert(a >= 0 );
int x = 0, aNc = a;
while (aNc--) x += b;
return x; }
I consider goto (like some before me) obsolete and its use obnoxious.
This function screams for a simple for-loop. I know it is possible to use fewer local variables and a little less arithmetic. Probably there is also a more elegant recursive solution, however tending to be less readable and more error-prone. Coding-quality should prevail above beauty and elegance?
SOLUTION EXAMPLE
int multiply( int a, int b)
{
int x=0;// define local variable
int y=1;// define local variable
if (!( a >
(Ayee, my regrets: my former reply was added as HTML where it should have been plain-old text.)
I consider goto (like some before me) obsolete and its use obnoxious.
This function screams for a simple for-loop. I know it is possible to use fewer local variables and a little less arithmetic. Probably there is also a more elegant recursive solution, however tending to be less readable and more error-prone. Coding-quality should prevail above beauty and elegance?
Edit: And see the stack overflow error when run with a big number.
Maybe a coding standard should be don't use recursion when not needed; and don't try to be clever.
Actually it (thanks to 2s complement arithmetic) gives the right answer. "A" decrements down to MIN_INT then wraps round to MAX_INT then decrements all the way down to 0. It effectively calculates the unsigned equivalent of "a" (being 2**32 -a) times "b". The result of that is b*2**32 - b*a. The first part doesn't fit in your int, so you're left with -b*a.
Of course, if you have 64-bit ints you may have wait a few decades.
When you copy code you also copy whatever bugs exist in that code. If something needs to be reused several times then it should be made into a function.
When you copy code you also copy whatever bugs exist in that code. If something needs to be reused several times then it should be made into a function.
Crap, you're right! Fortunately there's an easy way to fix this :
I hope you're joking as well. Each of the 2^32 possible values for 'a' gets de facto 4 lines. And for each the number of extra lines is 'a', which means the total number for the body of the function will be closer to f(M) = 4*M + M!
If M is 2^32 then that would be... mmmh hold on.. calc.exe is having a tough time with (2^32)!
However you might want to just avoid calculating the result of a*b is it's >= 2^31 anyways, so the real number of lines in that case is more complicated.. And that's assuming the inte
I think the fact that I only make friends with people who have senses of humor just blinds me to the fact that there are people out there who don't have them.
OK, seriously, what the hell is wrong with you guys. If the content of my comment didn't make it clear enough (or too subtle, if it was even subtle) that it was a sarcasm, and that the +5 Funny moderation wasn't a big enough clue, you could have at least seen the half dozen other replies that were only slight variations of the very code you just posted, or even all the other replies that made it clear enough it was originally a joke.
Or were you too eager to demonstrate your 1337 coding skills you straight j
Wow... where to start.. do you realise that if you really were to implement that function using my approach the resulting fucking would weight exabytes, right? lol so, improving the efficiency isn't required here. hehe. It was all a joke you know;-)
Keep it simple! (Score:0, Insightful)
Re: (Score:5, Insightful)
Make it "cut and paste" friendly, and as small as possible.
That's a really bad idea. Cut and paste causes code cloning, which is among the most difficult maintenance problems.
Code should be designed, when possible, in small chunks (methods, functions, etc.). This keeps the need to think about refactoring to a minimum, since the code is already factored. Well factored code has many other benefits, including easier-to-write unit tests and better understandability.
I maintain software that was originally written by someone as a prototype and eventually given pro
Re:Keep it simple! (Score:5, Funny)
Make it "cut and paste" friendly, and as small as possible.
Cut and paste causes code cloning, which is among the most difficult maintenance problems. Code should be designed, when possible, in small chunks (methods, functions, etc.).
Wait.. are you trying to say that copying the same lines of code over and over again must be avoided? So tell me genius, how else would you implement such a function without copying?
Re:Keep it simple! (Score:5, Funny)
Duh, you so need to learn about this little thing called structured programming, which can totally help cut down on code duplication like that crap.
Here's a hint:
See? Much easier to understand than your spaghetti code, and much more maintainable too.
Re: (Score:3, Funny)
I'm sorry, but that code goes against our coding standard by having non-const parameters and a goto. I suggest the following before submitting your changes:
Re: (Score:2, Funny)
That way you won't create an extra copy of the variable when returning the value, it saves like an entire registry cell!!!1!
Re: (Score:2)
And for bonus points, attempting to multiply by 0 probably yields an overflow. :-)
Re: (Score:2)
Not only that, the overflow check is missing from all of the above examples. Here's the proper way:
Multiply contents of $c000, $c001 and store the result in $c002, $c003, little endian.
LDX $c000
DEX
BPL nozero
LDA #$00
STA $c002
STA $c003
nozero:
LDA #$00
ADC $c001
STA $c002
BCC nocarry
LDA #$01
ADC $c003
STA $c003
nocarry:
DEX
BPL nozero
Ofc. this should be optimized by using bit-shifts, and is probably buggy since I haven't used 6502 assembly for at least 2 decades.
Re: (Score:2)
I see at least 2 bugs right after posting, go find them.
Re: (Score:1, Funny)
My eyes! They burn!
Re: (Score:1)
Easier?!?
It should be "(a-- > 0)"; no space between the "a" and the decrement operator.
That's the sort of nasty bug you get with so called "structured programming". With the grandparents post everyone understands exactly what it's doing.
Re: (Score:2)
Ahh thanks.
I was going to ask what language the code fragment was written in because I've never seen a --> operator.
Re:Keep it simple! (Score:4, Funny)
How about just x = a * b;
How about just WHOOOOOOSH!!
Re: (Score:1, Funny)
That's the effect of global warming: frosted structured programmers resurface :-)
Re: (Score:2)
Re: BEHOLD! (Score:1, Funny)
Re: (Score:1)
Re: (Score:1)
(Ayee, my regrets: my former reply was added as HTML where it should have been plain-old text.)
I consider goto (like some before me) obsolete and its use obnoxious.
This function screams for a simple for-loop. I know it is possible to use fewer local variables and a little less arithmetic. Probably there is also a more elegant recursive solution, however tending to be less readable and more error-prone. Coding-quality should prevail above beauty and elegance?
SOLUTION EXAMPLE
int multiply( int a, int b)
{
Re: (Score:1)
Re: (Score:1)
You could use a for loop?
Re: (Score:3, Funny)
Re: (Score:3, Funny)
You'd better hope so. For loops and sparse data usually make a pretty inefficient combination...
Re: (Score:3, Interesting)
Re:Keep it simple! (Score:5, Funny)
Try multiplying it by -1 and see if your stack is large enough.
Re: (Score:1)
int multiply(int a, int b) {
....if (b < 0) {
........return multiply(a, b+1) - a;
....} else if(b > 0) {
........return a + multiply(a, b-1);
....} else {
........return 0;
....}
}
Which will may be made more efficient after this. Gotta love it
Sorry about the code layout
Re: (Score:1)
Re: (Score:2)
Re:Keep it simple! (Score:4, Informative)
Even in languages that recurse properly that'll overflow on big numbers. To not overflow in properly recursing languages, you need:
Re: (Score:2)
That's just using iterative recursion which is just as easily represented as a loop in a language like C. :)
Re: (Score:1)
Re: (Score:1)
Re: (Score:1)
return a+1;
}
int subone(int a) {
return a-1;
}
int add(int a, int b) {
if (b == 0) return a;
else if (b > 0) return(add(addone(a), subone(b)));
else return(add(subone(a), addone(b)));
}
int mult(int a, int b) {
if (b == 0) return(0);
else if (b > 0) return(add(a, mult(a, subone(b))));
else return(add(-a, mult(a, addone(b))));
}
Re: (Score:1)
Re:Keep it simple! (Score:4, Funny)
When you copy code you also copy whatever bugs exist in that code. If something needs to be reused several times then it should be made into a function.
Crap, you're right! Fortunately there's an easy way to fix this :
:%s/x+=b/x = addition(x, b)/
Re: (Score:2)
That does not address the duplication of "=".
Re: (Score:2)
I hope you're joking as well. Each of the 2^32 possible values for 'a' gets de facto 4 lines. And for each the number of extra lines is 'a', which means the total number for the body of the function will be closer to f(M) = 4*M + M!
If M is 2^32 then that would be... mmmh hold on.. calc.exe is having a tough time with (2^32)!
However you might want to just avoid calculating the result of a*b is it's >= 2^31 anyways, so the real number of lines in that case is more complicated.. And that's assuming the inte
Re: (Score:2)
I think the fact that I only make friends with people who have senses of humor just blinds me to the fact that there are people out there who don't have them.
It's strange. Er, "dipshit."
Re: (Score:3, Funny)
Only bad coders/dumb asses write 20 lines of code for basic arithmetic.
Only bad Slashdotters (if there can be any such thing)/dumb asses miss entirely such an obvious joke.
I gotta say, nice code snippet though. I would have never thought of that one!
Typing != copying (Score:1)
Remember it's not copying if you type it over and over again. That's the way I avoid my code copy issues, works like a charm!
Re: (Score:3, Funny)
Am I missing something, is the International Sarcasm Missing Day today??
Re: (Score:2)
Or were you too eager to demonstrate your 1337 coding skills you straight j
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)
I think managers should all read the above posts and think very very hard about their jobs.
This is real.
Re: (Score:1)
Re: (Score:2)
Re: (Score:1)
Re: (Score:1)
Now, for 10 bonus points, write that in Commodore 64 BASIC.
Re: (Score:1)
int multiply(int a, int b)
{
int x = 0;
switch (a) {
case 2: x += b;
case 1: x += b;
case 0: x += b;
}
}
Still copy and pasting, but the code is simpler, easier to c+p, and more efficient. Isn't it just beautiful? :)
Re: (Score:2)
int multiply( int a, int b )
{
int x=0;
x += b * (a== 1) + (b
Re: (Score:2)
Re: (Score:2)
Re: (Score:1)