fwd-tutorial #4: Who needs a graphing calculator when you’ve got javascript?
One of the great things about JavaScript is that it’s very permissive. JavaScript makes it relatively easy to access and change just about anything in the Document Object Model (the object model for web pages). Just because JS allows you do almost anything with the DOM, doesn’t mean you ought to! When you’re writing JS, it’s easy to accumulate bad habits. Though it may seem counter-intuitive, we’re going to explore some of these bad habits in Tutorial 4.
In this tutorial, I’ve built a calculator for Markov Chains. For further discussion of what a Markov Chain is, see my “adventures in comp. sci. article“.
The design of my calculator is a little rigid. It only accepts a 2x2 transition matrix, and no absorbing states are allowed. This calculator is meant to serve as a proof-of-concept: the disposable version of an application that I’ll build out in a more robust way. Because I want to point out some of the bad habits that JS permits, I’ll be using comments in the code itself as a critique.
Ideally, developers should be writing code that can be easily read and understood. This is more difficult than it sounds. I usually start out writing code in a messy way first, just to get things working. Then I go back over what I’ve written and clean it up, with the idea that I’m making it easier for others to understand. Always keep in mind the fact that you’re writing code for your future self: a self that may have forgotten exactly why you did things in a certain way. Sometimes it’s good to take a little break from the code you’re working on. Come back to it after some time has passed, and see if it still makes sense to you. If it’s difficult for you to understand, then you can imagine what it must be like for someone who’s never read it before.
You can find my Markov Chain calculator here.
I think Glitch is particularly well-suited for demonstrating a proof-of-concept. You can show your client what you you’re planning to do. Get some feedback early on in the process, and use it to write something more robust.
If you’ve made it this far, then you might be interested in creating your own Glitch account. They've made a really friendly, supportive community where you can learn more about web development and practice what you learn!