QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Dimster on July 04, 2019, 08:53:09 am

Title: Using the underscore
Post by: Dimster on July 04, 2019, 08:53:09 am
I was wondering about the usage of the underscore at the end of a line. What advantage does it have and when exactly can/should it be used (ie only with an IF statement?)

x = 10

if x = 10 then_
print "X = ";x

SLEEP
IF x = 10 THEN PRINT "X = "; x

SLEEP

if x = 10 then_
print "x = 10"_
'print " X = ";x
'end if

SLEEP

DO
x = x + 1_
'print "X now equals ";x
LOOP UNTIL x = 10

SLEEP
DO UNTIL x = 10
if x = 5 then_
print "X now equals ";x
LOOP
SLEEP

Title: Re: Using the underscore
Post by: FellippeHeitor on July 04, 2019, 09:03:47 am
I use it when I have a line that's too long to fit my usual editing area, and yes, that usually means some long series of conditions in an IF block. Some examples from InForm's code:

Code: QB64: [Select]
  1.     IF This.Redraw OR This.ControlState <> TempControlState OR This.FocusState <> (__UI_Focus = This.ID) OR This.Value <> This.PreviousValue OR This.PreviousParentID <> This.ParentID _
  2.        OR __UI_ForceRedraw OR This.PreviousFont <> This.Font THEN

Code: QB64: [Select]
  1. IF Control(__UI_FirstSelectedID).Height - (Control(__UI_FirstSelectedID).BorderSize * ABS(Control(__UI_FirstSelectedID).HasBorder)) <> _HEIGHT(Control(__UI_FirstSelectedID).HelperCanvas) OR _
  2.                            Control(__UI_FirstSelectedID).Width - (Control(__UI_FirstSelectedID).BorderSize * ABS(Control(__UI_FirstSelectedID).HasBorder)) <> _WIDTH(Control(__UI_FirstSelectedID).HelperCanvas) THEN
Title: Re: Using the underscore
Post by: RhoSigma on July 04, 2019, 09:26:02 am
Or you may use it for long string concatenations, here some out of GuiTools:

Code: QB64: [Select]
  1. dummy$ = MessageBox$("Error16px.png", appExeName$,_
  2.                      "Error handler reports too many|" +_
  3.                      "recursive Errors !!|~" +_
  4.                      "Program will cleanup and terminate|" +_
  5.                      "via internal emergency exit.",_
  6.                      "{IMG Error16px.png 39}Ok, got it...")
  7.  

Code: QB64: [Select]
  1. Frame1$ = FrameC$("INIT",_
  2.           NewTag$("LEFT", "25") +_
  3.           NewTag$("TOP", "160") +_
  4.           NewTag$("WIDTH", "525") +_
  5.           NewTag$("HEIGHT", "250") +_
  6.           NewTag$("FORM", "solid") +_
  7.           NewTag$("TEXT", "Frame 1 (raised)") +_
  8.           NewTag$("TEXTPLACE", "topleft"))
  9.  

The only backdraw from using an underscore as line continuation is, that these constructs are no longer autoformatted by the IDE, you have to indent everything by yourself and also add all the space separators between variables, operators etc..
However, at least the compiler will not complain about misformatted lines as long as there are no other errors present.
Title: Re: Using the underscore
Post by: FellippeHeitor on July 04, 2019, 10:20:37 am
The only backdraw from using an underscore as line continuation is, that these constructs are no longer autoformatted by the IDE, you have to indent everything by yourself and also add all the space separators between variables, operators etc..
However, at least the compiler will not complain about misformatted lines as long as there are no other errors present.

Indeed, the IDE will skip autoformat in that case. My personal solution to that is: I write the long line as a single line and only after auto format takes place I break it down into multiple ones using the underscore.
Title: Re: Using the underscore
Post by: Dimster on July 04, 2019, 01:50:10 pm
The wiki also mentions the use of an underscore in the middle of a variable name, and multi underscores on the same line. What advantage is there to an underscore in a variable name? Is that basically a way of substituting the dash ( ie done-like-dinner$ = "My hotdog" v's done_like_dinner$ = "My hotdog") or is it referring to the use of multiple key QB64 words which begin with the underscore?