Congratulations, you’ve learned the basics of programming! That’s wonderful, but you’d better not go out into the world and write crappy code. Before we set you free, here are some best practices and good things to keep in mind.
Comment Your Code and Comment It Well
// Single-line comment
/* Multi-line comment
One more! */
You’ll mostly use single-line comments for making actually comments about your code and use multi-line comments to remove parts of your code without deleting them. This makes it easier to isolate a problem when you’re debugging.
So how do you write good comments? Well, you’re probably going to feel inclined in the beginning to write comments that explain how everything works. If that helps you remember, that’s not a bad thing to do in the beginning. For the most part, however, you’re going to be naming your functions and variables clearly and you won’t need to explain how something works or what a particular function does because that clarity already exists. What you want to explain in your comments is information the code can’t already tell you: why you made the choices you made. Anything that will help another programmer (or yourself, when you look back at this code months/years later) better understand your code is worth putting in a comment. If it’s redundant information, it’s not helpful. If it helps bring clarity to the code you’ve written, it is.
Don’t Use The eval() Function
Oh boy, the eval() function isn’t the easiest thing to explain. If this explanation doesn’t make enough sense and the video doesn’t get the idea across either, just remember not to use eval() whenever possible. The main reason is that eval() slows down your code. The other main reason is that you can almost always find a better way to get the job done than to use eval().
So what does eval() do? Well, it’s short for evaluate and it evaluates a string as if it were a variable. So let’s say you had a variable called numberOfApples and wanted to alert it to a user. You could do it like this:
What if you put numberOfApples in quotes? Well, the alert wouldn’t alert whatever number you set numberOfApples to, but instead just alert the text “numberOfApples”. Eval solves that problem:
Why would you ever use this? Well, someday you’ll probably find a reason why eval() will be useful to you. Generally it’s because you don’t necessarily know the name of the variable, or the name of the variable you need is contained in another variable. It gets complicated and we’re certainly not going to get into it here, so just remember: don’t resort to using eval() unless there is no other way—and I can’t think of a situation where you wouldn’t have an alternative.
Create Arrays and Objects Faster
We didn’t really talk much about objects but you should definitely remember arrays from Lesson 3. When we created them earlier, we did it like this:
var myArray = new Array(“item1”, “item2”, “etc”);
You don’t really need the new Array statement, however, as you can just do it like this:
var myArray = [“item1”, “item2”, “etc”] ;
You also have a similar shortcut with objects, a type of variable we haven’t discussed yet. Objects are very similar to arrays in that they can hold more than one piece of data at a time, but they’re accessed a little differently and you can easily name each item in your object. It’s really easy to see the difference when you look at the long way of creating an object:
var myObject = new Object();
myObject.item01 = “Ponies”;
myObject.item02 = “Unicorns”;
myObject.item03 = “Rainbows”;
The long way works fine, but here’s an easier way to do it:
var myObject =
These shortcuts make writing your code a bit faster and make your code a lot easier to read.
Use Your Semicolons!
DO NOT Use W3Schools
When you search for help online, one of the first results is often W3Schools (which I’m explicitly not linking to here). The short version is that it sucks. It’s full of errors, it’s missing information, and while it’s not 100% useless it isn’t a good resource. Avoid it. For the long version, visit W3Fools, a site put together by the jQuery team (and some other helpers).
- Mozilla Web Development Resources
- CarlHProgramming Lessons are a great collection of programming lessons posted to Reddit.
- Lynda.com offers a large selection of online education, but it’ll cost you. The lessons are excellent, however, and I feel it’s worth the cost. It costs around $US25 (depending on the type of account you get), so if you can fit a course into a month you really won’t be paying that much in the end.
- MIT OpenCourseWare’s Introduction to Computer Science and Programming is a great big programming learning resources complete with video lessons.
- If you’re a Firefox user, you need to have Firebug. Most other browsers have built-in developer tools, but Firebug Lite is a bookmarklet that will work in pretty much any modern browser.
- When you’re ready to really step things up, downloading and learning to use libraries like jQuery, Dojo, MooTools, and Yui will make your life much, much easier. When you get to the user interface stuff, check out jQuery for Designers for some great tutorials.
- The official PHP Manual is how I learned PHP. I just searched for functions, browsed around, and learned by example. PHP was also the first language I learned, so it’s not as if I knew what I was doing. The manual is very informative and has great examples. While I’d probably recommend learning programming basics before diving right in, you just did in this series so playing around with PHP shouldn’t be too tough for you.
- Zend offers up PHP for the Absolute Beginner. Zend is a company, but also a powerful PHP framework. If you want to start from step zero (which isn’t a bad idea), check it out.
- Agile Web Development with Rails is how I learned Ruby and Rails. I can vouch for it being very good, although technically I read the second edition.
- RubyOnRails.org’s screencasts are another great way to learn.
ActionScript (Flash/Cross-Platform AIR Apps)
- gotoAndLearn is filled with excellent tutorials that teach you how to make really useful stuff in ActionScript, whether you’re using Flash or Flex to deploy to the desktop or web. One of the best ways to learn is by doing, and gotoAndLearn offers plenty of opportunities to do just that.
Mobile App Development
- Standford’s iPhone Application Development is a course on iTunes U that you can download for free. You’ll also find the class resources here.
- The Android Developer site is a great resource (especially the guide) for learning how to develop for the Android platform. It’s available for free from Google.
Special thanks to my friend Colin Snover for his input. He currently works on jQuery and is much smarter than me. Follow him on Twitter. Also, a special thanks to CnEY?! for a few of the resources listed here and making sure I had nothing good to say about W3Schools.