Hey,
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,
Chris
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,
Chris