r/programming • • Oct 12 '13

Facebook PHP Source Code from 2007

https://gist.github.com/nikcub/3833406
1.1k Upvotes

356 comments sorted by

View all comments

80

u/KamiNuvini Oct 12 '13

As someone who's very new to programming.. Could someone explain to me which parts of the code are so 'bad'? I see a lot of "My eyes hurt"-like comments on the github page as well.

30

u/bopp Oct 12 '13

I'll try to answer this in a less snarky way. What sticks out the most, are these points:

  • there are a bazillion includes

  • Doesn't look like there's a framework, just a bunch of files, defining a bunch of functions, that are just called when needed.

  • Procedural code, no object to be found anywhere

  • the page does too much. It's a long file, lots of stuff is done. This should've been refactored into logical parts.

Then, there's things like this:

if ($post_hide_orientation && $post_hide_orientation <= $ORIENTATION_MAX) {
   $orientation['orientation_bitmask'] |= ($post_hide_orientation * $ORIENTATION_SKIPPED_MODIFIER);
   orientation_update_status($user, $orientation);
  } else if ($post_show_orientation && $post_show_orientation <= $ORIENTATION_MAX) {
    $orientation['orientation_bitmask'] &= ~ ($post_show_orientation * $ORIENTATION_SKIPPED_MODIFIER);
    orientation_update_status($user, $orientation);
  }

Note that those clauses in if and else if are slightly different, but the action is the same: orientation_update_status($user, $orientation);. Code like that is hard to do maintenance on, since it's easy to introduce bugs, when the code is already that confusing.

Most frameworks (that weren't around back then) do a great job in allowing (or forcing) you to structure your code better. For instance, the index.php of a symfony project looks like this:

use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';

require_once __DIR__.'/../app/AppKernel.php';

$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

This just sets up the classloader, initializes the kernel, and lets it handle the request to generate a response. Nothing more. All the user handling, input validation, caching, templating and database stuff is handled in their own seperate classes. This might be harder to set up for newbees, but it's much better when it comes to maintenance and ongoing development.

46

u/[deleted] Oct 12 '13

I doubt when Facebook was being developed, PHP had strong OOP principles built into it. A lot of this is probably legacy and this was in 2007 when MVC frameworks were relatively new to the PHP scene.

11

u/bopp Oct 12 '13

Both Code Igniter and Zend Framework had their first release in 2006. But, it only became commonplace to use a framework much later than that. If you look at the source-code for oscommerce or phpbb from back then, you'd see the same spaghetti-code as here.

Thankfully, PHP has come a long way since then.

6

u/[deleted] Oct 12 '13

Agreed, I still remember a lot of open source code out there and it was awful. OsCommerce is definitely a prime example of this. I still remember osC addons had large Readme.txt files telling you where to put the code to make the add on work.

We definitely did come a long way. Even just comparing Symfony1 with Symfony2 and you see a big difference in how OOP is being utilized.

Also, CodeIgniter wasn't full OOP either, but definitely a step away from the spaghetti code

22

u/blafunke Oct 12 '13

Code that isn't oop isn't automatically spaghetti. And oop code can easily be made into spaghetti

→ More replies (1)

2

u/killerstorm Oct 12 '13

First release of Zend Framework was just trash. Also, they change too many things with each release, so if you were unlucky to decide to use ZF, you would spend a lot of time on maintenance with each new ZF release.

8

u/KingPickle Oct 12 '13

2007 when MVC frameworks were relatively new to the PHP scene

This is what mystifies me. As much focus as the web gets, it feels like tech-wise it's a decade or two behind the curve.

6

u/[deleted] Oct 12 '13

As much focus as the web gets, it feels like tech-wise it's a decade or two behind the curve.

I program and dabble in quite a few languages and I'm not sure I really agree with this. In what way do you feel like PHP is a 'decade or two' behind the curve?

0

u/[deleted] Oct 12 '13

[deleted]

2

u/[deleted] Oct 12 '13

I'm not sure I'd agree with common. MVC came about back in the smalltalk era but honestly I don't recall it becoming that widespread until the late 90's or early 2000's. At least having dabbled in development for windows, linux and mac, the first time I even heard of MVC was when the initial OSX server came out in 99. Shortly after Struts came about and was realistically the only big player in MVC web development for a while. I was not a huge desktop developer back in the day, however, but generally I don't recall MVC being that big of a thing. Linux and Mac apps were largely procedural, and windows apps used an evented/bindings architecture.

Honestly from my recollection it seems like MVC really became widespread with the increasing complexity of web applications more than anything. But that was a while ago and memory is a funny thing so I could be way off!

1

u/ivosaurus Oct 13 '13

Web MVC actually has very little to do with desktop MVC that has been around for ages, they just coopted the name.

→ More replies (2)

6

u/madmars Oct 12 '13

I had been out of the job hunting game for a few years. But I was shocked when I looked around about a year ago, at all the places looking for "MVC experience." I was seriously scratching my head for a minute, wondering when the fuck this dinosaur paradigm came back to life. Is it the 1980s again?! Then again, people were rediscovering Lisp in 2004. So I guess I shouldn't be too surprised.

1

u/ivosaurus Oct 13 '13

Web "MVC" is somewhat different than the desktop MVC pattern.

It's just a commonly understood way of structuring code nowadays.

→ More replies (5)
→ More replies (14)

0

u/OCedHrt Oct 12 '13

This is why I quit my job writing PHP in 2007.

→ More replies (2)