User:Www2/temp: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 38: Line 38:
This applies to both Nasal and XML. I will use XML in this example.
This applies to both Nasal and XML. I will use XML in this example.
Use spaces to line up code within a code block; that is:
Use spaces to line up code within a code block; that is:
<syntaxhighlight lang="XML">
<filter>
<name>VS-target-cmd</name>
<type>gain</type>
<gain>1</gain>
<update-interval-secs type="double">0.05</update-interval-secs>
<input>
<expression>
<table>
<property>/it-autoflight/internal/vert-speed-fpm</property>
<entry><ind>-50000</ind><dep> -750</dep></entry>
<entry><ind> -2000</ind><dep> -750</dep></entry>
<entry><ind> -1000</ind><dep> -250</dep></entry>
<entry><ind>    0</ind><dep>    0</dep></entry>
<entry><ind>  1000</ind><dep>  810</dep></entry>
<entry><ind>  2000</ind><dep> 1640</dep></entry>
<entry><ind>  3000</ind><dep> 2100</dep></entry>
<entry><ind>  4000</ind><dep> 2440</dep></entry>
<entry><ind>  5000</ind><dep> 3000</dep></entry>
<entry><ind> 10000</ind><dep> 5000</dep></entry>
<entry><ind> 50000</ind><dep>50000</dep></entry>
</table>
</expression>
</input>
<output>/systems/pressurization/targetvs-cmd</output>
</filter>
</syntaxhighlight>
Look particularly at the <table> part. Note how the last line lines up. This makes the code easy to read.

Revision as of 10:17, 6 July 2017

# Checks whether we should create ice
# Icing conditions: freezing fog or low temp and below dp or in advanced wx cloud

var spread = temperature - dewpoint; 
if ((spread < maxSpread and temperature < 0) or (temperature < 0 and visibility < 1000) or (visibLclWx < 5000 and temperature < 0)) { 
# we use two visibility properties: one is for basic wx and the other is for advanced wx 		
   setprop("/systems/icing/icingcond", 1);
} else {
   setprop("/systems/icing/icingcond", 0);
}

Spaces vs Tabs

This has divided many developers! I feel the best method is to use tabs at the start of a line and to use spaces within a line. For example:

# we use spaces within the line to make it easier to read
if ((spread < maxSpread and temperature < 0) or (temperature < 0 and visibility < 1000) or (visibLclWx < 5000 and temperature < 0)) { 
# we tabs to move the "true" and "false" statement to the second column	
   setprop("/systems/icing/icingcond", 1);
} else {
   setprop("/systems/icing/icingcond", 0);
}

Another example:

var abc = 1;
var def = getprop("/abc/def/ghi");
# Note that there is a space around the '='!

Whichever method you choose, be consistent throughout your coding!

Lining up code

This applies to both Nasal and XML. I will use XML in this example. Use spaces to line up code within a code block; that is: