Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
daveatkins
Partner - Creator III
Partner - Creator III

fundamental programming questions: semicolon and assignment

Sorry to ask a dumb questions or two but:

1) is a semicolon recommended to terminate every line? I have found cases where code seems to work without it but lots of inconsistency. The lack of strictness promotes laziness in coding of which I am guilty but I would like to know if there is some purpose or rule.

2) is it best practice to always assign variables with LET x = whatever? I am familiar with the confusing LET vs SET distinction and how that can be a "feature" but it seems that one can simply say x = 1 and this works as fine as LET x = 1. What is the default behavior; i.e. I say thisDate = today(); or LET thisDay=today()?  

1 Solution

Accepted Solutions
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

1) Script regular statements must be terminated by a semicolon.  Script control statements, such as CALL,  may be terminated by either a semicolon or end of line.  Also the contents of a control statement must be contained within one line. 

So semicolons are only optional in control statements, which is a very small number (or zero) of lines in a typical  script. I'm not sure why control statements were designed with a different restriction -- perhaps someone else has an explanation.   It was years before I even knew that you could use a semicolon on a control statement.  I teach beginners to use semicolons on control statements but point out that they are optional because they are likely to encounter script without them.

2) "thisDate = today()" is the same as "Let thisDate = today()".  In my opinion it's a best practice to always include the "Let" so others don't have to wonder what the behavior is.  Just my opinion.

-Rob
http://www.easyqlik.com
http://masterssummit.com
http://qlikviewcookbook.com

 

 

 

View solution in original post

3 Replies
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

1) Script regular statements must be terminated by a semicolon.  Script control statements, such as CALL,  may be terminated by either a semicolon or end of line.  Also the contents of a control statement must be contained within one line. 

So semicolons are only optional in control statements, which is a very small number (or zero) of lines in a typical  script. I'm not sure why control statements were designed with a different restriction -- perhaps someone else has an explanation.   It was years before I even knew that you could use a semicolon on a control statement.  I teach beginners to use semicolons on control statements but point out that they are optional because they are likely to encounter script without them.

2) "thisDate = today()" is the same as "Let thisDate = today()".  In my opinion it's a best practice to always include the "Let" so others don't have to wonder what the behavior is.  Just my opinion.

-Rob
http://www.easyqlik.com
http://masterssummit.com
http://qlikviewcookbook.com

 

 

 

daveatkins
Partner - Creator III
Partner - Creator III
Author

thanks; my main questions were around control statements while/for loops, etc. which I had seen with and without semicolons. I was thinking that the control statement was essentially starting a kind of unpunctuated block...but if the semi is recommended then stuff like this is valid and recommended?:

FOR i = 1 to 10;

LET value = PEEK($(tablename),$(i));

LET resultStr = resultStr & ', ' & value & '<BR \>';

NEXT;

The whole let thing also seems weird but this is part of the mystery of qlik coding.

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

The semicolons on control statements do look a bit silly when you understand the complete picture.  My motivation for teaching beginners to include them is twofold:

1) Beginners are frequently confused between "control statements" and "regular statements".  So I tell them for now,  just code the semicolon and will spend our class time on something more useful.  😉

2) I'm hoping that if we all use semicolons, I can convince Qlik to allow control statements to span lines so I can write statements like:

CALL MySub (
  parm1, 
  parm2
);

So not really a recommendation. More like if I have to fall off the fence one way or another, that's why I fall the way I do. 

-Rob