Comments, Comments & Comments…Did I forget to mention…Comments?

Every developer out there has had the following scenario happen to them…

You take over a project and begin to dive into the code to get a handle on the project. As you begin wading through the code, you begin to drown because there are no comments…or minimal ones. The variables are perhaps even named in an unusual fashion. So it takes you much longer than necessary to figure out the code and what it is even doing. This is even before you dive into the project updates that are on your plate.

This ends up being costly and time-consuming. Additionally, you might even want to smack the previous developer for putting you through this torture. If only they would have commented their code, it would have made your life so much easier.

So here is my question…Since every developer has been faced with this scenario, just who writes the poorly commented code that drains us of every ounce of energy? I would say that we all have. So we are all guilty.

Here are some tips for you to avoid this bad behavior and save not only your sanity, but that of your fellow developers.

Planning Lists

Take time to write out planning lists that contain the following:

  • Input variables – what variables are going to house data supplied by the end user?
  • Output variables – what variables are going to be used to output information to the screen?
  • Temporary variables – what variables are going to be used for processing?
  • Global variables – what variables are going to be global, if any?
// Variables Section  
// Input variables 
// userName  
// Output variables 
// greeting

Pseudocode

Write out in simple, text format what is going to happen in your program. Describe the process and steps the code will go through and what you expect to occur.

What is great about Planning Lists and Pseudocode is they can turn into your program comments. So while you are coding, you are following your plan.

Following just these two tips will help out any developer who takes over your project and save your own sanity when you have to return to the project and forget how and why you built something.

// When the end user loads the page, they will be prompted for their name. 
// The user's name will be stored in the userName variable. 
// The userName variable along with a text greeting (Hello, userName. How are you?) will be stored in the greeting variable. 
// The greeting variable will be outputted to the screen so the end user can see the greeting.

Happy Coding!

Clay

JavaScript Alert…Debugging Tool

In JavaScript, like many languages, there are built in functions/methods to make the developer’s job easier. One that is highly useful is the alert() method. First, let me say that I do not typically use this method in production code because it tends to annoy end users (think pop-ups). With that said, it is highly useful during development to see how your code is functioning. So, let’s dive in and take a look at how alert() works…

Syntax

All the alert() function does is create a pop-up window with whatever information you pass to the alert function. You can explicitly put content into the alert function or alert the value of a variable in the function. Here is the basic syntax…

// alert() function hello world example 
alert('Hello, World!');

The aforementioned code yields the following results (in FireFox)…

b2ap3_thumbnail_helloworldalert.png

Usage

So, as you can see, you can type text directly into the alert function and have it display that text in a message window. You can also do the same with variables…

 

// Variables section 
// Declare variable to utilize in alert function example 
var helloWorld = "Hello, World!"; 
// Output section 
// Use alert function to display value of variable 
alert(helloWorld);

The code above will return the same results as the earlier screenshot. The difference is that this time, we are alerting a variable and not the text directly. This allows us to see what values are in the variables.

Here is an example where this might be useful in a development environment…

Imagine you have a loop that is changing the value of a variable, then you are outputting that variable to the screen. When you run your code, the out put you are expecting is not what displays. You can use an alert to watch your variable value change while it is looping.

// Processing and output section 
// Use alert function to display value of variable within a loop 
for(firstNum = 0;firstNum < 3; firstNum++){
     alert(firstNum); 
}

The aforementioned code should loop three times and output the value of the firstNum variable. Since it runs three times, it will alert three times. The first alert displays ‘0’ because the value of the firstNum variable is ‘0’. We are adding one (1) to it each loop. So the second alert displays ‘1’. The third time through, it displays ‘2’.

So the alert function is a very useful function to be able to watch your variables and output the data within them so you can see how your code is functioning. Just a quick side note on using alerts in loops like I demonstrated…do not use it on large loops, unless you like clicking a lot! There are better ways to output that data (hint: console).

Happy Coding!

Clay Hess

What is Programming?

Many times in my line of work, people have asked me questions like…

  • “What is programming?”
  • “Isn’t it hard?”
  • “How do I get started?”
  • “What language(s) should I learn?”

First off, programming is not that difficult. It only seems hard for those that have not learned how to program. All programming boils down to a few essential topics…

  • Anticipation – you need to be able to put yourself into the shoes of your end user.
  • Logic (critical thinking) – you need to be able to think logically through a problem.
  • Problem simplification – most programs that seem complex are just a lot of problems that each have been broken down. A good programmer breaks down a project into little solvable problems.

Programming is really taking something that is dumb and making it smart. Think about your computer. Without programming, it is simply a hunk o’ junk.

So…is it hard? Only if you try to tackle the entire ‘mountain’. Start small and learn the basics of programming. As you learn the basic tenets of programming, you will find the entire programming picture coming together before too long.

As to how someone might get started, I suggest beginning with some online tutorials and working through some hands on programming lessons. If you are looking to take some classes, we have some great ones that my colleagues and I teach at Northcentral Technical College. I suggest Introduction to Programming.

As to what language(s) to learn, there are several great languages out there. So any of them would be a good choice, but if I would have to boil it down to one choice, then I would suggest JavaScript. The reasons are…

  • Easy to get started
    • Just need a text editor and a browser
  • JS (JavaScript) is everywhere, even in enterprise environments

Resources

There are a lot of resources available (just Google and you can find tons of them)…here are some that might help you out…

  • JSLint – JS code quality tool
  • JSHint – JS code quality tool forked off of JSLint
  • JSFiddle – – JS code quality tool that includes libraries, HTML and CSS
  • W3schools – good beginner tutorial resource on various programming topics
  • Codecademy – online programming training course. Some are paid.
  • Codeavengers – online programming training course. Some are paid.
  • Browsers – Chrome, FireFox, Safari and Internet Explorer
  • Inspectors – all browsers have built in or add-on inspectors (FireBug) that allow you to debug/inspect your code when you have issues
  • Text editors (IDE) – you will need a text editor to be able to write your code. There are a lot out there. Here are some of the more popular ones…

I would encourage everyone to at least learn a little about programming, even non-programmers. If you think about it, nearly everything we do in our lives is touched by programming in some way…and most of us do not know how to code/program. It would benefit everybody to have at least a basic understanding of programming.

If you are looking for a field to enter and wonder if programming might be for you. Well, I am not certain if it is for you or not. What I can tell you is that there will be many, many job opportunities for programmers now and in the future. It is estimate that by 2020, there will be one million more jobs than programmers (data). So start learning how to code/program today!

Happy coding!

Clay Hess