• (cs)

    Hey 1st post :)

    But OO looping is so much more powerfull that a simple FOR....

     

  • (cs)

    That new syntax doesn't look any less confusing to me. It's certainly not as simple looking as "For i = 1 to 4"

  • (cs)

    (you probably dont want to hear this but...) if you want syntax like that, use VB.NET.
    I mean seriously, why go through all that to put training wheels on your code?

  • E (unregistered)

    His syntax is far more confusing because it is non-standard, and I expect it's significantly less efficient than a real for-loop as well.

    Comical.

  • PresidentBeef (unregistered)

    /// The ForTo class avoids confusing syntax of 
    /// "for (int i=1;i<=4;i++) {...}". This can be
    /// replaced with "foreach (int i in new ForTo(1,4)) {...}".



    Because that is so much less confusing....

  • (cs)

    [+o(]

    I thought that people who like pre-compile macros didn't like VB style syntax such as "For i = 1 To 4"?

    Beer? When is too much not enough ?

    Beer: The goggles are good but they wear off quickly.

  • (cs)

    While we're at it, why don't we redfine that peskey if...else...elseif commands, I can never get them right!

  • (cs)

    Can somebody explain to me how the constructor is supposed to be less confusing?

  • deja (unregistered) in reply to PresidentBeef

    This can't be real code...

    (And why can't I insert emoticons in opera?...)

  • (cs)

    I think next time I reinvent the wheel, I'll make it an octagon or maybe a triangle...

  • (cs)

    /// The ForTo class avoids confusing syntax of
    /// "for (int i=1;i<=4;i++) {...}".

    Anyone who has spent any time writing code at all would have no problem with this.

  • Huh (unregistered)

    This is fake. Has to be.

  • Anonymous (unregistered)

    Style aside, it looks like it's busted for m_Step != 1.  He should "m_Current = pBegin - pStep" in the constructor.

  • (cs)

    Wow!

    This guy should write a book. "How to get your teammates to resent you in 1 week"

    1) If he's not comfortable with C# and is (obviously) more comfortable with VB, then use VB.NET..

    2) If C# is the only option then get used to the native syntax and don't screw your teammates with some crappy contrived bass-ackwards construct like "foreach (int i in new ForTo(1,4)) {...}". THAT is confusing sytax if I ever saw it. If I saw that, I'd track him down and smack him upside the head.

  • QuackImADuck (unregistered)

    <FONT color=#000000>This is marvelously asinine.</FONT>

    <FONT color=#000000>The coder is smart enough to write this class yet is uncomfortable with "for (int i=1;i<=4;i++) {...}".   A great WTF!!!  [:D]</FONT>

    <FONT color=#000000>In Engineering it's known that simpler is better; doesn't this hold true for programming as well?</FONT>

    <FONT color=#000000>And on that note, aren't lazy programmers the best (smart and lazy, that is)?  They get the job done the easiest way!</FONT>

  • WanFactory (unregistered) in reply to Anonymous

    Well, the WTF does have one tiny advantage over the usual way (not that it justifies it [+o(]).

    The ForTo enumerator does not have the vulnerability that an accidental i-- or i = something in the body can cause an infinite loop. Yeah, I know I need to get past my hangup of preferring definite iteration (guaranteed to terminate) over indefinite iteration but I still havent registered for the 12 step program yet... [*-)]

  • Anonymous Coward (unregistered)

    The design is certainly ahem interesting, but the idea itself isn't a WTF.  Functional languages such as Ruby include such facilities, and coded/designed correctly this isn't a bad idea with C#, especially under 2.0 with generics, iterator functions and anonymous delegates.  In fact, I think the power collections package contains similar ideas, though trying to look this up is not working so well for me at this moment (the site is unbelievably slow).

  • Random Bowser (unregistered)

    Looks like someone didn't quite get the joke in this CodeProject article:  http://www.codeproject.com/useritems/ForCSharp.asp

    The article was a joke, right?

  • (cs)

    This guy is so retarded. I really hope somebody punches him in the nose soon. He deserves it.

  • Jonas (unregistered)

    Am I the only one who sees at least some good reasons for doing this?

    First off, I would never do it myself. I think you should follow the ways of the current programming language and environment. But I do argue that the normal for construct is error prone. Each day you see tens/hundereds for statements, and it's easy to miss small details, and spot the difference between < and <= and so forth.

    When I first started programming Python, the lack of the C-like for statement bothered me, but then I realized that most for statements are used to iterate over an array/list/collection, where foreach(i in list)/for i in list is great. And for all those other for loops I think that for i in range(n) is clearer/less error prone than for(int i=0;i<n ;i=""></n>.

    But I write C++ every day so I'm quite used to normal for statements, but I think Python has made a better choice.

  • (cs)

    Verily, three left turns effect a right turn, at the risk of some dizziness...
    Jimmy, is the buddy in question a frustrated pythonista?

  • (cs) in reply to smitty_one_each

    I can only assume he wrote that for someone else to use (a struggling VB programmer who doesn't understand real for loops for instance).

  • (cs) in reply to mugs

    If you can't understand or keep track of

    for(int i = 0; i < 10; ++i)

    you shouldn't be allowed to write any code.

  • Otto (unregistered) in reply to mugs

    What's funny is that for the specific case of a counter where the start and end points are just numbers that don't change, you could do this equally well:

    foreach (int i in (1,2,3,4)) { ...

     

  • Jonas (unregistered) in reply to Jonas
    for(int i=0;i

    Ouch! The forum didn't like "lesser-than n". I know how to write a proper for statement, I promise! :-)

  • TiredCoder (unregistered) in reply to Random Bowser
    Anonymous:

    Looks like someone didn't quite get the joke in this CodeProject article:  http://www.codeproject.com/useritems/ForCSharp.asp

    The article was a joke, right?

    Chad (the article author) is a long time Delphi/Pascal coder... this is just like pascal.h!

  • (cs)

    /// <summary>
    /// The ForTo class avoids confusing syntax of
    /// "for (int i=1;i<=4;i++) {...}". This can be
    /// replaced with "foreach (int i in new ForTo(1,4)) {...}".
    /// </summary>

    Bwahahaha!  I wonder what he'd think of my use of for loops... that don't use integers!
            for(dCurrGPSDistance = dPrevGPSDistance, dCurrDistance = dPrevDistance;
                dCurrGPSDistance < dNextGPSDistance;
                dCurrGPSDistance += dMileInterval, dCurrDistance += dAdjInterval)

    or better yet... (okay, so it's regular C++ but whatever)
                    for(strSensor = str.Tokenize(",", nCount);
                        strSensor.Find("N") == -1 && strSensor !="";
                        m_cSensors++, strSensor = str.Tokenize(",", nCount))

    This is like reinventing the wheel then asking your hamster to run on it backwards, or else it will spontaneously combust.

  • (cs)

    I see now! The "summary" should be "remarks" and the "replaced with" should go into "example" and the class itself should go into the recycle bin. No wait - it should just be deleted....

  • (cs) in reply to nonDev
    nonDev:
    I see now! The "summary" should be "remarks" and the "replaced with" should go into "example" and the class itself should go into the recycle bin. No wait - it should just be deleted....


    When it's deleted are the locations in memory re-written to zero or just earmarked to be overwritten later?  It's better to be safe then sorry...
  • Gabe (unregistered) in reply to WanFactory
    Anonymous:

    Well, the WTF ... does not have the vulnerability that an accidental i-- or i = something in the body can cause an infinite loop. Yeah, I know I need to get past my hangup of preferring definite iteration (guaranteed to terminate) over indefinite iteration but I still havent registered for the 12 step program yet... [*-)]

    Depending on how they iterate, are you worried it might be more than twelve steps? :D

  • Gabe (unregistered) in reply to Gabe

    son of a monkey! Didn't mean for that quote to get so fubared. :( Sorry, all... (Previewed, looked good, hit post -- is this a known bug? is it only in Opera?)

  • (cs) in reply to strongarm
    strongarm:
    I think next time I reinvent the wheel, I'll make it an octagon or maybe a triangle...

    ... and call it brainf.h
  • (cs) in reply to Charles Nadolski

    Heh, or let him try:

    for (item * it = head; it != NULL; it = it->next)

    Oh, wait, is that what foreach is *supposed* to be for?

    --RA

  • bob (unregistered)

    Whoa, this is like so GREAT!!!  I'm gonna copy and paste this and use it right now!!!

    --Auto slit of throat started--

  • Jonathan. (unregistered)

    If he's so stupid, why doesn't he write a function that returns an ArrayList of integers (if the range function doesn't already exist).

    Then he can go:
    foreach (int i in BitchinListGenerator.Range( 1 , 4 ) )
    {
        // AWESOME CODE IN HERE.
    }

  • (cs)

    Forget string-replacement macros, what D-Flat needs is Hygenic Syntactic Macros! ;)



    (define-syntax for
      ; a Pascal-style for loop in Scheme
      (syntax-rules (:= to downto step)
        ; rule for complete upwards for loop
        ((for index := start to finish step modifier expr1 expr2 ...)
         (begin
           (define index start)
           (let loop ()
             (if (<= index finish)
                 (begin expr1 expr2 ... (set! index (+ index modifier)) (loop))
                 (set! index (- index modifier)))))) ; adjust index to correct final value
       
        ; rules for complete downwards for loop
        ((for index := start downto finish step modifier expr1 expr2 ...)
         (begin
           (define index start)
           (let loop ()
             (if (>= index finish)
                 (begin expr1 expr2 ... (set! index (- index modifier)) (loop))
                 (set! index (+ index modifier)))))) ; adjust index to correct final value
       
        ; rule for upward loop with default step
        ((for index := start to finish expr1 expr2 ...)
         (for index := start to finish step 1 expr1 expr2 ...))
       
        ; rule for downward loop with default step
        ((for index := start downto finish expr1 expr2 ...)
         (for index := start downto finish step 1 expr1 expr2 ...))))

  • Anonymous (unregistered)

    The title of this WTF reads, "C-Pound". I'm pretty sure that should be, "C-Sharp", as that's how it's pronounced. I like the idea of pronouncing it "D-Flat", though. That's pretty good.

  • Mr. Brain (unregistered)

    Are we seeing the heights of Object Orientation? 

  • (cs)

    Man this should be punishable by death !!  Half the required interface methods are defined nor implemented.  This guy would have received the idiot of the month award at our company !!.

  • (cs) in reply to Anonymous

    D - Flat - now that is sharp (forgive the pun)

  • (cs) in reply to vhawk

    Bah, since everything in C# is 0 based anyway, what's so confusing about using

    for (intI = 0; intI < 4; intI ++)

    ??? I never use '< =' in a for loop in C/C#/C++ if I can help it.

     

    Drak

  • (cs) in reply to Charles Nadolski

    Charles Nadolski:
    nonDev:
    I see now! The "summary" should be "remarks" and the "replaced with" should go into "example" and the class itself should go into the recycle bin. No wait - it should just be deleted....


    When it's deleted are the locations in memory re-written to zero or just earmarked to be overwritten later?  It's better to be safe then sorry...

    You mean we should flip all the bits to 0?

  • em (unregistered)

    Well, the name of the class is terrible, he should call it something like "Range". But other than that, this guy is just reproducing the semantics of the Python xrange() function:

    for i in xrange(4): <do whatever with i in {0, 1, 2, 3}>

    With my proposed name change, the C# code becomes the more readable:

    foreach (int i in new Range(1,4)) {...}

  • Stern (unregistered) in reply to Gabe
    Anonymous:
    (Previewed, looked good, hit post -- is this a known bug? is it only in Opera?)

    No, it's "telligentsystems Community Server", the biggest pile of WTF that ever WTFed. Sounds like a great business idea to clone phpBB et. al. in proprietary Microsoft-only technologies with the added value of SUCK to entice customers.
  • J Drakes (unregistered)

    The intent is not completely evil, as it allows you to define range objects (although the author decides to obscure readibility with a redundant new ForTo syntax).  providing a function building the object could make it nicer:

    <FONT color=#848284>foreach (int i in range(1,4))</FONT>

    btw, I don't agree that

    <FONT color=#848284>for (int i=1;i<=4;i++) </FONT>

    is inherently clearer than the above. quite the opposite.

    Look at them and you'll realize that we think it's clearer only because of habit.

    The first goes straight to the intent, without getting us into concepts such as <= and ++, that are really irrelevant to the underlying intention of moving from 1 to 4.  The C-like syntax confuses the what with the how.

     

  • (cs) in reply to smitty_one_each
    smitty_one_each:
    Jimmy, is the buddy in question a frustrated pythonista?


    I am a "frustrated pythonista" (mostly due to insufficient amount of Python in my daily work) but I wouldn't write such mess. ;)
  • (cs) in reply to zgoda

    In a sense, the counting loop we're all accustomed to is actually a hard way of doing what is conspicuously absent in nearly all languiages: an easy 'count from here to there.'

    The issue I have with the (var i=0; i < length; i++) syntax is that because these loops constitute about 90% of all loops, and one tends to forget that you can put anything in the three ; ; slots, which is what it's made for.

    For Arrays in ECMA (presumably in most other langs too), you can use for...in just as easily, although for some reason that never caught on, and people still use the 'manual' apporach with the counting for loop.

    A side effect is that new coders often don't realise the versatility of the for loop syntax, and only know the classical (var i=0; i < length; i++).

    Often it even takes a switch of mind to see that you can start with a large i and count downwards: i--, or that the middle param doesn't have to be an equation at all. I've used series of HTML elements with number-formatted ids, like 'menu1', 'menu2',... 'menun', and then use the following loop format to pick them all off in JS:

    for (var i = 1; elemById('menu' + i) ; i++)

    So his intentions are well. It's just that writing a bulky class for it is something of a WTF.

  • (cs) in reply to J Drakes
    Anonymous:

    The intent is not completely evil, as it allows you to define range objects (although the author decides to obscure readibility with a redundant new ForTo syntax).  providing a function building the object could make it nicer:

    <font color="#848284">foreach (int i in range(1,4))</font>

    btw, I don't agree that

    <font color="#848284">for (int i=1;i<=4;i++) </font>

    is inherently clearer than the above. quite the opposite.

    Look at them and you'll realize that we think it's clearer only because of habit.

    The first goes straight to the intent, without getting us into concepts such as <= and ++, that are really irrelevant to the underlying intention of moving from 1 to 4.  The C-like syntax confuses the what with the how.



    I disagree with you.  I think that

    <font color="#848284">for (int i=1;i<=4;i++)

    </font>is pretty clear -- but it is a *generic* loop construct, which is what it was designed to be.  It specifies:
        Variable initialization (i.e. which value to start with)
        Exit condition (i.e. which value to end with)
        Increment (i.e. how much to increment the variable after each iteration).

    That's pretty simple to me.  I'd admit that it is a primitive construct, but it allows for myriad types of loops, whereas constructs like "Foreach x in y" and "For i = x to y" have specialized functions.

    I do agree that since iterating through array indexes or object collections is a very common function in modern programming languages, that they should include specialized constructs for them -- but I still believe that the generic loop construct should be kept, to give the developer the choice.  However, if the language does not provide for it, making up your own arbitrary syntax does not necessarily make it "clearer" -- especially for third parties, which most likely will have to maintain your code.

        -dZ.

  • J Drakes (unregistered) in reply to DZ-Jay

    I agree that the generic construct does more than just that the plain old "go from a to b".  I have to think about this, but I wonder if the more generic uses make for expressive code - I wonder if alternative wording (maybe using while loops) makes code more expressive.  I certainly remember to have used this idiom a lot, but at the time I was appreciating more idiomatic programming than expressive programming.

    I disagree with the fact that is simple.  Look, I realize that a line of code can't be *that* hard - so it's simple in that respect - , but I assume we are trying to appreciate the details here.  I bet that every time we read <FONT color=#848284>for (int i=1;i<=4;i++) </FONT><FONT color=#000000>we are really reading  <FONT color=#848284>for .. i..1..4 </FONT></FONT><FONT color=#000000>, filtering out everything else.  I don't have hard figures, but I believe that a good 90% of the loops we use falls in that case.</FONT>

    You say:

    "However, if the language does not provide for it, making up your own arbitrary syntax does not necessarily make it "clearer" -- especially for third parties, which most likely will have to maintain your code."

    This is a complex issue.  I don't find that syntax arbitrary.  I mean, you can read it out loud and it says what it does: <FONT color=#848284>foreach (int i in range(1,4))</FONT>

    <FONT color=#848284></FONT>Even old boys like C++ are trying to "modernize" in this respect.  There is a trade-off here between innovation and existing culture.  I disagree with the fact it's not clear, but I realize that for someone not coming from that culture it may look alien.  I certainly found somehow displacing to find names using underscores in java, but I can put it down only to cultural reasons.  Again, it's a matter of culture and tradeoffs.


     

    <FONT color=#000000>
     

    </FONT>

     

  • (cs)

    Words come too short ...

Leave a comment on “Why C-Pound Needs Macros”

Log In or post as a guest

Replying to comment #34517:

« Return to Article