Jump to page: 1 2
Thread overview
[Issue 3847] New: To avoid a C code bug
Feb 24, 2010
Ellery Newcomer
Feb 24, 2010
BCS
Feb 27, 2010
Stewart Gordon
May 03, 2010
Walter Bright
May 03, 2010
Adam D. Ruppe
May 03, 2010
Brad Roberts
May 03, 2010
Ary Borenszweig
May 04, 2010
Walter Bright
Sep 21, 2010
Stewart Gordon
Sep 21, 2010
Brad Roberts
Sep 21, 2010
BCS
February 24, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3847

           Summary: To avoid a C code bug
           Product: D
           Version: 2.040
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-02-23 18:11:52 PST ---
What does this D2 program print, and why?


import std.stdio;

bool thirdElementIsThree(int[] a) {
    return a.length >= 3 & a[2] == 3;
}

void main() {
    int[][] tests = [[6, 5, 4, 3, 2, 1],
                     [1, 2],
                     [1, 2, 3],
                     [1, 2, 3, 4 ],
                     [1]];
    int n = 0;

    try {
        int i = 0;
        while (true) {
            if (thirdElementIsThree(tests[i++]))
                n++;
        }
    } catch(Error e) {
        // No more tests to process
    }

    writeln(n); // prints?
}



Using 'and' and 'or' instead of && and || helps reduce some possible programmer mistakes, because the programmer may write & or | and the compiler may not catch the mistake. Using those keyword (as in Python and many other languages) avoids such mistakes, and C programmer will not be confused by this change because the || and && becomes deprecated, as the 'l' suffix for numbers. 'and' and 'or' are also quite less cryptic than && and || symbols.

&& and || can be kept to keep C compatibility, but their usage can be discouraged in new normal D2 code.

In real code it happens to write a & where you want to write &&, this is not an uncommon bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 24, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3847


Ellery Newcomer <ellery-newcomer@utulsa.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ellery-newcomer@utulsa.edu


--- Comment #1 from Ellery Newcomer <ellery-newcomer@utulsa.edu> 2010-02-23 18:39:12 PST ---
(In reply to comment #0)
> What does this D2 program print, and why?
> 
> 
> import std.stdio;
> 
> bool thirdElementIsThree(int[] a) {
>     return a.length >= 3 & a[2] == 3;
> }
> 
> void main() {
>     int[][] tests = [[6, 5, 4, 3, 2, 1],
>                      [1, 2],
>                      [1, 2, 3],
>                      [1, 2, 3, 4 ],
>                      [1]];
>     int n = 0;
> 
>     try {
>         int i = 0;
>         while (true) {
>             if (thirdElementIsThree(tests[i++]))
>                 n++;
>         }
>     } catch(Error e) {
>         // No more tests to process
>     }
> 
>     writeln(n); // prints?
> }


Ooh! Ooh! I know! it segfaults because the loop doesn't terminate and eventually test[i++] attempts to access memory not allocated to that process!

We're talking about things that would happen in C, right?


Personally, I prefer and,or, etc over &&,||, etc because they take less finger gymnastics to type.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 24, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3847


BCS <shro8822@vandals.uidaho.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |shro8822@vandals.uidaho.edu


--- Comment #2 from BCS <shro8822@vandals.uidaho.edu> 2010-02-23 20:24:24 PST ---
In order of preference:

1: close a won't fix, 'is' and 'in' are bad enought,we don't need more keyword-as-operators

2: replace the less common bitwise operators '&' and '|' with 'and' & 'or'

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 27, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3847


Stewart Gordon <smjg@iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com


--- Comment #3 from Stewart Gordon <smjg@iname.com> 2010-02-27 13:02:48 PST ---
(In reply to comment #2)
> 2: replace the less common bitwise operators '&' and '|' with 'and' & 'or'

I somehow think that would confuse people, who generally expect 'and' and 'or' to be logical rather than bitwise.

Though people coming from certain M$ Basic backgrounds have instead to deal with boolean true being 1 rather than -1.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 03, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3847



--- Comment #4 from bearophile_hugs@eml.cc 2010-05-03 10:45:16 PDT ---
and, or operators exist in C++: http://en.wikipedia.org/wiki/Iso646.h

This C++ code compiles:

#include "stdio.h"
#include "stdlib.h"
int main() {
    bool a = atoi("1");
    bool b = atoi("1");
    bool c = atoi("0");
    printf("%d\n", (a and b) or c);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 03, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3847


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com


--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> 2010-05-03 11:00:26 PDT ---
C++ has had the 'and' and 'or' alternate keywords for well over a decade. To my knowledge, nobody uses them, and few C++ programmers even know they exist. It's a failure.

Therefore, I don't think it is a good idea to adopt them.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 03, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3847


Adam D. Ruppe <destructionator@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |destructionator@gmail.com


--- Comment #6 from Adam D. Ruppe <destructionator@gmail.com> 2010-05-03 11:26:55 PDT ---
I think we should replace all the operators to avoid bugs.

= should be changed to SET_TO

== should be changed to IS_EQUAL_TO

+ should be changed to ADD

- should be SUBTRACT

a++ should be INCREMENT_AND_RETURN_OLD_VALUE

++a should be INCREMENT


and so on. This way, the user's intention is always clear because he has to type out the value. Each has a different length and/or starts with a different letter to aid in autocomplete and noticing if the autocomplete picks the wrong one.

Check out how clear this code is:

for(a SET_TO 0 TERMINATE_INITIALIZATION
    a LESS_THAN 10 TERMINATE_CONTINATION_CONDITION
    INCREMENT a) {
    if(a LESS_THAN 5 AND a REMAINDER_AFTER_VALUE_DIVIDED_BY 2 IS_EQUAL_TO 0)
         writefln("%INTEGER_FORMAT", a);
}

Amazing and guaranteed to be bug free!


----------


So seriously now, how many times have you written & when you meant && and not noticed it quickly?

If anything were to change here, I'd say the rules on implicit conversion to boolean should he changed, not the operator. (And, ideally, the bitwise precidence!)

Then you'd do

bool f(int x) {
     return x & 1; // compile error - can't implicitly cast int to bool
}

bool f(int x) {
     return (x & 1) == 1; // works
}

But I doubt this is a problem often enough to warrant any change.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 03, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3847



--- Comment #7 from bearophile_hugs@eml.cc 2010-05-03 12:10:07 PDT ---
In C++ no one used them. But it can be just a matter of nudging D programmers in the right direction with the style guide and commonly accepted idioms for writing D code fist, and few years from now with warnings of deprecation of the bug-prone || &&, before removing them from some future version.

Python uses the "and" and "or" and shows how better they are: they just look better and more natural, are simpler and faster to type, they make the code less visually noisy, are written as they are read aloud, and they avoid some bugs like the one I've shown at the top (the presence of such bugs can be seen from the fact that both the Gimpel lint warns about an error like the one of the original top, and recent GCC versions have warning for such probably erroneous usage of bitwise operators). It's a change for the better.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 03, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3847


Brad Roberts <braddr@puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |braddr@puremagic.com


--- Comment #8 from Brad Roberts <braddr@puremagic.com> 2010-05-03 12:50:11 PDT ---
'and' is isn't particularly less ambiguous than && vs &.  The use of a word doesn't help tell me logical vs bitwise.  Additionally, if you're going to word-ize some of the logical operators, you'd better do that for all of them.. so don't forget exclusiveor and equals and lessthan and...

Rather than let this ticket become a debated endlessly ticket, given Walter's response, I suggest it be closed as wontfix aka notabug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 03, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3847


Ary Borenszweig <ary@esperanto.org.ar> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ary@esperanto.org.ar


--- Comment #9 from Ary Borenszweig <ary@esperanto.org.ar> 2010-05-03 12:58:09 PDT ---
'and' and 'or' also help with readability.

The thing is, you can make mistakes with && and &. You can't make mistakes with 'and' and '&'.

Nobody used 'and' and 'or' as variables or function names so I don't think it's a problem at all to add them as synonymous to && and ||.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2