473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Should you simplify this code? Write only code

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 <intv;
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
Sep 9 '08 #1
1 2472
raylopez99 wrote:
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 <intv;
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.

--
--
--John
to email, dial "usenet" and validate
(was jclarke at eye bee em dot net)
Sep 9 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Al Davis | last post by:
Note: I tried cross-posting this message to several newsgoups, including comp.lang.perl.misc, c.l.p.moderated, comp.infosystems.www.authoring.cgi, comp.lang.javascript and comp.lang.php. Nothing...
5
by: beliavsky | last post by:
To print a list with a specified format one can write (for example) for j in : print "%6d"%j, print The code print "%6d"%
29
by: Flzw | last post by:
Alright, here is a simple function I coded, won't be hard understanding what it does, and YES, I know, you will probably tell me it's awful code, I would like to know how to write it better, maybe...
4
by: Josh | last post by:
I have a database design question and wanted to see if folks could lend some advice. For simplicity sake, I'll illustrate using a book (but the hierarchy is the same in the real application - just...
0
by: Stylus Studio | last post by:
DataDirect XQuery(TM) is the First Embeddable Component for XQuery That is Modeled after the XQuery API for Java(TM) (XQJ) BEDFORD, Mass.--Sept. 20, 2005--DataDirect Technologies...
6
by: Patrick | last post by:
Hi All, Kind of new to this. What I have below works, but I am wondering if there is a way to make it more efficient/simpler instead of having to write an if, tr, td, blah for each datatype. How...
3
by: tshad | last post by:
I have dataGrid that I am filling from a List Collection and need to sort it by the various columns. So I need to be able to sort the Collection and found that you have to set up your own...
1
AmLegacy
by: AmLegacy | last post by:
I'm having a hard time figuring out how to simplify the fractions. Can anyone look at this code and see if you can see something I don't. //This is the fraction adding function void add_fractions...
5
by: Jon Skeet [C# MVP] | last post by:
On Sep 9, 9:41 am, raylopez99 <raylope...@yahoo.comwrote: It's tricky in .NET for two reasons: 1) LINQ doesn't have any concept of "remove" 2) List<T>.RemoveAll doesn't pass in the index...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.