Go to Post The best thing that could be said about 2015 was things could only go up from there. - Koko Ed [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #12   Spotlight this post!  
Unread 26-01-2007, 10:32
Validius's Avatar
Validius Validius is offline
There is a shadow following me.....
AKA: Mark Ratering
FRC #1549 (Washtenuts)
Team Role: College Student
 
Join Date: Feb 2005
Rookie Year: 2005
Location: South Lyon
Posts: 159
Validius will become famous soon enoughValidius will become famous soon enough
Send a message via AIM to Validius
Re: Why Static Variables?

Quote:
Originally Posted by TimCraig View Post
Good programming practices usually suggest that variables be given the smallest scope to do the job. This prevents accidental access via things like mispelling the variable name. It also limits name clashes for commonly used variable names and makes the logic easier to follow. That said, most programmers don't use block scope in C and the smallest common scope is the function. Using more descriptive names helps with name clashes but it's nice to be able to use a simple descriptive name without having to worry too much about it until the linker complains. Some coding styles preface global variables with "g_" and file scope statics with "s_" to help with clashes and to make it more obvious what the variable really is.
Very true. Keeping scope small also leads to much more readable code. For example, lets assume you are debugging a function that was written with no global variables. In this case you know that all function input MUST be passed through the arguments given to the function at the time the function was called. Once you verify the integrity of the paramaters passed the scope of your debugging is entirely within that function because it is impossible that any data besides the originally passed paramaters had made its way into that function.

On the other had if you have use of global variables then you have no conrtol over the data that the function is using. Vars could be changing mid-function making debugging work very hard.

Example: Your using variable x which is global. You call function process_x() passing it no args since x is global and you dont need to. At some point within function process_x you call another function process_y. Since its been a while since you wrote process_y you forgot that it tweaks the value of x. Because of this process_x starts screwing up and you spend WAY to long debugging it.
__________________
When a sociaty comes to believe that human life is not inherently worth living, it is a slippery slope to the gas chamber. You wind up on a low road that twists past Columbine and leads toward Auschwitz.

-Peggy Noonan
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Static Cling?? orelinde Electrical 17 27-02-2006 17:33
Static Variables amateurrobotguy Programming 8 04-03-2005 00:40
Static Electricity JeffO Championship Event 5 19-04-2004 11:04
Static on the HDPE Al Skierkiewicz Technical Discussion 60 12-03-2003 17:52
Static Electricity archiver 2001 12 24-06-2002 00:34


All times are GMT -5. The time now is 04:05.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi