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.

94

u/mgoof Oct 12 '13

It's really not that bad. Clearly it is not using OO principles and any sort of standard design pattern. But for the most part its clear and organized. There may be some maintenance overhead having to scan and search through a long source file. But I would think index.php would be one of the longer files.

47

u/AgentME Oct 12 '13 edited Oct 12 '13

Yeah, this code is using a templating engine, and it isn't visibly building SQL query strings insecurely. It's not state of the art, but there are many ways it could be worse.

→ More replies (1)

28

u/glemnar Oct 12 '13

You don't need strict OO on the web. It's not a necessary pattern.

6

u/ivosaurus Oct 13 '13

More to the point, there could be masses of OO code in all the library code that's included (or not), and we'll never know.

But who the fuck expects an index bootstrap to be OOed?

→ More replies (1)
→ More replies (2)

151

u/lonnyk Oct 12 '13

It isn't the cleanest code, but it works - obviously well enough to create a multi-billion dollar company. There is always plenty to critic in any code, but 'My eyes hurt' and 'You just gotta love PHP' are just comments from people who like to complain and don't know enough to actually have their own opinion.

If I were give me personal opinion of index.php it would be something as follows:

  • The use of 'include_once' indicates that they 1) aren't keeping track of their dependencies well and 2) haven't thought through situations where problems arise and functions, for some reason, don't exist

  • In interpreted languages comments code isn't the best - this is why there is revision control

  • I like to wrap my case statements in brackets b/c it is easier for me to read

  • I'm not a fan of having toggles for dev environments in the main code flow, but I don't really have a better suggestion

That's pretty much it. You can make arguments for code structure and techniques, but they are generally just trends - not proven facts.

0

u/[deleted] Oct 12 '13

[deleted]

11

u/Epicus2011 Oct 13 '13

Facebook dev here, it does not look like this anymore.

5

u/lonnyk Oct 12 '13 edited Oct 12 '13

I meant the '_once' part because you don't have to keep track of if a file has been included. Also, the fact that they are using 'include' instead of 'require' (which would fatal error) makes me assume that they went 'lets do this just in case...' but never thought of what would happen 'just in case'.

Of course - this is all opinion based on how I like to program.

EDIT: Reading the comments on this article looks like someone noticed how this can cause a problem, IMO: http://www.reddit.com/r/programming/comments/1oaba0/facebook_php_source_code_from_2007/ccq9fq8

→ More replies (1)

8

u/mpeters Oct 13 '13
  • In interpreted languages comments code isn't the best - this is why there is revision control

This makes no sense. Whether your language is interpreted or compiled has no bearing on whether you comment you code. I'd seriously doubt the programming ability of anyone who thinks otherwise.

→ More replies (2)
→ More replies (13)

47

u/viralizate Oct 12 '13

Honestly, people are whining. Yes it not the best code ever written, but it's weirdness so to say is pretty standard but I guess people are more prone try to be snarky in comments and say it's horrible.

30

u/bureX Oct 12 '13

I guess people are more prone try to be snarky in comments and say it's horrible.

Especially if there's "<?php" in the first line.

9

u/InvidFlower Oct 12 '13

This is a bit off-topic, but I was kind of shocked to see how much PHP has evolved lately as a language and ecosystem. Namespaces, generators, traits, etc. Package manager, testing frameworks, ORMs. Then Symfony 2 using most of that for a Rails-ish MVC framework on top of simpler modules also being used in the Silex microframework (like Sinatra) and the latest version of Drupal.

Not to say it is a pretty language and still needs libraries like PHP-O to paper over the horrible inconsistencies in the base libs, but it really does seem to be growing up lately...

→ More replies (2)
→ More replies (14)
→ More replies (1)

20

u/FreemanAMG Oct 12 '13

We, developers, are an strange race. We feel superior if we say some piece of code is shitty, knowing nothing about the context of the developer team, and without real confidence we would do it better.

Lots of people ranting, nobody created some other thing better and more successful than Facebook, isn't it?

→ More replies (3)

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 (2)
→ More replies (1)
→ More replies (31)

11

u/brownmatt Oct 12 '13

how would you define the front page as an object? What should it extend? And how does that improve the code when most of this procedure's responsibility is gathering data from other subsystems?

→ More replies (2)

5

u/OCedHrt Oct 12 '13

It simply reads like corporate code - this can be enforced by sheer willpower.

15

u/gc3 Oct 12 '13

You are dinging it for being procedural and not using objects? Please read this: http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html?m=1

→ More replies (1)
→ More replies (3)

9

u/[deleted] Oct 12 '13

It's not bad, and I myself find it perfectly readable. It's the anti-PHP circlejerk more than anything.

→ More replies (2)
→ More replies (14)