Then statement for ONLY single line IFs

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
OgreVorbis
User
User
Posts: 77
Joined: Thu Jan 16, 2020 10:47 pm

Then statement for ONLY single line IFs

Post by OgreVorbis »

I wouldn't be surprised if this was mentioned before, but I didn't find it. I know a macro could be used, but that's "hacky".
Is there a reason why the compiler doesn't support single line If ... Then? In other BASICs, you can write a simple one line IF statement. However, in PB, it either has to be multiline or concatenated like If [expression]: [do thing]: EndIf.

That's just not elegant or pure. Don't get me wrong, I'm not saying there should be a "then" statement used everywhere, but instead, if the compiler sees a "then" after an "If", it automatically knows it's a one line statement and the EndIf is implied. I would think it would be pretty trivial to add.

This is the ONLY aspect of PureBasic that I don't like.
My blog/software site: http://dosaidsoft.com/
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Then statement for ONLY single line IFs

Post by STARGÅTE »

PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
OgreVorbis
User
User
Posts: 77
Joined: Thu Jan 16, 2020 10:47 pm

Re: Then statement for ONLY single line IFs

Post by OgreVorbis »

Thanks. Nothing relevant came up when I searched for some reason.

I guess that one Macro is decent, but not perfect.
Seems like some people don't like it, but some do. I think it should still be included for the people who want it - others won't have to use it.

And no, I don't believe it to be "unclean" or a hangover from the past. For example, in C, you can compress single line If statements and not include the { } to make the code more compact. I see this as a similar thing, but in BASIC form.

It probably shouldn't work with else though. If you need an else on the same line, that would make the line needlessly long.
My blog/software site: http://dosaidsoft.com/
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Then statement for ONLY single line IFs

Post by BarryG »

STARGÅTE wrote: Sat Sep 18, 2021 9:30 pm If ... Then (17 Jun 2019)
IF with THEN to continue in a single line (10 Jul 2016)
Test-keyword for single-line if-constructs (04 Mar 2009)
And Tip: If/Then in PureBasic! (20 Mar 2006)
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Then statement for ONLY single line IFs

Post by BarryG »

BTW, having multiple statements on one line (with If/Then) can make it harder to debug your app when an unexpected crash occurs, because the compiler will highlight the entire If/Then line, so you won't always know which command in that line caused it. With one command per line, the exact line of the error is always shown.

[Edit] This code example demonstrates what I mean. When you run it, the error highlights the entire third line if you enter 0 for one of the variables. Now, assume you didn't manually enter 0, but that number was gotten from somewhere automatically. Since the entire line is highlighted with the error, you have to take a moment to manually work out if it's variable A or B causing the problem.

Code: Select all

a=Val(InputRequester("Set variable A","Enter 0 or 1 here",""))
b=Val(InputRequester("Set variable B","Enter 0 or 1 here",""))
If 10/a=5 : a$="do one thing" : Else : c=20/b : a$="do something else" : EndIf ; Error somewhere on this line...
But with one statement/command per line, the actual line causing the division by zero error is highlighted, leaving no guesswork and faster debugging:

Code: Select all

a=Val(InputRequester("Set variable A","Enter 0 or 1 here",""))
b=Val(InputRequester("Set variable B","Enter 0 or 1 here",""))

If 10/a=5 ; Error highlighted either here...
  a$="do one thing"
Else
  c=20/b ; ...or here, so no guesswork!
  a$="do something else"
EndIf
Last edited by BarryG on Sun Sep 19, 2021 4:38 am, edited 1 time in total.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Then statement for ONLY single line IFs

Post by skywalk »

It is best to pick a boring way to code and stick with it.
This means never multiple lines on 1 line.
No multiple ways of creating and closing conditionals.

Forget about convenience and think of debugging errors and automated code.
Simplifying syntax improves automation and reduces errors.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Then statement for ONLY single line IFs

Post by BarryG »

OgreVorbis wrote: Sat Sep 18, 2021 9:12 pmThis is the ONLY aspect of PureBasic that I don't like.
You'll get used to it.
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: Then statement for ONLY single line IFs

Post by Tenaja »

BarryG wrote: Sun Sep 19, 2021 2:53 am
OgreVorbis wrote: Sat Sep 18, 2021 9:12 pmThis is the ONLY aspect of PureBasic that I don't like.
You'll get used to it.
My thoughts, too... And it was expressed back in one of those links above, too.

And for the OP, if you've only got one pet peeve, then you are pretty darned lucky. Most of us came here with a different basic mindset, and try to conform pb to our preference... But I quickly learned that was futile, and confirmed my style to match pb, and became much happier.
OgreVorbis
User
User
Posts: 77
Joined: Thu Jan 16, 2020 10:47 pm

Re: Then statement for ONLY single line IFs

Post by OgreVorbis »

BarryG wrote: Sun Sep 19, 2021 1:53 am BTW, having multiple statements on one line (with If/Then) can make it harder to debug your app when an unexpected crash occurs, because the compiler will highlight the entire If/Then line, so you won't always know which command in that line caused it. With one command per line, the exact line of the error is always shown.
Yeah, I agree on that %100, and I wouldn't be using multiple statements on one line. When there is an Else condition, it makes sense to have that on a separate line for readability and debugging. However, that doesn't mean that a single line If...Then without Else is bad. That's why I say it shouldn't work with Else.

There are many situations where you just have a simple condition with no else.

In C, you can just do:

Code: Select all

If (x == 10) x = 0;

//You don't have to do this:
If (x == 10)
{
  x = 0;
}
But in PB, it's like forcing you to use that style. It's blatantly clear that it should have a Then. Of course you can use ": x = 0 : EndIf", but that's not clean.

Not to mention, there are many people who want to switch from Visual Basic to PureBasic, for both speed, lack of runtime, etc. and not including this feature will make it slightly more cumbersome for them to adapt. There are other intricacies of this language too: like variable types being declared with a ".w" or ".s" for example or items in a struct being separated with "/". But those are all things that are BETTER and make sense.
We should have PB be more flexible for different coding styles.
My blog/software site: http://dosaidsoft.com/
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Then statement for ONLY single line IFs

Post by BarryG »

Well, If/Then has been asked for several times since at least 2006 (see viewtopic.php?t=20619) so I doubt it's going to get added.
OgreVorbis wrote: Sun Sep 19, 2021 5:23 pmIn C, you can just do:

Code: Select all

If (x == 10) x = 0;
Here's an alternative to your single-line scenario above:

Code: Select all

Macro IfThen(what,then)
  If what : then : EndIf
EndMacro

x=10
IfThen(x=10,x=0)
Debug x ; 0
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: Then statement for ONLY single line IFs

Post by DoubleDutch »

imho it should be added, I wouldn't use it that much - but I don't seen any harm as it' would be optional - it may even be trivial to add?

THEN could simply mean that the next CR is actually an ENDIF CR combo.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: Then statement for ONLY single line IFs

Post by DeanH »

I will stick my neck out and weigh in. It would be a nice touch to have THEN on a single line IF statement without needing the EndIf. More elegant and easier to read. Many years ago I used the cross-platform ZBasic interpiler a great deal before changing to PureBasic. It had IF ... THEN for single line and LONGIF ... ENDIF for multiline. There was no Then after Longif. I also agree with BarryG that putting multiple statements after THEN is not a good idea. 2c entered.
Post Reply