the tricky business of programming - Techist - Tech Forum

Go Back   Techist - Tech Forum > Computer Software > Programming
Click Here to Login
Reply
 
Thread Tools Display Modes
 
Old 05-19-2009, 07:26 PM   #1 (permalink)
Seg Fault'n
 
Join Date: Jul 2005
Location: The dried husk where America came from
Posts: 5,801
Default the tricky business of programming

Inspired by this post: Tgif!!!!, 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)
{
printFunction("You don't want this to print");
}
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);
{
printFunction("Hello world");
}
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)
{
printFunction(current); top++; current++; pauseExecution(1, SECOND);
}
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.
__________________

__________________


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
"Get your money and let's go to court!" - Craig Mudie, MS
kmote is offline   Reply With Quote
Old 05-19-2009, 07:35 PM   #2 (permalink)
Oldie but a goodie
 
oldskool's Avatar
 
Join Date: Jun 2008
Location: Maine, USA
Posts: 1,762
Default Re: the tricky business of programming

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 !
__________________

oldskool is offline   Reply With Quote
Old 05-19-2009, 07:58 PM   #3 (permalink)
Seg Fault'n
 
Join Date: Jul 2005
Location: The dried husk where America came from
Posts: 5,801
Default Re: the tricky business of programming

Quote:
Originally Posted by oldskool View Post
Nice idea, but regarding your first examples, what language is that ? Java ?
It's not a specific language, call it psudocode. For fun though the first one can't be java, the other two could be.

Quote:
Originally Posted by oldskool View Post
Another NON-syntax mistake TONS of people make is believing that Java = Javascript !!
Yeah seen that one before.
__________________


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
"Get your money and let's go to court!" - Craig Mudie, MS
kmote is offline   Reply With Quote
Old 05-19-2009, 08:19 PM   #4 (permalink)
Oldie but a goodie
 
oldskool's Avatar
 
Join Date: Jun 2008
Location: Maine, USA
Posts: 1,762
Default Re: the tricky business of programming

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.
oldskool is offline   Reply With Quote
Old 05-19-2009, 09:45 PM   #5 (permalink)
Lord Techie
 
Kharn's Avatar
 
Join Date: Feb 2007
Posts: 7,638
Default Re: the tricky business of programming

Very good work, thread stickied.
__________________
Visit this and do something for the world!
www.hackersforcharity.org

Want to do even more? Take a look here and join the fight on human trafficking.

Hear and you forget, see and you remember, do and you understand.

Kharn is offline   Reply With Quote
Old 05-19-2009, 11:06 PM   #6 (permalink)
Lord Techie
 
Luke's Avatar
 
Join Date: Dec 2007
Location: Fonthill, Ontario, Canada
Posts: 5,601
Send a message via AIM to Luke Send a message via MSN to Luke
Default Re: the tricky business of programming

good stuff mate
__________________
3770K @ 4.7 Ghz H100 Water Cooled | GTX 970 MSI Gold Edition SLI
32GB G.Skill RipJaws (Rendering/ Virtualization) | Corsair 650D | CORSAIR RM1000W w/ White Cable KIt
Samsung 850 Pro 256 GB SSD Boot | OCZ Vertex 3 240GB SSD Secondary | WD Black 1 TB | WD Green 3 TB
Server: Core i7 920 | 8GB RAM | Intel 240gb 730 SSD Boot | 3 x 2TB WD RED | Fractal R4
Luke is offline   Reply With Quote
Old 05-20-2009, 12:57 AM   #7 (permalink)
Lord Techie
 
S0ULphIRE's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 8,628
Send a message via MSN to S0ULphIRE
Default Re: the tricky business of programming

Yes, very 'good' work indeed

who said what about spell-checkers?
__________________
"As a result of all this hardship, dirt, thirst, and wombats, you would expect Australians to be a sour lot. Instead, they are genial, jolly, cheerful, and always willing to share a kind word with a stranger, unless they are an American." -- Douglas Adams
S0ULphIRE is offline   Reply With Quote
Old 05-20-2009, 01:46 AM   #8 (permalink)
Bake a Pretty Cake!
 
Spit-wad's Avatar
 
Join Date: Jun 2008
Posts: 904
Default Re: the tricky business of programming

Quote:
Originally Posted by saxon View Post
very good work, thread stickied.
godlike!!
__________________
Spit-wad is offline   Reply With Quote
Old 05-20-2009, 01:53 AM   #9 (permalink)
Oldie but a goodie
 
oldskool's Avatar
 
Join Date: Jun 2008
Location: Maine, USA
Posts: 1,762
Default Re: the tricky business of programming

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!" ;
I have on a number of occasions forgot that one little semicolon and none of the code would work ! Uggh lol
oldskool is offline   Reply With Quote
Old 05-20-2009, 03:21 AM   #10 (permalink)
Lord Techie
 
S0ULphIRE's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 8,628
Send a message via MSN to S0ULphIRE
Default Re: the tricky business of programming

^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.
__________________

__________________
"As a result of all this hardship, dirt, thirst, and wombats, you would expect Australians to be a sour lot. Instead, they are genial, jolly, cheerful, and always willing to share a kind word with a stranger, unless they are an American." -- Douglas Adams
S0ULphIRE is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Falcon Programming Language: a brief tutorial Kharn Programming 2 02-27-2009 03:43 AM
Underage Business... and Online Business? commandercup Technology Careers and Education 1 05-14-2008 04:53 PM



Copyright 2002- Social Knowledge, LLC All Rights Reserved.

All times are GMT -5. The time now is 12:08 PM.


Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2017, vBulletin Solutions, Inc.