Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Sneaky Internal State

Name: Anonymous 2012-03-28 23:59

I've boiled what I want out of a programming language down to a single example.  Problems like this bite me in the ass often and I haven't used a language, yet, that helps.

Suppose I'm writing a library for working with vectors.  For simplicity, it's not fancy n-dimensional vectors, etc., let's say just three floats.  So I write some code to perform operations on vectors, like add, subtract, dot product, cross product, rotate, scale... and then I come to "normalize."

Here, I come up with this optimization that vectors could also store a flag indicating whether or not they are normal.  If that flag is set, then normalize has no work to do.  The catch is that I have to decide which operations clear the flag, which operations set it, and which operations leave it unchanged.  So I do the work and end up with a nice, efficient vector library.

I use my library for a long time -- long enough that I stop caring how that optimization worked.  Eventually, months later, I decide to add some new functions to my vector library.  Of course, I've forgotten all about the "normal" flag, and I forget to set/clear it in the new functions.  Of course, there's no compile error or even a run-time error.  I just observe strange behavior when the application runs and I have to spend hours stepping through floating-point math to figure out what's wrong.

How does your language of choice improve this situation?

Name: Anonymous 2012-03-29 3:56

>>22
I would either explicitly normalize the vector, and then the compiler would know that it was normalized.
Dude, I gave you the benefit of the doubt here for like five posts, but seriously, you are apparently not understanding what a compiler does.

It's like this:
1) I compile my program.  THIS IS COMPILE-TIME
2) I run my 3D modeling program and generate a file full of data.
3) I run my program.  THIS IS THE BEGINNING OF RUN-TIME
4) My program loads the file.
5) A bunch of functions from my vector library are called to manipulate the data.

Everything that the compiler does happens in step 1.  I could delete the compiler from my hard drive, entirely, right after step 1 and my program would still run.  The compiler cannot make any decisions during step 5.

And not every vector needs to be normalized...  You seem to also be suggesting that the answer is just to normalize them all.  It's just that if you call "normalize" on a vector that happens to be (0, 1, 0), you want the function to just exit without doing anything.  If you call "add" with (5, 2, 7) and (10, 10, 10), you want to get a result of (15, 12, 17).  No normalization happens there on the inputs or the outputs.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List