• (cs) in reply to Kasper
    Kasper:
    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?
    It's not usually too evil for a top-level application to use global variables; just a little bit evil, like pulling bad faces at next-door's cat. But doing this for any code that is (or should be) in a library is Bad (with a few very limited exceptions that don't really apply to this discussion).
  • PHP, say wha?! (unregistered)

    Anyone else notice how butt hurt PHP programmers seem to get when anyone bad mouths their language at all? Saying how only bad developers write bad code!

    Yet somehow that compassion disappears when it's VB, cause that's TRWTF am i rite?

    At least we don't need to import our globals... wtf?!?

    Captcha: Plaga - What is a bad developer!

  • Synchronos (unregistered) in reply to ekolis
    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...

    You can declare new globals just the same way. If the variable doesn't exist, it will be created. So you really do can declare globals inside functions in PHP.

  • iToad (unregistered)

    There's a popular chemistry blog that has a section called "Things I Won't Work With". PHP seems to be the software equivalent of thioacetone.

  • Gary B (unregistered) in reply to PiisAWheeL

    Actually I've been there, done that - in fact the biggest system at my present employer started out just like the one described. But over time, I and later others have gradually been migrating the old code to a new system that uses OO. It's generally not that big a deal to slice off a section that sits fairly well by itself, and create a class around it. Then one can pass (for example) the db connection handle as a parameter to the constructor. I don't work on that system any more (haven't for several years) but these 'baby steps' have fixed a lot of problems on pieces of code as they have been subject to modification for other reasons, without ever having a budget to do a big changeover.

    It's worth noting that redesigning from scratch (I've done that before as well, with a code base of several hundred thousand lines of code in multiple languages.) is almost NEVER a good idea. This amounts to trying to build a ferrari that is backwards compatible with a Model T. You have to duplicate the 'bugs' as well as the 'features', and you rarely know which exists, much less which is which.

    Also, these projects almost always take 2 or 3 times as much time and effort (= money) as planned, and have a much higher probability of failure. Refactoring piecewise can be difficult, but can almost always be done piecewise, and limits development risk.

  • (cs) in reply to iToad
    iToad:
    There's a popular chemistry blog that has a section called "Things I Won't Work With". PHP seems to be the software equivalent of thioacetone.
    http://pipeline.corante.com/archives/things_i_wont_work_with

    A great page. Regarding triazadienyl fluoride, he has these thought-provoking words (italics mine):

    "...The next step is introduction of the fluorine, and when elemental fluorine is the most easily handled reagent in your scheme, let me tell you, you're in pretty deep."

  • (cs) in reply to Kasper
    Kasper:
    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?
    As shown, PHP permits them, but you have to ask for them, instead of them just being given to you by default (because why would you want them by default?). Even in the case of lexical closures you have to explicitly declare which variables in the outer scope you want to import.
  • (cs) in reply to PHP, say wha?!
    PHP:
    Anyone else notice how butt hurt VB programmers seem to get when anyone bad mouths their language at all? Saying how only bad developers write bad code!

    Yet somehow that compassion disappears when it's PHP, cause that's TRWTF am i rite?

    There, that makes more sense.

  • teabag (unregistered)

    Off topic: Somebody knows how the pictures in the topic got obfuscated?

  • (cs) in reply to iToad
    iToad:
    There's a popular chemistry blog that has a section called "Things I Won't Work With". PHP seems to be the software equivalent of thioacetone.

    Yep. Talk about code smell.. :-)

    (My personal favourite would be the FOOF or ClF3 entries though.. :-)

    Yazeran

    Plan: To go to Mars one day with a hammer

  • Smitt-Tay (unregistered)

    Opening a DB connection shouldn't be a significant processing burden for either the client app or the DB in question.

    The idea that you open a database connection once, and then hang on to it indefinitely is stupid.

  • (cs)

    Nice to see a fellow NetBeans user.

  • TrXtR (unregistered)

    I'm sure with 'enterprisey', they mean small company needing internal tools... no real enterprise system can exist from php :-P

  • Steve (unregistered) in reply to michael
    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.

    int myGlobal = 0; // This is a global variable. This is bad.

    int someFunc() { // trivial example of a variable that is, technically, globally allocated but isn't within global scope static myStaticVariable = 1; return myStaticVariable++; }

    int main() { int someInt = myStaticVariable; // Compiler error, not in sope

    return; }

  • rfoxmich (unregistered)

    Using php correctly in web applications is a piece of Cake(PHP)

  • Marc (unregistered) in reply to 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.
    It will pay for itself - the budget comes from maintenance time saved.
  • (cs) in reply to Marc
    Marc:
    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.
    It will pay for itself - the budget comes from maintenance time saved.

    Yes, but first 'down the line' and as we all know no PHB will authorise an 'unnecessary' expense this year (even if it means savings next year)

  • corroded (unregistered) in reply to Nagesh
    Nagesh:
    ekolis:
    Globals can be declared INSIDE FUNCTIONS???

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

    Your knowledge isn't best as far as I know.

    You can declare a global inside a function just fine within PHP.

  • (cs) in reply to corroded
    corroded:
    Nagesh:
    ekolis:
    Globals can be declared INSIDE FUNCTIONS???

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

    Your knowledge isn't best as far as I know.

    You can declare a global inside a function just fine within PHP.

    Unfortunate, I disagree.

  • (cs) in reply to Nag-Geoff
    Nag-Geoff:
    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.

    Yes... but its ok... They know me here.

  • (cs) in reply to Jack
    Jack:
    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?

    I think bolding it cause it to get pointed.

  • Blum Blum Shub (unregistered) in reply to Smitt-Tay
    Smitt-Tay:
    Opening a DB connection shouldn't be a significant processing burden for either the client app or the DB in question.

    Still, why do it if you dont have to? Sure, your code may be "only" 5% faster without it - but all those 5%s add up.

    Smitt-Tay:
    The idea that you open a database connection once, and then hang on to it indefinitely is stupid.

    Yes, that would be stupid. Fortunately, the connection is automagically closed when the script finishes, so the problem doesnt arise.

    And while we are on the subject, hating on globals for its own sake without understanding why is every bit as bad as using them for its own sake without understanding why.

    I think the problem is something to do with the "without understanding" part, not with the "globals" part.

    HTH.

  • ClaudeSuck.de (unregistered) in reply to Darkstar
    Darkstar:
    Errhh __FILE__, anyone?

    Not Found!

  • ClaudeSuck.de (unregistered) in reply to Nag-Geoff
    Nag-Geoff:
    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.

    It's the same like with documentation: we make it when we have time

  • corroded (unregistered) in reply to Nagesh
    Nagesh:
    corroded:
    Nagesh:
    ekolis:
    Globals can be declared INSIDE FUNCTIONS???

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

    Your knowledge isn't best as far as I know.

    You can declare a global inside a function just fine within PHP.

    Unfortunate, I disagree.

    Maybe you should write code more instead of badly trolling?

  • golddog (unregistered) in reply to RichP
    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)

    Wow. Just wow.

    Everyone knows it's "bob".

  • Franta (unregistered) in reply to michael
    michael:
    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).

    Come on, you can have local variable in

    int main()
    , that can be accessed from all over the place, because it simply lives long enough to be used :)

  • (cs)

    Just think what this fabulous usage pattern would be like in a multi-threaded application.

    (Even the thought gives me nightmares.)

  • spurting whitewash into the white-hot skies of Minraud (unregistered) in reply to Dazed
    Dazed:
    I initialise them at the beginning and never change them after that.
    Its global *variables* that are the problem under discussion, not global constants
  • (cs)

    You had me at "PHP".

  • (cs) in reply to Franta
    Franta:
    Come on, you can have local variable in
    int main()
    , that can be accessed from all over the place, because it simply lives long enough to be used :)

    Smartass.

  • Gibbon1 (unregistered) in reply to Blum Blum Shub
    Blum Blum Shub:
    Smitt-Tay:
    Opening a DB connection shouldn't be a significant processing burden for either the client app or the DB in question.

    Still, why do it if you don't have to? Sure, your code may be "only" 5% faster without it - but all those 5%s add up.

    I agree don't think having a utility functions open and closing db connections is really good programming practice. Also get into that habit and will come a day when someone removes the code that closes the db connection.

    Blum Blum Shub:
    And while we are on the subject, hating on globals for its own sake without understanding why is *every bit* as bad as using them for its own sake without understanding why.

    I think the problem is something to do with the "without understanding" part, not with the "globals" part.

    It's in the same vein as 'don't end a sentence with a preposition, never start a sentence with the word and. In some cases there is no other option (interrupt routines for instance) Or a small program, that does one thing and runs once through, global's aren't bad.

    The PHP program, the real wtf is the culture that allowed that problem to fester for so long with no attempt to mitigate it. No attempt at all to deal with maintenance issues.

  • AN AMAZING CODER (unregistered) in reply to Nagesh

    Heard of Technical Debt?

    In the long run, the little time spent incrementally refactoring would save the company more money than continuing to make the code less maintainable.

    Granted, there's a "fuck it" point, but that's besides the point of this WTF.

  • (cs) in reply to Gibbon1
    Gibbon1:
    The PHP program, the real wtf is the culture that allowed that problem to fester for so long with no attempt to mitigate it. No attempt at all to deal with maintenance issues.

    It's a culture that tolerates people even when they freely admit to not understanding how to use arrays or functions and prefer to avoid finding out.

  • (cs) in reply to AN AMAZING CODER
    AN AMAZING CODER:
    Heard of Technical Debt?

    In the long run, the little time spent incrementally refactoring would save the company more money than continuing to make the code less maintainable.

    Granted, there's a "fuck it" point, but that's besides the point of this WTF.

    I have read of technical debt, but have you heard of "golden goose"? Any system that is total mess up that no sane programmer is willing to go near it is a golden goose for service providing company.

  • (cs)
    Amber continues, "what happened here was that, in file A, a global database link identifier pointed to database 1 but in the include file B, the same variable was supposed to point to database 2.

    This is utter nonsense. Use a different variable in include file B. It is not expensive to use a different variable. Even if you use globals all over, why not GlobalProdDB, GlobalDevDB, etc? You can't go around "temporarily" switching a global var that points to a database. People are talking to the database every microsecond, apparently using that single variable. And of course if you're being sane, just pass the db connection string/object/whatever this is as a parameter.

  • (cs) in reply to Yazeran
    Yazeran:
    Marc:
    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.
    It will pay for itself - the budget comes from maintenance time saved.

    Yes, but first 'down the line' and as we all know no PHB will authorise an 'unnecessary' expense this year (even if it means savings next year)

    It wouldn't take much longer than this fix did. And unlike this abomination of a fix, people in production won't be talking to the test database for brief period where the db con variable is reassigned. How many orders inserted into the test database instead of the prod database do you imagine it would take to cover 20 extra minutes of coding?

  • (cs) in reply to Gibbon1
    Gibbon1:
    I agree don't think having a utility functions open and closing db connections is really good programming practice. Also get into that habit and will come a day when someone removes the code that closes the db connection.

    Wait... what? You DON'T think it's a good idea for a utility function to manage the db connections? You think it's better for every dev to manage it manually every single time they talk to the database? Which is more error prone, copy/pasting 1000 times, or one core library that is re-used?

    Gibbon1:
    Also get into that habit and will come a day when someone removes the code that closes the db connection.
    You don't have source control and dev/qa systems?
  • (cs) in reply to wbrianwhite
    wbrianwhite:
    Amber continues, "what happened here was that, in file A, a global database link identifier pointed to database 1 but in the include file B, the same variable was supposed to point to database 2.

    This is utter nonsense. Use a different variable in include file B. It is not expensive to use a different variable. Even if you use globals all over, why not GlobalProdDB, GlobalDevDB, etc? You can't go around "temporarily" switching a global var that points to a database. People are talking to the database every microsecond, apparently using that single variable. And of course if you're being sane, just pass the db connection string/object/whatever this is as a parameter.

    That is purpose of properties files in java.

  • Xenious (unregistered)

    captcha: Asshole.

  • Neil (unregistered)

    I still don't get why the production application is accessing the test database. (Or indeed, why it needs more than one database at all.)

  • (cs) in reply to Neil
    Neil:
    I still don't get why the production application is accessing the test database. (Or indeed, why it needs more than one database at all.)

    Sometime it is not possible to have test system. I provide one example.

    1. You are working on client website and client want to introduce credit card payment on website.
    2. You call some company, they give you payment gateway Api.
    3. You set up API and use test credit card number that the company give you.

    Ask yourself, is that true test of the system? How will you trust the system to work with real credit card? can you use your own credit card to test?

    IN CONCLUSION, NOT POSSIBLE TO HAVE TEST SYSTEM.

    Kthxbai.

  • (cs) in reply to Nagesh
    Nagesh:
    Neil:
    I still don't get why the production application is accessing the test database. (Or indeed, why it needs more than one database at all.)

    Sometime it is not possible to have test system. I provide one example.

    1. You are working on client website and client want to introduce credit card payment on website.
    2. You call some company, they give you payment gateway Api.
    3. You set up API and use test credit card number that the company give you.

    Ask yourself, is that true test of the system? How will you trust the system to work with real credit card? can you use your own credit card to test?

    IN CONCLUSION, NOT POSSIBLE TO HAVE TEST SYSTEM.

    Kthxbai.

    So your solution is to spend real money buying items in the test database? GENIUS.

  • (cs) in reply to Nagesh
    Nagesh:
    Neil:
    I still don't get why the production application is accessing the test database. (Or indeed, why it needs more than one database at all.)

    Sometime it is not possible to have test system. I provide one example.

    1. You are working on client website and client want to introduce credit card payment on website.
    2. You call some company, they give you payment gateway Api.
    3. You set up API and use test credit card number that the company give you.

    Ask yourself, is that true test of the system? How will you trust the system to work with real credit card? can you use your own credit card to test?

    IN CONCLUSION, NOT POSSIBLE TO HAVE TEST SYSTEM.

    Kthxbai.

    Is it just my long absence, or did Nagesh get a whole hell of a lot funnier? Seriously, dude, that was good!

  • (cs) in reply to wbrianwhite
    wbrianwhite:
    Nagesh:
    Neil:
    I still don't get why the production application is accessing the test database. (Or indeed, why it needs more than one database at all.)

    Sometime it is not possible to have test system. I provide one example.

    1. You are working on client website and client want to introduce credit card payment on website.
    2. You call some company, they give you payment gateway Api.
    3. You set up API and use test credit card number that the company give you.

    Ask yourself, is that true test of the system? How will you trust the system to work with real credit card? can you use your own credit card to test?

    IN CONCLUSION, NOT POSSIBLE TO HAVE TEST SYSTEM.

    Kthxbai.

    So your solution is to spend real money buying items in the test database? GENIUS.

    My solution is not exist. There is no solution. This is what I am saying, but you're not willing to accept it. Step in my shoes and you will know what I talk of.

  • Ryan (unregistered)

    Legitimately, although I truly respect the people who have the tenacity to work in that kind of garbage, I'd quit.

    Everybody's different, but having to maintain code like that would be worse than taking a paycut. A friend of mine offered to get me an interview at a company that develops using a framework that I feel is only capable of producing horrible code, and I declined to interview...despite the fact that the wages would be roughly 1.8 times my current salary. My sanity is worth far more than $90,000/year.

  • amber (unregistered) in reply to Ben Jammin

    I know the name testdb implied test environment but it actually pointed to a production database...Yea, it got me too when I started.

  • amber (unregistered) in reply to someone

    Guilty as charged. It's another WTF just to ensure that the next function call, which required that global identifer to point to the correct database, would work without having to modify the function itself, and going around fixing the dependencies from extra tight coupling.

  • amber (unregistered) in reply to manu
    manu:
    Search results in the screenshot seem to talk about instances of '$sql' ?

    I renamed $sql to $dbCon in the code to clarify that it is connection identifier.

  • (cs) in reply to Nagesh
    Nagesh:
    wbrianwhite:
    Nagesh:
    Neil:
    I still don't get why the production application is accessing the test database. (Or indeed, why it needs more than one database at all.)

    Sometime it is not possible to have test system. I provide one example.

    1. You are working on client website and client want to introduce credit card payment on website.
    2. You call some company, they give you payment gateway Api.
    3. You set up API and use test credit card number that the company give you.

    Ask yourself, is that true test of the system? How will you trust the system to work with real credit card? can you use your own credit card to test?

    IN CONCLUSION, NOT POSSIBLE TO HAVE TEST SYSTEM.

    Kthxbai.

    So your solution is to spend real money buying items in the test database? GENIUS.

    My solution is not exist. There is no solution. This is what I am saying, but you're not willing to accept it. Step in my shoes and you will know what I talk of.

    Nagesh has many personalities. This one is "Funny Nagesh". Some other personalities include: "Boring Nagesh", "Incoherent Nagesh", "Blantly Feke Hinglish Nagesh", "Hates boog Nagesh", "Hates frits Nagesh", "Hates boog and frits Nagesh", and the rare "Serious Nagesh".

Leave a comment on “Globally Coupled”

Log In or post as a guest

Replying to comment #:

« Return to Article