Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #25015

Re: XC8 novice question

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.arch.embedded
Subject Re: XC8 novice question
Date 2016-09-12 14:35 +0200
Organization A noiseless patient Spider
Message-ID <nr67eb$1t4$1@dont-email.me> (permalink)
References (4 earlier) <nr3pu9$2ro$1@dont-email.me> <nr3res$87d$1@dont-email.me> <nr4e1a$bb8$2@dont-email.me> <nr5jgd$3i8$1@dont-email.me> <nr5t7a$ci$1@dont-email.me>

Show all headers | View raw


On 12/09/16 11:41, Don Y wrote:
> On 9/11/2016 11:55 PM, David Brown wrote:
>> On 11/09/16 22:15, Don Y wrote:
>>> On 9/11/2016 7:58 AM, David Brown wrote:
>>>> Well, clearly he needs to pick a better name for his "variable".  I
>>>> don't know
>>>> if he just picked a bad name, or if he is confused about variables and
>>>> constants.
>>>>
>>>> But if "my_variable" is declared as a "const", then writing
>>>> "my_variable = 7"
>>>> will give a compile-time error just as surely as it would with a
>>>> pre-processor
>>>> symbol.
>>>
>>> Yes, but IME much easier to see whay you've done wrong (looking at the
>>> cpp(1) output).
>>>
>>> Similar to writing:
>>>      if (4 == count)...
>>> instead of:
>>>      if (count == 4)...
>>
>> Well, there may be differences of opinion here - and certainly
>> experience and preferences are personal, making it hard to say "you are
>> wrong".
>>
>> But you are wrong.
>>
>> Writing "if (4 == count)" is wrong.  The last thing you need in your
>> code is to write it backwards - it messes with your thinking, your
>> writing, your reading and your understanding.
>>
>> There are only three reasons why you would want to write "if (4 ==
>> count)".  Either /all/ of these are true:
> 
> No, the answer is much simpler than any of that!
> 
> I started writing code in C (almost exactly) 40 years ago.  Owning
> my first "personal" SysV machine more than 30 years ago.
> 
> Compilers "back then" were far less "helpful" than what folks
> are used to using, today.  And, far less "accessible" (imagine
> writing your code on a piece of PAPER and frantically typing
> it in when you get a chance -- so you can actually RUN it a few
> times, TODAY).  Does your pen/paper "run lint"?

Yes, long ago compilers had little in the way of helpful warnings or
checks.  And long ago, lint was not as good (though it's history is
nearly 40 years old), and not as easily available.

But /today/, there is no reason not to have reasonable error checking
and warnings on your compiler.  And if you compiler doesn't do a good
enough job by itself, you can use PC-Lint, splint, or even gcc/clang for
a different target in order to get good checking.  (It's not needed here
- XC8 will report "if (count = 4)" errors.)

> 
> [Do you remember when C didn't have a "void" type?  Or, using '\!'
> and '\!!' in your sources?  Or, when members of one type of
> struct had to have different names from any members of any
> OTHER structs (i.e., all tags shared a SINGLE namespace)?]

The earliest C I have worked with is C89/C90/ANSI.  It is fortunately
rare that I have to work with such old tools, and I think it would be
madness to limit my modern programming to suit such tools.

> 
> And, over the years, have collected a bunch of compilers for
> oddball processors where the capabilities are essentially
> "take it or leave it -- no one else HAS a compiler for this
> processor!".  If all your development has been for mainstream
> products, you're probably "spoiled" by the tools available!

I have products for which there are /no/ decent C compilers, or at least
products that could never have been made if we used the available
compilers.  And I have products where the only choices are poor or
limited compilers.  But for the most part, quality of tools is an
important criteria when choosing a microcontroller.

> 
> I would NOT, for example, write:
>     x=-1;

Neither would I - I would write "x = -1;".  But I believe I am missing
your point with this example.

> And, I *would* write:
>     p = &buffer[0];

So would I - because I think the code looks clearer.  When I want p to
be a pointer to the first element of "buffer", that's what I write.

> I'd use parens in places that you'd consider superfluous -- but that
> made bindings very obvious (cuz the compiler would do what I *told* it
> without suggesting that I might be doing something unintended):

Me too.  But that's for clarity of code for both the reader and the
writer - not because I worry that my tools don't follow the rules of C.

>     y = x / (*p);

Me too.

>     (*f)();

I'd write

	f();

Though I'd use a better name than "f" for a function pointer, and I try
to avoid function pointers where possible.

> Or, lengthy expressions laid out as:
>     foo = ( a + b )
>         / ( c * d )
>         - ( e % f );
> 

If the expression were complicated enough, I would write:

	int x = a + b;
	int y = c * d;
	int z = e % f;
	foo = x / y - z;


> And, to catch (some) '=' instead of "==" errors, write tests as:
>     if ( CONSTANT == expression ) ...

No, I would /never/ write that.  I would write it in a way that makes
logical sense when reading or writing.  You would say "if the number of
apples is 3, the price is $1" - you would never say "if 3 is the number
of the apples, $1 is the price".

Thus I would always write:

	if (expression == constant) { .... }

(I'm not a fan of shouting either - I left all-caps behind with BASIC
long ago.)

What benefit do you think "if (CONSTANT == expression)" gives you?  Why
is it useful to invert the logic and make code hard to read, just in
case you make a mistake and write "if (expression = CONSTANT)"?  It is
highly unlikely that you would make such a mistake, and even less likely
that you would make it and not find it quickly - and you compiler (or
linter) will spot it for you.

> 
> [would you consider "if (foo==bar())" as preferred to "if (bar()==foo)"?]
> 

I'd usually try to avoid a function call inside an expression inside a
conditional, but if I did then the guiding principle is to make it clear
to read and understand.

> Much of this is a consequence of how I learned to "read" (subvocalize)
> code to myself:
>     x = y;
> is "x gets the value of y", not "x equals y".  (how would you read x ::=
> y ?)
>     if ( CONSTANT == expression )
> is "if expression yields CONSTANT".  Catching myself saying "if CONSTANT
> gets
> expression" -- or "if variable gets expression":
>     if ( CONSTANT = expression )
>     if ( variable = expression )
> is a red flag that I'm PROBABLY not doing what I want to do.
> 
> I'll wager that most folks read:
>     x = y;
> as:
>     x equals y

I can't say I vocalize code directly - how does one read things like
brackets or operators like -> ?  But "x = y" means "set x equal to y".

> and subtly change their intent, but NOT their words, when encountering:
>     if ( x == y )
> as:
>     if x equals y

"if x equals y", or "if x is equal to y".

> (how would they read "if ( x = y )"?)

I read it as "warning: suggest parentheses around assignment used as
truth value" :-)

It's simply a mistake, and I would not read it at all without mentioning
the error.

> 
> People reading my code never seem to have any problem understanding
> that:
>    if ( NULL == malloc(...) )
> is testing for a NULL result from malloc (i.e., "malloc yielding NULL").
> 

Certainly people will know what the code does.  That does not mean it is
the clearest choice of code for the job.

> [They also don't seem to find parallel loading a (hardware) counter with
> 0 as the same as "clearing" it.]
> 
> This sort of "reading" makes it easy to discuss:
>      (x, y) := cursor_position();
> ("instantiate an x,y tuple with the current cursor position") vs.
>      (x, y) = cursor_position();
> ("the x,y tuple gets the current cursor position") or:
>      x = cursor_X_coordinate();
> ("x gets the cursor's X coordinate")
> 
> What's important is consistency.  It takes very little time for someone to
> pick up on a particular style -- if it is used CONSISTENTLY throughout a
> document/project.  Ever notice code that walks an array using an index
> into the array; then, somewhere else, a pointer into it?  As if the
> decision was arbitrary (or, made by someone other than the original
> developer -- having a different style?)  Or, that refers to screen
> coordinates
> as "(x,y)" in some places, "(row,column)" in others (did you notice that
> row,column is really y,x?) and "(r,c)" in still others?
> 
> You can read my code over the years and see the same style propagated
> throughout (discounting changes to the language, as alluded to earlier).
> It doesn't change when the "style du jour" comes -- and goes! -- along
> the way!  (though identifiers have lengthened as the tools have supported
> them)
> 

My code style has been fairly similar over the 20 years I have been
professionally programming C, as the guiding principles have been
consistent.  But a number of aspects have changed over time - when a
change of style has made sense and led to better or clearer code, I have
made that change.  And when newer and better tools or language features
have allowed better or clearer code, then I have embraced those features
and changes.  I certainly don't believe I started my career with a
perfect style that must be maintained for ever.

Consistency is a good thing - there's no doubt there.  But it is not the
overriding concern in all cases - breaking with old habits is necessary
in order to improve.


And remember, in this thread and context, we are giving advice to a new
programmer.  It is one thing to say that /you/ want to write "if (4 ==
count)" to remain consistent with the vast quantities of code you have
written over the last 40 years.  It is entirely different to consider
how this /new/ programmer should write the vast quantities of code he
will produce over the /next/ 40 years.  /You/ have to live with the
decisions from your past - the new programmer does not.

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

XC8 novice question rwood <rwoodl@nowhere.com> - 2016-09-11 15:02 +0800
  Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-11 02:55 -0700
    Re: XC8 novice question rwood <rwoodl@nowhere.com> - 2016-09-11 18:25 +0800
      Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-11 13:35 +0200
        Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-11 07:32 -0700
          Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-11 16:58 +0200
            Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-11 13:15 -0700
              Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-12 08:55 +0200
                Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-12 02:41 -0700
                Re: XC8 novice question Mike Perkins <spam@spam.com> - 2016-09-12 13:16 +0100
                Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-12 14:56 +0200
                Re: XC8 novice question Mike Perkins <spam@spam.com> - 2016-09-12 14:33 +0100
                Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-12 16:11 +0200
                Re: XC8 novice question Jack <jack4747@gmail.com> - 2016-09-12 07:50 -0700
                Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-12 22:23 +0200
                Re: XC8 novice question Les Cargill <lcargill99@comcast.com> - 2016-09-12 15:34 -0500
                Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-12 08:44 -0700
                Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-12 14:35 +0200
                Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-12 08:19 -0700
                Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-12 23:11 +0200
                Re: XC8 novice question Dennis <dennis@none.none> - 2016-09-12 17:04 -0500
                Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-12 22:49 -0700
                Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-13 09:04 +0200
                Re: XC8 novice question Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2016-09-13 00:16 +0200
                Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-12 21:22 -0700
                Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-13 12:25 +0200
                Re: XC8 novice question Dennis <dennis@none.none> - 2016-09-13 10:22 -0500
                Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-13 20:08 +0200
            Re: XC8 novice question rwood <rwoodl@nowhere.com> - 2016-09-12 16:50 +0800
          Re: XC8 novice question Tom Gardner <spamjunk@blueyonder.co.uk> - 2016-09-11 17:13 +0100
            Re: XC8 novice question Tom Gardner <spamjunk@blueyonder.co.uk> - 2016-09-11 17:19 +0100
              Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-12 09:00 +0200
      Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-11 07:28 -0700
    Re: XC8 novice question upsidedown@downunder.com - 2016-09-11 13:56 +0300
      Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-11 13:28 +0200
      Re: XC8 novice question Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2016-09-11 13:36 +0200
        Re: XC8 novice question upsidedown@downunder.com - 2016-09-11 16:14 +0300
          Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-11 07:57 -0700
          Re: XC8 novice question Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2016-09-12 22:52 +0200
            Re: XC8 novice question upsidedown@downunder.com - 2016-09-13 10:11 +0300
              Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-13 01:11 -0700
                Re: XC8 novice question upsidedown@downunder.com - 2016-09-13 13:24 +0300
                Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-13 12:39 +0200
                Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-13 15:41 -0700
                Re: XC8 novice question upsidedown@downunder.com - 2016-09-14 09:07 +0300
                Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-13 23:29 -0700
      Re: XC8 novice question Don Y <blockedofcourse@foo.invalid> - 2016-09-11 07:50 -0700
  Re: XC8 novice question Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2016-09-11 13:42 +0200
  Re: XC8 novice question Tim Wescott <tim@seemywebsite.com> - 2016-09-11 20:14 -0500
  Re: XC8 novice question rwood <rwoodl@nowhere.com> - 2016-09-12 16:54 +0800
    Re: XC8 novice question David Brown <david.brown@hesbynett.no> - 2016-09-12 11:05 +0200
      Re: XC8 novice question rwood <rwoodl@nowhere.com> - 2016-09-12 18:48 +0800
  Re: XC8 novice question John Temples <usenet@xargs-spam.com> - 2016-09-12 17:33 +0000

csiph-web