reading code to write code better

Programmers look at other programmers’ code. That’s one of the points behind having the blog. This is a Good Thing because as someone who writes code, I learn from reading it. I am not alone.

What sometimes happens though is a coder will lift a snippet out. In most programming languages, with limited syntax, once someone has the best algorithm coded, there’s not a lot of room for improvement. While there may always be more than one Right Way to accomplish a task, there may only be one Best Way and once someone’s found it, it’s found forever (until someone finds An Even Better Way—which does sometimes happen, but very rarely).

So it makes sense to use a standard implementation of a known algorithm. It will look exactly like everyone else’s. These algorithms all have names though. Swap. Binary Search. Merge Sort. Whether you lift a merge sort from Joe Schmoe’s code or roll your own, it’ll be a merge sort. It would be 100% possible for you to create the exact same merge sort as Joe’s without ever seeing his.

On the other paw, if you have code that is the exact same as Joe’s for something else, then chances are it is Joe’s code and that is not only illegal, it is wrong.

There are all kinds of ethical reasons why this is a bad idea, but I’m not here to be your conscience. The number one practical reason it’s wrong is that if you didn’t write it, you do not deeply understand it, and if you don’t understand it, you cannot modify it or fix it if it breaks (or if it is already broken). Heck, if it’s broken and you don’t understand it, you won’t even know.

The number two practical reason is if you get caught, you could lose your job, or worse, your stuff (your bank account(s) are first, and then they sell your assets). You could go to jail. It has happened to other coders.

Now some code is okay to use, but it’s clearly marked with licenses (such as the GNU Public License) that specifically say so. If you’re in school, though, your class and school rules override any licenses. When in doubt, check with your instructor. If you’re working for someone else, your employer’s rules apply. Check with your boss if you have a question.

Study code. Read it. Learn to spot the good and the bad in it. Then use that knowledge to build your own solutions.

the clock strikes again

My students will be doing final Snap projects soon and that means I will be guiding them. Part of that process is showing them other Snap projects written by other students. And wouldn’t you know it? This is the weekend the Snap folks are modding the cloud and it is unavailable. That means there is no access to the projects stored on the Snap cloud and no saving new projects to the Snap could, either.

What’s a woman to do? Write a new project and save it locally, that’s what. Here is the new Pomodoro Clock, Snap edition:

No link because … no Snap cloud. I’ll be GitHubbing it soon, but I want to beta it first. This has no custom blocks, no lists, many costumes, and many broadcast messages.

every rose has a thorn … every script has a sprite

Cliches get that way by being true. People remember them for the next situation because they have an established track record.

Every script has a sprite that it’s attached to.  If you want to do something in Snap, you pretty much need to hang it on a sprite (or the stage, but that’s a special case). It’s a feature of Snap.   Sprites don’t need scripts.  You can have a sprite sit there on the stage and look pretty and do nothing but you can’t have a script run without there being a sprite for it.

This is important because if you want to do something in a Snap project, you have to decide which sprite is going to be in charge of it.  Suppose there is a game where the player tries to catch falling raindrops in a cup.  If a raindrop lands in the cup, a message is broadcast to add a point to the score. Which sprite sends that broadcast?  The landing raindrop or the receiving cup? There are good arguments either way. It’s up to the coder to decide.  But the coder has to attach it to some sprite.

That’s why I say every script has a sprite.

planning and implementation

One of the hardest lessons to learn is that it’s easier to work from a plan than to wing it. Most things that most people do most of the time don’t require a lot of planning or forethought because they’re repetitive tasks that have been done before. For example, I don’t make a plan to drive to my grocery store because I know the route, I know what I’m likely to encounter, etc.

The truth is, the first time, I didn’t know.  And when you’re creating a project, it’s the first time. When I go to a new place for the first time, I use Google to map it, I figure out how long it will take to get there so I can leave at the right time, I maybe even use the street view to see what the landmarks might be.  A planning worksheet is an equivalent for a software project.  It’s a way to break down the problem of how do I get from nothing to a solution in steps.

Depending on the complexity, a planning sheet will have different kinds of information but since Hangman is my current project, I’m going to frame this topic from that perspective.  There is a worksheet that was thoughtfully provided, but what if there hadn’t been?  What kinds of things would need to be on it?

Well, looking at the rubric, Hangman has to be able to do a lot of things. 

So any planning sheet has to account for that. Since this is Snap, there will be sprites and the stage.  Every script will belong to either a sprite or the stage so sprite ownership is something that needs to be part of the planning sheet.  Also, information will need to be stored. That means variables will be there, too.  I’ll need a column for pseudocode and a column for issues or pitfalls or whatever I want to call the potential snags I’ll run into.  This is not what the planning sheet looks like that was handed out.  
This is a matrix with two examples filled in. Kinda like a spreadsheet.  The first column is the rubric item and the rest are the columns I identified that I, as the project coder, would need to cut the project down to size.  Other people might see the project with different frameworks and have different columns.

The important thing is to have a plan.  And a planning sheet gives you a good place to create one. The better you plan, the less actual work you have to do (at least that’s the theory).  And in my experience, I’ve found it true often enough that I make it a habit to plan many facets of my life, not only my programming projects.

it’s all about the lists

Fundamentally, Hangman is a game of lists.  You have the alphabet, the list of letters that make up a secret word and a list of letters that have been guessed.

This is why it was chosen as the project for the list unit in our curriculum. Snap has a lot of blocks to deal with lists but if you use the import Tools, you get access to even more.  My favorites are andThe first one turns a word into a list and the second one turns a list into a sentence.  Both are useful for Hangman.

back in business

I am back,  I’ve hiatused long enough and I have fixed the images and code snippets.  What you’ve missed is lists.  Traversals and sequential searches of same.  I’ll cover them as part of Hangman, which I wrote in Snap, but as my students are writing it now, I’ll not be posting mine quite yet.

I’ll post my platform game though. CatStuff.  Here’s a screenshot.Basically, the cat can jump over the saucerbot but if they collide, the game is over.  That’s it.  It wraps. It’s boring.

Itsy-Bitsy Webspinner

Before Pong we had to do a nursery rhyme. With animation, not simply a storyboard with text.  In Snap, the main way to animate is to change the sprite’s costume.  There are two costume-switching blocks. These are in the Looks section.  One is which changes to the next costume in the set of costumes loaded for that particular sprite. The other iswhich allows the coder to choose which of the loaded costumes to switch to at that point in the code.

Animation also often involves movement from one position on the screen to another and there are two blocks for that, too.  (well, several actually, but two that are useful in animation).  These are found in the Motion section and are  and Both of these take numbers or variables as parameters in the little ovals that I left blank.

There was a rubric for the nursery rhyme.  Students can earn up to 20 points for their project. Here is a screenshot of my nursery rhyme.You can see it here.  Assets are from except the background (included with Snap) and the waterspout (created in GIMP).  The text sprite was also done in GIMP.  Most assets were modded in some way—either to make a second costume or (in the case of the sun) to make the text sprite more readable.

Apparently, Unit 3 is a “platform game” and I don’t even like those (except maybe Cosmo’s Cosmic Adventure). Watch this space for more exciting Snap stuff as I figure out what kind of platform game I’m going to create and how it will work.

pong, baseball-style

Ever had one of those days when your IDE decides to rebel? Today was that day for me in Snap and what it did was lost several of the blocks in the Variables category that I needed to do my Pong.  Namely, I lost the ability to set variables and the ability to change them.  These were the blocks I lost.This meant I couldn’t track the score or change the speed of the ball (the particular things I was attempting to do when I noticed the blocks had gone missing from the palette).  This was highly annoying.

About all I could do was open another Snap in another browser window and rebuild my code.  Because exporting to XML and importing back into Snap brought the anemic palette with me.

It took a long time, as I was mostly finished, too.  I was struggling with reflections and had finally figured them out (including how to apply  what I think is called “English”).  And I was to the part of putting in sounds.  I got all my sounds from which is searchable.

Of course my Pong is baseball-themed—what else would you expect? I use sprites for all the edges because I know how to easily test for that and play a sound when sprites touch (and I heard there was a glitch with other kinds of detection and rather than explore them, I figured I’d be lazy).  I don’t know if my sprites need to be as big as I make them, but I’ve noticed that size does not equate to pixels, but to “steps” and (again) I’m too lazy to figure it out.

On the off-chance someone messes with my field, when the green flag is clicked, I put things in their places.  I uncheck the box at the top of each sprite for draggability but that might not stop someone who is determined.  Here’s a screenshot of the pregame.
You can see it here.  Don’t know that I’m off hiatus, but I may be.


This blog is on hiatus due to Harvey.  I’m plugging away at Pong (working on how to get my ball to reflect), but sporadically due to things like lines at the grocery store to get essentials that I thought we had a sufficiency of (but was, apparently, mistaken).

I am safe and dry, but have friends in need of help.  Also I plan to donate blood this week if the center is open.  I’ll be back with Pong as soon as I am able.

Prayers and good thoughts are welcome, but donations are even moreso. Please, use Charity Navigator if you are unsure of where to give as they will help you find the places where your donations go the furthest.

repeat after me (kaleidoscope)

No, Pong is not the next Snap project—a kaleidoscope is.  Here is the example of the kind of thing they mean by a kaleidoscope.

Four sprites moving in synchrony.  This example was presented as you see it—in .gif format.  No clues on how to make one like it except some platitudes. “You will need four sprites.” Well no shaving cream, Sherlock! “The easiest way to create three more is to duplicate the one you have.” You mean there are other ways?  The only way I could figure out to make more sprites without cracking the manual (there is one) is by right-clicking the existing sprite and clicking duplicate.  That also duplicates the code.  So I did break down and look at the manual.

It turns out that green flag is at the top of the stage.  Quick peeky-see at the names of all the areas on the Snap screen—from the actual homework that I actually did that had the actual kaleidoscope program assigned. Here’s that picture.

See the flag?  When you click that, it counts for all the sprites that have that block of code so you can start …. say, four sprites, and have them do synchronous stuff. This is what I came up with.  It is similar, but different, you can look at the code by exiting full-screen mode.  I think that it’s more of the sort of thing I think of as kaleidoscopic.  I think the color should be the same for all sprites but change over time.

Now, I’m ready for Pong.  Well, no, that’s not right, now the next major project i turn in is Pong.  I’ll probably do some other things in the meanwhile.  Pong has a rubric. For those who don’t know, a rubric is a scoring chart that lists aspects of my program and how many points each is worth.  Everything from “Players can control paddles with required keys” to “Program is well-documented and exhibits good style.” Whoa, documented? I don’t think I saw comment blocks in Snap (not that I was looking, mind you).  I’m usually adequate at commenting. I hope that’s good enough for my grader. I guess I’ll find out.

Wish me luck!