473,401 Members | 2,127 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,401 software developers and data experts.

My silliest extention made yet 2008...

... and it is only January. This might get worse. =)

I really like languages like C#, that has a foreach on IEnumerable<T>.

But sometimes you need to go by index and you have to for-loop.
In C-dervived languages it typically looks like this:

for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }

I have have always liked 0-based offsets, even if we talk in terms of index.
But for being a index, I have always disliked the -1 part of the code on
Count/Length.

I don't think I am alone when confessing that sometimes you miss that little
minus one, with a index out of bounds exception. A lame reason for a bug and
your program to be flawed.

So I tried creating extentions on IList<T>
Simple function that simply returns Count-1

First up was UpperBound()
for (int i = 0; i < theList.UpperBound() ; i++) { [etc.. etc..] }

That made it look way tooo much like Visual Basic for me, so no thanks.

In Delphi/Pascal you have pred() and succ() on ordinal types, so you could
(and should) do:
for i := 0 to pred(theList.Count) do begin [etc.. etc..] end;

I tried that:
for (int i = 0; i < theList.PredCount() ; i++) { [etc.. etc..] }

That made no sense at all, so I tried end:
for (int i = 0; i < theList.End() ; i++) { [etc.. etc..] }

I've tried several combinations of stop and end words and the best I can
come with is .ToEnd()

for (int i = 0; i < theList.ToEnd() ; i++) { [etc.. etc..] }

Any thoughts, except that I should ToEnd even thinking of this? =)

- Michael Starberg

Jan 17 '08 #1
22 1324
Michael Starberg wrote:
.. and it is only January. This might get worse. =)
There's some inspiring quote about a day without stupid questions being like
a day without Usenet.

Actually, I just made that up, but there ought to be.
I really like languages like C#, that has a foreach on IEnumerable<T>.

But sometimes you need to go by index and you have to for-loop.
In C-dervived languages it typically looks like this:

for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }

I have have always liked 0-based offsets, even if we talk in terms of index.
But for being a index, I have always disliked the -1 part of the code on
Count/Length.
Probably because it's wrong. Your loop will go from 0 through theList.Count
- 2... You're missing the last element, which is theList.Count - 1. That is,
unless the list is empty. Then your loop will immediately barf on an invalid
index, since 0 is certainly not smaller than -1.

for (int i = 0; i != theList.Count; ++i) { ... }

This is a perfectly natural way of writing things. In fact, there's a reason
people prefer zero-based indexing: it gives you much less opportunity for
off-by-one errors.
I don't think I am alone when confessing that sometimes you miss that little
minus one, with a index out of bounds exception. A lame reason for a bug and
your program to be flawed.
See above... don't write that, then.
Any thoughts, except that I should ToEnd even thinking of this? =)
Zero is the number thou shalt start from, and the number of the starting
shall be zero. One thou shalt not start from, unless thou by counting
apples. Two is right out.

--
J.
Jan 17 '08 #2
On Thu, 17 Jan 2008 09:59:46 -0800, Michael Starberg
<mi***************************@gmail.comwrote:
But sometimes you need to go by index and you have to for-loop.
In C-dervived languages it typically looks like this:

for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }

I have have always liked 0-based offsets, even if we talk in terms of
index.
But for being a index, I have always disliked the -1 part of the code on
Count/Length.
I have no idea what you're talking about. If you want to enumerate every
element in the list, by index, and the condition is "<" instead of "<="
(which is typical), you need "theList.Count", not "theList.Count - 1".

The code you posted is going to miss the last element.

Pete
Jan 17 '08 #3
Michael Starberg wrote:
for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }

I have have always liked 0-based offsets, even if we talk in terms of
index. But for being a index, I have always disliked the -1 part of
the code on Count/Length.
Huh? In C#, you don't use -1 at all (you must be confusing it with a
Delphi for loop):

for (int i = 0; i < theList.Count; ++i)

Note that the second is a while condition, IOW:

for (A; B; C)
{
D;
}

is equivalent to:

A;
while (B)
{
D;
C;
}

--
Rudy Velthuis http://rvelthuis.de

"A lie gets halfway around the world before the truth has a
chance to get its pants on."
-- Sir Winston Churchill (1874-1965)
Jan 17 '08 #4
"Jeroen Mostert" <jm******@xs4all.nlwrote in message
news:47***********************@news.xs4all.nl...
Michael Starberg wrote:
Actually, I just made that up, but there ought to be.
I will do my best =)
Zero is the number thou shalt start from, and the number of the starting
shall be zero. One thou shalt not start from, unless thou by counting
apples. Two is right out.
Does that works on cute mammals with sharp teath, garding a cave?

I like this quote:

Should array indices start at 0 or 1? My compromise of 0.5 was rejected
without, I thought, proper consideration. (Stan Kelly-Bootle)

Jan 17 '08 #5
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 17 Jan 2008 09:59:46 -0800, Michael Starberg
<mi***************************@gmail.comwrote:

>I have no idea what you're talking about. The code you posted is going to
miss the last element.
>Pete
Glad you noticed. The whole thread is like a joke, or just a way to get some
input.
But you get the gold-medal for spotting the problem.

Maybe some future maintainer would see the ToEnd, write the loop with i <
myList.ToEnd(), and then miss two elements.

My post was to show that there is always a problem with for-loops and why
foreach is better. =)

- Michael Starberg


Jan 17 '08 #6
"Rudy Velthuis" <ne********@rvelthuis.dewrote in message
news:xn****************@news.microsoft.com...
Michael Starberg wrote:
Thanks dentist! =)

- Michael Starberg
Jan 17 '08 #7
On Thu, 17 Jan 2008 10:59:22 -0800, Michael Starberg
<mi***************************@gmail.comwrote:
[...]
My post was to show that there is always a problem with for-loops and why
foreach is better. =)
So, when will you actually show that? We await with bated breath...

Pete
Jan 17 '08 #8
Michael Starberg <mi***************************@gmail.comwrote:
.. and it is only January. This might get worse. =)

I really like languages like C#, that has a foreach on IEnumerable<T>.

But sometimes you need to go by index and you have to for-loop.
I realise this thread is a joke, but you could easily write an
extension method of:

public static void ForEach<T>(this IEnumerable<Tsource,
Action<T,intaction)
{
int index=0;
foreach (T t in source)
{
action (t, index++);
}
}

Then call it with:

theList.ForEach((value, index) =>
{
Console.WriteLine ("The value at index {0} is {1}",
index, value);
});

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 17 '08 #9
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 17 Jan 2008 10:59:22 -0800, Michael Starberg
<mi***************************@gmail.comwrote:
>[...]
My post was to show that there is always a problem with for-loops and why
foreach is better. =)
>So, when will you actually show that? We await with bated breath...
>Pete
Then you have to wait long, Pete.
I don't start silly threads and then explain myself.
I just had fun doing some extentions, no harm done.

- Michael Starberg
Jan 17 '08 #10
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
I realise this thread is a joke
Thanks.
, but you could easily write an
Or just use a for-loop and make sure to get the operator correct.
Hehe, I don't think this is really a problem.

But as this joke moves forward, I really don't like the Action<T>
extentions.
You start off with a one-liner like myList.ForeEach(x =>
DoSomething(x+whatever)) and then realize you need more complexity and other
stuff happening. So you break it out into a proper foreach.

Same with asp.net and the so-called server-side web-controls. Works fine at
start, but with just one exception, or a pixel.error in one browser, you
have to roll it out into a for- or foreach-loop.

But again, my extentions was just a joke.

- Michael Starberg


Jan 17 '08 #11
Michael Starberg <mi***************************@gmail.comwrote:
, but you could easily write an

Or just use a for-loop and make sure to get the operator correct.
Hehe, I don't think this is really a problem.

But as this joke moves forward, I really don't like the Action<T>
extentions.
I love the Action<...and Func<...delegates. I don't expect to have
to define my own delegate type again for a long time :)
You start off with a one-liner like myList.ForeEach(x =>
DoSomething(x+whatever)) and then realize you need more complexity and other
stuff happening. So you break it out into a proper foreach.
Another alternative is to wrap the original IEnumerable in another one
- I have a SmartEnumerable class which gives you (for each value):

o The original value
o The index
o Whether it's the first value
o Whether it's the last value

I get annoyed with having to do things only on the first or last
iteration :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 17 '08 #12
On Jan 17, 10:59*am, "Michael Starberg" <michael.spam-
captcha.starb...@gmail.comwrote:
.. and it is only January. This might get worse. =)

I really like languages like C#, that has a foreach on IEnumerable<T>.
Okay, the joke was cute to begin with. However...

Why not use the STL-like begin/end concept? It works nicely there, and
is
fairly easy to read. As someone that does a LOT of mainteance coding
in
his life, one of my pet peeves is that most code (and here I include
C, C#,
Perl and its ilk) is write-only. You write the code, I have no idea
what you
meant the code to do, I can only read the code and know what it
actually
DOES.

So,

for ( i=array.begin(); i<array.end(); i=array.next())

or something equivalent (yes, I know STL uses ++ to increment, but
what's
the big deal about English?).

Not that the thread was really all that serious, but I find that
reading code is
a fairly annoying, and serious, business. I shouldn't HAVE to dig into
things like

for ( i=0; i<list.Count-1; ++i ) // Blah

and then find out you missed the last element.

Thanks for listening to the rant :)

Matt
Jan 17 '08 #13
Matt <ma********@sprynet.comwrote:

As someone that does a LOT of mainteance coding
in his life, one of my pet peeves is that most code (and here I include
C, C#, Perl and its ilk) is write-only. You write the code, I have no idea
what you meant the code to do, I can only read the code and know what it
actually DOES.
That's certainly true for badly written C#, but well written and well
refactored C# can be very readable indeed.

This is increased significantly (IMO) by the improvements in C# 3,
which often contribute to explaining what code is meant to do while
leaving out the details of how it does it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 17 '08 #14
On Jan 17, 12:59*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Matt <matttel...@sprynet.comwrote:
As someone that does a LOT of mainteance coding
in his life, one of my pet peeves is that most code (and here I include
C, C#, Perl and its ilk) is write-only. You write the code, I have no idea
what you meant the code to do, I can only read the code and know what it
actually DOES.

That's certainly true for badly written C#, but well written and well
refactored C# can be very readable indeed.
Readable is a relative thing, Jon. I'm not exactly a newbie at this.
With 20+
years of experience in the programming world, and 7 years with C# ( I
was
at Microsoft when the Alpha came out ), I know more than the average
bear :)
>
This is increased significantly (IMO) by the improvements in C# 3,
which often contribute to explaining what code is meant to do while
leaving out the details of how it does it.
This is true. However, our problem stems not from the language, but
rather from our inability to say what it is we are GOING to do.

Hey, I did say it was a rant.

Matt
>
--
Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet* Blog:http://www.msmvps.com/jon.skeet
World class .NET training in the UK:http://iterativetraining.co.uk
Jan 18 '08 #15

"Matt" <ma********@sprynet.comwrote in message
news:c9**********************************@v4g2000h sf.googlegroups.com...
On Jan 17, 10:59 am, "Michael Starberg" <michael.spam-
captcha.starb...@gmail.comwrote:
Okay, the joke was cute to begin with. However...
Reading your rant, it is a great rant.. =)
for ( i=array.begin(); i<array.end(); i=array.next())
Hmm, what if that was done with properties. Having properties define
behavour would break the PME-model and Anders Hejlsberg would probably kill
me if he found out.. But it would look good:

for (i = myList.Begin; i < myList.End; i = myList.Next)

Once I was asked to maintain a C++-project. My task was not to change any
existing code, just code a new class with a few simple methods. First thing
I did was to #define begin and end as { and } and then added more and more
#define until the code looked more or less like pascal. I was never asked to
join their project after that... *s*

But the code got committed and is running fine. Nobody bothered to rewrite
it. But they still laugh everytime they open the file.

That is how you get out of work you don't want to do. =)
and then find out you missed the last element.
Bug was inserted on purpose, just to show how easy it is to screw up using
for-loops.
Hehe, what a great excuse.

Customer: - When I press this button, the cpu goes to 100%, stalls and the
database gets corrupted.
Me: - Yes, I did that on purpose just to show how easy it is to screw up!

As for bugs, I have a bug-cup on my desk. Everytime I make a silly mistake,
I have to give the cup one SEK. That is about 1/6 dollar, not much money.
But the sound of the coin going into the cup makes everyone around go: -
What did you screw up this time, Starberg?

Note that I only pay for silly bugs, like the one in my example, or
enumerating the wrong list, or having bad logic in a if-statment and
similar. A major cause of lost coins to the cup is refreshing a webpage and
getting frustrated that my changes had no effect, just to realise that I am
browsing the test server, which of course does not have my changes comitted.
Grr!
Thanks for listening to the rant :)
Good rant

Matt

- Michael Starberg
Jan 18 '08 #16
"Matt" <ma********@sprynet.comwrote in message
news:a9**********************************@s13g2000 prd.googlegroups.com...

Readable is a relative thing, Jon. I'm not exactly a newbie at this.With
20+
years of experience in the programming world,
Well Matt, you are a c-coder. Readability is not even concept in the
c-world.

It is not relative, it is non-existant. Have you noticed how c-coders frowns
upon wowels?

int cnt = GetCount();
string cstmr = GetCustomer();
i is index, if not used to do pass a 2d-matrix, then i and j and why not k
can be used. or x and y. It seems like c-coders make a thing of being terse.
job-security above all! =)

C is a write-only language. Whenever you have to maintain old c-code, you
just scrap the file the and just re-type the darn thing. Only machosists
read c-code just to punish themselves.

Hehe. I just remember a great quote. - Pearl is a language that looks the
same, before and after encryption.
Hey, I did say it was a rant.
As for your 20+ experience, I promise to read all your rants, as you seem
like a smart and nice dude.

And this is a rant-thread. I get to say so as I started it. Please
continue....
Matt
May Your Kung-Fu Be Strong And Dandy
- Michael Starberg

Jan 18 '08 #17
"Matt" <ma********@sprynet.comwrote in message
news:aa**********************************@i7g2000p rf.googlegroups.com...
While I agree that years of experience have NOTHING to do with quality
of
code
20 years of usenet experience would tell you not to use enter when typing.
=)
But I agree on what you say, even if it was hard to read.
That's a separate rant, and comes under the "one off" problem.
I am still with you.
Agree 100%.

Hehe, I remember when I described the fence-problem to my mom. She is not
very bright, but has love and care as a special attribute. But brains, nope.
She had a hard time understanding that you need 11 poles to build a 10 meter
fence, with one pole at each meter.

Teaching my mom to code, would be a nightmare. I think I stopped trying when
I was 13 years old =)

We talked about this this christmas, when she got golden, that is celebrated
her 60 year birthday; that she did liked having a smart son, but did not
like to be outsmarted by an 11-year boy. - If I was not against childabuse,
I could have smacked you right in the face!

mommo is p0wned! =)

Unit tests do prove that the code does what you think it does. It does
not, however, show what the code was meant to do. If I assume that 1 +
1 = 3 and my code proves it, the unit test will pass. That doesn't
mean the code is right...
Hehe. What people found out when doing test driven application development,
is that the tests are 3 times larger than the actual code being tested, and
that the bugs (thereby) are often in the tests.

My test-driven application method is simply called eye-balling-n'-mouthing.
Take a good coder and friend, have a sitdown and read the code out loud and
debate if the code is dandy. Works every time. No bugs at all in 2007. We
shall se if I go clean in 2008.
Matt
- Micke
Jan 18 '08 #18
"Gatherings are a set of meetings. Each meeting can take place at anytime
while coding. This is why we recommend you to save only when you have clean
code, to avoid weird class-mutations, that subjects would laugh at during a
gathering.

It is in gatherings the power of subject oriented programming comes into
play. During a gathering, each subject will enumerate every other subject,
forget half of them, and discard but a few that they will call friends. The
rest is ignored.

Friends will participate in meetings and will mostly be associated with the
default value of non-humpable, until proven otherwise; which happens way too
often. This will be fixed in the paradise-edition. Some subjects will still
be friends even after a hammering of whynot-, evenifnot- and
foreachtry-attempt."

Very interesting poetry !

best, Bill Woodruff

Jan 18 '08 #19
"Bill Woodruff" <ms*********@dotscience.comwrote in message
news:e8**************@TK2MSFTNGP05.phx.gbl...
>
Very interesting poetry !

best, Bill Woodruff
Thanks, I am proud of it.

- Michael Starberg
Jan 18 '08 #20
On Jan 18, 9:33*am, "Michael Starberg" <michael.spam-
captcha.starb...@gmail.comwrote:
"Michael Starberg" <michael.spam-captcha.starb...@gmail.comwrote in
messagenews:eF**************@TK2MSFTNGP03.phx.gbl. ..
"Matt" <matttel...@sprynet.comwrote in message
news:ea**********************************@m34g2000 hsf.googlegroups.com...

C+- 2.0 in action.
If someone invents this language, and it turns out to look like your
code, I shall hunt you down and make you program in FORTH for the rest
of your life.

Matt
Jan 18 '08 #21
"Matt" <ma********@sprynet.comwrote in message
news:7d**********************************@k2g2000h se.googlegroups.com...

>If someone invents this language, and it turns out to look like your
code, I shall hunt you down and make you program in FORTH for the rest
of your life.
>Matt
So even if someone else does the implementation, I get to die in horrible
pain?
(also known as FORTH)

Well, guess that is a fair cup.

But agree that it is funny
- Michael Starberg




Jan 18 '08 #22
On 18 Jan, 20:42, "Michael Starberg" <michael.spam-
captcha.starb...@gmail.comwrote:
"Matt" <matttel...@sprynet.comwrote in message

news:7d**********************************@k2g2000h se.googlegroups.com...
If someone invents this language, and it turns out to look like your
code, I shall hunt you down and make you program in FORTH for the rest
of your life.
Matt

So even if someone else does the implementation, I get to die in horrible
pain?
(also known as *FORTH)

Well, guess that is a fair cup.

But agree that it is funny
- Michael Starberg
so "2 2 + ." gets 4 as a result? simple and logical...
Jan 20 '08 #23

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

Similar topics

2
by: Jesper Olsen | last post by:
I have a python extention implemented in C/C++ - the extention itself is internally implemented in C++, but the interface is pure C, so that it can easily be called from a python C-wrapper. The...
0
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
1
by: Carl Ogawa | last post by:
How do I make .cgi extention work? I installed ActivePerl 5.8. My CGI scripts work fine with .PL extention but not .CGI extention although I associated CGI extention as exactly same as PL...
1
by: MFA | last post by:
Hi all I have installed Front page server Extention on IIS 5.0 it was working fine. I was able to load project from Visual Interdav and doing well. Yesterday I installed a security update from...
3
by: Tuvas | last post by:
I am currently writing an extention module that needs to recieve a list of characters that might vary in size from 0 to 8. This is written as a list of characters rather than a string because it's...
1
by: godfather96 | last post by:
I am using a web control called eXml which is an extention to ms xml web control which supports xslt 2.0 when trying to group elements i receive the following error: group-by is not a valid...
5
by: Krustov | last post by:
I have the following list of image files . When searching the latest (numbered file) in this particular case its background_4.*** and its a .jpg file - but - the latest file in the list could...
26
by: Max2006 | last post by:
Hi, C# 3.0 extension methods become useful for us. Do we have the similar concept for extension properties? Thank you, Max
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.