MudBytes
» MUDBytes Community » Language Discussions » C and C++ » global variables
Spawned Topic
This topic was spawned from [this post] in mysql_fetch_row() crash
Pages: << prev ... 3, 4, 5, 6, 7 next >>
global variables
Runter
Wizard






Group: Developer
Posts: 3,412
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#91 id:54015 Posted Feb 24, 2011, 7:04 am

It sounds like it's probably something pretty easy to do.  You just need to search the DOM and change the visibility property on matching elements.  Well, might be a little tougher since you'd need to set the visibility of parent elements based on that search.  Although, I think it's a little unclear what Tyche meant.    Is it completely filtering out posts from certain users?

Last edited Feb 24, 2011, 7:04 am by Runter
Davion
Idle Hand






Group: Administrators
Posts: 1,783
Joined: May 14, 2006

Go to the bottom of the page Go to the top of the page
#92 id:54017 Posted Feb 24, 2011, 9:01 am

plamzi said:

KaVir said:
Tonitrus said:
Tyche said:
About six months ago I wrote some javascript that "skins" the site by filtering out features and posts/quote blocks coming from a particular source.  It makes most threads 25-50 percent shorter and eliminates any temptations to respond to the deliberate misinterpretations and mischaracterizations of what I and other posters have written.

Share?

I'd very much like a copy as well please Tyche, if you're willing to share.  I've asked several times for an "ignore" feature, and at one point it looked like it might be added, but by now I'm willing to try pretty much anything short of painting TipEx on the screen.


[offtopic]

Sounds like a job for a greasemonkey script. Is that what you used?

Maybe a new thread is in order...

[/offtopic]




Works beautifully :D
.........................
http://mudbytes.net/mudbytessignature-davion2.png

Last edited Feb 24, 2011, 9:02 am by Davion
David Haley
Wizard






Group: Members
Posts: 7,841
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#93 id:54018 Posted Feb 24, 2011, 9:08 am

plamzi said:
If you disagree with his view

The disagreement is not with the view per se, but with his insistence that other people hold such a silly view in the first place. (It makes it easier and more fun to post insulting pictures -- yes, you're correct that they're personal attacks.)
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Tyche
Wizard






Group: Members
Posts: 2,038
Joined: May 23, 2006

Go to the bottom of the page Go to the top of the page
#94 id:54021 Posted Feb 24, 2011, 10:36 am

Davion said:



Works beautifully :D


The quoting is broken since then.
Change this:
Code (javascript):
            if (divElements[j].className == "quotebox") {
            var strongElement = divElements[j].childNodes[0];
            var whoSaid = strongElement.innerHTML.split(" ", 1)[0].toLowerCase();
            for (var k = 0; k < usersToIgnore.length; k++) {
            if (usersToIgnore[k] == whoSaid) {
            divElements[j].childNodes[1].innerHTML = "IGNORED";
            break;
            }
            }
            }

To:
Code (javascript):
            if (divElements[j].className == "quotebox") {
            var strongElement = divElements[j].childNodes[0];
            for (var k = 0; k < usersToIgnore.length; k++) {
            if (strongElement.innerHTML.match(new RegExp(usersToIgnore[k],"i"))) {
            divElements[j].childNodes[1].innerHTML = "IGNORED";
            break;
            }
            }
            }
.........................
Proud member of Team Hetero
http://jlsysinc.gotdns.com/ladybug_laugh2.jpghttp://jlsysinc.gotdns.com/teensymud_250x80.pnghttp://jlsysinc.gotdns.com/palin_calendar.jpg
For now we see through a glass, darkly; but then face to face: now I know in part; but then shall I know even as also I am known.

Last edited Feb 24, 2011, 10:39 am by Tyche
sankoachaea
Conjurer






Group: Members
Posts: 137
Joined: Dec 1, 2010

Go to the bottom of the page Go to the top of the page
#95 id:54034 Posted Feb 24, 2011, 7:49 pm

"Don't use global variables" is kind of a stylistic choice.

If you're learning to program and planning to write non-trivial software, it's understood to be a "safe" and "proven" stylistic choice.

Tyche seems to think that finding any reason to support that stylistic choice makes a person ignorant, inferior, "going through a phase" or possessive of some other undesirable characteristic and that arguing against that stylistic choice by making attacks on the persons holding that view is somehow demonstrative of a superior understanding of both programming and social behavior.

I think it's more likely to be interpreted as him being an asshole or a troll - as a number of people have confirmed.
.........................
I'm looking for a project to join. C/C++/Lua? Sounds good.

Orrin
Sorcerer






Group: Moderators
Posts: 437
Joined: Aug 26, 2008

Go to the bottom of the page Go to the top of the page
#96 id:54040 Posted Feb 25, 2011, 12:13 am

Let's move on please and try not to derail the thread with personal attacks, accusations of trolling or discussion of who is or isn't an asshole.
.........................
MudGamers | FMud | My blog | @bcdevMatt

sankoachaea
Conjurer






Group: Members
Posts: 137
Joined: Dec 1, 2010

Go to the bottom of the page Go to the top of the page
#97 id:54046 Posted Feb 25, 2011, 7:03 am

Agreed.

Google C++ Style Guide (Chromium) said:

Static or global variables of class type are forbidden: they cause hard-to-find bugs due to indeterminate order of construction and destruction.
Objects with static storage duration, including global variables, static variables, static class member variables, and function static variables, must be Plain Old Data (POD): only ints, chars, floats, or pointers, or arrays/structs of POD.

The order in which class constructors and initializers for static variables are called is only partially specified in C++ and can even change from build to build, which can cause bugs that are difficult to find. Therefore in addition to banning globals of class type, we do not allow static POD variables to be initialized with the result of a function, unless that function (such as getenv(), or getpid()) does not itself depend on any other globals.

Likewise, the order in which destructors are called is defined to be the reverse of the order in which the constructors were called. Since constructor order is indeterminate, so is destructor order. For example, at program-end time a static variable might have been destroyed, but code still running -- perhaps in another thread -- tries to access it and fails. Or the destructor for a static 'string' variable might be run prior to the destructor for another variable that contains a reference to that string.

As a result we only allow static variables to contain POD data. This rule completely disallows vector (use C arrays instead), or string (use const char []).

If you need a static or global variable of a class type, consider initializing a pointer (which will never be freed), from either your main() function or from pthread_once(). Note that this must be a raw pointer, not a "smart" pointer, since the smart pointer's destructor will have the order-of-destructor issue that we are trying to avoid.


Google on Global Functions said:

Prefer nonmember functions within a namespace or static member functions to global functions; use completely global functions rarely.

Pros:
Nonmember and static member functions can be useful in some situations. Putting nonmember functions in a namespace avoids polluting the global namespace.

Cons:
Nonmember and static member functions may make more sense as members of a new class, especially if they access external resources or have significant dependencies.

Decision:
Sometimes it is useful, or even necessary, to define a function not bound to a class instance. Such a function can be either a static member or a nonmember function. Nonmember functions should not depend on external variables, and should nearly always exist in a namespace. Rather than creating classes only to group static member functions which do not share static data, use namespaces instead.

Functions defined in the same compilation unit as production classes may introduce unnecessary coupling and link-time dependencies when directly called from other compilation units; static member functions are particularly susceptible to this. Consider extracting a new class, or placing the functions in a namespace possibly in a separate library.

If you must define a nonmember function and it is only needed in its .cc file, use an unnamed namespace or static linkage (eg static int Foo() {...}) to limit its scope.

Place a function's variables in the narrowest scope possible, and initialize variables in the declaration.
C++ allows you to declare variables anywhere in a function. We encourage you to declare them in as local a scope as possible, and as close to the first use as possible. This makes it easier for the reader to find the declaration and see what type the variable is and what it was initialized to. In particular, initialization should be used instead of declaration and assignment, e.g.

Code (C):
int i;
i = f();      // Bad -- initialization separate from declaration
int j = g();  // Good -- declaration has initialization

Note that gcc implements for (int i = 0; i < 10; ++i) correctly (the scope of i is only the scope of the for loop), so you can then reuse i in another for loop in the same scope. It also correctly scopes declarations in if and while statements, e.g.

while (const char* p = strchr(str, '/')) str = p + 1;


There is one caveat: if the variable is an object, its constructor is invoked every time it enters scope and is created, and its destructor is invoked every time it goes out of scope.

Code (text):
// Inefficient implementation:
for (int i = 0; i < 1000000; ++i) {
  Foo f;  // My ctor and dtor get called 1000000 times each.
  f.DoSomething(i);
}

It may be more efficient to declare such a variable used in a loop outside that loop:

Foo f;  // My ctor and dtor get called once each.
for (int i = 0; i < 1000000; ++i) {
  f.DoSomething(i);
}



For the Objective-C style guidelines, they defer to the C++ guidelines in regards to globals. They also mention the following.

Google Python Style Guidelines said:
Avoid global variables.

Definition:
Variables that are declared at the module level.

Pros:
Occasionally useful.

Cons:
Has the potential to change module behavior during the import, because assignments to module-level variables are done when the module is imported.

Decision:
Avoid global variables in favor of class variables. Some exceptions are:

Default options for scripts.
Module-level constants. For example: PI = 3.14159. Constants should be named using all caps with underscores; see Naming below.
It is sometimes useful for globals to cache values needed or returned by functions.
If needed, globals should be made internal to the module and accessed through public module level functions; see Naming below.
.........................
I'm looking for a project to join. C/C++/Lua? Sounds good.

Tyche
Wizard






Group: Members
Posts: 2,038
Joined: May 23, 2006

Go to the bottom of the page Go to the top of the page
#98 id:54048 Posted Feb 25, 2011, 8:52 am

sankoachaea said:
"Don't use global variables" is kind of a stylistic choice.

If you're learning to program and planning to write non-trivial software, it's understood to be a "safe" and "proven" stylistic choice.


The choice to use globals can be for functional reasons or stylistic (form) reasons or for both.
But proven?  See Heuristic Rule.  It's also at the top of the page, GlobalVariablesAreBad, that you copied almost verbatim into your first post.

sankoachaea said:
Tyche seems to think that finding any reason to support that stylistic choice makes a person ignorant, inferior, "going through a phase" or possessive of some other undesirable characteristic and that arguing against that stylistic choice by making attacks on the persons holding that view is somehow demonstrative of a superior understanding of both programming and social behavior.


You appear to be engaged in channeling, reading auras, or some other nonsense. 
I am the authority on what Tyche thinks.

"I don't think you need to justify your use of globals either."

"It's purists versus pragmatists.  Conditioning versus critical thinking. You don't need to do any thinking to regurgitate the children's stories that may or may not have been taught in CS 101(?) like "Globals are bad" or "GOTOs are bad".  Advice to newbies aren't commandments for experts."
Gotos Considered Harmful and Other Programmers Taboos said:

Programmers are constrained not just by conscious application of rules and procedures, but also by
taboos that they have acquired as part of their formal education or informally from colleagues. These
taboos usually embody perceived sound advice and have generally been concerned with the breaking
of abstraction boundaries. However their effect can be to needlessly restrict the range of solutions to
design problems that programmers consider. This paper examines a set of common programming
taboos, and addresses both social aspects and technical reasons as to why programming taboos have
arisen.


"Some people have to code with these [pink mittens with strings] on and think everybody else should too. Some people don't."
Gotos Considered Harmful and Other Programmers Taboos said:

People are loathe to cross boundaries, either for reasons of safety or for lack of skill. And it is
the latter that makes taboos dangerous. As we have shown, the introduction of a taboo can cause
knowledge and experience to be lost.


'GOT0 Considered Harmful' Considered Harmful said:
It is like butchers banning knives because workers sometimes cut themselves.


You might wear mittens for safety.  You might wear them because you have no skill.  You might wear them because you aren't confident in your skills yet.  You might wear them because you don't want to get your hands dirty.  All mud programmers are not equal.  All of us have different experiences and different skills.  There are both professionals and amateurs here.  Conforming to some egalitarian orthodoxy of 'we' do this or 'we' are that simply has no place here, IMNSHO.

Since the forum is C/C++ and the topic is global variables, it seems appropriate to post the only formal study I am aware analyzing globals in C and C++ programs.
Assessing the Impact of Global Variables on Program Dependence and Dependence Clusters said:
The results show that, while most global variables have essentially no impact on program dependence, there are a few that have a large and significant effect. Moreover, though there may be few such global variables, many programs (more than half those studied) have at least one such significant variable.


My take on what this study is that on average 80% of globals are perfectly harmless, don't create maintenance nightmares, and don't influence the ability of a programmer to comprehend programs.  YMMV


.........................
Proud member of Team Hetero
http://jlsysinc.gotdns.com/ladybug_laugh2.jpghttp://jlsysinc.gotdns.com/teensymud_250x80.pnghttp://jlsysinc.gotdns.com/palin_calendar.jpg
For now we see through a glass, darkly; but then face to face: now I know in part; but then shall I know even as also I am known.

quixadhal
Wizard






Group: Members
Posts: 2,603
Joined: Oct 17, 2007

Go to the bottom of the page Go to the top of the page
#99 id:54132 Posted Mar 3, 2011, 12:09 am

For the benefit of Mr. Kite
There will be a show tonight on trampoline
The Hendersons will all be there
Late of Pablo-Fanques is there, what a scene
Over men and horses hoops and garters
Lastly through a hogshead of real fire!
In this way Mr. K. will challenge the world!
.........................
https://lh3.googleusercontent.com/-vYoSYr4luwg/UdpJ_fYLt8I/AAAAAAAAAUw/B-8sQAoGOtA/s800/MUDBYTES_zps028f0a68.gifhttps://lh5.googleusercontent.com/-S1rE61rTCMM/UdrboSwRJsI/AAAAAAAAAYI/MVUkOP_baKs/s800/kool-aid_zpsf0068bff.png

Rarva.Riendf
Wizard




Group: Members
Posts: 1,298
Joined: Jul 15, 2010

Go to the bottom of the page Go to the top of the page
#100 id:54138 Posted Mar 3, 2011, 3:36 am

Scandum said:
For my mud I use a global data structure called mud, so I've got mud->time, mud->first_player, mud->first_area, etc, about 80 nested variables total, and 1 global variable. It gives the ease of use of global variables, without the worry about rogue variables.

It is the best use of a global variable: the one you need like everywhere, in every kind of methods.
It would be hard to access the player list everywhere you need it in a mud if it were not global.
And if you do not use multithread, it is quite safe.
But you have to limit them to the strict minimum (the start of the list of char is obviously the start, but what if a newbie decide to put the index to some value while in a method, then call another that is not aware of that, before returning and putting it back to the start of it).
A global should always be true to its name (if it is called mud->first_player, well it should NEVER be something else). Otherwise it is hell to debug.

Pages:<< prev ... 3, 4, 5, 6, 7 next >>