CSharp/C# >> Should you simplify this code? Write only code

by raylopez99 » Tue, 09 Sep 2008 16:41:24 GMT

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

CSharp/C# >> Should you simplify this code? Write only code

by J. Clarke » Tue, 09 Sep 2008 22:57:54 GMT



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)

Similar Threads

1. Should you simplify this code? Write only code

On Sep 9, 9:41燼m, raylopez99 < XXXX@XXXXX.COM > 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? 燩seudo code is OK, I'm just looking for
> a conceptual answer.

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 (which makes sense as
it would then need to

If List<T> supported some sort of "view" which also exposed RemoveAll,
it would be easy:

int lastIndex = list.FindLastIndex(value => value >= y);
list.SubList(lastIndex).RemoveAll(value => value < x);

But as "SubList" doesn't exist, I suspect in this case I'd have to go
back to a foreach loop.
In many other cases it *does* make sense to chain functions together -
there are just limitations in this particular question.
I personally don't like the C++, but that's as much to do with the
library design and lack of lambda expressions as anything else.


Note that if instead of removing data from the current list you were
happy with a new list with the retained elements, it's really easy:

int lastIndex = list.FindLastIndex(value => value >= y);
List<X> newList = list.Where((index, value) => index < lastIndex ||
value >= x).ToList();

Still two statements (to avoid finding the last index repeatedly) but
it's still sound. (I haven't tested the above, admittedly.)

Jon

2. Will the code be compiled while I write the code - CSharp/C#

3. write a C# code inside a javascript code, before body tag

Hello,

How can I write a C# code inside a javascript code, before body tag, as
we wrote in classic asp?
(I have a code file separated with a lot of C# code)

I would like anything like this:

<head runat="server">

       <style type="text/css">

                <%

                int x = 0;

                // Load Data Table
                DataTable rs = fnTabelaGradeCartas();

                // Load the grid with recordset
                foreach (DataRow cLinha in rs.Rows)
                {

                    Response.Write (".active-column-" + x.ToString() +
" {width:  80px; text-align: right;}");

                };

                %>

		        .active-grid-column {border-right: 1px solid threedshadow;}
		        .active-grid-row {border-bottom: 1px solid
threedlightshadow;}
	        </style>

    <title></title>
</head>
<body>
.
.
.


But the cod inside <% %> tags is ignorated :(
How could I write any c# code there?
Can I write this using the behind code or I must use like classic asp?

Please!

4. Bit fiddlers only : Can you simplify this C# code - CSharp/C#

5. Simplify this code

Is there a way to simplify the below code into one *readable* line

if (_pListener) {
    if (_pListener->OnChanged())
        printf("Changed");
}
else
    printf ("Changed");

I know I could do :
if (_pListener) { if (_pListener->OnChanged()) printf("Changed");
}
else
    printf ("Changed");

or
if (_pListener) { if (_pListener->OnChanged()) printf("Changed"); } else
printf ("Changed");

but that's not what I'm looking for :)

The closest I've found that looks clean (to me) is:
bool bChanged = (_pListener) ? _pListener->OnChanged() : true;
if (bChanged) printf ("Changed");

But I know there's alot more experts out there...am just looking for ideas
(PS.  the final code wont be printf - but rather some convoluted function
call that I'd rather not post here nor have in the code twice :) )

Peter


6. Need some basic advice (ideas to simplify code)

7. Simplifying Soap Web Services calls for client code

8. simplified code - Asp.Net