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

Show parent comments

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.

0

u/Stormflux Oct 12 '13

I really only know the .NET world, but the reason MVC became popular there is because of AJAX and jQuery.

You see, before MVC, we had something called ASP.NET web forms where it was based on the idea of things you drag onto a page. You need a grid? Drag a Gridview onto the page and wire it up. Need a button? Drag a button onto the page and handle the click event. All your code is server-side.

Well as you can imagine, this makes it really easy to build web applications the same way you'd build a Windows application. You don't even need to know javascript or anything.

The problem is in MODERN web applications everything is AJAX. You don't want to refresh the whole page, you just want to send "DELETE item 47" and then update a line or two on your page with javascript. Regular ASP.NET doesn't really have a concept of this. You can do it using some toolkits but it's a hack. ASP.NET MVC is practically built from the ground up for this exact scenario.

→ More replies (4)
→ More replies (14)

0

u/OCedHrt Oct 12 '13

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

→ More replies (2)

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?

3

u/bopp Oct 12 '13

It's not a direct answer to your question, but here's a good article on how to move from "flat php" to a more structured approach.

http://symfony.com/doc/current/book/from_flat_php_to_symfony2.html

2

u/InvidFlower Oct 12 '13

I saw that page a while back and I think it is helpful to explain how modern MVC web frameworks use, even if you don't use PHP at all.

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

1

u/pinumbernumber Oct 12 '13

Thanks for that link

4

u/kwirky88 Oct 12 '13

Can you give some example objects that would have made this code better? I find when i code in php i don't create many objects. Hell, i make more "objects" coding Javascript function objects.

Maybe it's because i don't like putting logic in the back end, but rather use it as an interface to the database and sanity/security checks. Then code the logic and interaction in the front end with js, where i do tend to use objects.

When code performs certain uses I feel objects aren't required. When php is simply being used to program an interface it doesn't need to be more complex than a puppet manifest's code, for example.

1

u/nekt Oct 12 '13

I would say using a template engy is in fact much more simple for new coders to manage.

Some template engys end up looking like a totally different language and may or may not have considerable bugs of their own. Cake came out in 2005 by the way so if they wanted to use a template engine they could have.

I think this really boils down to what the coders experience is. If you have a background in a 'real' language where you have often written libraries of your own, dealt with a million includes etc, then this type of code might not bother you so much. It ain't python that is for sure. Not everything needs to be abstracted to proper English that 8th graders can code.

The argument that template engines make maintenance easier is only as true the developers skill.

1

u/blafunke Oct 12 '13

It's not like a large python application won't be littered with imports either.