QB64 Phoenix Edition
Curious if I am thinking about this right. - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: Curious if I am thinking about this right. (/showthread.php?tid=3395)

Pages: 1 2


Curious if I am thinking about this right. - protocog - 01-19-2025

I am trying to get good at writing simple code that is easy to read. I took up that assignment of writing a Inches to millimeters conversion calculator in Lesson 2 of the tutorial. All I am asking is is there any way to improve my code? I added the option to convert to millimeters to inches. 

Code: (Select All)

Dim inches# 'container for inches
Dim op% 'which operation (either in to mm or mm to in
Dim mm# 'container for mm

Const Conv# = 25.4 '25.4 mm every inch

Print "1. mm to in"
Print "2. in to mm"
Input "Please enter the operation you would like to do. > ", op%

If op% = 1 Then
    Input "Enter mm >", mm#
    Print mm# / Conv#
ElseIf op% = 2 Then
    Input "Enter Inches", inches#
    Print inches# * Conv#
Else
    Print "No Op..."
End If



Thanks for any feedback!


RE: Curious if I am thinking about this right. - bplus - 01-19-2025

Looks good to me, commenting your variables is great habit for readability.

Welcome!


RE: Curious if I am thinking about this right. - bplus - 01-19-2025

A way for less lines employs the new _IIF keyword Plus some strategic rearrangement:
Code: (Select All)
Const Conv# = 25.4 '25.4 mm every inch

Dim op% 'which operation (either in to mm or mm to in
Dim num As Double ' a number to convert

Input "Enter a Number to convert "; num
Input "Enter Either 1 for conversion to mm from inches or 2 vice versa "; op%
If op% = 1 Or op% = 2 Then Print _IIf(op% = 1, num * Conv#, num / Conv#)



RE: Curious if I am thinking about this right. - protocog - 01-19-2025

(5 hours ago)bplus Wrote: A way for less lines employs the new _IIF keyword Plus some strategic rearrangement:
Code: (Select All)
Const Conv# = 25.4 '25.4 mm every inch

Dim op% 'which operation (either in to mm or mm to in
Dim num As Double ' a number to convert

Input "Enter a Number to convert "; num
Input "Enter Either 1 for conversion to mm from inches or 2 vice versa  "; op%
If op% = 1 Or op% = 2 Then Print _IIf(op% = 1, num * Conv#, num / Conv#)

Definitely much simpler. Thanks for taking the time to do that.


RE: Curious if I am thinking about this right. - CharlieJV - 01-19-2025

(5 hours ago)protocog Wrote: ...

I think the readability of your code is top-notch.  Anyone might have tiny nit-picky things they might prefer, but then those would be things others might not prefer.

For the giggles ...

I have a cognitive disability made worse by Fuchs Dystrophy (blurry vision).  So I'm on a really fussy extreme in regards to code format/organization to keep me from getting stuck in the mud or having sticks stuck in my wheels (i.e. avoid slowdowns trying to process what I'm looking at, and avoid playing "where's Waldo" games.)

You probably don't want to write code this way because I imagine you are going to quickly annoy other people.  Me, I'm at an age at which I don't really give a rat's caboose what anybody thinks (unless they want to pay some significant dinero, at which point I'm like: any way you prefer.)

   


RE: Curious if I am thinking about this right. - bplus - 01-19-2025

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#)



RE: Curious if I am thinking about this right. - protocog - 01-19-2025

(5 hours ago)CharlieJV Wrote:
(5 hours ago)protocog Wrote: ...

I think the readability of your code is top-notch.  Anyone might have tiny nit-picky things they might prefer, but then those would be things others might not prefer.

For the giggles ...

I have a cognitive disability made worse by Fuchs Dystrophy (blurry vision).  So I'm on a really fussy extreme in regards to code format/organization to keep me from getting stuck in the mud or having sticks stuck in my wheels (i.e. avoid slowdowns trying to process what I'm looking at, and avoid playing "where's Waldo" games.)

You probably don't want to write code this way because I imagine you are going to quickly annoy other people.  Me, I'm at an age at which I don't really give a rat's caboose what anybody things (unless they want to pay some significant dinero, at which point I'm like: any way you prefer.)
I respect it. As long as the code works and to me I think works just as well. And I agree, some folks are insanely picky.

(5 hours ago)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#)
Above and beyond thanks brotha. I have yet to use a _IIf function.


RE: Curious if I am thinking about this right. - CharlieJV - 01-19-2025

(5 hours ago)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#)
Oh you speed demon,  I was in the midst of doing something similar while you posted that.

   


RE: Curious if I am thinking about this right. - bplus - 01-19-2025

Half again (I probably should have tested this)
Code: (Select All)
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 * 25.4, num / 25.4) ' 25.4 mm per inch

Are the Input questions not explanatory comment enough for the variables?

The beauty of Basic is that you don't have to Dim plain variables.

EDIT: a comment added to explain where the 25.4 number is coming from


RE: Curious if I am thinking about this right. - SMcNeill - 01-19-2025

Personally, I'd just do something like this in the simplest repeatable way possible:

Code: (Select All)
Do
    Input "Give me a value =>"; value#
    Print Using "###.## inches = ######.## millimeters"; value#, value# * 25.4
    Print Using "######.## millimeters = ###.## inches"; value#, value# / 25.4
Loop Until value# = 0

For a quick and easy tool like this, why bother to choose an operator?  Just give both results and save the work, with the process inside a loop to keep it going until you're finished with it.

And that's not to say that the original code is wrong, or bad, in any sort of way.  The important thing in coding is to end up with a *finished product that works and does the job desired*.   It does that, and it does it nicely.  There's nothing in the world wrong with it.   It's just nice to sometimes see how other people would handle the same task, and compare methods so that we can optimize our own way of thinking and learn from them, or teach them a trick or two which they may not have thought of for themselves.  

*YOUR* way works just fine.  *My* way works just fine.  Neither is better.  I just personally prefer to make my programs where they require the least possible amount of input and effort at run time.  I don't to choose an op here, I just provide a simple number; and I don't need to restart to produce a second number.   Run it, let it go until I hit <enter> or try and convert a value of 0, and then I can simply type in one value at a time and just glance at the screen as to what my desired converted number is.  

I'm lazy.   I like to program in a manner that supports me being as lazy as possible.  Big Grin