Friday, May 20, 2011

reusability

Programming before sleeping results in code dreams.

I realized last night (err... in a dream) that I'm copy/pasting a lot of code. Every function in the toolkit I'm making has a large chunk of semi-identical code for opening a file and seeking/reading through it. The only difference is that what particular piece of data is skipped over or read changes. I considered making one function for everything and having the lines for dumping the data out be keyed to a bunch of conditions you pass to the function, but that started to look obfuscated.

Is this what OOP is for? Could I have made one class for reading data and then have the subclasses be slight modifications? Is that the right word? I dunno.

Ok, so one way I could have done it was have one block of code in a function for reading out everything, but the difference between functions is the return values (or rather what data gets dumped to previously allocated storage).

I decided to have the size functions return the number of data instead of the total size in bytes. This way you have a number to use as your index when scanning through the returned data.

EDIT: I think I hashed out in the shower how to handle this with ONE function that goes through the file. The trick is passing the function a pointer to other "which data to get" functions. I haven't completely worked it out yet but it's promising. If I can write a program that does memory allocation AND function pointers in one go I'll be very happy!

No comments:

Post a Comment