Should you simplify this code? Write only code

R

raylopez99

Scott Meyers in his 2001 book on STL / C++ claims that the following
code is optimal--what is the C# way and should you follow his example
and try and do this in LINQ? Pseudo code is OK, I'm just looking for
a conceptual answer.

"Suppose you have a vector <int> [this is like List<> in C#--RL] and
you'd like to get rid of all the elements in the vector whose value is
less than x, except that elements preceding the last occurrence of a
value at least as big as y should be retained"

Answer:

vector <int> v;
int x,y;
....
v.erase (remove_if (find_if (v.rbegin(), v.rend(),
bind2nd(greater_equal<int>(),y)).base(),
v.end(), bind2nd(less<int>(),x)), v.end());

Meyers goes on to claim, which I suspect some people here would agree,
that this functional type expression, which uses ten different
functions in one line, including reverse iterators, bind2nd, and
anonymous function objects, is actually more efficient (though less
readable) than trying to do it with many lines of code "by hand".

He calls such code "write only" code since only the author really
knows what's going on, and the reader is perplexed, so he recommends
lots of comments but still believes you should use the STL when
possible.

Comments?

RL
 
J

J. Clarke

raylopez99 said:
Scott Meyers in his 2001 book on STL / C++ claims that the following
code is optimal--what is the C# way and should you follow his
example
and try and do this in LINQ? Pseudo code is OK, I'm just looking
for
a conceptual answer.

"Suppose you have a vector <int> [this is like List<> in C#--RL] and
you'd like to get rid of all the elements in the vector whose value
is
less than x, except that elements preceding the last occurrence of a
value at least as big as y should be retained"

Answer:

vector <int> v;
int x,y;
...
v.erase (remove_if (find_if (v.rbegin(), v.rend(),
bind2nd(greater_equal<int>(),y)).base(),
v.end(), bind2nd(less<int>(),x)), v.end());

Meyers goes on to claim, which I suspect some people here would
agree,
that this functional type expression, which uses ten different
functions in one line, including reverse iterators, bind2nd, and
anonymous function objects, is actually more efficient (though less
readable) than trying to do it with many lines of code "by hand".

He calls such code "write only" code since only the author really
knows what's going on, and the reader is perplexed, so he recommends
lots of comments but still believes you should use the STL when
possible.

Comments?

Comments are needed badly if you are doing that. The style of
programming is instantly recognizable to anyone who has worked with
APL--marvelous language for solving one-shot problems, but try to
figure out what someone else's uncommented code is doing.
 

Ask a Question

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.

Ask a Question

Top