Lasso Soft Inc. > Home

  • Articles

Best Practices

This page is a summary of best practices for coding Lasso sites. It contains tips about how to code your site so that you can catch errors easily and create the most efficient code possible.

Conditionals

Place the literal value in comparisons on the left side of the operator. If you accidentally mistype the operator as a single = equal sign then Lasso will catch the error.

  [If('literal' == $mystring)] ... [/if]  Best practice
  [If('literal' = $mystring)] ... [/if]  Typo generates error

If the literal is on the right side of the variable and you accidentally mistype the operator as a single = equal sign then the variable will be assigned the value of the literal and no error will be generated. The conditional will be passed a value of Null or False.

  [If($mystring == 'literal')] ... [/if]  Common practice
  [If($mystring = 'literal')] ... [/if]  Typo modifies $mystring variable

Testing against multiples values

If you need to check a value against several other values, there is a better way than :

  If: $mystring == 'literal' || $mystring == 'literal2' || $mystring == 'literal3' || $mystring == 'literal4';
  ...
 /if;

You can group the allowed values in an array like this :

  If: array('literal', 'literal2', litteral3') >> $mystring;
  ...
  /if;

or even better, lets you use dynamic conditional tests :

  var('allowed_values') = array('literal', 'literal2', litteral3');
  If: $allowed_values >> $mystring;
  ...
  /if;

Setting and returning variables

A variable is created using the [Variable]- or [Var]-tag:

  Variable: 'VariableName' = 'SomeValue';
  Var: 'VariableName' = 'SomeValue';

Once a variable is created it can be set and retrieved using that same tag, but it is better to use the shorthand metod, $VariableName. The reason being that in case of a typo, the first method creates a new variable which can lead to difficult debugging, whereas the shorthand method throws an error:

  var: 'VariableName' = '';
  ...
  $VariableName = 'Hello world!';  Best practice
  Output: $Variable_Name;  Typo generates error


  var: 'VariableName' = '';
  ...
  var: 'VariableName' = 'Hello world!';
  Output: (var: 'Variable_Name');  Typo creates a new variable

Author: FLetcher Sandbeck
Created: 18 Feb 2008
Last Modified: 3 Mar 2011

Comments

Use Lasso 9 syntax

These are good and valid tips even in the Lasso 9 era. But why not use a Lasso 9 savvy syntax and skip colons...



if('literal' == $mystring) => {} -> Best practice
if('literal' = $mystring) => {} -> Typo generates error

if($mystring == 'literal') => {} -> Common practice
if($mystring = 'literal') => {} -> Modifies $mystring variable

if($mystring == 'literal' || $mystring == 'literal2' || $mystring == 'literal3' || $mystring == 'literal4') => {}

if(array('literal', 'literal2', litteral3') >> $mystring) => {}

var(allowed_values) = array('literal', 'literal2', litteral3')
if($allowed_values >> $mystring)

A variable is created using the [Variable]- or or better [Var]-method:

variable(variablename = 'somevalue')
var(variablename = 'somevalue')
var(variablename) = 'somevalue'


var(variablename = '')
...
$variablename = 'Hello world!' -> Best practice
$variable_name -> Typo generates error

var(variablename = '')
...
var(variablename = 'Hello world!') -> Bad practice
var(variable_name) -> Typo creates a new variable

by Jolle Carlestam, 04 May 2011

You must be logged in to comment.

Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft