Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Curious if I am thinking about this right.
#21
(01-20-2025, 12:01 AM)SMcNeill Wrote:
(01-19-2025, 08:12 PM)bplus Wrote: the dueting continues

"Wrong direction"  ??? me with a true one-liner single statement showing another feature of QB64pe that can not be found in that other vesion of QB64.
Low bytes is good too, for sure!, but that is another direction ("colonscopey" let's call it) which I don't dare call "wrong", just a little misguided maybe. Smile

Code: (Select All)
Print Val(_InputBox$("Convert number from inches to mm.", "Enter a number")) * 25.4

Wrong direction in the fact that you went from a fully functional program to one that now does only half the required work in that single line of code. The above only converts from inches to mm, but it can't convert from mm to inches. It might be half the lines of what you had before, but it does half the work as well. I'd consider that going in the "wrong direction" in the code. You want to compare apples to apples, not apples to half-apples. Wink

I don't see any way to get full functionality and do both processes in a single line of code, without using a colon to cram multiple statements on that line. Maybe later tonight I'll brainstorm something crazy to do so, but I doubt it. Tongue

(01-19-2025, 11:59 PM)CharlieJV Wrote: Hey, silly question.  What about the BAM code I shared would not work in QB64PE ?

There's no INPUT _IFF format with QB64PE. Wink

Well so Steve can get a good nights sleep tonight:
Code: (Select All)
Print Val(_InputBox$("Convert number from inches to mm.", "Enter a number")) * 25.4, Val(_InputBox$("Convert number from mm to inches.", "Enter a number")) / 25.4

It's up to the user if he wants to enter the same number both times, so hey it's a little more flexible. Big Grin
b = b + ...
Reply
#22
(01-20-2025, 01:01 AM)bplus Wrote: Well so Steve can get a good nights sleep tonight:
Code: (Select All)
Print Val(_InputBox$("Convert number from inches to mm.", "Enter a number")) * 25.4, Val(_InputBox$("Convert number from mm to inches.", "Enter a number")) / 25.4

It's up to the user if he wants to enter the same number both times, so hey it's a little more flexible. Big Grin

Beat you to it. Try my insane code above yours. Big Grin
Reply
#23
@CharlieJV yeah you got it about the Input statement, a major fix allowing variable strings with more variables for a prompt. Another plus would be the ability to paste in the user part. Steve did one like that.

Still the screenshot you showed had a weird thing going with If Then it looked like there was some missing code too that might explain why it looked weird.

(01-20-2025, 01:03 AM)SMcNeill Wrote:
(01-20-2025, 01:01 AM)bplus Wrote: Well so Steve can get a good nights sleep tonight:
Code: (Select All)
Print Val(_InputBox$("Convert number from inches to mm.", "Enter a number")) * 25.4, Val(_InputBox$("Convert number from mm to inches.", "Enter a number")) / 25.4

It's up to the user if he wants to enter the same number both times, so hey it's a little more flexible. Big Grin

Beat you to it. Try my insane code above yours. Big Grin

So what are we telepathically connected? I was just watching a show on TV about that! Spooky
b = b + ...
Reply
#24
Hello!

This is a slippery and a potentially controversial question you've asked old timers. Be ready for the lectures, and heated opinions Smile

BEGIN OPINION:

Less lines does not = more readable code, IMO. There is a balance between terseness and verbosity which you can discover when you think about what you are writing from a compassionate lens with consideration of not only yourself, your future self, but also someone coming behind you and working on the same code you wrote. From this perspective, you have to consider your audience.

While a ternary statement like _IIF is fantastic brevity and a great thing once you have command of a language, if you are learning I would avoid it for all but the simplest of cases.

What are some of those? Things like checking if the number of things is more than 1 to add a plural version of a quantity.

Why do I suggest this?

Because you increase the cognitive load on yourself by making things harder to parse visually. Since you brain is a computer, it needs to be working in the space of a mental compiler.

Code: (Select All)
' Trivial example where _IIF is more readable than IF...THEN...ELSE IMO
DIM fruit as STRING
DIM num_fruits AS _UNSIGNED _BYTE
thing$ = "apple"
' First we have 1 apple so don't pluralize with "appleS"
num_fruits~%% = 1
fruit$ = _IIF(num_fruits~%% > 1, thing$ + "s", thing$)
PRINT "I have "; _TOSTR$(num_fruits~%%); " "; fruit$; "."
' Second we have more than 1 apple so pluralize with "appleS"
num_fruits~%% = 2
fruit$ = _IIF(num_fruits~%% > 1, thing$ + "s", thing$)
PRINT "I have "; _TOSTR$(num_fruits~%%); " "; fruit$; "."
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Same code with IF...THEN...ELSE
num_fruits~%% = 1
IF num_fruits~%% > 1 THEN
    fruit$ = thing$ + "s"
ELSE
    fruit$ = thing$
END IF
PRINT "I have "; _TOSTR$(num_fruits~%%); " "; fruit$; "."
num_fruits~%% = 2
IF num_fruits~%% > 1 THEN
    fruit$ = thing$ + "s"
ELSE
    fruit$ = thing$
END IF
PRINT "I have "; _TOSTR$(num_fruits~%%); " "; fruit$; "."

While a bit contrived, this example is perfect for _IIF.

If you have to nest more than 2 statements in a ternary, you might be better off with full on IF statements.

Be kind to your current self, your future self, and those who will come behind you.

---8<---

One more suggestion.

I do not recommend ONLY using the sigils for your types (%,$,#,!) and instead use both the full word style DIM statements AND the sigils.

Why?

Because when you are learning you need to constantly reinforce as you go what a type is. In QB64 There are many more types than in QBasic or QB45, etc. Therefore if you fall into a habit of ONLY using sigils (while not wrong and will work fine), you are not reinforcing what those sigils mean by creating the connection in your code to reinforce that meaning over and over and over.

Once you get the hang of it, then you can do whichever you like. There is no WRONG way to do it. However, I find myself always using this style still, and it's served me well. The initial burden of types and the ambiguous freedom presented about them --- to sigil or not to sigil --- to DIM or not to DIM, was much easier to come to grips with when I added my own rules and style. There is no official style.

Some may say the IDE formatting is the official style of QB64, but I dislike this style. I prefer keywords in UPPERCASE (which fucks up my brain in other languages for sure as for example in python that makes a constant, etc.) I initially was fearful of creating bad habits writing QB64 vs. these other types of modern languages, but I was pleasantly surprised to find that my brain-compiler and my language-specific thingies in my thought processes was not impacted. In fact, I found that I started preferring more strongly typed ways of working, e.g. explicit declarations vs. implicit assumptions.

---8<---

Another thing that will help you write better code is using the OPTION _EXPLICIT stuff:

Code: (Select All)
' Put this at the top of your code and brace for impact
OPTION _EXPLICIT
OPTION _EXPLICITARRAY

Wiki documentation:
- OPTION _EXPLICIT
- OPTION _EXPLICITARRAY

Given my code above, it found that I didn't DIM thing$:

Code: (Select All)
Beginning C++ output from QB64 code...
[...............                                   ] 30%
Variable 'thing' (STRING) not defined
Caused by (or after):THING$ = "apple",5
LINE 9:thing$ = "apple"

Even with this small example, i've broken my own rules Smile This is why that reinforced style of DIMs and sigils is helpful for me.

---8<---

If you find yourself repeating the same code and only variables changing, that's a great candidate for a SUB or FUNCTION.

Same code as above wrapped in FUNCTIION plural:

Code: (Select All)
' Same code with function to simplify
' First we have 1 apple so don't pluralize with "appleS"
num_fruits~%% = 1
PRINT "I have "; _TOSTR$(num_fruits~%%); " "; plural$(thing$, num_fruits~%%); "."
' Second we have more than 1 apple so pluralize with "appleS"
num_fruits~%% = 2
PRINT "I have "; _TOSTR$(num_fruits~%%); " "; plural$(thing$, num_fruits~%%); "."
''
' Function to simplify pluralization of a thing
' @param STRING the thing to pluralize
' @param _UNSIGNED _BYTE num_things the number of things
' @return STRING the thing with an "s" appended if num_things > 1
'
FUNCTION plural$(thing$, num_things~%%)
plural$ = _IIF(num_things~%% > 1, thing$ + "s", thing$)
END FUNCTION

For the FUNCTION I document the function explicitly (because I've done this in other languages) using a weird PHP-Doc like syntax in a docblock above it.
In a strongly typed source, this is redundant. It's pretty obvious what things are, so some may say this is over-documented. Again, I do this ritualistically even if it is so, because it reinforces again the linkage in my brain compiler between the sigils and the word types.

It is perfectly legal to use this instead:
Code: (Select All)
FUNCTION plural$(thing AS STRING, num_things AS _UNSIGNED _BYTE)
plural$ = _IIF(num_things~%% > 1, thing$ + "s", thing$)
END FUNCTION

But I find that to be more cognitive load than the other way. You can fit more on a line in a function signature this way.

Again, this is one of those things that was "find out the hard way" for me.

WHATEVER YOU DO BE CONSISTENT! Find your comfort zone, and stick to it.

Hope this helps!

P.S. One more BASIC specific thing that helps my inner OCD love my various selves and others:

Do not forget you can use the line continuation character to wrap lines and adhere to maximum column widths!

Code: (Select All)
_

[Image: 8hvaVja.png]

Same code in 80 columns or less:
Code: (Select All)
' First we have 1 apple so don't pluralize with "appleS"
num_fruits~%% = 1
PRINT "I have "; _TOSTR$(num_fruits~%%); " "; _
plural$(thing$, num_fruits~%%); "."
' Second we have more than 1 apple so pluralize with "appleS"
num_fruits~%% = 2
PRINT "I have "; _TOSTR$(num_fruits~%%); " "; _
plural$(thing$, num_fruits~%%); "."

[Image: NSbqtPc.png]
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply
#25
I would like to add a reference to this thread to the "Discussion" tab on the WiKi's _IIF topic.

Is this allowed? and if so, How would I go about doing that?
Reply
#26
The original topic has been completely lost! And the thread starter flickers before the eyes. - May I also?  Tongue
According to the textbook for Basic:

Code: (Select All)
' Umrechnung Zoll/Inch in Millimeter 20. Jan. 2025
Option _Explicit
Dim As Integer auswahl
Dim As Double inch, inMillimeter, ergebnis
Const Millimeter = 25.4
Locate 3, 3
Print "Berechnung Inch in Millimeter und umgekehrt"
Locate 4, 3
Print "==========================================="
Locate 6, 3
Print "Auswahl:"
Locate 7, 3
Print "1. Inch/Zoll in Millimeter"
Locate 8, 3
Print "2. Millimeter in Inch/Zoll"
Locate 10, 3
Input "Ihre Auswahl: ", auswahl
If auswahl = 1 Then
  Locate 13, 3
  Input "Geben Sie die Anzahl Inch ein: ", inch
  ergebnis = inch * Millimeter
  Locate 15, 3
  Print Using "###.## Inch sind ###,### Millimeter"; inch, ergebnis
ElseIf auswahl = 2 Then
  Locate 13, 3
  Input "Geben Sie die Anzahl Millimeter ein: ", inMillimeter
  ergebnis = inMillimeter / Millimeter
  Locate 15, 3
  Print Using "####.## Millimeter sind ###.## Inch"; inMillimeter, ergebnis
End If
End

[Image: Inch.jpg]
Reply
#27
(01-20-2025, 10:30 PM)Kernelpanic Wrote: The original topic has been completely lost! And the thread starter flickers before the eyes. - May I also?  Tongue

You're right.  I've tried to split the topic to keep it organized a little better for what it was originally and then for the discussion where we sidetracked and got off course.  Does it look a little cleaner and more on-topic now?  (The rest has been moved into the General Discussion forums.)
Reply
#28
Smile we could have been done with the first reply to the OP

I'm OK with the split, the 5 star stuff is still in this thread.
b = b + ...
Reply
#29
(01-19-2025, 04:39 PM)bplus Wrote: Ha! even less work
Code: (Select All)
Const Conv# = 25.4 '25.4 mm every inch
Dim op% ' 1 if desired conversion is from inches to mm else vise versa
Dim num As Double ' a number to convert
Input "Enter a Number to convert "; num
Input "Enter 1 for conversion to mm from inches any other does vice versa "; op%
Print _IIf(op% = 1, num * Conv#, num / Conv#)

Wouldn't this mean that you would not be able to convert from 1 mm to 0.0393701 inches?
The noticing will continue
Reply
#30
(01-21-2025, 03:52 PM)SpriggsySpriggs Wrote:
(01-19-2025, 04:39 PM)bplus Wrote: Ha! even less work
Code: (Select All)
Const Conv# = 25.4 '25.4 mm every inch
Dim op% ' 1 if desired conversion is from inches to mm else vise versa
Dim num As Double ' a number to convert
Input "Enter a Number to convert "; num
Input "Enter 1 for conversion to mm from inches any other does vice versa "; op%
Print _IIf(op% = 1, num * Conv#, num / Conv#)

Wouldn't this mean that you would not be able to convert from 1 mm to 0.0393701 inches?

well let's see
   

I just hit enter for the 2nd input for op%
b = b + ...
Reply




Users browsing this thread: 1 Guest(s)