www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Less commas

reply bearophile <bearophileHUGS lycos.com> writes:
I'd like to restrict a bit the usage of the comma operator in D2, disallowing
at compile-time some currently usages that some C style guides already suggest
to avoid.

If I see production code like the two examples below in production code, I
change the code, and remove the commas. I think they are confusing, and may
hide bugs. Do you know other situations where you like to disallow the comma
operator in D2? Later I will probably write an enhancement request on this.

----------------

Is x the result of foo() or bar()?

int foo() { return 10; }
int bar() { return 20; }
void main() {
    int x;
    x = foo(), bar();
}

----------------

The comma could be mistaken for a semicolon:

void main() {
    int x;
    if (x > 0) x = 0,
               x = 1;
}

Bye,
bearophile
Dec 31 2010
next sibling parent reply Caligo <iteronvexor gmail.com> writes:
--000e0cd2dcec35cb3a0498b1fa14
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Dec 31, 2010 at 2:09 AM, bearophile <bearophileHUGS lycos.com>wrote:

 I'd like to restrict a bit the usage of the comma operator in D2,
 disallowing at compile-time some currently usages that some C style guides
 already suggest to avoid.

 If I see production code like the two examples below in production code, I
 change the code, and remove the commas. I think they are confusing, and may
 hide bugs. Do you know other situations where you like to disallow the comma
 operator in D2? Later I will probably write an enhancement request on this.

 ----------------

 Is x the result of foo() or bar()?

 int foo() { return 10; }
 int bar() { return 20; }
 void main() {
    int x;
    x = foo(), bar();
 }

 ----------------

 The comma could be mistaken for a semicolon:

 void main() {
    int x;
    if (x > 0) x = 0,
               x = 1;
 }

 Bye,
 bearophile

What font are you using? --000e0cd2dcec35cb3a0498b1fa14 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><div class=3D"gmail_quote">On Fri, Dec 31, 2010 at 2:09 AM, bearophile = <span dir=3D"ltr">&lt;<a href=3D"mailto:bearophileHUGS lycos.com">bearophil= eHUGS lycos.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" = style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 20= 4); padding-left: 1ex;"> I&#39;d like to restrict a bit the usage of the comma operator in D2, disal= lowing at compile-time some currently usages that some C style guides alrea= dy suggest to avoid.<br> <br> If I see production code like the two examples below in production code, I = change the code, and remove the commas. I think they are confusing, and may= hide bugs. Do you know other situations where you like to disallow the com= ma operator in D2? Later I will probably write an enhancement request on th= is.<br> <br> ----------------<br> <br> Is x the result of foo() or bar()?<br> <br> int foo() { return 10; }<br> int bar() { return 20; }<br> void main() {<br> =A0 =A0int x;<br> =A0 =A0x =3D foo(), bar();<br> }<br> <br> ----------------<br> <br> The comma could be mistaken for a semicolon:<br> <br> void main() {<br> =A0 =A0int x;<br> =A0 =A0if (x &gt; 0) x =3D 0,<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 x =3D 1;<br> }<br> <br> Bye,<br> <font color=3D"#888888">bearophile<br> </font></blockquote></div><br>What font are you using?<br> --000e0cd2dcec35cb3a0498b1fa14--
Dec 31 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Caligo:

 What font are you using?


Inconsolata-g, a very good non-proportional font designed to be used with anti-aliasing. Its main fault is that it doesn't have bold yet. One of its features is to tell apart commas and semicolons better, by the way. In it the lower-case L and the 1 glyphs are easy to tell apart. Yet, the D choice to deprecate number literals like 1l was the right one. Bye, bearophile
Dec 31 2010
prev sibling next sibling parent Caligo <iteronvexor gmail.com> writes:
--000e0cd3329afac8160498b25475
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Dec 31, 2010 at 4:17 AM, bearophile <bearophileHUGS lycos.com>wrote:

 Caligo:

 What font are you using?


Inconsolata-g, a very good non-proportional font designed to be used with anti-aliasing. Its main fault is that it doesn't have bold yet. One of its features is to tell apart commas and semicolons better, by the way. In it the lower-case L and the 1 glyphs are easy to tell apart. Yet, the D choice to deprecate number literals like 1l was the right one. Bye, bearophile

I use Anonymous Pro and I think it does a better job in case of commas and semicolons. --000e0cd3329afac8160498b25475 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Fri, Dec 31, 2010 at 4:17 AM, bearoph= ile <span dir=3D"ltr">&lt;<a href=3D"mailto:bearophileHUGS lycos.com">bearo= phileHUGS lycos.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quo= te" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204= , 204); padding-left: 1ex;"> Caligo:<br> <div class=3D"im"><br> &gt; &gt; What font are you using?<br> <br> </div>Inconsolata-g, a very good non-proportional font designed to be used = with anti-aliasing. Its main fault is that it doesn&#39;t have bold yet.<br=

One of its features is to tell apart commas and semicolons better, by the w= ay. In it the lower-case L and the 1 glyphs are easy to tell apart. Yet, th= e D choice to deprecate number literals like 1l was the right one.<br> <br> Bye,<br> <font color=3D"#888888">bearophile<br> </font></blockquote></div><br>I use Anonymous Pro and I think it does a bet= ter job in case of commas and semicolons.<br><br> --000e0cd3329afac8160498b25475--
Dec 31 2010
prev sibling next sibling parent reply Peter Alexander <peter.alexander.au gmail.com> writes:
On 31/12/10 8:09 AM, bearophile wrote:
 I'd like to restrict a bit the usage of the comma operator in D2, disallowing
at compile-time some currently usages that some C style guides already suggest
to avoid.

 If I see production code like the two examples below in production code, I
change the code, and remove the commas. I think they are confusing, and may
hide bugs. Do you know other situations where you like to disallow the comma
operator in D2? Later I will probably write an enhancement request on this.

I don't believe it is the job of the compiler to enforce personal style preferences. If people /really/ want to write obfuscated code then that's up to them -- you can't stop them. If organisations want to enforce style guides then that's up to them to manually enforce the style or produce tools to do it automatically. Whatever criteria you select for distinguishing acceptable comma use from unacceptable comma use will be subjective, and inevitably some people will be angered by the decision. I think that this should be *at most* an optional warning, but even then I think it would be a massive waste of programmer time to add this feature.
Dec 31 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Peter Alexander:

 I don't believe it is the job of the compiler to enforce personal style 
 preferences.

D2 compiler enforces several good style preferences. Those two style preferences are not just mine, they are enforced by a known C lint tool too. So are the two things I've shown bad/important enough to justify avoiding them?
 If people /really/ want to write obfuscated code then that's up to them 
 -- you can't stop them.

This is a very wrong idea. It's the job of the language designers to avoid some syntax traps. So the designers need to find a good middle point between chaos and rigidity.
 If organisations want to enforce style guides 
 then that's up to them to manually enforce the style or produce tools to 
 do it automatically.

Most programmers don't use those tools or style guides, so those tools don't avoid you to find bad code in the modules you find in some public online repository. Style guides are not enough to avoid many troubles.
 Whatever criteria you select for distinguishing acceptable comma use 
 from unacceptable comma use will be subjective,

Is this true in the two cases I've shown? This is a little C program that compiles with no errors and not even warnings (with -Wall) with GCC 4.5.1: int main() { int x; x = 10, 20; return 0; } The same program compiled with DMD 2.051 produces this error: test.d(3): Error: long has no effect in expression (20)
 and inevitably some people will be angered by the decision.

That's what discussions and votes are for :-) (Despite some votes have more weight). Thank you for your comments, bye, bearophile
Dec 31 2010
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
bearophile wrote:

 This is a little C program that compiles with no errors and not even 

 int main() {
     int x;
     x = 10, 20;
     return 0;
 }

This is totally off topic but gcc's -Wall does not mean "all warning options": http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html I can't test it now, but -Wunused-value seems to be the option for the case above. Ali
Jan 01 2011
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
bearophile wrote:
 I think they are confusing, and may hide bugs.

This is, again, an example of what I was talking about earlier. I've never, ever seen this as an issue in 30 years of programming. There are plenty of problems that actually adversely affect programs one can work on. We don't need to spend time crafting solutions to problems that affect nobody. It's like building a set for a movie. The set is meant to be seen only from the angle the camera is set up at. Any other angle and the set looks completely fake, and it being a facade is obvious. The reason is simple - there's no reason to waste time and money making it look real from any angle other than that of the camera.
 The same program compiled with DMD 2.051 produces this error:
 test.d(3): Error: long has no effect in expression (20)

The reason dmd checks for that (and not the former) is that such are actual problems that often appear in real code and bedevil programmers. If you really want to be productive with this, rather than sitting back and thinking up imaginary problems, do things like peruse the bug database of a large open source project. Look for patterns of problems that might be headed off with language solutions.
Dec 31 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Walter:

 If you really want to be productive with this, rather than sitting back and 
 thinking up imaginary problems, do things like peruse the bug database of a 
 large open source project. Look for patterns of problems that might be headed 
 off with language solutions.

OK, I will try. Thank you for your answer. Bye, bearophile
Jan 01 2011