469,295 Members | 1,991 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,295 developers. It's quick & easy.

is there an existing pattern/algorithm to solve my oft occurring softw. requirement?


I've been developing for about 4 years now, and in that time I must have written the same particular piece of code umpteen times to solve the same requirement that popped up in many different projects. I'm not looking for a debate on code reuse, but yeah, I should probably have done some of that! I think it's a requirement that many of you will also have knocked up as bread and butter coding on any normal dev day. Put simply, that requirement asks that the dev should take a collection of data (let's say it's a list of sales of comic books) and produce a table (can be HTML, doesn't matter) that contains those transactions with a subtotal summing all the transactions of each individual comic book, and a grand total at the very end. Wow, easy huh!?

Like This :

|| Book || Sale Date || Salesman || Cost($) ||
| Batman | 1-Jan | Jon | $5 |
| Batman | 8-Jan | Tom | $4 |
| *SubTotal *| N/A | N/A | $9 |
| Spiderman | 1-Jan | Jon | $6.5 |
| Spiderman | 8-Jan | Tom | $7 |
| *Sub Total *| N/A | N/A | $13.5 |
| *Grand Total*| N/A | N/A | $22.5 |

This code doesn't take long to write and anyone worth their salt should be able to knock it up pretty quickly by :
* grouping/sorting the data in the collection so all comic sales of a particular comic book are together
* iterating through the collection, keeping track of the running sub total and grand total, and simply recognising when to display the subtotal.

That's all people - no biggie right, but it bother me I end up spending 60 mins writing and debugging this code every few months. Surely this is something nearly everyone has had to build at some point?

On a sleepy day this is ever-so-slightly tricky code to write because it requires a surprisingly decent short term memory and there are a few places where you can trip up.

Accordingly, is there a pattern that exists so I dont ever need to write this code again? Is there a name for this particular algorithm? I'll try and make my own if there isn't.

Let me know what you think, any help is appreciated.
Cheers in advance,
Jul 19 '10 #1
0 997

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Andy Read | last post: by
16 posts views Thread by cody | last post: by
7 posts views Thread by Sb | last post: by
3 posts views Thread by Brad Quinn | last post: by
4 posts views Thread by Ben Fidge | last post: by
6 posts views Thread by Daniel Santa Cruz | last post: by
1 post views Thread by =?Utf-8?B?RXJpYw==?= | last post: by
4 posts views Thread by dustin | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.