Scilab Coding Style

Some rules to write Scilab macros...These rules will be the basis for Scilab 6.0 pretty printer.

Scilab 5.x

Character string declaration

Only use double-quotes to define character strings. Simple quotes will only be available in Scilab 6.0 for ascendant compatibility.

mystr = "mystr";

No more use function ''deff'' where ''function''/''end'' can be used

No more use function deff, this function makes the code unreadable and will be removed in a future version. Only use function/end to declare functions. If you need to declare a function with a generated declaration, use execstr.

   1 // This code is OK
   2 function foo()
   3   disp("hello")
   4 end
   5 
   6 // This code is not OK
   7 generatedcode = "disp(""hello"")";
   8 execstr("function toto();" + generatedcode + ";endfunction");

Rationale

The use of the "execstr" function makes the scripts very difficult to understand and to maintain. Indeed, adding a new statement in the function forces to manage the double quotes in complicated ways, which leads to bugs.

Matrix declaration

While defining matrices, use commas between row elements and semi-colons between rows.

   1 // This code is OK
   2 mymatrix = [1,2,3;4,5,6];
   3 myothermatrix = [1,2,3; ..
   4                  4,5,6];
   5 
   6 // This code is not OK
   7 mymatrix = [1 2 3
   8             4 5 6];

One statement per line

For code readability, only write one statement per line:

   1 // This code is OK
   2 a = 1;
   3 b = 2;
   4 c = 3;
   5 
   6 // This code is not OK
   7 a = 1;b = 2;c = 3;

Rationale

Semi-colon at the end of each statement

In a function, each statement must finish with a semi-colon. In a script, the usage of a semi-colon at the end of a line is a mean to manage display of execution results.

   1 // This code is OK
   2 a = 1;
   3 b = 2;
   4 c = 3;
   5 
   6 // This code is not OK
   7 a = 1,
   8 b = 2
   9 c = 3

Loops/Conditions

For code readability, "if" blocks, "for" loops, "while" loops must be written in at least three lines as in :

   1 // This code is OK
   2 if part(XNAME,k)==',' then
   3   index_commas=[index_commas,k];
   4 end
   5 
   6 // This code is not OK
   7 if part(XNAME,k)==',' then index_commas=[index_commas,k],end

Rationale

These rules have to be applied inside macros you just modify.

Scilab 6.0

Function declaration using ''function'' and ''end''

Only use end keyword to close a function declaration (endfunction will only be kept for ascendant compatibility)

Discussion

public: Scilab_Coding_Style (last edited 2019-01-05 16:07:23 by static-176-158-62-11)