BlitzBASIC and JavaScript: One way vs the other

Community Forums/General Help/BlitzBASIC and JavaScript: One way vs the other

En929(Posted 2012) [#1]
I’m trying to figure out the construction of BlitzBASIC vs. the construction of JavaScript. It seems like in BlitzBasic an entire game could be run with just the “While..Wend” loop and one need not call the “function” command even though it’s available but it's not always necessary, but in JavaScript, it seems like the “function” command is used for every single thing. I was wondering why? Are there any examples of JavaScript being done with just the top-down fashion as with BlitzBasic? Below are two videos of what I’m talking about. Two completely different ways of programming.

BlitzBASIC (the game runs with just the While..Wend loop:

http://www.youtube.com/watch?v=Q7lO6bqoEnc&feature=relmfu

JavaScript (the game runs via calling the "functions" command hundreds times):

http://www.youtube.com/watch?v=CSrAbLQPrtI&list=PL290A4D2398C97186&index=14&feature=plpp_video

Thanks

P.S. It would be nice to get JavaScript so that I can do things the way they're done in Blitz as much as possible (if possible) i.e. l more used to the way things are done in the first video.

Last edited 2012


Yasha(Posted 2012) [#2]
There are several parts to the answer to this.

First, the simplest one: while you technically can make a game without using functions, you really, really shouldn't do it that way. It dramatically overcomplicates the whole program structure and makes life very difficult.

The second thing is a language difference. BlitzBasic doesn't permit the passing of functions as values: when a function is named in code, it is constant, static, a rock of eternity. In JavaScript, functions can be put into variables and do all sorts of wacky things, because they are values, and in fact objects, too. This means you can define three functions, pick one at random, put it in a variable, and when the program gets around to that point it can call the variable and do that thing directly, without ever having to engage a weird Select structure or anything like that.

This makes JavaScript strictly more powerful than BlitzBasic, because it can capture behaviour in variables and act on it later. BlitzMax can almost do something like this, but not quite (BlitzMax does have function pointers, but they're not as powerful).

JavaScript code takes advantage of this, then, by using functions to describe and wrap specific actions, and then passing the actions to a controlling object or array or whatever that will call them, and make the world go, when the time is right. This is close to flat-out impossible in BlitzBasic, because there's no way to pass "actions" around at all (the best you can do is pass flags, and have them interpreted by massive dispatch structures). The top-down architecture of a BlitzBasic program is a natural response to the fact that you can't pass code around, or create new actions on the fly, but everything is static and permanent and fixed in place.

A final note is that JavaScript is almost universally used as a browser scripting language. A web page is not a program; it's a collection of elements that can, if scripted, respond to the user. JavaScript's ability to package up actions and wait for them to be triggered from outside makes it very suitable for scripting a document, because you can "give" an action to a document element and wait for the document to use it. You could never express this in BlitzBasic. This inheritance from The Document informs most modern JavaScript programming idioms: code is something that you wait for the document to run in response to user input, not something that loops and grabs input itself the way a BlitzBasic program would.


So, in conclusion:

-- Yes, JavaScript is capable of programming like BlitzBasic, because it does provide all the basic flow-control primitives (while, for, etc.). But you won't find people using them as much because it involves a lot more writing and a lot more complicated logic. I would be surprised if there are even any examples, simply because writing like that is so much more complicated and difficult than using functions.

-- JavaScript is designed for use in a reactive, document-like environment. BlitzBasic-style code is not appropriate to respond to events in this way, and you can't write Blitz-like code that will fit into the niche JavaScript evolved to fill.


You will learn and grow a lot more if you use the tool in the way it's meant to be used! Even if you can write in a non-idiomatic way, you need to understand how the community as a whole writes if you ever want to use their code - and in this case, the alternative way is objectively superior anyway.


En929(Posted 2012) [#3]
Thanks for the insight. I guess as time goes on, I'll get used more used to the new format. It has been another planet with Java/Javascript. I used the functions in Blitz too and I guess alongside what you said, such also makes things reusable too (in case I'd want to make another game - it's a matter of changing the images and a few coordinates). Thanks again.