I am trying to learn I2C from this website https://forum.dronebotworkshop.com/arduino/i2c-part-one-tutorial-and-slave-demo-sketch-for-platformio/. In the website section "Slave Demo Sketch" (Arduino), there is one line code that I don't understand.
What is type of Byte? What does the byte inside the brackets mean?
for (byte i=0; i<ANSWERSIZE; i++) {
response[i] = (byte)answer.charAt(i);
}
First of all, type naming is somewhat subjective, though wide consensus exists.
Various sketchy, home-brewed types tend to exist in some libraries. byte
, BYTE
, U8
and other such non-standard types. These are almost always just some flavour of typedef unsigned char slop;
, which is a completely useless typedef
.
Some of the cornerstones of good programming practices:
Weird typedefs like byte
go against both of these practices. In addition "I don't like typing" isn't a valid argument - programming is all about typing, those who don't like it or who can't figure out how copy/paste or code completion works picked the wrong trade. Besides, if you can't type uint8_t
in around 1 second, it might be an indication that you need to practice typing on a keyboard way more, at least if you are serious about the programmer trade.
The only time when you should be using such non-standard types is when the existing code base is truly hellbent on using them and a lot of code like that has already been written. Using a different, although more correct type might just make the code more confusing at that point.
Good practice is to use the standardized types whenever possible, instead of inventing your own non-standard. Some rules of thumb of below:
Acceptable byte types:
uint8_t
(highly recommended)unsigned char
Acceptable boolean types:
bool
with false
and true
(highly recommended)_Bool
with false
and true
(C only, not C++)