meganursula: (cthullu)
[personal profile] meganursula
Question for object oriented gurus:

I am currently reviewing some code implementing a current standard of an algorithm i frequently use. (I want to examine some modifications to the algorithm, but, i need a good baseline to compare to.) In it we see something like:

struct velocity
{
int size;
double v[D_max]
};


There are a lot of these structs - position, quantum, etc.

Thing is, in my code, i generally declare
num_dim = n; // this is what they are using size for up above
double position[num_dim];
double velocity[num_dim];


(quantum, for the record, appears to be taking the place of what i usually declare as a constant Eps, and is used to get around numerical issues when looking for zero.)

etc. I do not have additional structs. Thing is, i find all this structifying to be sort of pointless and irritating. Pointless because i do not know what the structs are adding to the code. Irritating because i think they add a level of obfuscation, rendering the code not only longer, but also much less readable.

My question - what, if anything, am i missing in this situation? I get, generally, what object oriented-ness does for you. But i haven't used it very much in the past 6 or so years. (Matlab's excuse for object oriented isn't worth bothering with.) Right now i find myself faced with a few examples of modern code that are object oriented up the ass, and it just seems like it all has been taken too far. If i give myself three months will i become a believer? Will i stop feeling like there should be some sort of natural progression through code and adapt to having objects interacting at will?

Date: 2008-09-12 05:00 am (UTC)
From: [identity profile] bhudson.livejournal.com
Say you declare a native array in C like you did, and you screw up and you write to the n+1st element of it. You just lost, badly -- now maybe you clobbered the loop variable and you'll sometimes not stop even though other times seem to work fine. To avoid that, you need to define a vector type, a bunch of functions on that type, and then you are stuck using the type and its functions That takes a lot of verbiage.

Also, I don't think you mean higher-order function the way I usually mean it, but I can't quite figure out what you mean. Solving Ax=b is written "A \ b" (as in, "divide" A by b, but with a backslash because it's not really division). Maybe matrix multiply would have been a better example, but I forget what the matlab operator is for that.

Date: 2008-09-12 05:40 am (UTC)
From: [identity profile] mh75.livejournal.com
yes, the term 'higher order function' may have been misleading here.

So, the C code doesn't actually do anything to ensure that you don't overwrite an array. So they're not writing extra code to make a vector type. Thats not the reason.

And my point was that, yes, there is a lot of funcionality built into matlab thats not built into C, but, this code doesn't use any of it. The thing that it could be doing is replacing loops with matrix operations, but my matlab code doesn't do that. The python code does, and its still longer.

Date: 2008-09-12 02:56 pm (UTC)
From: [identity profile] bhudson.livejournal.com
So, port your matlab to C, how much code is that?

Date: 2008-09-12 04:43 pm (UTC)
From: [identity profile] mh75.livejournal.com
My code - *maybe* two pages.

Date: 2008-09-12 05:18 pm (UTC)
From: [identity profile] bhudson.livejournal.com
You write really verbose matlab if you could port it to C in just 6 times as many lines. One project I was on, we told the team in charge of the ROM how many SLOC we had, they multiplied by 18 bytes/SLOC and told us that's how much ROM we could use. We begged for 10x as much because we were using C++, not C as they had assumed, and thus we could express a lot more machine instructions in one line of code. Then, I got tasked with getting the code down to just 180 bytes per line -- eventually we got there.

One thing I should probably have thought of, but wasn't thinking it since I don't usually write this kind of code: if you're basically trolling through dense matrices and vectors, then, yeah, making lots of structures is pretty silly (except for error handling, but your competition isn't doing that, which makes me weep). It matters a lot more when you have complex data structures all linked together by pointers -- which is what I usually deal with. And once I've gone down that bridge, somewhere I probably needed a vector, and so I want your dense linear algebra function to be able to take that vector type instead of forcing me to copy it into an array.

Date: 2008-09-12 05:24 pm (UTC)
From: [identity profile] mh75.livejournal.com
You write really verbose matlab if you could port it to C in just 6 times as many lines.

No, i don't think so. I think you're overestimating the complexity of the actual algorithm.

I suppose all these numbers are fuzzy, though, so maybe before i make people think i'm being too precise i'd have to go through the exercise and actually count the code lines (as opposed to comment lines).

Profile

meganursula: (Default)
Megan Hazen

May 2020

S M T W T F S
     12
3456789
101112 13141516
17181920212223
24252627282930
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 15th, 2025 07:13 pm
Powered by Dreamwidth Studios