the tricky business of programming

kmote

Seg Fault'n,
Messages
5,801
Location
The dried husk where America came from
Inspired by this post: http://www.techist.com/forums/1654284-post12.html, I thought it might be good to have a thread explaining some of the general programming errors that sneak into code all the time. Hopefully over time this will form a nice descriptive list which will help people avoid and, if necessary, detect them. Post up ones you have made, seen or think could easily happen a bit like this (witty names are non-optional lol):

Is it equal then?
Example:
Code:
int aNumber = 0;

if (aNumber = 1)
{[indent]printFunction("You don't want this to print");[/indent]}
Fairly simple, you put it in an if statement so you expect it to check aNumber is 1 before printing. Unfortunately = is the assignment operator meaning that aNumber is set to 1 then evaluated to provide the answer for the if, result... it will always print. And don't think you're safe from this by using a language such as java which only accepts a boolean answer to if because the exact same thing applies. This one can be caught at compile time (as a warning) in some languages such as c but others are more of a problem.

The "nevermind" conditional
Example:
Code:
boolean iWantToPrint = false;

if (iWantToPrint);
{[indent]printFunction("Hello world");[/indent]}
You don't expect Hello world to be printed but guess what... your program has a surprise for you and it's all because of the sneaky semicolon after the if. This particular error is nasty because it doesn't show at compile time and may not show for most of the runtime. If you expect iWantToPrint to be true most of the time, you won't be surprised when it does print and on the odd occasion iWantToPrint is false the outcome way be more subtle than a print statement.

The moving goalposts
Example:
Code:
int current =0;
int top = 1;

while (current < top)
{[indent]printFunction(current);
top++;
current++;
pauseExecution(1, SECOND);[/indent]}
It seems so simple here because the difficulty of this one lies in the complexity of the code inside the loop so as soon as it is put into a nice simple example it seems obvious. The general idea though is that the programmer has inadvertantly created an infinite loop by continually moving the target that would otherwise break it.

The leaky case
Example:
Code:
int aVar = 1;

switch(aVar)
{
	case 1:
		doSomething();
	case 2:
		doSomethingElse();
	default:
		orThis();
}
Again completely legal, this is actually a powerful and useful feature of switch that allows one case to "flow" into another - that's the best way I can describe it without saying that the above code runs doSomething(), doSomethingElse() and orThis() and that if aVar was 2 it would run doSomethingElse() and orThis(). Only problem is that it is easy to do accidentally and it is a matter of the programmers disipline to put a break before the next case if you don't want this.
 
Nice idea, but regarding your first examples, what language is that ? Java ?

Well as far as my own blunders, one of the top easiest mistakes to make is forgetting to end { with the obligatory } braces, whether it is Java, C#, Javascript, etc.

What helps there is to put the braces in every for every nest as you create the nest, including the closing }. It cuts down on that from reoccuring so much.

Another NON-syntax mistake TONS of people make is believing that Java = Javascript !!

Java is not Javascript, no way no how.

As I think of some coding mistakes per the original post, I will put them here. Great idea, kmote !
 
Yeah, as I looked it over again it did appear to be pseudocode. Probably the best way to present stuff in this thread anyways...

Well I for one was making the mistake you mentioned, by making a variable "=" when I needed it to be comapared to another variable, a ala "= =". That is a big one for people.

What kmote and I are referring to for anyone else that might not know, but when you have two variables and you want to compare there variables you have to use the two consecutive equals signs, which is NOT the same as when a variable is set to something. By the way, a variable is a "container" , like a memory "box". You make the variable by declaring it, and naming it, usually at the same time, such as:

In Javascript:

Code:
var someVariable = "Hello !";

That same thing in Visual Basic would be:

Code:
dim someVariable As String = "Hello"

The two equals signs would be used to compare the contents of two variables in this Javascript case:

Code:
If (someVariable == someOtherVariable)  {
 //then something happens right here between the brackets
}

All too often people confuse the "=" scenario with the "= =" scenario.
 
Another often forgotten thing is the semicolon >> ;

Java, Javascript, C++, C#, PHP (I think), they need a ";" at the end of a line of code in many places.
For example:

Javascript:
Code:
var example = "Dont forget your semicolons!"[B] ;[/B]

I have on a number of occasions forgot that one little semicolon and none of the code would work ! Uggh lol
 
^The compiler will usually catch things like that though, it's more other less subtle things which do the damage. Things that aren't technical errors, but aren't what you want to do (like the = and == functions)

I guess the moral of the thread is take the time to re-read every line you write and ask yourself what that line does.
 
Back
Top Bottom