5 ms·
Ask HN: What code should I be looking at?
I started programming some months ago (PHP and javascript) and have grasped the basics. The more I learn the more I realise that one of the most important things is to write well structured code.
Can you recommend some open source projects in PHP that are well written and well structured that I should be looking at to see how it's done right?
- khangtoh 18y agoWordpress?
- mechanical_fish 18y agoI got into PHP by way of Drupal. I tend to think of myself not as a PHP programmer, but as a Drupal programmer who is therefore stuck with PHP. I don't work with any other PHP frameworks; If I wanted something that wasn't Drupal I'd be using Ruby. Now that my biases are clear: You should take up Drupal. It's a big system with a lot of architecture, but the core is not too big; it's being hacked on by many thousands of people and is continuously evolving as the community learns; and it hits a sweet spot between being well architected and being open to the kind of simple cut-and-paste anarchy that makes PHP thrive -- it's designed to let you obsessively customize everything, but it tries to encourage you to contain your creativity within pluggable modules that can (at least in theory, but often in practice as well) be manipulated by noncoders. If you want to try Drupal and you are a coder you must read VanDyk's Pro Drupal Development, Second Edition ASAP. Trust me on this. As for Javascript, which is arguably more important: jQuery, jQuery, jQuery. Also: Douglas Crockford, Javascript: The Good Parts. And learn your CSS if you haven't already; I did that by reading Eric Meyer's O'Reilly book, followed by Dan Cederholm's book.
- mixmax 18y agoThanks :-) I'll look through the drupal code with your post in mind - sounds like a good learning experience. About javascript: I agreee, it seems that jquery makes a lot of stuff trivial, unfortunately I only discovered it after having (poorly) implemented quite some javascript myself. As for the CSS - I've got that covered pretty well.
- mechanical_fish 18y agounfortunately I only discovered it after having (poorly) implemented quite some javascript myself. That's only unfortunate if your poorly-implemented JS is live on a popular site and you have to maintain it. :) Otherwise it's a great educational experience! You don't realize how great your tools are until you try to live life without them.
- aichcon 18y agoIn addition to jQuery, check out YUI. It is incredibly well documented and featured. I see it overtaking the other JS platforms soon.
- jncraton 18y agoI actually learned a lot about coding good PHP by learning Rails. I'm not an expert by any means, but it forces tons of good programming practice on you which can carry over to other languages and frameworks.
- gnaritas 18y agoIt suggests, not enforces, you can still do boneheaded things like putting logic in the view or inappropriate logic in the controller that should be in the model. The presence of a model and controller doesn't make you write good code.
- jmtulloss 18y agoFirst, I really don't want to start a flame war. That being said, my opinion is that PHP as a language is not well structured or well written. You might want to check out Python or Ruby, both of which have great web frameworks that encourage structure.
- stcredzero 18y agoIt's the same problem in any language. Most of the code out there is mediocre or bad. The code you want to learn from is a few standard deviations to the right. When you are learning something always Remember Sturgeon's Law! I would rather work with someone who produced excellent PHP than someone who wrote lots of weird hybrid procedural/functional Python full of esoteric array slicing hacks. Being able to apply your intellect to revealing intention in code is a valuable skill in any language, and I suspect that it transcends any particular programming language.
- neovive 18y agoAgreed. Just because a language has a low barrier to entry, doesn't mean it should be blamed for bad code. I linked to the KohanaPHP framework source as an example of very well written PHP code.
- jmtulloss 18y agoI certainly agree that anybody can write bad code in any language, but my argument is that PHP itself is bad code. The libraries are inconsistently named, there's no coding standard in standard libraries, and the language changes behavior according to configuration. PHP as a project is poorly structured, and all other things being equal, it should not define your programming worldview.
- gnaritas 18y agoIt's not just bad code, it's a bad language full of silly inconsistencies that don't make sense and trip up even the most experienced programmers. That's what happens when you build a language by piling hack upon hack instead of actually designing it. Languages like Ruby and Python actually have a philosophy and style that keep the languages self consistent. Consistency is a critical core feature of any good language, PHP doesn't have it.
- Dobbs 18y agoHate to hijack the thread but can anyone offer some examples of good Python and/or C code to look through/read?
- simplegeek 18y agoFor C, FreeBSD. For Python, BitTorrent. IMHO.
- neovive 18y agoI would highly-recommend reviewing the source code of the KohanaPHP framework. It is very clean, elegant and well-structured OOP code written by some very talented developers. http://www.kohanaphp.com http://www.kohanaphp.com
- Maascamp 18y agoBrowse through the Zend Framework. I don't know why more people don't recommend it as a well written large size PHP code base. It's a little exception happy but that's more of a style issue.
- taw 18y agoI learned two things from reading code: Unless you have something specific in mind, like fixing a particular bug, or trying to reverse engineer undocumented behaviour, just staring at code is most likely going to be too unfocused to bring you any value. Most of the Open Source code is crap. Most of the closed source code is crap. I think it's crap because almost all successful projects expanded far beyond their original purpose and architecture which supported it, and never quite got to whole codebase refactoring necessary to make code nice again. If it's working, they don't care. The only really pretty code I've seen were small things that never went through the massive expansion phase.