Comment On Reusable Code

Code reuse is one of the key steps to maintainability. There are many ways a developer might make their code reusable. For example, Steve’s co-worker wrote this block, which generates 1000 log entries: [expand full text]
« PrevPage 1 | Page 2Next »

Re: Reusable Code

2013-01-30 08:03 • by snoofle
Premature optimizations, premature unrolling of loops; I wonder what else at which he's premature?

Re: Reusable Code

2013-01-30 08:04 • by Cujo (unregistered)
That's beautiful. As soon as I start getting paid by the line, I plan on going to this model.

Re: Reusable Code

2013-01-30 08:09 • by no laughing matter
Well, everyone knows these crazy for-loops have different indexing in every other programming language, leading to the treaded off-by-one errors.

This code is inherently safe against off-by-one errors.

And off course it gives programmers the correct wage if they are payed by the line.

Re: Reusable Code

2013-01-30 08:13 • by John (unregistered)
Surely 995 + 6 = 1001?

Hmm - the spam filter says this is spam - not sure way.

Add the magic word - suscipit!

Re: Reusable Code

2013-01-30 08:13 • by Steve The Cynic
400243 in reply to 400241
no laughing matter:
leading to the treaded off-by-one errors.

I have a sneaking suspicion you didn't mean to talk as if they are tyres... The word you more likely wanted is "dreaded".

Re: Reusable Code

2013-01-30 08:14 • by Steve The Cynic
400244 in reply to 400242
no laughing matter:
This code is inherently safe against off-by-one errors.


John:
Surely 995 + 6 = 1001?


Off by one...

Oops.

Re: Reusable Code

2013-01-30 08:16 • by Rick O'Shay (unregistered)
And it's perfectly scalable!

Re: Reusable Code

2013-01-30 08:21 • by RonBeck62
You're missing the point. It's optimized for parallel processors.

Re: Reusable Code

2013-01-30 08:46 • by edgsousa
400248 in reply to 400246
RonBeck62:
You're missing the point. It's optimized for parallel processors.


No it's not: next introduces a data dependency.

Re: Reusable Code

2013-01-30 08:51 • by Shoreline
Maybe this code was written by another program.

Re: Reusable Code

2013-01-30 08:53 • by Gaza Rullz (unregistered)
while(1){
facePalm();
}

Re: Reusable Code

2013-01-30 09:00 • by Major Douchebag (unregistered)
Fixed.


var allDataSimulated = new List<FeedSearchTransactionLogResult>
{
new FeedSearchTransactionLogResult(next++),
new FeedSearchTransactionLogResult(next++),
new FeedSearchTransactionLogResult(next++),
new FeedSearchTransactionLogResult(next++),
new FeedSearchTransactionLogResult(next++),
...
new FeedSearchTransactionLogResult(next++)
};

Re: Reusable Code

2013-01-30 09:01 • by Steve The Cynic
400252 in reply to 400250
Gaza Rullz:
while(1){
facePalm();
}

You haven't unrolled the loop... Get with the program, dude!

Re: Reusable Code

2013-01-30 09:03 • by Jay Blanc (unregistered)
It's been optimised to do away with that extra comparison against a constant on the for loop!

If only there were a way that compilers could do this automatically.

Re: Reusable Code

2013-01-30 09:04 • by Java User (unregistered)
Is that a Java code ?
If so, there is 1 syntax error (eh, wait... 1001 errors), as the 'add' method in the java.util.List interface does not start with an uppercase 'A'. It won't compile like that.
C'mon 'magic' developer, take your keyboard and correct all of these lines (without search and replace, it'll be the punishment).

Re: Reusable Code

2013-01-30 09:04 • by F (unregistered)
400255 in reply to 400248
edgsousa:
RonBeck62:
You're missing the point. It's optimized for parallel processors.


No it's not: next introduces a data dependency.


Thereby ensuring that all but one of your parallel processors are free to do other work. Brillant!

Re: Reusable Code

2013-01-30 09:09 • by flop
400256 in reply to 400243
Steve The Cynic:
no laughing matter:
leading to the treaded off-by-one errors.

I have a sneaking suspicion you didn't mean to talk as if they are tyres... The word you more likely wanted is "dreaded".

Or even threaded.

If the access is atomic there's no data dependency.

Re: Reusable Code

2013-01-30 09:11 • by codegeneratorcode (unregistered)
400257 in reply to 400249
Shoreline:
Maybe this code was written by another program.




// removed StringBuilder to make the code easier to read

if(dataSimulatedCount == 1){
code += "allDataSimulated.Add(new FeedSearchTransactionLogResult(next++));";
}
else if(dataSimulatedCount == 2){
code += "allDataSimulated.Add(new FeedSearchTransactionLogResult(next++));";
code += "allDataSimulated.Add(new FeedSearchTransactionLogResult(next++));";
}
...
else [
return Bool.FileNotFound;
}

Re: Reusable Code

2013-01-30 09:12 • by Tim (unregistered)
400258 in reply to 400254
Java User:
Is that a Java code ?
If so, there is 1 syntax error (eh, wait... 1001 errors), as the 'add' method in the java.util.List interface does not start with an uppercase 'A'. It won't compile like that.
C'mon 'magic' developer, take your keyboard and correct all of these lines (without search and replace, it'll be the punishment).

probably c#. it's basically the same as java but everything starts with an uppercase :-)

Re: Reusable Code

2013-01-30 09:28 • by dkf
400259 in reply to 400256
flop:
If the access is atomic there's no data dependency.
No, but thank you for playing. (The value expected by one call is not available until after the other call is done, which is pretty much the definition of a data dependency.)

Re: Reusable Code

2013-01-30 09:38 • by DiskJunky (unregistered)
400260 in reply to 400241
*dreaded. I don't think the code goes walkabout ;-)

Re: Reusable Code

2013-01-30 09:40 • by emaNrouY-Here (unregistered)
I love the html comments in this one. It reminds me of the times when my programming teachers would tell me I don't have enough comments in my code. This was usually after I had turned in code that had comments that described what was going on or the code itself was self documenting.

// returns 0 if successful

int main() {
// sets the variable temperature and initiates to 72
int temperature = 72;
// print hello, world to the console
printf("hello, world");
// print the temperature to the console
printf(temperature,%d);
// return out of main
return 0;
}

A lot of useless information for every single line of code.

Re: Reusable Code

2013-01-30 09:48 • by iMortalitySX (unregistered)
As "modern" programmers "we" no longer have the "need" for such archaic concepts as "reusable" code or "loops" or "double quotes". That is was AOP was invented for right???

(OBTW I'm just joking, AOP may have it's place, but is abused beyond belief)

This comment brought to you by the work "odio" which is latin for "I hate".

"Odio AOP"

Re: Reusable Code

2013-01-30 09:50 • by Xarthaneon the Unclear (unregistered)
Remy, you have finally added enough comments to a CodeSOD.

CAPTCHA: opto - I'll have to see one of these after reading those additional comments!

Needs moar comments

2013-01-30 09:51 • by DaveK
Remy:
Hey, folks complained that I didn&apos;t put enough comments in my CodeSODs. Hopefully I&apos;ve addressed your complaints.
That's still only one comment.

Re: Reusable Code

2013-01-30 10:01 • by Chris P. Peterson (unregistered)
I'm sure he re-uses the 'next' variable with this masterpiece:

//reset next back to zero
next--;
next--;
next--;
//snip

next--; //next is now zero!

Re: Reusable Code

2013-01-30 10:05 • by F (unregistered)
400266 in reply to 400261
emaNrouY-Here:
I love the html comments in this one. It reminds me of the times when my programming teachers would tell me I don't have enough comments in my code. This was usually after I had turned in code that had comments that described what was going on or the code itself was self documenting.

// returns 0 if successful

int main() {
// sets the variable temperature and initiates to 72
int temperature = 72;
// print hello, world to the console
printf("hello, world");
// print the temperature to the console
printf(temperature,%d);
// return out of main
return 0;
}

A lot of useless information for every single line of code.


ISTR that this is what is expected if you are taught to program using the pseudocode paradigm. You start by writing the comments, which state what the program is to do. You then insert the actual code, leaving the comments in place.

I'm not sure whether it's an effective way to teach programming to those who are temperamentally unsuited to be programmers. I'm pretty sure it's a crap way to teach it to those who are suited. I'm also fairly sure it's a successful cop-out by poor teachers.

Re: Reusable Code

2013-01-30 10:06 • by Steve The Cynic
400267 in reply to 400259
dkf:
flop:
If the access is atomic there's no data dependency.
No, but thank you for playing. (The value expected by one call is not available until after the other call is done, which is pretty much the definition of a data dependency.)

Curiously the variable next++ doesn't create a data dependency between one execution of .Add() and the next. The data dependency caused by next++ is between one *call* and the next. Granted, the .Add() method of a container is unlikely to spawn a thread just to do the addition, but it *could*.

It would be trivial (ha!) to replace the 1000 (1001?) next++'s with the numbers 0 to 999 (1000?), thus eliminating next++ as a source of data dependency.

The data dependency between successive executions of .Add() is the container itself, and the actual addition is likely to be mutexed / synchronised, and therefore not parallelisable.

Re: Reusable Code

2013-01-30 10:09 • by Steve The Cynic
400269 in reply to 400248
edgsousa:
RonBeck62:
You're missing the point. It's optimized for parallel processors.


No it's not: next introduces a data dependency.

A second data dependency. The container itself is the major dependency, and one that cannot be removed by converting the next++s to consecutive constants.

Re: Reusable Code

2013-01-30 10:13 • by Valued Service (unregistered)
400270 in reply to 400266
F:
I'm not sure whether it's an effective way to teach programming to those who are temperamentally unsuited to be programmers.


TRWTF is that temperamental is pronounced tempermental, thus destroying any chance of winning a spelling bee.

Re: Reusable Code

2013-01-30 10:13 • by conquer (unregistered)
<quote>Hey, folks complained that I didn't put enough comments in my CodeSODs. Hopefully I've addressed your complaints.</quote>

Well you certainly highlighted a WTF on the daily wtf website. The wordcount for an article includes comments in it's html source.

Now I'm wondering if it's actually counting words or something even more fucked up! :)

Re: Reusable Code

2013-01-30 10:17 • by Valued Service (unregistered)
400272 in reply to 400267
Steve The Cynic:


The data dependency between successive executions of .Add() is the container itself, and the actual addition is likely to be mutexed / synchronised, and therefore not parallelisable.


Maybe that's why he has the variable
next
. It's possible that they mean to thread this, and they want a way to know the correct order in case the threads operate out of order.

Re: Reusable Code

2013-01-30 10:20 • by Valued Service (unregistered)
400273 in reply to 400269
Steve The Cynic:
edgsousa:
RonBeck62:
You're missing the point. It's optimized for parallel processors.


No it's not: next introduces a data dependency.

A second data dependency. The container itself is the major dependency, and one that cannot be removed by converting the next++s to consecutive constants.


You might still see benefit if you lock the add, if the new is performed outside the lock and the construction of the FeedSearchTransactionLogResult object is able to be threaded and is very complex.

Re: Reusable Code

2013-01-30 10:20 • by conquered (unregistered)
400274 in reply to 400271
conquer:
<quote>Hey, folks complained that I didn't put enough comments in my CodeSODs. Hopefully I've addressed your complaints.</quote>

Ah I see I was still in slashdot commenting mode :(

Re: Reusable Code

2013-01-30 10:35 • by Kalman (unregistered)
What about:
...
{
int next = 0;

List<FeedSearchTransactionLogResult> allDataSimulated = new List<FeedSearchTransactionLogResult>();

Generate1000LogEntries(&allDataSimulated, &next);
}

void Generate1000LogEntries(List<FeedSearchTransactionLogResult> *allDataSimulated, int *next) {
Generate500LogEntries(allDataSimulated, next);
Generate500LogEntries(allDataSimulated, next);
}

void Generate500LogEntries(List<FeedSearchTransactionLogResult> *allDataSimulated, int *next) {
Generate100LogEntries(allDataSimulated, next);
Generate100LogEntries(allDataSimulated, next);
Generate100LogEntries(allDataSimulated, next);
Generate100LogEntries(allDataSimulated, next);
Generate100LogEntries(allDataSimulated, next);
}

void Generate100LogEntries(List<FeedSearchTransactionLogResult> *allDataSimulated, int *next) {
Generate10LogEntries(allDataSimulated, next);
Generate10LogEntries(allDataSimulated, next);
Generate10LogEntries(allDataSimulated, next);
Generate10LogEntries(allDataSimulated, next);
Generate10LogEntries(allDataSimulated, next);
Generate10LogEntries(allDataSimulated, next);
Generate10LogEntries(allDataSimulated, next);
Generate10LogEntries(allDataSimulated, next);
Generate10LogEntries(allDataSimulated, next);
Generate10LogEntries(allDataSimulated, next);
}

void Generate10LogEntries(List<FeedSearchTransactionLogResult> *allDataSimulated, int *next) {
GenerateLogEntry(allDataSimulated, next);
GenerateLogEntry(allDataSimulated, next);
GenerateLogEntry(allDataSimulated, next);
GenerateLogEntry(allDataSimulated, next);
GenerateLogEntry(allDataSimulated, next);
GenerateLogEntry(allDataSimulated, next);
GenerateLogEntry(allDataSimulated, next);
GenerateLogEntry(allDataSimulated, next);
GenerateLogEntry(allDataSimulated, next);
GenerateLogEntry(allDataSimulated, next);
}

void GenerateLogEntry(List<FeedSearchTransactionLogResult> *allDataSimulated, int *next)
{
allDataSimulated->Add(new FeedSearchTransactionLogResult(*next++));
}

Re: Reusable Code

2013-01-30 10:39 • by Steve The Cynic
400276 in reply to 400270
Valued Service:
F:
I'm not sure whether it's an effective way to teach programming to those who are temperamentally unsuited to be programmers.


TRWTF is that temperamental is pronounced tempermental, thus destroying any chance of winning a spelling bee.

Hmm. This may be a to-may-toe / to-mah-toe thing, but I pronounce temperamental as if it is spelled tempramental.

Re: Reusable Code

2013-01-30 10:54 • by chubertdev
TDWTF Drinking Game:

Go home, read this article's comments, and every time someone says "data dependency", you take a sip of beer.

Re: Reusable Code

2013-01-30 10:54 • by Steve The Cynic
400278 in reply to 400270
Valued Service:
F:
I'm not sure whether it's an effective way to teach programming to those who are temperamentally unsuited to be programmers.


TRWTF is that temperamental is pronounced tempermental, thus destroying any chance of winning a spelling bee.

No, TRWTF is that anybody feels it is worth having competitions like spelling bees. In a language where spelling and pronunciation are more closely linked than in English, you can know how to spell unknown words just by hearing them. French has half the problem that English has, in that while spellings usually have unique pronunciations ('ch' is troublesome), pronunciations often have ambiguous spellings, especially for a native English speaker like me who has a hard time distinguishing the subtle variations of 'u' and 'ou' - a final "eh" sound can be spelled at least ten different ways. I find it very hard to read "phonetically equivalent French" (where you use a series of letters that would be pronounced the same as some word or other) because I read French using the same "look-say" technology that I use in English.

Ive traned itt tue bee abel too reckogneyes alturnativv spelllings inn Inglisch, butt knot yett inn Frenntch, espeshulliwenuruntheewurdztuggeththuh.

Bah.

Re: Reusable Code

2013-01-30 11:00 • by laggg (unregistered)
I guess he get paid by the number of lines of code

Re: Reusable Code

2013-01-30 11:16 • by no laughing matter
400280 in reply to 400246
RonBeck62:
You're missing the point. It's optimized for parallel processors.

Yes, as Steve the Cynic correctly pointed out, i was writing about threaded off-by-one-errors.

Re: Reusable Code

2013-01-30 11:21 • by Mike (unregistered)
lol

I wondered why the front page said "1753 Words" for such a small article.

View source explained.

Re: Reusable Code

2013-01-30 11:29 • by conquered (unregistered)
400282 in reply to 400278
Steve The Cynic:
No, TRWTF is that anybody feels it is worth having competitions like spelling bees.

Aye C watt U mien.

Re: Reusable Code

2013-01-30 11:50 • by Jack (unregistered)
Very funny Remy, but we're still missing over half (several hundred lines) of the snipped code. Plus you lied, either about how many lines you snipped, or the total.

With such poor attention to detail, how can we be sure there wasn't something important in one of those snipped lines?

Re: Reusable Code

2013-01-30 12:21 • by Your Name (unregistered)
When you measure productivity in SLOC/hour, and quality in "defects per KSLOC", you're gonna get a lot of SLOC.

Re: Reusable Code

2013-01-30 13:07 • by jbabs (unregistered)
You only skipped 318 lines, not 995. I am insulted.

Re: Reusable Code

2013-01-30 13:12 • by HomeBrew (unregistered)
400288 in reply to 400286
Your Name:
When you measure productivity in SLOC/hour, and quality in "defects per KSLOC", you're gonna get a lot of SLOC.

When you measure productivity in SLOC/hour, you're guaranteed to get a FLOC.

Re: Reusable Code

2013-01-30 13:15 • by Yaos
I'll just use my patented YOSPOS language that's a random mashup of other languages that are inferior to mine in every way. I don't like what the original program is doing so whatever it was trying to accomplish is wrong. The correct thing to do is to write to the console and use AutoHotKey to copy and paste into Notepad.

write1000lines(float lines){
float x=0
while(x<=lines){
switch(x){
case 0:
writeline(x);
case 1:
writeline(x);
..........
case 532:
magicnumber(x);
..........
case 1500:
writeline(x);

At some point I'll add in some error checking to make sure the number of lines is equal to the function name by storing the variables in unused fields on the included $1000 PDF printer that was last updated in 1997 and only works on Windows XP x86 with less than 2 GB of RAM.

Re: Reusable Code

2013-01-30 13:20 • by Aargle Zymurgy (unregistered)
400291 in reply to 400261
emaNrouY-Here:
I love the html comments in this one. It reminds me of the times when my programming teachers would tell me I don't have enough comments in my code. This was usually after I had turned in code that had comments that described what was going on or the code itself was self documenting.

[code]// returns 0 if successful
int main() {
// sets the variable temperature and initiates to 72
int temperature = 72;


At least you DID learn the difference. My syllabus for my students bolded and italized "useful" in the requirement for programs that read "Must include USEFUL documentation."

Useless documentation is worse than no documentation in the sense that it's a distraction. On my computer's hard-drive is an archive of some "professional" code that has a long poem inserted in a function header. SRSLY... a poem.

Re: Reusable Code

2013-01-30 13:40 • by ¯\(°_o)/¯ I DUNNO LOL (unregistered)
When I wake up, (when I wake up),
Well I know I'm gonna be
I'm gonna be the man who wakes up at ten A.M., (Yeah I know)
When I go out, (when I go out, )
Well I know I'm gonna be
I'm gonna be the man who gets to work at one P.M., (who?)
If I get drunk, (If I get drunk, )
well I know I'm gonna be
I'm gonna be the man who gets drunk down at the pub, (yeah) Yes!
And when I get fired, (when I get fired, )
well I know I'm gonna be I'm gonna be the man who leaves this crappy code with you

But I would log 500 lines
And I would log 500 more
Just to be the man who logged a thousand lines
To fall down at the door

Re: Reusable Code

2013-01-30 14:07 • by eVil (unregistered)
400294 in reply to 400293
Baaah'da dah daah!!
« PrevPage 1 | Page 2Next »

Add Comment