PHP, the Enterprise, and copypasta?



  • From php.net: (http://www.php.net/manual/en/language.oop5.patterns.php

    A singleton is the "enterprise professional scalable business solution" version of a global variable. It'd be a good idea if this were the IOCCC, but since it isn't, it's better to simply say:

    global $lol;

    It's also faster and more maintainable, and has the same advantages and disadvantages.

    Also, I'll advise against all "enterprise object-oriented industry-standard design patterns". Some of these patterns are correct, but their existence is not. Programming is not about copypasta. It's about thinking, abstracting and combinating. If you can't think of these patterns for yourself when you need them — and in the case of needing them, manage to abstract them so that your code doesn't become a copypasta fest, then you shouldn't be programming in the first place.

    Furthermore, the existence of software patterns is a proof of the object-oriented model shortcomings. You shouldn't ever need to do any copypasta in your code. If something is done two or more times, you abstract it. And the language should support a way for you to. There's no reason for a "design pattern" to exist more than once, and since they are trivial (they better be trivial to you, if you are programming), you don't need to study them.

    The best advice I can give you is that instead of reading "design patterns", you read Structure and Interpretation of Computer Programs (SICP), a book freely available from MIT where you will learn what you need to program properly in any programming language.

    i don't think that i really need to say anything else. 



  • @iks said:

    [/snip] Programming is not about copypasta. [snip]

    MMmmm... copypasta, now in tomato sauce!



  • Wow, what a moron.

    Design patterns aren't about copy and pasting code, but I'm sure I'm preaching to the choir by stating this here.

    His rant about design patterns just really show that he doesn't understand them. 



  • Furthermore, the existence of software patterns is a proof of the object-oriented model shortcomings. You shouldn't ever need to do any copypasta in your code. If something is done two or more times, you abstract it. And the language should support a way for you to

    Is he trying to say that OOP leads to copying/pasting of code?

    And here I was thinking that OOP actually helps with abstraction and less copy / pasting!

    Silly me.



  • @yet another Matt said:

    @iks said:
    [/snip] Programming is not about copypasta. [snip]

    MMmmm... copypasta, now in tomato sauce!

     

    Oh man i am SO hungry, thankfully lunch is comming up.

    Copypasta... The best food there be!



  • @SpoonMeiser said:

    His rant about design patterns just really show that he doesn't understand them. 

    Just like 90% of the people that use them ;)

    Guns are not bad, just when you shoot people with them. 



  • I somewhat agree with the anti-design-pattern blurb. Some people stick to them like they were God commandments, and refuse to think thereafter.

    But I can't resist to... #define TRUE (TRWTF == PHP)



  •  Browsing the posts under the php.net documentation for any commonly used function is a great source of WTFs.



  •  I hear carbonara has a lot of copypasta in it.



  • I understand where he's coming from, and I agree. He's basically saying that you shouldn't code by copypasting together canned blocks of logic (patterns), which enterprise programmers tend to do because these patterns are available in such an easy, copy-pastable way, and very often with existing abstract implementations in the framework. Very often this leads to needlessly overengineered solutions, in which the programmer doesn't actually have an understanding of what's going on.

    Patterns are bits of logic to which people have arrived over experimentation and understanding of the best logic needed to achieve some kind of result. For a good programmer, patterns are simply names for bits of logic that one could have easily arrived at on his own, simply because it makes logical sense to do things in this way. Patterns, for a good programmer, are a handy, standard, naming convention when you discuss these bits of logic with other people. A good programmer would realize that if you need a global variable, PHP provides a good mechanism in the form of the 'global' keyword, and that a Singleton would only offer the advantage of automatic instantiation.

    On the other hand, lots of people out there read a book on patterns, and go 'Hey, I could smack a singleton here! Why? Because the book said that when you need a global object, you should use a singleton! That's the Enterprise way!". In this case, patterns become the WYSIWYG editors of coding logic, leading to horrible, braindead code. It doesn't help that lots of patterns have started to have abstract implementations in languages and frameworks, leading to people having FactoryFactory::getInstance()->getFactory('MyObjectFactory')->getObject('MyObject') all over their code for no good reason other than the urge to reuse someone's abstract factory class. That's the copypasta at work.

    If you understand OOP, and programming logic in general, then patterns will come to you naturally. If you don't, then patterns are shiny things you put in your programs to make it enterprisey. That's what the rant is about.



  • Some Singletons is implemented as an object that is in a global variable.

    And they does not need to be instantiated directly either.



  • @m0ffx said:

     I hear carbonara has a lot of copypasta in it.

    Groan.

    By the way: I have an Apple-related WTF, but it's kind of obscure, so I've been hesitating to put it up. ("Kind of obscure" as in "requires about three dense paragraphs to explain the setup to this Windows-centric audience".) Should I post it?



  • @Sunstorm said:

    I understand where he's coming from, and I agree. He's basically saying that you shouldn't code by copypasting together canned blocks of logic (patterns), which enterprise programmers tend to do because these patterns are available in such an easy, copy-pastable way, and very often with existing abstract implementations in the framework. Very often this leads to needlessly overengineered solutions, in which the programmer doesn't actually have an understanding of what's going on.

    Patterns are bits of logic to which people have arrived over experimentation and understanding of the best logic needed to achieve some kind of result. For a good programmer, patterns are simply names for bits of logic that one could have easily arrived at on his own, simply because it makes logical sense to do things in this way. Patterns, for a good programmer, are a handy, standard, naming convention when you discuss these bits of logic with other people. A good programmer would realize that if you need a global variable, PHP provides a good mechanism in the form of the 'global' keyword, and that a Singleton would only offer the advantage of automatic instantiation.

    On the other hand, lots of people out there read a book on patterns, and go 'Hey, I could smack a singleton here! Why? Because the book said that when you need a global object, you should use a singleton! That's the Enterprise way!". In this case, patterns become the WYSIWYG editors of coding logic, leading to horrible, braindead code. It doesn't help that lots of patterns have started to have abstract implementations in languages and frameworks, leading to people having FactoryFactory::getInstance()->getFactory('MyObjectFactory')->getObject('MyObject') all over their code for no good reason other than the urge to reuse someone's abstract factory class. That's the copypasta at work.

    If you understand OOP, and programming logic in general, then patterns will come to you naturally. If you don't, then patterns are shiny things you put in your programs to make it enterprisey. That's what the rant is about.

     

    I want to do a copypasta of your text into a t-shirt and wear it.



  • He's (mostly) right, with a few exceptions.

    Design patterns simply define solutions to common and well-known problems.  The "spirit" of a design pattern is separate from its implementation.  He's ranting against a common method of implementation.

    The existence of a design pattern as copy/paste boilerplate code (often seen in Java) indicates a shortcoming in the language itself.  Nearly all of this sort of code duplication disappears when you have a dynamic type system and first-class functions (or closures) at your disposal.  You can't criticize the practice copy/pasting of commonly used code in a language that doesn't allow for abstraction!

    I also have issue with impying that "if you can't think of these patterns for yourself" you're a moron.  I do, however, think that if you can't learn about the pattern (or refer to the idea) and implement it without copy/pasting code then you have room for improvement.



  • Variables named $lol, use of the term "copypasta", and references to SICP. Yup, we've just buried the needle on my 4chan troll detector.



  • Oh, I see it. He's an idiot, but he's been talking to somebody who knows what they are talking about. This is his attempt at repeating what he was told, but he clearly didn't manage to understand it, so has mangled the points. Here's what somebody was explaining to him originally:

    A singleton in Java or C++ can be implemented by using a static instance of the class in global scope. The only reason not to is if you really want to allocate it lazily; most of the time, you should be using a global rather than wasting time on a singleton wrapper. (I do not think that this works in PHP)

    In any reasonably well-designed language, any worthwhile "design patterns" will be standard library functions. Notable examples are common lisp and Haskell; a notable example of a language that lacks this capability is C++.

    Lego-brick programming does not work.

    Enterprise consultants are worthless slime.



  • Wow, this is a real shock; a procedural/script-oriented coder who doesn't understand the difference between a singleton and a global variable.

    Just in case anybody's confused - singletons, when implemented correctly, are immutable, lazy, and thread safe.  Global variables are none of the above.  You can certainly hack together these properties using a global variable and some additional functions, but by then you're just implementing an ersatz singleton.

    Nothing wrong with global variables under certain circumstances.  His example isn't one of them.

    And indeed, design patterns generally are indicative of deficiencies in the language or environment, but I'm not sure what his point is.  Procedural languages such as PHP have those same deficiencies in addition to all the deficiencies solved by OOP.  Smart designers and programmers, instead of bitching about the problem, are slowly but surely ironing out this grind with automatic properties, extension methods, LINQ, that sort of thing.  What has PHP.net contributed to this landscape?

     

    Edit: And Haskell?  Show me one worthwhile application written in that.  They've barely managed to hack together text-based I/O.  Maybe it will get better, but to claim that Haskell is anything but horrendously feature-deficient is, well, ridiculous. 

    <hints id="hah_hints"></hints>


  • @Aaron said:

    singletons, when implemented correctly, are immutable, lazy, and thread safe.  Global variables are none of the above.

    Global variables are made immutable by const in C++ and final in Java, as any first-year undergrad could tell you. They are thread safe unless you completely bugger up a run-time dynamic code loader, because they are constructed at load time before any code runs and then never updated because they're immutable.

    Unless you need lazy instantiation, use a global instance. Anything more is enterprise-style time wasting.



  • @Aaron said:

    Haskell?  Show me one worthwhile application written in that.

     

    Wikipedia suggests LOLITA (a natural-language processor) and Darcs (a version control system).  A good look at those would probably answer your question pretty well, one way or the other.

     



  • @iks said:

    Also, I'll advise against all "enterprise object-oriented industry-standard design patterns". Some of these patterns are correct, but their existence is not. Programming is not about copypasta. It's about thinking, abstracting and combinating. If you can't think of these patterns for yourself when you need them — and in the case of needing them, manage to abstract them so that your code doesn't become a copypasta fest, then you shouldn't be programming in the first place.

     

    It's a common misconception (and probably a common mispractice) that using design patterns means copying and pasting.  It doesn't.  It's a way of evaluating some standard ways of solving a specific problem, and if one works, providing some guidelines about the details.  If you architect a particular object around a state machine, you don't start by copying and pasting a state machine.  Still, thinking of the object's behavior according to that basic pattern is very useful.

    Imagine this scenario: You're just about to go home at 8pm, you're second to the last person in the office and an urgent bug request comes in from the last person in the office.  The bug is in somebody else's code, but you wind up taking it anyway, because some critical thing or other depends on it and can't wait.  Might working on foreign code that somewhat faithfully executes a well-known design pattern be preferable to some alternatives?



  • @The Vicar said:

    @m0ffx said:

     I hear carbonara has a lot of copypasta in it.

    Groan. By the way: I have an Apple-related WTF, but it's kind of obscure, so I've been hesitating to put it up. ("Kind of obscure" as in "requires about three dense paragraphs to explain the setup to this Windows-centric audience".) Should I post it?
     

    Go ahead.  Most of the people here would miss your point (regardless of what it is) then start arguing about bush, taxes, and/or food anyway.  Oh wait, you said Apple.  In that case it'll degenerate into how jacked up Vista is.

    Livin' it up at the hotel California...

     

     



  • @arty said:

    Imagine this scenario: You're just about to go home at 8pm, you're second to the last person in the office and an urgent bug request comes in from the last person in the office.  The bug is in somebody else's code, but you wind up taking it anyway, because some critical thing or other depends on it and can't wait.  Might working on foreign code that somewhat faithfully executes a well-known design pattern be preferable to some alternatives?

    People have been throwing the "imagine if everybody wrote their code the same way, wouldn't that make life so much easier?" line about since the 1950s. The problem is that it doesn't actually work. Software is not "hard to work with because people don't work the same way". It's hard because every problem you face is different and requires actual thinking. The bug is almost always in the part of the code that is not copied from a textbook, or sometimes in the fact that the code was copied from a textbook.



  • Combinating.  Hmm!  Looks like I learned a new non-word! 



  • @operagost said:

    Combinating.  Hmm!  Looks like I learned a new non-word! 

    It's the action of using combinators



  • @asuffield said:

    Oh, I see it. He's an idiot, but he's been talking to somebody who knows what they are talking about. This is his attempt at repeating what he was told, but he clearly didn't manage to understand it, so has mangled the points. Here's what somebody was explaining to him originally:

    I think you are right.  Although if you're going to be an idiot, it's better to be an anti-pattern idiot than a pattern-whore idiot.

    Not all of us were lucky enough to learn about patterns through a skilled mentor, though. I had a year or so of pattern-love where my answer to every problem was to try shoehorning it into some pattern. You have to learn how to use something before you can learn when not to use it.

     



  • @yet another Matt said:

    @iks said:
    [/snip] Programming is not about copypasta. [snip]

    MMmmm... copypasta, now in tomato sauce!

    Surely "copypasta" = "spaghetti code 2.0"? It's more versatile. E.g. for a layered architecture you can use lasagne code; build a listener with orichiette code; obviously you want ravioli code for your wrappers ... it's all still copypasta.



  • Copypasta - A combination of copy/paste code and spaghetti code. 



  • Another WTF on php doc website is on the do while page and comes from the php team itself:
    [url]http://www.php.net/manual/en/control-structures.do.while.php[/url]

    Don't worry if you don't understand this right away or at all. You can code scripts and even powerful scripts without using this 'feature'.

    Sorry but if a "programmer" can't understand at all the difference between a while and a do while, he better stop trying to code "powerful scripts".



  • @Aaron said:

    Haskell? Show me one worthwhile application written in that.

    Well, there's a version control system, a tiling window manager, a Haskell compiler whose output can rival gcc-compiled C for speed, a perl6 implementation, even a first-person shooter. I could go on.



    Oh, and it's used by Credit Suisse, Galois, Nokia, and BarCap, among others..
    @Aaron said:
    They've barely managed to hack together text-based I/O.

    Hahahaha. That line makes it perfectly clear you're arguing against something about which you know practically nothing. I think Paul Graham would say you're suffering from a bit of a Blub paradox.



  • @SpoonMeiser said:

    Wow, what a moron.

    Design patterns aren't about copy and pasting code, but I'm sure I'm preaching to the choir by stating this here.

    His rant about design patterns just really show that he doesn't understand them. 

     

    Is this why everyone is so clueless about the importance of desktop search?

     


     



  • Actually, I agree with the quoted text on many aspects.
    Must be my englesh.



  • @asuffield said:

    The bug is almost always in the part of the code that is not copied from a textbook, or sometimes in the fact that the code was copied from a textbook.

     

    That's... Almost like not saying anything at all.

    Most design patterns are so trivial that you'd easily invent them yourself. A facade, for example. However, some are a little more involved, a little less trivial.

    When you come to a problem that can be solved using a design pattern, you're basically solving a problem that has already been solved, and documented. You might aswell at least look at the existing solution. Design patters have been designed, and refined by smart people, and tested in many applications, I have come across many examples of coders who have had an idea of how to solve a problem, and gone ahead with their solution, which turns out to be similar to an existing design pattern, but not as well thought out and far less elegant.

    I've never seen a situation where using a design pattern would mean copying and pasting code. It simply means having a clear idea of what you need to do to solve a problem because you've read about it, and not having to hack through it yourself.

     



  • @SpoonMeiser said:

    I have come across many examples of coders who have had an idea of how to solve a problem, and gone ahead with their solution, which turns out to be similar to an existing design pattern, but not as well thought out and far less elegant.

    That's funny, because while I've seen a few of those, I've seen far more messes that were obviously created by somebody who had read about a solution but never thought about it. 



  • You shouldn't ever need to do any copypasta in your code. If something is done two or more times, you abstract it. And the language should support a way for you to.

    So his proposal for what you do, if you're not using design patterns...  is a design pattern.



  • @asuffield said:

    @operagost said:

    Combinating.  Hmm!  Looks like I learned a new non-word! 

    It's the action of using combinators

     

    Consider me newly informed. 



  • @lepidosteus said:

    Another WTF on php doc website is on the do while page and comes from the php team itself:
    Don't worry if you don't understand this right away or at all. You can code scripts and even powerful scripts without using this 'feature'.

    Sorry but if a "programmer" can't understand at all the difference between a while and a do while, he better stop trying to code "powerful scripts".

     

     

    Reminds me of my CS102 class.  I went faithfully to class for about a week.  They were talking about the while loop at the time.  After a week of skipping class I decided to drop in and see if I was missing anything.  In a bit over a week they had managed to move from while to do wile. 

    I actually lost points on my lab since I had used a do while loop before they introduced it in class. 

     


Log in to reply