Pages: 1 [2]   Go Down
Author Topic: Best coding practice.  (Read 5096 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have a link to the promotion to int for byte that does not happen to bool?

Sorry, I do not.  My gut tells me the standard leaves that to the compiler folks to decide.

Quote
Do you have a link that explains the optimizations that can happen?

Nope.  Don't have that either.

I have a small amount of personal experience (but no examples at hand; too much time has passed).
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

bool, true, and false are all part of C++, but not part of C, [citation needed]
I think the text I quoted states that: "bool, true, and false are all part of C++" and "boolean, true, and false are all part of C (since C99)"
Interesting. C99 is, unfortunately, still not used all that frequently, which makes me not really think of it as C (although I'm not blaming anyone for doing so; it is indeed C)

Quote
It's just that you said the arduino files defined them, which is not true.
Can you please quote my statement that makes you think I said so?
Quote
in arduino, bool maps to _Bool which is a native GCC type (but I'm not sure what it translates to)"

Quote
boolean is completely different and I don't see why anyone would ever use it.
Wikipedia states "C++ has a separate Boolean data type ('bool'), but with automatic conversions from scalar and pointer values that are very similar to those of C."
You state "completely different" wikipedia states "very similar". Please take up your concerns with wikipedia so we all can learn?
wikipedia is talking about `bool' not `boolean.' Also, even if wikipedia was talking about `bool' it says ``very similar'' in respect to a different topic --- C vs C++, not bool vs int. Context is always relevant.

Quote
Wikipedia states "One problem with this approach is that the tests if(t==TRUE){...} and if(t) are not equivalent."
Which I think is a really good reason not to use bool.
They're the same if `t' is a bool (assuming the capitalization issue is fixed). They may not be the same if `t' is something else (like an int), which I think is a really good reason to use bool.

---

I'm not trying to be antagonistic; I just like correct things. And discussion.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I had hoped to learn something following this thread, but I am more confused now then before this thread was started.  smiley-confuse

This topic has descended into a discussion of the nuances of the two languages so it's not surprising there isn't much to take away.  In other words, I suspect we're all confused.   smiley-wink

Quote
So can someone explain it in simple words that a hardware guy could understand? What must I come away with from this topic that is important to my writing 'better' code in the future?

By far, the most important thing is to write code that your future self can understand and maintain.  Don't use something just because someone else claims it is the right thing to do.  Very sparingly use things that you don't understand well.  Write code that you can quickly understand.  In other words, know thyself and code for thyself.  Doing so helps now with correctness and debugging and helps later with maintenance.

The simple fact is that bool, byte, and boolean are almost perfectly interchangeable.  true is almost perfectly interchangeable with 1 and false is almost perfectly interchangeable with 0.  They are all just a means to an end.  Use whatever works best to help you understand the code now and in the future.

Quote
What kind of unintended bugs could I put into my sketch if I misunderstand what is trying to be communicated here.

I can't think of any.
« Last Edit: September 16, 2012, 03:15:13 am by Coding Badly » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks CB, I was afraid I was missing out of something important if not useful.
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1899
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I had hoped to learn something following this thread, but I am more confused now then before this thread was started.  smiley-confuse

This topic has descended into a discussion of the nuances of the two languages so it's not surprising there isn't much to take away.  In other words, I suspect we're all confused.   smiley-wink

Quote
So can someone explain it in simple words that a hardware guy could understand? What must I come away with from this topic that is important to my writing 'better' code in the future?

By far, the most important thing is to write code that your future self can understand and maintain.  Don't use something just because someone else claims it is the right thing to do.  Very sparingly use things that you don't understand well.  Write code that you can quickly understand.  In other words, know thyself and code for thyself.  Doing so helps now with correctness and debugging and helps later with maintenance.

The simple fact is that bool, byte, and boolean are almost perfectly interchangeable.  true is almost perfectly interchangeable with 1 and false is almost perfectly interchangeable with 0.  They are all just a means to an end.  Use whatever works best to help you understand the code now and in the future.

Quote
What kind of unintended bugs could I put into my sketch if I misunderstand what is trying to be communicated here.

I can't think of any.

I fullly agree with this one. That is why I stated several times it is "splitting hairs"
Best regards
Jantje

@WizenedEE
I'm not trying to be antagonistic; I just like correct things. And discussion.
I to like correct things. That is why I don't like someone putting words in my mouth which I have not stated.
saying "in arduino, bool maps to _Bool which is a native GCC type (but I'm not sure what it translates to)"
is not the same as saying "the arduino files defined them"

Anyone who likes correct things should know so.
Jantje
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not trying to be antagonistic; I just like correct things. And discussion.
I to like correct things. That is why I don't like someone putting words in my mouth which I have not stated.
saying "in arduino, bool maps to _Bool which is a native GCC type (but I'm not sure what it translates to)"
is not the same as saying "the arduino files defined them"

Anyone who likes correct things should know so.
Can you lay off the attacks please? Let's try to keep the discussion civil.

The arduino distribution includes a lot of things. Think of it as a house. One part of the house is the compiler itself. Another is avr-libc. Think of both of those as rooms with those names. The other rooms are frequently just called "arduino," just like the house as a whole. So, when one says something is part of "arduino" they generally don't mean "part of the compiler" or "part of avr-libc." So I hope you'll understand my confusion and not think I'm "Putting words in your mouth" --- that's how I understood them.

Also, I tend to think of avr-libc as part of gcc (while that is not entirely correct, they are almost always together). So, to me, "a native GCC type" could mean one that's defined in avr-libc or gcc (although I see now that "native" probably means it's part of the compiler itself). Thus, when one says that something is mapped to a native gcc type, they generally mean that it's mapped outside of gcc itself, since the internals are largely irrelevant in this context.
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1833
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

_Bool is a C99 type, bool is a C++ type.  Both take a single byte, same as unsigned char, signed char, and char data types.  Note, _Bool is C only and bool is C++ only, unless some other include file has done a #define to map one into another.  Since the Arduino IDE uses C++ for compiling .ino/.pde files, you should use the 'bool' type.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Speaking of splitting hair... Oddly, the arduino ide (I've tried 1.0.1) highlights boolean but not bool...
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5341
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

bools are bools.

They shouldn't really be interchangable with ints, it's one of the language compromises made for backwards compatibility with C.

I think you code will improve if you treat them as if they weren't interchangable. The compiler is perfectly capable of optimizing away statments like "if (x==1)", it output the same code as "if (x)".

Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think your code will improve if you treat them as if they weren't interchangeable.

I totally agree.
Logged

Pages: 1 [2]   Go Up
Jump to: