Then statement for ONLY single line IFs
-
- User
- Posts: 77
- Joined: Thu Jan 16, 2020 10:47 pm
Then statement for ONLY single line IFs
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.
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/
Re: Then statement for ONLY single line IFs
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)
IF with THEN to continue in a single line (10 Jul 2016)
Test-keyword for single-line if-constructs (04 Mar 2009)
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 more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
-
- User
- Posts: 77
- Joined: Thu Jan 16, 2020 10:47 pm
Re: Then statement for ONLY single line IFs
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.
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/
Re: Then statement for ONLY single line IFs
And Tip: If/Then in PureBasic! (20 Mar 2006)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)
Re: Then statement for ONLY single line IFs
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.
But with one statement/command per line, the actual line causing the division by zero error is highlighted, leaving no guesswork and faster debugging:
[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...
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.
Re: Then statement for ONLY single line IFs
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.
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
Re: Then statement for ONLY single line IFs
You'll get used to it.
Re: Then statement for ONLY single line IFs
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.
-
- User
- Posts: 77
- Joined: Thu Jan 16, 2020 10:47 pm
Re: Then statement for ONLY single line IFs
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.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.
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;
}
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/
Re: Then statement for ONLY single line IFs
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.
Here's an alternative to your single-line scenario above:OgreVorbis wrote: ↑Sun Sep 19, 2021 5:23 pmIn C, you can just do:Code: Select all
If (x == 10) x = 0;
Code: Select all
Macro IfThen(what,then)
If what : then : EndIf
EndMacro
x=10
IfThen(x=10,x=0)
Debug x ; 0
- DoubleDutch
- Addict
- Posts: 3219
- Joined: Thu Aug 07, 2003 7:01 pm
- Location: United Kingdom
- Contact:
Re: Then statement for ONLY single line IFs
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.
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
https://reportcomplete.com <- School end of term reports system
- DeanH
- Enthusiast
- Posts: 230
- Joined: Wed May 07, 2008 4:57 am
- Location: Adelaide, South Australia
- Contact:
Re: Then statement for ONLY single line IFs
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.