J
Jorgen Grahn
Sounds like old-style C programmer: "I believe it is too expensive".
Of course. What I wrote was meant as an example of how C programmers
tend to think, not as a recommendation.
/Jorgen
Sounds like old-style C programmer: "I believe it is too expensive".
User-defined data types improve code readability. Under some circumstances,
it also help reduce the propensity to inadvertently add bugs to the code.
Knuth's quote tends to be abused a bit. Some individuals employ it to,
under Knuth's authority, imply that any attention to efficiency, however
remote it might be, is also evil, and therefore no attention whatsoever
should be ever paid to it. This may pose a serious problem in applications
which do need to run efficiently, as it isn't possible to fix performance
problems caused by fundamental design decisions by postponing any attention
to the application's efficiency to a testing stage.
Knuth's quote tends to be abused a bit.
Some individuals employ it to,
under Knuth's authority, imply that any attention to efficiency, however
remote it might be, is also evil, and therefore no attention whatsoever
should be ever paid to it. This may pose a serious problem in applications
which do need to run efficiently, as it isn't possible to fix performance
problems caused by fundamental design decisions by postponing any attention
to the application's efficiency to a testing stage.
none said:The key is the difference between premature optimization and premature
pessimization. Sutter/Alexandrescu did follow their "Don't optimize
prematurely" with a "Don't pessimize prematurely". They were also
careful to define premature optimization as making code more complex
and less readable when it is not justified by a proven performance
need (my emphasis).
And I am not so sure one couldn't just return std:air<float,float>
since it essentially does the same thing, except that its members are
not called 'x' and 'y'... Why invent another type when a standard one
exists?
Only start optimizing once you have tested your code, and are
satisfied that it is correct. And only start to optimize when
you have seen that there is a performance issue, and have
pinpointed the issue with a profiler.
Probably not the /only/ way though -- returning structs has been
possible in C for ~25 years now. I always believed it was expensive,
but I suspect that at some point it became cheaper with popular
compilers.
For functions with external linkage, the compiler doesn't have much
choice, as the platform's ABI will normally dictate how structures are
returned.
I think those suggesting change in the interface other than replacing pointersBGB said:yep, and in many cases where points/vectors are in use, they are a fairly
fundamental building-block type, rather than an "afterthought" (or "misc") type,
making them more suited to having their own dedicated type-names and their own
dedicated logic.
granted, a lot depends on the particular domain as well.
for example, I am prone to think of a "money" or "date" types as fairly misc,
since I don't use them much for what I do, but there are people who write types
of software where such things make a good deal more sense (and languages like
SQL treat both as fundamental types).
nevermind other strangeness (observed in accounting contexts): thinking as dates
strictly in terms of days, or even ignoring the existence of things like leap
years and similar in calculations (vs the more "sane" option of, say, thinking
as time more strictly in terms of an absolute number of seconds, as a real
value, between events).
but, it could be argued that maybe for the accountant, there is little reason
for the accountant to care if "this time next year" may easily differ by hours
or maybe days from "this time next year" as far as a scientist or engineer would
be concerned (in terms of the number of seconds between "now" and "this date and
time exactly one year from now"...).
or, for that matter, mentioning doing something "tomorrow" when it is after
midnight, and said "tomorrow" is maybe only 10 or 14 hours away and technically
the same day (as far as the calender is concerned...).
and, say, if one writes a 3D engine, they will be seeing points, vectors,
matrices, ... quite a fair amount.
so, yeah, sometimes it makes a lot of sense to make dedicated types for these
sorts of things.
Pavel said:I think those suggesting change in the interface other than replacing
pointers with references spoke without trying to understand how the
function is used.
For example, if the call site converts separate arrays of the two
velocity components, changing from output parameters to return type
would do more harm than good.
Jeff said:std::tuple<float, float>
NewPos(float angle, float speed, float *x, float *y)
std::tuple said:{
angle = (angle-90.0)*0.0174532925199433;
return std::make_tuple( cos(angle) * speed
, sin(angle) * speed);
}
int main()
{
...
std::tie(x, y) = NewPos(angle, speed);
}
I see no harm here, in fact the intent is more explicit.
Jeff
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.