Howto:Start using vectors and hashes in Nasal: Difference between revisions

Jump to navigation Jump to search
m
→‎Learn more about vectors: https://gitorious.org/fg/fgdata/source/55a9523d833a9c65bab431e2cffcf99d09c78dd4:Aircraft/777/Nasal/efb.nas#L137-1890
m (→‎A hash based version (recommended): http://forum.flightgear.org/viewtopic.php?f=71&t=23204&p=211634#p211629)
m (→‎Learn more about vectors: https://gitorious.org/fg/fgdata/source/55a9523d833a9c65bab431e2cffcf99d09c78dd4:Aircraft/777/Nasal/efb.nas#L137-1890)
Line 51: Line 51:


Just imagine we'd need to support more than just 5 waypoints, like maybe 10, 20, 50 or maybe even 1000 waypoints. As you can see, this method is very inflexible, complicated, tedious and error-prone.
Just imagine we'd need to support more than just 5 waypoints, like maybe 10, 20, 50 or maybe even 1000 waypoints. As you can see, this method is very inflexible, complicated, tedious and error-prone.
Such code contains so called "code smells", another example would be code like this:
<syntaxhighlight lang="php">
var l0 = "";
var l1 = "";
var l2 = "";
var l3 = "";
var l4 = "";
var l5 = "";
var l6 = "";
var l7 = "";
var l8 = "";
var l9 = "";
var l10 = "";
var l11 = "";
var l12 = "";
var l13 = "";
var l14 = "";
var l15 = "";
var l16 = "";
var l17 = "";
var l18 = "";
var l19 = "";
var l20 = "";
var hist1 = "";
var hist2 = "";
var hist3 = "";
var hist4 = "";
var hist5 = "";
var hist6 = "";
IDENTl3 = "";
IDENTl4 = "";
IDENTr4 = "";
IDENTl5 = "";
IDENTr5 = "";
IDENTl8 = "";
setprop("/instrumentation/efb/display/line0-l", l0);
setprop("/instrumentation/efb/display/line1-l", l1);
setprop("/instrumentation/efb/display/line2-l", l2);
setprop("/instrumentation/efb/display/line3-l", l3);
setprop("/instrumentation/efb/display/line4-l", l4);
setprop("/instrumentation/efb/display/line5-l", l5);
setprop("/instrumentation/efb/display/line6-l", l6);
setprop("/instrumentation/efb/display/line7-l", l7);
setprop("/instrumentation/efb/display/line8-l", l8);
setprop("/instrumentation/efb/display/line9-l", l9);
setprop("/instrumentation/efb/display/line10-l", l10);
setprop("/instrumentation/efb/display/line11-l", l11);
setprop("/instrumentation/efb/display/line12-l", l12);
setprop("/instrumentation/efb/display/line13-l", l13);
setprop("/instrumentation/efb/display/line14-l", l14);
setprop("/instrumentation/efb/display/line15-l", l15);
setprop("/instrumentation/efb/display/line16-l", l16);
setprop("/instrumentation/efb/display/line17-l", l17);
setprop("/instrumentation/efb/display/line18-l", l18);
setprop("/instrumentation/efb/display/line19-l", l19);
setprop("/instrumentation/efb/display/line20-l", l20);
</syntaxhighlight>
Whenever you see such code, it should be obvious that using arrays/vectors would be a good idea.


So, it would be better to use a vector of waypoints instead. A vector is a list of things (variables) that can be easily accessed using an index into the vector. Pretty much like an array in C or C++, with the added advantage that the vector can be dynamically resized, e.g. using the setsize() library call. Consider the following example:
So, it would be better to use a vector of waypoints instead. A vector is a list of things (variables) that can be easily accessed using an index into the vector. Pretty much like an array in C or C++, with the added advantage that the vector can be dynamically resized, e.g. using the setsize() library call. Consider the following example:

Navigation menu