8 ms·
Clean Code concepts adapted for PHP
- bahjoite 9y agoA nice thing about this being open and editable is that the original author has obviously learnt quite a bit since their initial commit. How to do for and foreach loops for example: https://github.com/jupeter/clean-code-php/commit/08d73d655efcbab0ae789326fc016ffacc2fdaa2 https://github.com/jupeter/clean-code-php/commit/08d73d655ef...
- deleted 9y ago[deleted]
- radicalbyte 9y agoThe best thing about open source is that even seasoned PHP Architects can learn useful language features such as for and for each loops.
- throwaway2016a 9y agoI'm giving him the benefit of the doubt that this was a copy and paste error. With that said, as someone who has written physical paper programming books... this kind of thing would get me 10 x 1 star reviews on Amazon.
- radicalbyte 9y agoI think that you're being kind. Unfortunately these types are more the rule than the exception, and are most certainly not restricted to PHP. The dotnet world is full of failed developers who manage to talk themselves up. For all the good that Uncle Bob and the like do they have become enablers for these snake oil architects.
- mtzaldo 9y agoI can't agree more about the dotnet "architects"
- brian-armstrong 9y agoI can't be the only one expecting it to say "just don't", right?
- mhd 9y agoBecause of PHP or Uncle Bob?
- _pmf_ 9y ago> Because of PHP or Uncle Bob? Savage, but not entirely unfounded. Uncle Bob and his promoters try to keep an aura of Zen master around him, but there are no deeper insights in his truisms. "Clean Code" is nice for a novice coder, but dangerous if taken to an architectural or inter-module level.
- dep_b 9y agoI need to maintain quite a few reasonably large code bases that I have been writing alone or have maintained for a long time. I do not have the mental hard disk space to remember every detail about every parameter and what are it's side effects. Having more clearly named everythings and more limitations in the API's I write for myself really help me maintaining all of the things that I've been writing in the last decade or so. If I'm in my IDE I expect that when I have an instance of an object and I type `.` (or it's equivalent) it will autocomplete with only the things that I should touch and those things should make sense immediately without having deeper knowledge of the object I'm consuming. Languages like PHP but Java and C# a bit less but just as well are prone to writing code that can confuse the consumer. More modern or functionally designed and stronger typed languages can take guessing and confusion away, combined with the ideas expressed in Clean Code. But a lot of other sources as well, don't treat the guy like he's god. I love to read the analyses of Carmack's code for example.
- ge96 9y agoIs using PHP bad? I've seen speed comparisons and it is slow. I feel bad so far it's the only back end language I know unless counting JavaScript but don't currently use Node in development. I just keep seeing it being labeled bad a lot. I guess it depends if you can avoid it right? The current company that I work for (contract hourly) is a LEMP stack built on WordPress. Ideally I could strip WordPress out and get to the core purpose of the web application but it's not at that stage yet. I like the idea of separated services but Edit: I saw some infographic saying PHP was too easy to use/get into and resulted in many bad coders who were using it. It's also old, whenever I'm on the manual site and see the comments dating back to 10 years haha. What about machine code running on computers.
- sitepodmatt 9y agoInteresting. I'm not sure if enterprise OO design patterns / clean code / solid / GoF / all golden OO etc.. is a help or hindrance in a typical build the world destroy the world per request model that is PHP. It might be in the spirit but if you're freshly creating all these objects and a big ass dependency graph from scratch per request for each request then you'd be better be sure the promise of PHP 7 VM being superbly improved holds true in your app's case. In other stacks you'd have a lot of this available for the next request at app level or cached somehow (already deserialized), in PHP you really are Creation Day 1 each hit. When you grow to something as complicated as an ecommerce frontend all nasty hacks come into play around the New World New Request model definitely undo a lot of benefits - see Magento as a good example (the cache system - essentially concat / rewriting PHP files) - people have even suggested let's go back to pure SQL and echo.. I initially thought this was going to be the use something else meme - http://devhumor.com/content/uploads/images/December2016/php-best-practices.jpg http://devhumor.com/content/uploads/images/December2016/php-...
- icebraining 9y agoWhich part of the linked article do you see advocating "creating all these objects and a big ass dependency graph"?
- wolco 9y agoHaving dozens of classes making up some big dependency class is probably not going to slow you down as much as you expect. Querying for data will be the bottle neck. Until you move to some in memory solution trying to reduce the number of classes called to increase speed will have little effect because that will be cached anyways.
- debacle 9y ago> if you're freshly creating all these objects and a big ass dependency graph from scratch per request for each request That's not how modern PHP works.
- icedchai 9y agoHow's it work then?
- huhtenberg 9y agoThis is an odd mix of common sense, random cosmetic advice, subjective bits and points that are plain wrong or contradictory to other points. For example: Bad: $ymdstr = $moment->format('y-m-d'); Good: $currentDate = $moment->format('y-m-d'); Both are inferior to "$ymd". Use searchable names addExpireAt(DateGlobal::SECONDS_IN_A_DAY); Who would ever need to search for that? You'd search for "Expire". This doesn't preclude from using named constants, but it has nothing to do with search-ability per se. Don't use flags as function parameters Good: function createFile($name) { touch($name); } function createTempFile($name) { touch('./temp/'.$name); } Now, this is how you create the good old copypasta. What you need instead is a single universal function that does in fact accept a boatload of parameters, but that is never meant to be called directly. You then supplement it with a host of simpler one-liners that fill in most of the parameters with defaults and forward just the select few. Avoid negative conditionals should be "Avoid double-negative conditionals". Avoid conditionals this is just overly broad to the point of being completely pointless. What the example proposes is to use abstraction and inheritance instead. In some cases it helps, but it many it leads to needlessly obscure code that's impossible to read off the page. Use method chaining Good: $car = (new Car()) ->setColor('pink') ->setMake('Ford') ->setModel('F-150') ->dump(); Except at some point you will need setModel() to fail for some arguments, e.g. setModel('Trabant'), and you will end up with some methods returning $this and some not. This is not going to be a "clean code" by any measure. (edited for clarity a bit)
- userbinator 9y agoWhat the example proposes is to use abstraction and inheritance instead. In some cases it helps, but it many it leads to needlessly obscure code that's impossible to read off the page. Reminds me of https://cirillocompany.de/pages/anti-if-campaign https://cirillocompany.de/pages/anti-if-campaign --- not sure if satire.
- Sujan 9y agoSo you created PRs for these with fixed examples at the repo. Right?
- aaronchall 9y agowrites a book on php best practices all the pages are blank
- mattdeboard 9y agoTop thread: People dickering over variable naming in PHP, based on a post about an open-source translation of an Uncle Bob book. It's like HackerNews BINGO.
- steinuil 9y agoI'd like to recommend "Taking PHP Seriously" by Keith Adams, a lecture by an engineer at Facebook on the alleged good points of using PHP.