|  | |
05-19-2009, 07:26 PM
|
#1 (permalink)
|
Join Date: Jul 2005 Location: England Posts: 2,035
| the tricky business of programming Inspired by this post: http://www.tech-forums.net/pc/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)
{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.
__________________ MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11 
There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
Last edited by kmote; 05-20-2009 at 03:55 PM.
|
| |
05-19-2009, 07:35 PM
|
#2 (permalink)
|
Electrical Systems Design Join Date: Jun 2008 Location: Maine, USA Posts: 1,603
| 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 ! |
| |
05-19-2009, 07:58 PM
|
#3 (permalink)
|
Join Date: Jul 2005 Location: England Posts: 2,035
| Re: the tricky business of programming Quote:
Originally Posted by oldskool 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 Another NON-syntax mistake TONS of people make is believing that Java = Javascript !! | Yeah seen that one before.
__________________ MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11 
There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page |
| |
05-19-2009, 08:19 PM
|
#4 (permalink)
|
Electrical Systems Design Join Date: Jun 2008 Location: Maine, USA Posts: 1,603
| 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. |
| |
05-19-2009, 09:45 PM
|
#5 (permalink)
|
Join Date: Feb 2007 Posts: 6,346
| Re: the tricky business of programming Very good work, thread stickied.
__________________ I am not here for long I am deploying soon so please don't expect anything long winded.
Last edited by Saxon; 05-20-2009 at 08:15 AM.
|
| |
05-19-2009, 11:06 PM
|
#6 (permalink)
|
FPS Addict Join Date: Dec 2007 Location: Fonthill, Ontario, Canada Posts: 4,217
| Re: the tricky business of programming good stuff mate
__________________ <<<<If you found me helpful in anyway, or enjoy my posts then click the checkmark<<<< |
| |
05-20-2009, 12:57 AM
|
#7 (permalink)
|
01001100011011110110110 Join Date: Mar 2007 Location: Perth, Australia Posts: 1,870
| 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 dour 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 Click this if I helped you >>>> <<<<
Last edited by Saxon; 05-20-2009 at 08:15 AM.
|
| |
05-20-2009, 01:46 AM
|
#8 (permalink)
|
Bake a Pretty Cake! Join Date: Jun 2008 Location: Madison, WI Posts: 718
| Re: the tricky business of programming Quote:
Originally Posted by saxon very good work, thread stickied. | godlike!!
Last edited by Saxon; 05-20-2009 at 08:15 AM.
|
| |
05-20-2009, 01:53 AM
|
#9 (permalink)
|
Electrical Systems Design Join Date: Jun 2008 Location: Maine, USA Posts: 1,603
| 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 |
| |
05-20-2009, 03:21 AM
|
#10 (permalink)
|
01001100011011110110110 Join Date: Mar 2007 Location: Perth, Australia Posts: 1,870
| 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 dour 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 Click this if I helped you >>>> <<<< |
| |  | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |