Showing posts with label goto. Show all posts
Showing posts with label goto. Show all posts

Sunday, September 23, 2018

Programming Inside the Lines

Why do people program computers?
  • Business - To automate a process or financial problem
  • Automation - To control homes, factories, machines
  • Research - Data mining, simulation, modeling
  • Communication - Email, telephony, broadcasting, journalism
  • Recreation - Games, movies, music
  • And for fun - Yes, programming is also an enjoyable recreational activity.
I was recently part of a social media thread where we were discussing, more or less, whether programming languages should remove all features that could lead to bad practices.  This was motivated by the perennial question, "Should GOTO be allowed in programming?"  GOTO is not bad.  GOTO is simply GOTO.  I'll leave it there, for now.

Let's compare programming to finger painting.  If you gave a child some finger paints and a sheet of paper and a smock, would you yell at the child for what was painted, or if the activity made a mess?

I started programming when I was 11 years old.  Nobody made me do it.  I sat down with my father's HP-67 calculator and the wonderful manual and I was hooked!  From that point forward I spent much less time on my electronics hobby and spent my energy programming.

So, what is fun about programming?  Here is a short list:
  • Programming involves learning, which lights up neurons all over and stimulates the feel good hormone dopamine.
  • Programming involves problem solving, which is also a kind of learning.
  • Programming is a creative activity, and people enjoy making things.
  • Programming is delightfully interactive.  Give to the computer and it gives back.
  • Programming as an enjoyable social activity, sharing/working/competing with others
I see people bashing programming languages because they have features that will "ruin the programming culture and practices of the masses".  Oh, please.  How many of us have enough experience to even begin to know what this means?  How we define 'ruin' in this context?  And why is it acceptable to 'prove' that something is good or bad by regurgitating some famous person's quote that says that it's so?  We should resist this sort of nearsighted ideology.

I learned programming using the so called bad languages, and I had a blast and nobody got hurt!  I learned a ton and nothing stopped me from learning the so called good languages later on.  I encourage everyone to learn new things but if you decide to only learn one programming language you have not committed a crime to society.  Don't let anyone ever tell you different.

I know that nobody needs my permission, but I'll give it to you anyway!  Go forth and have fun programming!  Color outside the lines and drum to your own beat!  Don't be afraid of the self appointed programming police!

Sunday, September 16, 2018

Python - The Emperor's New Clothes

I recently posted a couple of examples of a starfield simulation written in BASIC.  One of these was written in Commodore BASIC for the VIC-20.  The other was written in Liberty BASIC for Windows.

BASIC gets a lot of flack from people with a certain kind of snob factor (yes, you know who you are).  And there is the infamous comment by Edsger Dijkstra, and BASIC has never lived it down.  Nowadays when you see people discussing what is the best first language I probably see Python mentioned most.  In these forum discussions someone always seems to snidely denounce BASIC as that old thing with the GOTO and the unreadable spaghetti code.  They don't seem to have a good reason to recommend Python, except that it isn't BASIC.

People also recommend C and it's derivatives such as C++ which also has GOTO.  So go figure.

Wait, here is something scandalous!  On the Python Software Foundation website, in their design and history FAQ page, they actually answer the question about why there is no GOTO in Python by encouraging people to use exception handling as a hack to simulate something like GOTO!  Ummmm.   See for yourself.  https://docs.python.org/3/faq/design.html#why-is-there-no-goto

Okay, getting back to the starfield simulation example.  If you look at the version of the starfield simulation written in Liberty BASIC, it addresses some of the things that BASIC is frowned on about.
  • GOTO - Uh oh, not really.  There is one GOTO, but in this case I'm not sure a WHILE/WEND forever loop is really better, so...  The Commodore BASIC version has three GOTOs.
  • Structure - The Liberty BASIC version uses control structures properly the way modern languages do.
  • Line numbers - Liberty BASIC programs do not require numbers.  You can give your routines meaningful names instead of numbers.
Okay, let's be frank.  Liberty BASIC was not the first version of BASIC to solve these problems.  There are many BASICs with modern features, and so it isn't fair to criticize BASIC as being a bad language for beginners.  BASIC has been a proper language for teaching good programming practices for a very long time now.

What do you think?  Comments welcome.  :-)


Thursday, February 25, 2010

The SBASIC Preprocessor

I had become familiar with structured programming ideas from my casual experimentation with Forth. I wanted to do something to improve the quality of my Microsoft BASIC programming. I realized that I could write a program that would take BASIC source code without line numbers, and adding in named labels for the GOTO and GOSUB targets and output line numbered BASIC. Then I could run this code in an interpreter or compile it. This was a cool idea for me. After having done the CNC simulator this lit up a part of my brain that brought me closer to realizing that a compiler is just another kind computer program that treats source code as data.

I wasn't familiar with QuickBASIC at that time, so I didn't use Microsoft's syntax for this. Instead I used square brackets for branch labels like so:

[loopHere]
a = a + 1
print a
if a = 10 then [quit]
goto [loopHere]

[quit]
print "done."
end

Liberty BASIC programmers will recognize this syntax. I called this preprocessor SBASIC for Structured BASIC.

Here's what it looks like in QuickBASIC:

loopHere:
a = a + 1
print a
if a = 10 then quit
goto loopHere

quit:
print "done."
end

I think that my syntax is better. It certainly is more consistent because in QuickBASIC the branch label will either be quit or quit: but in SBASIC it is always [quit].

So here we see a direct precursor to Liberty BASIC. At this time I did not have plans to create my own BASIC. That wouldn't come for four more years.