Comment On Why C-Pound Needs Macros

One feature I really miss in C# is the ability to define macros. According to the C# team, compile-time replacement macros make code more confusing (they've obviously never seen pascal.h) and more bug prone. What they failed to realize, however, is how incredibly difficult it becomes for people who don't like the C# syntax to redefine their own. Just look at how much trouble the author of this code (found by Jimmy) goes through to get a loop looking like "For i = 1 To 4" ... [expand full text]
« PrevPage 1 | Page 2Next »

Re: Why C-Pound Needs Macros

2005-05-16 13:38 • by travisowens

Hey 1st post :)


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


 

Re: Why C-Pound Needs Macros

2005-05-16 13:42 • by spotcatbug
That new syntax doesn't look any less confusing to me. It's certainly not as simple looking as "For i = 1 to 4"

Re: Why C-Pound Needs Macros

2005-05-16 14:01 • by bitz
(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?

Re: Why C-Pound Needs Macros

2005-05-16 14:10 • by E

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.

Re: Why C-Pound Needs Macros

2005-05-16 14:11 • by PresidentBeef
/// 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....

Re: Why C-Pound Needs Macros

2005-05-16 14:15 • by Free

[+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.

Re: Why C-Pound Needs Macros

2005-05-16 14:17 • by bugsRus
While we're at it, why don't we redfine that peskey if...else...elseif commands, I can never get them right!

Re: Why C-Pound Needs Macros

2005-05-16 14:24 • by icelava
Can somebody explain to me how the constructor is supposed to be less confusing?

Re: Why C-Pound Needs Macros

2005-05-16 14:25 • by deja
34478 in reply to 34474
This _can't_ be real code...

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

Re: Why C-Pound Needs Macros

2005-05-16 14:27 • by strongarm
I think next time I reinvent the wheel, I'll make it an octagon or maybe a triangle...

Re: Why C-Pound Needs Macros

2005-05-16 14:38 • by seizethedave

/// 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.

Re: Why C-Pound Needs Macros

2005-05-16 14:45 • by Huh
This is fake. Has to be.

Re: Why C-Pound Needs Macros

2005-05-16 14:50 • by Anonymous
Style aside, it looks like it's busted for m_Step != 1.  He should "m_Current = pBegin - pStep" in the constructor.

Re: Why C-Pound Needs Macros

2005-05-16 15:02 • by Mike R

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.

Re: Why C-Pound Needs Macros

2005-05-16 15:04 • by QuackImADuck

This is marvelously asinine.


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


In Engineering it's known that simpler is better; doesn't this hold true for programming as well?


And on that note, aren't lazy programmers the best (smart and lazy, that is)?  They get the job done the easiest way!

Re: Why C-Pound Needs Macros

2005-05-16 15:06 • by WanFactory
34486 in reply to 34483

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... [*-)]

Re: Why C-Pound Needs Macros

2005-05-16 15:09 • by Anonymous Coward
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).

Re: Why C-Pound Needs Macros

2005-05-16 15:09 • by Random Bowser

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?

Re: Why C-Pound Needs Macros

2005-05-16 15:34 • by Tallies
This guy is so retarded. I really hope somebody punches him in the nose soon. He deserves it.

Re: Why C-Pound Needs Macros

2005-05-16 15:37 • by Jonas
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.



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

Re: Why C-Pound Needs Macros

2005-05-16 15:38 • by smitty_one_each
Verily, three left turns effect a right turn, at the risk of some dizziness...

Jimmy, is the buddy in question a frustrated pythonista?

Re: Why C-Pound Needs Macros

2005-05-16 15:42 • by mugs
34495 in reply to 34494

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).

Re: Why C-Pound Needs Macros

2005-05-16 15:47 • by rogthefrog
34496 in reply to 34495

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.

Re: Why C-Pound Needs Macros

2005-05-16 15:50 • by Otto
34497 in reply to 34495

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)) { ...

 

Re: Why C-Pound Needs Macros

2005-05-16 15:51 • by Jonas
34498 in reply to 34493
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! :-)

Re: Why C-Pound Needs Macros

2005-05-16 15:52 • by TiredCoder
34499 in reply to 34488
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!

Re: Why C-Pound Needs Macros

2005-05-16 16:09 • by Charles Nadolski
/// <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.


Re: Why C-Pound Needs Macros

2005-05-16 16:12 • by 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....

Re: Why C-Pound Needs Macros

2005-05-16 16:18 • by Charles Nadolski
34503 in reply to 34502
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...

Re: Why C-Pound Needs Macros

2005-05-16 16:40 • by Gabe
34504 in reply to 34486
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

Re: Why C-Pound Needs Macros

2005-05-16 16:42 • by Gabe
34505 in reply to 34504
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?)

Re: Why C-Pound Needs Macros

2005-05-16 16:48 • by bitz
34506 in reply to 34479
strongarm:
I think next time I reinvent the wheel, I'll make it an octagon or maybe a triangle...


... and call it brainf.h

Re: Why C-Pound Needs Macros

2005-05-16 16:57 • by Rank Amateur
34507 in reply to 34501

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

Re: Why C-Pound Needs Macros

2005-05-16 16:59 • by bob

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


--Auto slit of throat started--

Re: Why C-Pound Needs Macros

2005-05-16 19:20 • by Jonathan.
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.

}

Re: Why C-Pound Needs Macros

2005-05-16 20:46 • by Schol-R-LEA
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 ...))))


Re: Why C-Pound Needs Macros

2005-05-16 22:09 • by Anonymous
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.

Re: Why C-Pound Needs Macros

2005-05-17 00:05 • by Mr. Brain
Are we seeing the heights of Object Orientation? 

Re: Why C-Pound Needs Macros

2005-05-17 00:40 • by vhawk
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 !!.

Re: Why C-Pound Needs Macros

2005-05-17 00:42 • by vhawk
34519 in reply to 34516
D - Flat - now that is sharp (forgive the pun)

Re: Why C-Pound Needs Macros

2005-05-17 01:14 • by Drak
34520 in reply to 34519

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

Re: Why C-Pound Needs Macros

2005-05-17 02:24 • by nonDev
34522 in reply to 34503

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?

Re: Why C-Pound Needs Macros

2005-05-17 02:32 • by em
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):


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

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

Re: Why C-Pound Needs Macros

2005-05-17 02:42 • by Stern
34524 in reply to 34505
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.

Re: Why C-Pound Needs Macros

2005-05-17 03:37 • by J Drakes

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:


foreach (int i in range(1,4))


btw, I don't agree that


for (int i=1;i<=4;i++)


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.


 

Re: Why C-Pound Needs Macros

2005-05-17 03:41 • by zgoda
34527 in reply to 34494
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. ;)

Re: Why C-Pound Needs Macros

2005-05-17 04:46 • by dhromed
34528 in reply to 34527
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.

Re: Why C-Pound Needs Macros

2005-05-17 05:46 • by DZ-Jay
34530 in reply to 34526
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:


foreach (int i in range(1,4))


btw, I don't agree that


for (int i=1;i<=4;i++)


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



for (int i=1;i<=4;i++)



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.



Re: Why C-Pound Needs Macros

2005-05-17 06:22 • by J Drakes
34531 in reply to 34530

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 for (int i=1;i<=4;i++) we are really reading  for .. i..1..4 , 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.


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: foreach (int i in range(1,4))


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.



 



 


 

Re: Why C-Pound Needs Macros

2005-05-17 06:41 • by V.
Words come too short ...

« PrevPage 1 | Page 2Next »

Add Comment