Webolution Designs Musings

Various posts on technology (code/programming). Our hope is that these posts improve your world.

Recent blog posts

Posted by on in Programming

Debugging...it is a necessary evil that all developers must do, except you of course..;) As long as humans program, we will have bugs in our code. I have never built a program that did not have bugs in it. It is not so much that I have poor programming habits (and sometimes I do), but speaks more to human nature. So I want to look at some debugging tips and techniques.

The first step in proper debugging is not having to do it at all. What do I mean? I am talking about good coding techniques. Developing good coding techniques will aid in reducing the number of bugs that pop up in your code. SO what are good coding techniques? That can be somewhat of a subjective debate, but here are some fairly general accepted standard practices...

  • Comments...comment your code. This will save your sanity and those who have to work on your code later.
  • 4 spaces indentation for each level of code
  • Line length of no more than 80 characters
  • End every line with a delimiter (semi-colon in JavaScript)
  • Each code statement on its own line
  • Use whitespace to improve readability (note: this does not apply to minification for production, but development only)
  • Instantiation of variables at time of creation (as much as possible)
  • Utilization of code validators
    • W3C offers these. They can also be built into Brackets with plugins
    • JSHint. I use this a ton to make sure my code is elegant
  • Naming conventions
    • Use camelCase
    • Name variables/functions appropriately for their job

That is an extremely short list, but a list that, if followed, would go a long way to reducing your bugs and maintaining your sanity. In a future post, I will address some techniques with handling bugs that do crop up in our programs.

...
Hits: 213
Rate this blog entry:

Posted by on in Programming

All programming languages have the ability to make decisions and handle logic within various mechanisms. One of these is IF statements. If statements are great, but sometimes can become cumbersome. Imagine the following example...

// Example IF/ELSE/IF statement
if (someNumber == “1”){
	dosomething1();
}
else if (someNumber == “2”){
	dosomething2();
}
else if (someNumber == “3”){
	dosomething3();
}
else if (someNumber == “4”){
	dosomething4();
}
else if (someNumber == “5”){
	dosomething5();
}

The above code is not bad, but rather simple. Imaging more complex logic and even having the need to nest IF statements. It would not take long to get a block of code that is cumbersome.

Note: concerning nesting IF statements...there is nothing wrong with nesting, but it can get to be a pain rather quickly. My 'rule of thumb' is to not go more than three levels deep. If I have to do that, then I rethink my program structure and logic. I just find that I tend to get lost if I go beyond that, but there is nothing stopping you from going deeper.

...
Hits: 232
Rate this blog entry:
0

Posted by on in JavaScript

In today's post, I want to discuss scope (and I do not mean the minty, green kind). Scope is context. In other words, where data lives and how it can be accessed. In classical OO languages, such as C# and Java, there is a concept of private and public scope. In public scope, other code can access the information. In some scenarios, this is desired, but most of the time we want to disallow this as we want to encapsulate, or protect our data. Private scope does just that. We can have data that is private and thus protected from harm.

JavaScript is not a classical OO language. It is a prototypal OO language. The same concept lies within JavaScript, but the terminology is different and the methods to achieve it have some varying nuances. In JavaScript, we call it global and local scope. 

  • Global = Public
  • Local = Private

Tip: try to make as much as you can local so as to encapsulate your code. In fact, an object-oriented principle is to encapsulate what varies. This avoids odd behaviours in your program by some code changing your global information in an undesirable fashion.

...
Hits: 223
Rate this blog entry:
0

Posted by on in Programming

In the continuing saga of operators, we need to discuss multiple conditions. Thus far, we have only compared two items, but what if we needed to compare more than two? We can do this with and (&&) and or (||). Here is how they work...

And (&&)

If you wish to compare multiple items (more than two), you can use and if you need them all to be true. For example...

// Using && to compare multiples
var x = 5;
var y = 3;
if(x < 10 && y > 1){
    // Do something
}

In the aforementioned code, the condition statement reads, "If x is less than ten AND y is greater than 1". Since x has a value of 5 and 5 is less than 10, this side of the equation is true. Since y has the value of 3 and 3 is greater than 1, that side of the equation is true. So you could say that it is true and true, thus true. What I am getting at is that by using &&, both sides of the equation have to be true or the entire statement is false...i.e. true AND true = true...true AND false = false. Here is a false example...

...
Hits: 204
Rate this blog entry:
0

Posted by on in Programming

In our continuing saga of operators, we would be remiss if we did not address greater than/less than. Sometimes in programming, you want to test whether one value is greater than another...or less than another. We can do this with the comparison operators. Here is an example...

// Test greater than
var firstNum = 3;
var secondNum = 5;
if(firstNum > secondNum){
    // Do something
}

In the above code, the "do something" code would not run because 3 > 5 returns false.

So you can use greater than (>), less than (<), greater than/equal to (>=) and less than/equal to (<=).

...
Hits: 209
Rate this blog entry:
0

Get in touch

Go to top