Some well-known person in the software community wrote about why, I don’t know, mere mortals?, shouldn’t learn to write software.
To be honest I didn’t read it. Any time someone says that I shouldn’t do something for any non-scientific or non-moral reason, I add another check to the column for “this maybe should be done.”
I skimmed several of the thoughtful rebuttals on other blogs, and think their general consensus is valuable, which is basically, “yes, you should learn to write software, because it’s awesome.”
Anyway, this isn’t about whether you should or shouldn’t, can or can’t, will or won’t. This is about doing the actual bit that’s being discussed abstractly.
Let’s assume for the sake of argument that you are using Firefox and viewing this site in the browser. Let’s assume it’s on a non-mobile Operating System with a keyboard and other fancy stuff like a screen.
Let’s assume you were to press the key combination
Ctrl+Shift+K (Hey! Wait a damn minute there. Don’t press stuff without finding out what it does!) or go to the Firefox menu, to
Web Developer and then to
Web Console (which is what the key command before does).
You should now see some slick little bit of chrome fall out of the sky above the website.
You should see that it has a text input area at the bottom. Click there. Type something like,
alert("Boo!"); and press
Enter. Stop! Why are you typing stuff in before you know what it does?
Okay, to be fair, I type stuff in without knowing exactly what it will do, and aside from some wizards, most people who write software do this. But we know almost exactly, and we’ve got experience: think of it like cooking. A master chef can imagine how a recipe will taste just by reading it. Experience works like that, though even the experienced can be mistaken.
So before you type something, you should stop and think about what it might do, and what you want it to do.
alert, that might make lights flash and buzzers sound. But it has those parentheses around the quoted bit.
Maybe it creates some kind of alert using that quoted part.
Go ahead and try it.
Right! A happy little alert box pops up with the text
So why does that work? Think of it like cooking, again. The baker doesn’t just reach into the oven and pull out a pie. Each piece had to be made, including the pie tin (which was beyond the scope of the baker’s activity). In this case, think of the
alert() bit as the pie tin. The makers of the browser gave you the pie tin and said, “fill it with what you need to fill it.”
If you think about it, the parentheses almost look like a pie tin. Are you hungry yet?
So let’s do something a bit simpler this time. Just type
1 + 1 and press
Enter. Right, good thought, we have to ask what it might do. Well, if I wrote it down on paper and handed it to you, what would you probably say? Yes, you would say, “two.” That’s what the console will say back to you:
[13:25:34.419] 1 + 1 [13:25:34.423] 2
That bit there is taken directly from the console. If you select lines in it and then copy them, it puts the fancy timestamps in front. Those help you understand the behavior of more complex programs by letting you know what times the output corresponded to, how long things took (by seeing the difference in the times), etc.
Okay, one more thing to try for today. Start typing
document, but just the
d at first. Notice it gives you a handy drop-down of possible matches. You can use the up and down cursor keys to go through that list, or click on one with the mouse. If you keep typing, once it has a plausible match it will ghost-in the rest of the word for you. Once that happens, you can press the
Tab button to complete the word.
Once you have
document, put a
. after it, and then type
getElementById. You may have noticed that there were other
getElement* bits, and some other things too. I hope you noticed, that’s the main skill to work on as a programmer: seeing all the details and wondering why.
Later on, if you want to keep learning about programming, you might go back to the web console (
Ctrl+Shift+K) and just start typing things (don’t press
Enter, just type say,
c and see what pops up in the list). Ask yourself what they might do, and then use a search engine to ask the web what they actually do.
Okay, back on track:
document.getElementById(???) What do we need to replace the
??? with? You’ll see the so-called “CamelCase” names in software a bit, so get used to mentally putting spaces in:
get Element By Id. Right, if we hand it the
Id, it will hand us the
Let’s use one I know:
site-title. That’s the
id for the HTML element containing the blog title up top. So let’s do something with that.
We can do this two different ways. One way is to store the element we get back, and then speak about that, the other way is to do it directly.
The first way is to give the
Element a name. To do this we can type
var titleElement = document.getElementById('site-title'); and press
Enter. We type
var first, because we want it to be a regular variable. This has to do with scoping rules, which I won’t discuss here, but you will learn about if you keep growing your skill.
Once we have it as a variable named
titleElement (you could name it something else if you prefer), we can use the name to reference that
Now we can type
titleElement.style.backgroundColor = "black"; and press
style part means that you want to access the
style properties. The
style.backgroundColor means you want to access the background color of the
Element. And we’re setting it to the named color
Scroll up to the top and look at the result. The background around the blog title is now black!
It doesn’t look right, of course. The background was more of a white, so let’s try changing it back.
Press the up arrow, and it will show you the last thing you typed. You can navigate the input history in the console using the up and down keys.
white and press enter again. Oops, it’s not quite white, either. What do we type to get it back the way it was?!
Let’s set it back the way it was, but let’s do it the other way I mentioned before: type
document.getElementById('site-title').style.backgroundColor = "inherit"; and press
Enter. Here we aren’t bothering to store the
Element in a variable. We’re using it directly from the return of
getElementById(). And we’re setting the color to
inherit, which means “whatever the parent value is.”
The rules for Cascading Style Sheets are something you can learn about later, but you can think of it like cooking again. By default, a BLT sandwich has bacon, lettuce, and tomato. If someone wants a special one, then maybe its
bltSandwich.style.bread = "pita", but normally it’s just whole-wheat.
inherit just means, “whatever it would be for the environment it’s in.” (It means inherit the value from the parent, because elements in HTML are in a tree structure).
The main thing is that the color is back to normal!
That’s enough for now. You can close the console by clicking the
x in the corner of it, or by pressing
Ctrl+Shift+K again. If you make a live change while learning (live changes are those that you make to a webpage directly, such as by the Web Console), and you don’t know how to undo it, simply reloading the page should get you back to the default state.