KEY TAKE AWAY POINTS:
Variable is your helper friend to remember something for you! i.e. What kind of thing (variable type), how much of such thing (variable value)
You always need to tell this friend to remember a thing as a start (variable initialization on start)
Most typical types of variables: Integer (numbering), Boolean (true/false), String (text message)
ROUND RECTANGLE & DIAMOND shapes in MakeCode blocks
START A BIT > Knowledge Base > This page
Good practices of variable:
Readable and understandable name
Initialize it before use it! Good with on-start
Typically 2 types of using:
Reader/Viewer/Consumer
Fetch and consume it as soon, since the value could be updated quickly by writer on the other hand
Writer/Changer/Producer
To change/update the value of variable, good to handle this job as accountable complete within a clear logical block of codes, avoid doing the change at different places where mixing other jobs or logic.
Be careful of conflict parallel handling of the same variable.
Create variable
Initialize variable
In many cases, we need to look into the value of a variable closer and check if it really does correctly or not.
This is very useful block for debugging to send real time data back to PC when the Micro:bit board is executing the program.
Advanced -> Serial ->
Reader user of variable
This example's variable "speed" allows valid range -100~100. As accountable program, we should take care of this, secure the right interfacing.
Here we list some common solutions from our students, please take a serious and good engineering mindset to handle this, keep things as neat and efficient!
NOT good example#1 ☹️
It let things be wrong first and correct afterwards, waste of actions, and redundant logic
NOT good example#2 ☹️
It lets things be wrong here, but correct it later elsewhere, besides the drawbacks from example#1, it also creates a mess for variable being changed here and there, confusing logic to follow and maintain.