Computer Hope

Software => Computer programming => Topic started by: mat123 on February 12, 2011, 04:37:39 AM

Title: question about programming in genral
Post by: mat123 on February 12, 2011, 04:37:39 AM
my question is does anyone else get agitated when there is a variable and the next line uses the variable? ex
Code: [Select]
int var=0;
if (var==0)
    cout <<"var is equal to 0";
Title: Re: question about programming in genral
Post by: Salmon Trout on February 12, 2011, 05:46:08 AM
does anyone else get agitated when there is a variable and the next line uses the variable?

Why should anyone get agitated? Do you mean the use of "magic numbers"? Not quite sure what you mean. Demo code that is written to demonstrate some point or other (e.g. conditional execution) is not to be taken as providing a style guide.
Title: Re: question about programming in genral
Post by: umbra on February 12, 2011, 07:42:39 AM
Hi,

 In my opinion that code is only for learning how to write some school programs, never in production something like that should be used.

 I supposed its purpose is only a didactic one, and yes i saw many times code like that but i never become agitated because was never used in production.

All the best.
Title: Re: question about programming in genral
Post by: BC_Programmer on February 12, 2011, 10:06:16 AM
It doesn't make a bit of difference. the compiled code is still the same, unless the variable is then used in a for, like this:

Code: [Select]
int i=0;
for(int i=5;i<5;i++)
{
//code
}
Except in that case the inner definition of i simply shadows the "outer" definition of i (and in many languages you simply get a duplicate definition error so you can't do it anyway.


The example is silly because the if is redundant, as well.

Neither one is easier to read, and there is no reason to use one over another, even in the apparently magical whimsical fairlyland of "production" where apparently the requirements change to require code to adhere to redundant ruleset's based on a single persons subjective view.
Title: Re: question about programming in genral
Post by: mat123 on February 12, 2011, 03:48:17 PM
the point is that the if is redundant i get annoyed when people do that when the result of the conditional statement is determined at compile time rather then run time.
Title: Re: question about programming in genral
Post by: Geek-9pm on February 12, 2011, 05:23:41 PM
Quote
the point is that the if is redundant i get annoyed when people do that when the result of the conditional statement is determined at compile time rather then run time.
If you were a Jr programmer, and I was a Sr. I would say that you leave it the way I want it and I will tell you way next year. If you are still with us. Team players learn to work together, even when the reason for something is not readily apparent.

But.. that  is not the case. Do you want to know why we put that in production code? Go ahead, ask me.
Quote
int var=0;
if (var==0)
    cout <<"var is equal to 0";


Title: Re: question about programming in genral
Post by: Salmon Trout on February 12, 2011, 05:30:58 PM
the point is that the if is redundant i get annoyed when people do that when the result of the conditional statement is determined at compile time rather then run time.

But you are always going to find amazingly bad code. I don't think there is any point whatsoever in getting emotionally involved with source code. Maybe you should go out more?

How to write bad code:

http://www.exmsft.com/~hanss/badcode.htm

Title: Re: question about programming in genral
Post by: Geek-9pm on February 12, 2011, 07:13:48 PM
The above link took the words out of my mouth.
Quote
Job security
if you're the only one that can work on a piece of code that they need, they can't fire you. The flip side of this is that they can't let you go work on anything else. Being able to really screw up a piece of code and then ducking out to go screw up another project too is a truly special talent that only a few have demonstrated.
  8)
Title: Re: question about programming in genral
Post by: BC_Programmer on February 12, 2011, 08:24:30 PM
http://thedailywtf.com/Articles/14-Layers-Deep.aspx
Title: Re: question about programming in genral
Post by: Geek-9pm on February 12, 2011, 09:19:38 PM
http://thedailywtf.com/Articles/14-Layers-Deep.aspx
I Love it!
Does the OP still want to know why professional developers write such bloated code? It is one way to keep the completion from knowing how easy it could be.
Title: Re: question about programming in genral
Post by: BC_Programmer on February 12, 2011, 11:03:00 PM
http://thedailywtf.com/Articles/Annual-Y2K.aspx

Title: Re: question about programming in genral
Post by: Geek-9pm on February 12, 2011, 11:36:13 PM
He forgot about leap year!  :o
Where do you find this stuff, BC?  ;D

BTW, you can buy a digital watch in the Dollar Tree, a variety store, for just $1. If  the date is wrong non March 1st, you just click a couple of buttons an  it will be OK for the next four years. Why can't they put buttons on every computer so that the user can just press buttons if a program is wrong?
Title: Re: question about programming in genral
Post by: BC_Programmer on February 13, 2011, 09:16:56 AM
Where do you find this stuff, BC?  ;D
I'm subscribed to TDWTF via RSS; I also lurk and occasionally post on the forums when the forum software there isn't in "haiku mode".

Quote
Why can't they put buttons on every computer so that the user can just press buttons if a program is wrong?

That applies to a lot more then software. "Why can't they put buttons in a fighter jet so the pilot can just press buttons and win dogfights?"
Title: Re: question about programming in genral
Post by: Salmon Trout on February 13, 2011, 09:21:19 AM
Why can't they put buttons on TV sets to make the programmes more interesting?

One day you'll get quoting right, BC_P...



Title: Re: question about programming in genral
Post by: BC_Programmer on February 13, 2011, 09:24:34 AM
One day you'll get quoting right, BC_P...

fixed  :P
Title: Re: question about programming in genral
Post by: gluxon on February 16, 2011, 05:18:02 PM
the point is that the if is redundant i get annoyed when people do that when the result of the conditional statement is determined at compile time rather then run time.

Agreed, but most code like that is only used for tutorials, rather than real life applications.