Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Is it better to declare variables globaly or locally?

Ask ALL your questions here

Is it better to declare variables globaly or locally?

Postby glinka57 on Wed Oct 21, 2009 10:19 am

and point to them? which is better. interms of the machine or w/e
User avatar
glinka57
 
Posts: 195
Joined: Fri Feb 27, 2009 7:32 pm

Re: Is it better to declare variables globaly or locally?

Postby Moosader on Wed Oct 21, 2009 2:17 pm

You should never declare them globally, it's awful programming practice (same with using "goto"s)

Always declare them locally; preferably you'll have classes and such to encapsulate your variables a little more.
User avatar
Moosader
 
Posts: 25
Joined: Mon Jul 06, 2009 5:31 pm
Location: Kansas City

Re: Is it better to declare variables globaly or locally?

Postby zorro59 on Wed Oct 21, 2009 2:37 pm

Yea but do you know why???
Ignoring security...
zorro59
 
Posts: 80
Joined: Tue May 12, 2009 2:28 am

Re: Is it better to declare variables globaly or locally?

Postby antiRTFM on Wed Oct 21, 2009 5:39 pm

Its all part of the new generation of organizing your data & routines (variables & functions) amongst other such programming solutions like OOP. Objects should remain belonging/existing only where they are needed and used, not exposing everything to anyone/anywhere/anytime.
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Is it better to declare variables globaly or locally?

Postby glinka57 on Thu Oct 22, 2009 12:21 am

HOLY SMOKES! no GOTO's ????????

now what?

Zorro, thats true.

Antirtfm I see.

so It seems like its only a program structure and arrangement thing and not a machine thing.
I'll take it that the machine doesnt care if they are global or not, and wont make much difference even on a large scale. am I wrong.

mooseader whats the argument about goto's??
User avatar
glinka57
 
Posts: 195
Joined: Fri Feb 27, 2009 7:32 pm

Re: Is it better to declare variables globaly or locally?

Postby Vevix on Thu Oct 22, 2009 4:45 am

They shouldn't just be declared locally, they should be declared near first use - even if near the end of a function.

There is a performance loss to declare them globally, not to mention bad coding practice as Moosader said but also on more of a technical side (Globally declared variables being in the .data segment of a file, for example)

-----

As far as I know there really isn't any technical disadvantage to GOTO.

If you're at the point in a function where you need to use GOTO, There is probably a much better way you can write it.

But as Moosader said again, It's bad coding practice.

To quote the C++ FAQ: "Code must be written to be read, not by the compiler, but by another human being. "
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg. -- Bjarne Stroustrup
User avatar
Vevix
 
Posts: 142
Joined: Sat Oct 10, 2009 9:24 pm
Location: kernel32.dll

Re: Is it better to declare variables globaly or locally?

Postby noobgrammer on Thu Oct 22, 2009 7:52 am

Like Vevix said write better for you to read, If you have a big program going and you start tossing in goto's you can very easily get stuck in a infinite loop. gotos are a old program deal and using them now will make you look like a poor programmer with poor design.
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Is it better to declare variables globaly or locally?

Postby Moosader on Thu Oct 22, 2009 11:34 pm

basically, when people use gotos, it makes their code a tangled, ugly, hard-to-follow mess. Therefore, it's bad programming practice.

Unless you're writing in Assembly, anyway. Then it can't be helped.
User avatar
Moosader
 
Posts: 25
Joined: Mon Jul 06, 2009 5:31 pm
Location: Kansas City


Return to QA

Who is online

Users browsing this forum: No registered users and 0 guests