Author Topic: Using the underscore  (Read 2753 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Using the underscore
« 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


FellippeHeitor

  • Guest
Re: Using the underscore
« Reply #1 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

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Using the underscore
« Reply #2 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.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

FellippeHeitor

  • Guest
Re: Using the underscore
« Reply #3 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.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Using the underscore
« Reply #4 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?