Comment On Globally Coupled

"I work on a team maintaining a large and enterprisey PHP system," writes Amber, "and as such, my job mostly involves doing enhancements and fixing bugs." [expand full text]
« PrevPage 1 | Page 2Next »

Re: Globally Coupled

2012-01-23 11:18 • by massemo (unregistered)
FRIST maybe?

TRWTF is Enterprisey PHP

Re: Globally Coupled

2012-01-23 11:19 • by stupid (unregistered)
not frist because aksimet needs shooting...

Re: Globally Coupled

2012-01-23 11:30 • by Fyrilin (unregistered)
This is why people don't like PHP; they see code like this.

As a PHP supporter, I have to point out that PHP DOES allow OOP including some great frameworks.

Just preempting the PHP bashing.

captcha: damnum: my thoughts on this code

Re: Globally Coupled

2012-01-23 11:33 • by Shutterbug (unregistered)
372726 in reply to 372725
Agreed Fyrilin - PHP isn't as bad as that when it's used with an ounce of common sense.

Given the option if presented with code like this to maintain, it's either being rewritten from the ground up or I'll be making sure the door doesn't hit me too hard on the way out.

Re: Globally Coupled

2012-01-23 11:39 • by Steve (unregistered)
I don't do PHP, but rather C++ so I may be a little off here, but this is a prime reason why you don't use Global variables for anything (Variables that get allocated globally are a different matter - and yes, there is a difference).

Re: Globally Coupled

2012-01-23 12:02 • by JJ (unregistered)
Real
PHP
WTF

In no specific order

Re: Globally Coupled

2012-01-23 12:03 • by scott (unregistered)
Yeah, general "rule of thumb", if you cannot count your globals on your fingers, there's probably something wrong. On that note, I once had a vendor BRAG to me that the next release of their software used over 6,000 global variables. The poor fool thought that it was a measure of the size, and therefore sophistication, of their product. To me, well, let's just say that data point explained a lot ;-)

Re: Globally Coupled

2012-01-23 12:05 • by michael (unregistered)
372731 in reply to 372727
Variables that get allocated globally are a different matter - and yes, there is a difference."

Are you speaking of heap variables? If so, it's far better to call them "heap variables" than "variables that get allocated globally." If you mean something else, please do educate us.

Re: Globally Coupled

2012-01-23 12:16 • by Why'd he turn it down? (unregistered)
372732 in reply to 372731
michael:
Variables that get allocated globally are a different matter - and yes, there is a difference."

Are you speaking of heap variables? If so, it's far better to call them "heap variables" than "variables that get allocated globally." If you mean something else, please do educate us.

All global variables live in the heap, but not all heap variables are global.

A global variable refers to a static, a singleton, a variable where every function/class refers to the same value.

This is contrasted to a member variable, a local variable or a function argument.

This definition, by convention, tends to exclude global constant primitives.

An excess number of global variables is typically a sign something could be redesigned.

Re: Globally Coupled

2012-01-23 12:16 • by Ben Jammin (unregistered)
I may be confused here. You have a global db connection that everything seems to be using. In several places it is switched to a testdb and then back. Is this for testing the code against a test environment vs a production environment (which the global variable should be changed to point to the test db) or is there some other reason for this?

Re: Globally Coupled

2012-01-23 12:18 • by RichP
TRWTF is that "fred" is used in the comment block, but not used as a temporary variable name.

(one of my CS professors was in the habit of using "fred" as a temp/nonsense variable name in the same manner as "foo" is often used)

Re: Globally Coupled

2012-01-23 12:25 • by PedanticCurmudgeon
372738 in reply to 372730
scott:
Yeah, general "rule of thumb", if you have to count your globals, there's probably something wrong.
FTFY

Re: Globally Coupled

2012-01-23 12:45 • by michael (unregistered)
372741 in reply to 372732
All global variables live in the heap, but not all heap variables are global.

Global variables do not live in the heap. A compiler places them into a static section that is loaded straight into memory at load time. No malloc, no free, no heap.

I was asking about what was meant by "variables that are allocated globally" but which are not global variables. I suspected, perhaps incorrectly, that this was a reference to heap variables, which can be made globally accessible (as opposed to stack and member variables, which can only be used in context).

Re: Globally Coupled

2012-01-23 12:50 • by Boog, I Am Your Father! (aka Behold The Return Of Zunesis!)! (unregistered)
372744 in reply to 372723
stupid:
not frist because aksimet needs sodomizing...
Hear, Hear!

Re: Globally Coupled

2012-01-23 13:05 • by someone (unregistered)
"What I did was to swap out the link identifier"
so the real WTF is his/her solution to the problem imho.

what would hinder him/her to create another global var for db2?

Re: Globally Coupled

2012-01-23 13:05 • by ekolis
Globals can be declared INSIDE FUNCTIONS???

Re: Globally Coupled

2012-01-23 13:15 • by Rcxdude (unregistered)
372747 in reply to 372741
depends on the language. what you said is true for C and C++ but I suspect is not true for PHP, python, etc

Re: Globally Coupled

2012-01-23 13:18 • by Melnorme (unregistered)
372748 in reply to 372741
This is implementation dependent.
"Global variable" is a description of semantic scope, not of generated code.

Re: Globally Coupled

2012-01-23 13:21 • by anonymous (unregistered)
$GLOBALS['db2'] = ...;
and its done -.-

Re: Globally Coupled

2012-01-23 13:24 • by fishdude (unregistered)
Globals can be declared INSIDE FUNCTIONS???


If you want to access a global variable from inside a function in PHP, you first have to run the statement
global $variableName
. If not,
$variableName
is treated as a local variable.

There is also a supoerglobal array called $GLOBALS that would let you access it as $GLOABLS['variableName'].

Good stuff, huh?

Re: Globally Coupled

2012-01-23 13:26 • by Not Jimmy Wales (unregistered)
372751 in reply to 372735
No, that is a comment signed by someone named fred, who apparently worked on the code. It's not the name of a variable.

Ob captcha: WTF is a laoreet? Can I buy one at a petstore? Or is something I shouldn't mention in polite company?

Re: Globally Coupled

2012-01-23 13:44 • by Nagesh
Global variable increase window of vulnerability. This is well known fact. They are necesary evil in our trade.

Re: Globally Coupled

2012-01-23 13:56 • by C-Octothorpe
Is it just me that is shaken by the database calls directly in the page? Separation of concerns anyone?

Someone in this thread mentioned something about PHP supporting OO concepts and all these are bad examples of PHP, and that PHP really is good, really honestly... Please, show an example of what "good" PHP would look like...

Also, PHP is TRWTF, amirite?

</obligatory PHP bashing>

Re: Globally Coupled

2012-01-23 13:57 • by Dazed (unregistered)
372755 in reply to 372730
scott:
Yeah, general "rule of thumb", if you cannot count your globals on your fingers, there's probably something wrong.

I do sometimes use more than that. But only in stand-alone run-to-completion applications, where I initialise them at the beginning and never change them after that.

In server-side applications I generally reckon I can count the global variables on the fingers of one foot.

Re: Globally Coupled

2012-01-23 14:01 • by Zylon
372756 in reply to 372723
stupid:
not frist because aksimet needs shooting...

Anything that frustrates morons such as yourself is a good thing.

Re: Globally Coupled

2012-01-23 14:02 • by Andrew (unregistered)
What madness is this?! How can someone *design* an enterprise-y system with a whole mess of globals?!

This reeks like a simple application became a general application became a cross-functional business corporate scorecard analytical engine...

Re: Globally Coupled

2012-01-23 14:08 • by Chris (unregistered)
TRWTF is how many idiots think that they are clever or sophisticated because they bash PHP.

Re: Globally Coupled

2012-01-23 14:14 • by fuckingCommServerForgotMuUsername (unregistered)
372759 in reply to 372754
C-Octothorpe:
Is it just me that is shaken by the database calls directly in the page? Separation of concerns anyone?

Someone in this thread mentioned something about PHP supporting OO concepts and all these are bad examples of PHP, and that PHP really is good, really honestly... Please, show an example of what "good" PHP would look like...
</obligatory PHP bashing>


You could have a look at the symfony framework code, most of it is quite clean and dandy.

Re: Globally Coupled

2012-01-23 14:25 • by geoffrey (unregistered)
372760 in reply to 372752
Nagesh:
Global variable increase window of vulnerability. This is well known fact. They are necesary evil in our trade.


I could not have said it better myself. When the job needs done, a can-do programmer understands that sometimes everything can't be pretty and perfect, and must resort to these necessary evils.

Re: Globally Coupled

2012-01-23 14:27 • by Darkstar (unregistered)
372761 in reply to 372725
Errhh __FILE__, anyone?

Re: Globally Coupled

2012-01-23 14:39 • by Metro Sauper (unregistered)
Without discussing the pitfalls of globals, doesn't this have a direct refactoring pattern? replace the code of the function which uses globals with a function call to a new function passing in the globals as parameters. Implement the new function using the original code, replacing the global references with the parameters.

This would work for all old invocations and the new code could just call the new function.

Metro.

Re: Globally Coupled

2012-01-23 14:40 • by Nagesh
372765 in reply to 372764
Metro Sauper:
Without discussing the pitfalls of globals, doesn't this have a direct refactoring pattern? replace the code of the function which uses globals with a function call to a new function passing in the globals as parameters. Implement the new function using the original code, replacing the global references with the parameters.

This would work for all old invocations and the new code could just call the new function.

Metro.


And where is budget for code come from? Remember everything need money.

Re: Globally Coupled

2012-01-23 14:51 • by manu (unregistered)
Search results in the screenshot seem to talk about instances of '$sql' ?

Re: Globally Coupled

2012-01-23 14:56 • by The name (unregistered)
372767 in reply to 372765
Nagesh:
Metro Sauper:
Without discussing the pitfalls of globals, doesn't this have a direct refactoring pattern? replace the code of the function which uses globals with a function call to a new function passing in the globals as parameters. Implement the new function using the original code, replacing the global references with the parameters.

This would work for all old invocations and the new code could just call the new function.

Metro.


And where is budget for code come from? Remember everything need money.

Selling sex.

Re: Globally Coupled

2012-01-23 15:03 • by Pecos Bill
372768 in reply to 372732
Why'd he turn it down?:
An excess number of global variables is typically a sign something could be redesigned.

We knew that! ;-) I'd take it beyond "could be" and "typically" but most often have seen "as if" and "rarely is"

Re: Globally Coupled

2012-01-23 15:04 • by Anonymous (unregistered)
Not a single comment has pointed out that Amber is female????

Re: Globally Coupled

2012-01-23 15:09 • by Nagesh
372770 in reply to 372769
Anonymous:
Not a single comment has pointed out that Amber is female????



Ambar mean sky in Sanskrit. It is boy's name.

Re: Globally Coupled

2012-01-23 15:13 • by Nagesh
372771 in reply to 372746
ekolis:
Globals can be declared INSIDE FUNCTIONS???


Not to best of my knowledge and my knowledge is best as far as I know.

Re: Globally Coupled

2012-01-23 15:31 • by PiisAWheeL
I once read an article that stated that some companies make a mistake of "redesigning something from scratch" when the should have just fixed what was broken, and that there is rarely a reason to redesign from scratch.

This I believe falls into the "redesign from scratch" catagory.

Re: Globally Coupled

2012-01-23 15:45 • by Nag-Geoff (unregistered)
372774 in reply to 372773
PiisAWheeL:
I once read an article that stated that some companies make a mistake of "redesigning something from scratch" when the should have just fixed what was broken, and that there is rarely a reason to redesign from scratch.

This I believe falls into the "redesign from scratch" catagory.


Surely, you live in fantasy world.

Re: Globally Coupled

2012-01-23 15:53 • by Jeff (unregistered)
TRWTF is not PHP, TRWTF how we allow armies of incompetent grunts to work as programmer.

Im pretty sure one can create an enterprisey Fecosystem in any langage.

Re: Globally Coupled

2012-01-23 15:58 • by ekolis
372776 in reply to 372750
fishdude:
Globals can be declared INSIDE FUNCTIONS???


If you want to access a global variable from inside a function in PHP, you first have to run the statement
global $variableName
. If not,
$variableName
is treated as a local variable.


Oh, so you're not actually declaring new ones, you're just importing existing ones... that makes sense... sort of...

Re: Globally Coupled

2012-01-23 16:27 • by Gibbon1 (unregistered)
372779 in reply to 372760
geoffrey:
Nagesh:
Global variable increase window of vulnerability. This is well known fact. They are necesary evil in our trade.


I could not have said it better myself. When the job needs done, a can-do programmer understands that sometimes everything can't be pretty and perfect, and must resort to these necessary evils.


I often think that I'm choosing between two evils. Sometimes one is an orthodox evil, global's, goto's, functions with side effects, all these are evil, but sometimes to avoid them brings on worse evils.

Re: Globally Coupled

2012-01-23 16:50 • by Matt Westwood (unregistered)
372780 in reply to 372735
RichP:
TRWTF is that "fred" is used in the comment block, but not used as a temporary variable name.

(one of my CS professors was in the habit of using "fred" as a temp/nonsense variable name in the same manner as "foo" is often used)


In the UK (England in particular), "Fred" is used as a generic joke name. From what I understand from the lyric sheets of a certain Jefferson Airplane album, the same applies to a greater or lesser extent in the US too, yeah?

Re: Globally Coupled

2012-01-23 16:51 • by Matt Westwood (unregistered)
372781 in reply to 372755
Dazed:
scott:
Yeah, general "rule of thumb", if you cannot count your globals on your fingers, there's probably something wrong.

I do sometimes use more than that. But only in stand-alone run-to-completion applications, where I initialise them at the beginning and never change them after that.

In server-side applications I generally reckon I can count the global variables on the fingers of one foot.


I take it you're a code monkey?

Re: Globally Coupled

2012-01-23 17:03 • by frotl (unregistered)
372782 in reply to 372725
Fyrilin:
This is why people don't like PHP; they see code like this.

As a PHP supporter, I have to point out that PHP DOES allow OOP including some great frameworks.

Just preempting the PHP bashing.

captcha: damnum: my thoughts on this code


Someone could do significantly better than the above without using OOP. Bad programmers are bad whether its procedural programming or object oriented programming.

Re: Globally Coupled

2012-01-23 17:06 • by LOADING (unregistered)
372783 in reply to 372776
ekolis:
fishdude:
Globals can be declared INSIDE FUNCTIONS???


If you want to access a global variable from inside a function in PHP, you first have to run the statement
global $variableName
. If not,
$variableName
is treated as a local variable.


Oh, so you're not actually declaring new ones, you're just importing existing ones... that makes sense... sort of...


Make sense. Javascript opts for var to declare a variable locally. PHP opts for global to import from the global scope. They are opposites...

Re: Globally Coupled

2012-01-23 17:26 • by Jack (unregistered)
372784 in reply to 372770
Nagesh:
Anonymous:
Not a single comment has pointed out that Amber is female????



Ambar mean sky in Sanskrit. It is boy's name.


"Amber" means fossilized tree resin in English, and it is a girl's name. But why should someone want to point that out?

Re: Globally Coupled

2012-01-23 17:31 • by anon (unregistered)
372785 in reply to 372765
Nagesh:
Metro Sauper:
Without discussing the pitfalls of globals, doesn't this have a direct refactoring pattern? replace the code of the function which uses globals with a function call to a new function passing in the globals as parameters. Implement the new function using the original code, replacing the global references with the parameters.

This would work for all old invocations and the new code could just call the new function.

Metro.


And where is budget for code come from? Remember everything need money.


...and the bugs that stem from such ridiculous code cost money too.... it's a pretty easy argument to forge IMHO. You could also implement changes bit by bit, that is the true way of getting old code improved, don't try to justify doing it in one big hit... most of that can be done "out of scope" with other development too

Re: Globally Coupled

2012-01-23 17:39 • by Kasper (unregistered)
Seems many languages permit global variables and few languages prevent global variables. Is that because we are using really old languages from before anybody knew how to structure code? Or are new languages still designed with support for global variables?
« PrevPage 1 | Page 2Next »

Add Comment