beanz Magazine

Coding Mistakes

thebarrowboy on Flickr

Code can always be improved. Check out these tips to make you the best programmer you can be!

Appearances can be deceiving. Just because a block of code looks good doesn’t mean that it’s error free; the code could be inefficient, or confusing to read, or it could leave the door wide open for malicious hacking attacks. A good programmer wants to be vigilant against such coding faux pas!

Since the best way to learn is by example, we’ll start by diving into the nitty-gritty of a small 50-line Python script that acts as a simple “proof-reader”.

First, the program downloads a list of English-language words from the Internet and stores them in a list, creating a simple dictionary. Next, it prompts the user to enter the name of a text file. The program then reads though this file and puts the new words in a different list. Finally, the program works through the user’s words one by one to see if they’re in the dictionary. If not, it highlights the offending mistakes in red and displays the text back to the user.

There’s a few subtleties:

  1. The dictionary words are stored in a set, not a list. The only important difference (for us) is that lists have a specific order while sets don’t, and this makes it considerably faster to check for a particular word inside a set.
  2. The program “sanitizes” the user’s words, which is a fancy to say it cleans them up by removing attached punctuation marks and converting capital letters into lowercase equivalents. Otherwise words like “However,” and “as:” would be flagged as mistakes since they don’t exactly match the entries “however” and “as”.

Take a peek at the code and see if you can spot where it goes wrong, or where potential problems might crop up. Don’t be intimidated if there are lines you don’t instantly understand! If a function is unfamiliar, pop it into a Google search engine and see if you can find an explanation. You can also play around with removing lines to see how their absence changes the program.

Become a subscriber and get access to the rest of this article. Plus all our magazine articles.

Stories also include numerous links to help parents, kids, and teachers learn more. Get access today at just $15 per year for online magazine only or $29.99/year for print + online ($35/year outside US)!

Subscribe Today!

Also In The October 2018 Issue

Logic puzzles help develop reasoning skills useful for programming, computer science, and anything you might do.

Find perfect and fun gifts for your loved ones that teach STEAM concepts and skills.

From light-up bow-ties to conductive thread, you’ll be the life of the party with this STEAM-inspired gear.

A free online test service reveals how much personal data your web browser is giving away.

Add more tools to your command line arsenal, including running mini-scripts and making backup copies.

Use switches to take your robotic creations to the next level.

An old classic with a electronic twist, featuring JavaScript and micro:bit.

Create the American flag in SketchUp using this detailed tutorial.

From lasers to supernovas, Berboucha is making science communication a priority.

Code can always be improved. Check out these tips to make you the best programmer you can be!

It’s a programming language unlike any you’ve seen before. Check out this symbolic system designed for mathematical calculations.

New, improved, faster, and sleeker - it’s Scratch 3, your new favourite block language!

Learn about the brilliant algorithm behind all of your GPS devices.

It’s free, comprehensive, and available on-the-go. This cool app helps you master Python faster than ever before.

Open up whole new worlds to explore through these interesting, diverse add-ons.

Links from the bottom of all the October 2018 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for October 2018.