Re: need help..



dot@xxxxxxx writes:
> On Sat, 02 Apr 2005 20:00:39 GMT, Keith Thompson <kst-u@xxxxxxx> wrote:
>
>>>>3) Quality compilers warn about the ``if (foo = 0)'' construct.
>>>
>>> It should be a syntax error.

Please don't snip nested attributions.

>>In a different language, sure. In C, an assignment is an expression
>>that yields the value assigned; changing that would break too much
>>existing code.
>
> If the compiler can detect it sufficiently to warn, it can hand out an
> error, stop compilation and force you to fix it. And how many perfectly
> good programs have come crashing down over this one simple error? Even
> worse when comparisons ( < and > ) are single characters... it's confusing.

You can't make "=" not yield a value without breaking existing
programs. Making an assignment invalid in a condition but valid
elsewhere would be an ugly wart.

I don't find the fact that '<' and '>' are single characters confusing
at all. They match mathematical notation.

> Frankly, it would have made more sense to have the testing done on a single
> =, & or | marks and use the doubles for assignment along with >> and <<
> which are used for bit shifts.
>
> Think about it... 1 letter: compare, 2 letters: perform an operation. (or
> visa versa) It would be totally consistent. As it is now the rule is 1
> letter does an operation EXCEPT for bit shifts, 2 letters does a test EXCEPT
> for less and greater. It's inconsistent and when learning I found it
> confusing as all get out.

I've never assumed that the number of characters in an operator has
any significance. I just learned what each one means.

> So I have a little header I use with a bunch of defines...
>
> #define is ==
> #define lt <
> #define gt >
> #define ne !=
> #define or ||
> #define and &&
> #define shiftl <<
> #define shiftr >>
> etc.
>
> Gives me syntax like this:
>
> if (x and (y lt 7))
>
> if ((x lt y) or (y is 7))
>
> No more silly mistakes.

If I were going to do something like that, I'd use "eq" rather than
"is" for equality. (Some languages distinguish between equality and
identity, and use "is" for the latter.) I'd also use the symbols in
<iso646.h> whenever possible, and try to be consistent with them
otherwise.

But I'd much rather just learn the language and use it. If I'm going
to read your code, I have to undertand the language *and* the macros
you've chosen. For this:

if (x && (y < 7))

if ((x < y) || (y == 7))

I just have to know the language.

You can use your macros if you like, but I guarantee they'll make it
more difficult for anyone else to read or maintain your code.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: need help..
    ... >>> It should be a syntax error. ... an assignment is an expression ... in the context of a new language. ... CLC FAQ ...
    (comp.lang.c)
  • Re: assignment expression peeve
    ... > in code is that is it resembles, to some degree, natural language. ... specific disagreement about your contention that assignment - as - ... described and about which other imperatives are expressed within the ...
    (comp.lang.python)
  • Re: IsString
    ... Thus assignment NEVER copes the object, it simply stores a pointer to the bound object in the part of the local namespace allocated to that name. ... Although it appears at first glance that there is a direct correspondence between hands and things, it is crucial to realise that the relationship is mediated by a holding - the hand identifies a particular holding, which in turn identifies a particular thing. ... There is in fact another layer of indirection - the programmer refers to hands using strings, but this is just part of the language used to express programs textually: the correspondence between these strings and the hands they refer to is called a manual. ...
    (comp.lang.python)
  • Re: Larkin, Power BASIC cannot be THAT good:
    ... some languages don't even provide assignment. ... Perhaps you will explain to us why Lisp does not provide assignment? ... What language properties / features causes this to be the case? ... lists are evaluated as function calls. ...
    (sci.electronics.design)
  • Re: FpgaC developers wanted :)
    ... in many languages this requires a first assignment ... to the state of "don't care" that may or may not be overridden. ... doesn't have an explict implicant, ... there is no support in the language for this. ...
    (comp.arch.fpga)