David R. Heffelfinger

  Ensode Technology, LLC

 
Bookmark and Share

Confessions of a Java Snob


For the past couple of months I've been working on porting a PHP application to Java EE using JSF, JPA and EJB 3 (in case you are wondering, yes, I've been using NetBeans and GlassFish).

I've never had any real exposure to PHP, so this is a new experience for me. I'm not sure if what I'm seeing is typical PHP code, but comparing this legacy system to the typical enterprise Java system shows some striking differences in architecture. When analyzing the PHP code, it became obvious to me that the mindset of PHP developers is very different from your typical Java developer.

In the Java world, we love our design patterns, we can't live without our DAO's and MVC. In PHP, it seems to be no big deal to mix presentation, business logic and data access in a single file.

Having worked with Java for over 13 years and Java EE/J2EE for about 10 years, I have to confess that the architecture (if you can call it that, more like "lack of architecture") seemed appalling to me. In the enterprise Java world, we've been conditioned to think that separation of concerns is a good thing.

Our presentation logic should contain presentation only, that way if in the future we want to switch say, from straight JSPs and servlets to JSF, the rest of the code shouldn't be affected. Additionally, if we want to convert our web application to a desktop application using Swing, it should be fairly straightforward to do so.

Data access logic should be done via Data Access Objects (DAO's), that way if today we are using straight JDBC and tomorrow we want to use an object relational mapping tool such as JPA or Hibernate, all we need to do is change the data access layer, the rest of the code should not be affected.

Communication between layers in our applications should be done via Value Objects, which shouldn't really change if we change our data access layer or presentation layer.

Controllers should manage flow from one page to another, again these should only be rewritten if we change our presentation layer. Most Java web application frameworks provide their own controllers, however they are not self sufficient, for example, in Struts we need to use Actions and in JSF we need to write managed beans, therefore changing the presentation layer would usually involve changing the controller as well.

After analyzing the code for the legacy system I got the impression that PHP is a language for amateurs and Java/J2EE/Java EE is for professional software engineers and architects. Am I right? Or I am just a Java snob? Feel free to set me straight.

 
 
 
 
Comments:

Both PHP and especially Java are for amateurs who hijack any notion of "separation of concern" and tout it as a virtue.

If you want to understand separation of concerns in the true sense, you'll need to learn more advanced programming languages, particularly those that are purely functional and lazy.

After that, you won't make the mistake of thinking "the view" and "the model" are good places to "separate concerns".

Best of luck.

Posted by Tony Morris on May 23, 2009 at 08:47 AM EDT #

You seems to confuse two differents things : languages and frameworks.

Although php is an awful language, there are (good?) MVC php framework.

Also note that many pro choosed python to get the best of both worlds : easy prototyping and good smooth curve to a solid architecture.

Java can be very very heavy and cumbersome for anything else than a huge project.

So, you are a java snob but a nice one ;)

Posted by Lionel Barret on May 23, 2009 at 10:38 AM EDT #

Anybody can write shit in any language. Good programmers can write good software even in shit languages/frameworks ;)
We are the problem. Not the tools :)

Posted by gorlok on May 24, 2009 at 11:27 AM EDT #

@lionel

I think php not worst exactly but most important is php doesn't have a compiler. I can't imagine building the large application using php. It is horrible i guess.

Posted by Azizi Yazit on May 24, 2009 at 11:53 AM EDT #

Heh. I did the same thing recently after discovering some serious security problems in the PHP code (XSS, SQL injection, normal stuff). Of course there was no separation of the model and view, so every page was thoroughly broken.

I ported their expensively created consultant-driven PHP to Java... Threw the whole mess away.

I, too, was horrified by what I saw but I don't really blame the language. It's more of a cultural thing. I think Java has some high barriers to entry and those who use it are more likely to take programming seriously. PHP has much lower barriers and seems to attract those who just want to earn some easy money and move on.

Of course, that's a broad statement not applicable to all Java or all PHP programmers.

Posted by Mike Johnson on May 24, 2009 at 12:24 PM EDT #

Mixing BLL/DLL/Presentation in one file is shitty programming. Not PHP's fault. PHP 5 has decent enough OOP that you can write a DAO and write business objects and keep your presentation layer clean. And you don't need MVC for this either. The PHP community may be faulted for not encouraging best practices but you can write enterprise grade apps with the language if you want. It would be nice though if the PHP community developed a official MVC tool.

Posted by john on May 24, 2009 at 12:49 PM EDT #

PHP used to stand for Personal Home Page. It should have stayed that way. And i agree: Java is the ideal way to build Rube-Goldberg machines.
In a certain way, it is the best thing that happened to the IT industry: a guarantee for lifetime-employment.
Imagine a world where we all be coding in Ruby or Python. With a tenfold increase in productivity, we would all be unemployed.
Thanks Java.

Posted by Alain on May 24, 2009 at 01:05 PM EDT #

I very much agree with you. It's true that it doesn't have to be like you described, but in practice this is typically what it boils down too.

In Java you can code in the typical Php style; use only jsp and mix your business logic in between HTML tags . Then if you need to create a new page, just copy some old page and change some stuff. Don't care that your business logic is now duplicated. There is always search and replace, right?

In a typical Java development team you will most likely be called an amateur if you even think about coding this way. If you actually do code this way there's probably an angry mob equipped with pitchforkes waiting to 'gently' escort you out of the office.

In php on the other hand it's possible to code according to sane design patterns. As you mention, there are even third party frameworks to help you with this. Unfortunately, the fact that nothing in the core php platform moves you to such a sane approach makes the relative adoption rate among php coderz low. If you attempt some to apply some mild mvc pattern in your code, other php coderz will often scold you for making things 'needlesly complex'.People would start yelling: "Why do you have to move that business logic to a separate file where I can't find it anymore? It's far easier and better if that code is right next to the HTML button that invokes it!"

It's just a culture clash really, where the latter culture is far more attractive to amateurs than the former.

Posted by Peter on May 24, 2009 at 01:13 PM EDT #

I so much agree with this article that I could have written it myself ;)

@John
>Not PHP's fault. PHP 5 has decent enough OOP that you can write a DAO and write business objects and keep your presentation layer clean.

You theoretically could, but the fact of the matter is that hardly anyone using PHP ever does. There is no peer pressure at all to learn or apply design patterns. People who are absolutely disgusted by this amateurish way of working abandon PHP quickly for platforms such as Java EE or C#/.NET.

Posted by Henk on May 24, 2009 at 01:20 PM EDT #

I partially agree. A good developer do good code even coding in PHP. I agree that coding in Java is elegant applying Patterns (GoF, DAO, Value Objects, etc.) and MVC design. However I saw JSP coding "a la" average PHP, mixing JDBC connection, logic and presentation in a single page. It's means that one can code in Java/JSP using the -IMHO uggly- PHP average style. David Heinemeier Hansson, who knows PHP and Java, wanted the quick results of PHP with the elegant code of Java using Patterns. The result of that is the framework Ruby on Rails.
However I doesn't mean that you are just a Java Snob. Java code is beautiful when one code the way you do ;).

Posted by Ruben Diaz on May 24, 2009 at 01:24 PM EDT #

The author is absolutely right!

Where I work we do both Java and PHP development. Java people design their applications carefully and use appropriate design patterns. PHP people however code like there is no tomorrow. They brag out code and copy and paste all of over the place completely not hindered by their lack of knowledge.

Eventually all this PHP code comes crumbling down, but by that time most of those people have already moved on again.

Posted by Max Cohen on May 24, 2009 at 01:34 PM EDT #

I think you are a nice Java snob. IMO any programmer can code well in any language. What it really is all about is culture and Java does set a higher bar by encouraging following best practices. That said my personal website is done in the Symfony PHP Framework.

Posted by Kevin M Diffily on May 24, 2009 at 02:46 PM EDT #

I couldn't disagree more, i have seen spaghettis in every language. PHP and Java specially since they are the most popular. It's just a mather skills and knowledge...

“All the web frameworks in the world won’t turn a shitty programmer into a good one.”

So yeah, you're snob.

Posted by seb test on May 24, 2009 at 02:51 PM EDT #

Separation of concerns is not just about allowing switchability amond toolkits. An even more critical reason is simple maintainability: there is a canonical place to look when you are looking for what implements given functionality.

I must confess I agree with your conclusion about PHP. I have tried 3 times to learn it, because there is a huge base of web apps written in it. Each time I ran away shrieking in horror. I'd suggest looking at Python or Ruby (JRuby!) where at least people understand good OO.

Posted by bwtaylor on May 24, 2009 at 05:19 PM EDT #

>there is a canonical place to look when you are looking for what implements given functionality.

Exactly, this has to do with consistency, and ensuring we don't violate the principle of least surprise.

If the platform offers a default location or mechanism for certain artifacts, then this greatly helps with consistency and allows you to be faithful to this principle of least surprise.

In standard Java, business logic goes in EJB3 beans and rendering logic and component can be found an a JSP or Facelets page. You're not going to hunt for business logic between the TR and TD tags in the middle of some table.

In the PHP, there are well, PHP files. And there is definite location suggested by the platform to store business logic (or view code or control code for that matter).

Posted by 86.81.58.36 on May 24, 2009 at 05:33 PM EDT #

It might be snobby, but it's correct. A php shop usually admits their code looks like ass and next time "will totally do it right" and has a hard time doing anything so they spend 70% of the time pushing back on requirements.

Java EE still over-seperates imo, but more of that crap is going out of the window in Java EE6 where the "expert team" is finally coming to their senses.

At the end of the day, no language determines how good or bad someone is, but in a blind test, I'd work in a Java shop rather than a Php shop anyday.

Posted by Ivan on May 24, 2009 at 05:50 PM EDT #

well, comparing two languages is not a good idea to me. every language has their own pros and cons and i am sure you know that.

in real world you have seen VERY-LARGE-SCALE web applications like flickr, facebook, friendster which are developed in PHP. when friendster guys had ported their app from Java to PHP they must had some reason behind that.

software engineering is mostly upto the developers behind the machine. PHP developers also write excellent code - and they follow "SOFTWARE ENGINEERING".

if you see some guys standing up there with batons you will follow the path strictly automatically ;) - and when it is shiny, windy and sunny weather up there you will follow the path, with pleasure. the first one you can compare with java and the later one with php - lol.

we enjoy the freedom, but with responsibility. some people write messy code, but that doesn't mean everyone are like them :)

have a good day.

Posted by hasin hayder on May 25, 2009 at 02:47 PM EDT #

PHP is better than Java Servlets because escaping special characters in your HTML strings in a Java Servlet makes the HTML unreadable. That's why so many Java web developers use JSP for the display, but JSP has a horrible lack of modularization facilities.

The history of modularization techniques may have been 1. "include" files, 2. macro processors, 3. subprograms, 4. abstract data types, 5. polymorphism. JSP is at level 1., and frameworks such as Velocity raise it to level 2. That's pathetic.

PHP's features at least make it feasible to code HTML displays using a Servlet style, so you don't have to forgo the use of object-oriented techniques for abstracting out display-logic commonalities -- but not that many PHP developers take advantage of this. Most PHP programs are pretty amateurish.

Java becomes a superior tool for writing web applications, however, if you trash the servlets and JSPs and instead use the Wicket framework. That way, web application development becomes almost as easy as building Java application fat clients.

Posted by fsilber on May 26, 2009 at 10:44 AM EDT #

PHP is better than Java Servlets because escaping special characters in Servlet strings makes the HTML unreadable. That's why so many Java web developers use JSP for the display -- but JSP has a horrible lack of modularization facilities.

The history of modularization techniques may have been 1. "include" files, 2. macro processors, 3. subprograms, 4. abstract data types, 5. polymorphism. JSP is at level 1., and frameworks such as Velocity raise it to level 2. That's pathetic.

PHP at least make it feasible to code HTML displays using a Servlet style, so you don't have to forgo the use of object-oriented techniques for abstracting out display-logic commonalities. Unfortunately, few PHP programs do this.

Java does become a superior tool for writing web applications if you trash the servlets and JSPs and instead use the Wicket framework. That makes web application development almost as easy as building Java application fat clients.

Posted by fsilber on May 26, 2009 at 10:49 AM EDT #

I've known good java programmers who write bad php codes.

All languages come with a practical purpose. Criticizing a language because of those who use it tells me a lot about your analytical mind.

Think in terms of bicycles and cars. They are meant for different purposes even though they are within a general category.

You don't criticize a bicycle or a car on the basis that some people drive or ride clumsily.

Yes you're snob : you've been biased by too much extensive use of a paticular language without enough exposure to other languages and their respective purposes.

I know people who can drive a car but can't ride a bicycle : probably you and some other commenters are within that category.

Posted by Joseph on May 26, 2009 at 11:30 AM EDT #

@fsilber

Hello, did you comparing php with java servlet?? It is really an old style of Java. For me a lot of php programmer who think php is better than Java because they only know about Java servlet. And fyi as java programmer/developer we only using jsp as a view component and all the business logic we do it in the Java class.

Sorry to say, Java is a lots better than php. Maybe you only can comparing php between jsp not Java.

Posted by Azizi Yazit on May 26, 2009 at 12:16 PM EDT #

As a professional enterprise PHP developer for a few years now, I have been continuously inundated with ridicule and contempt regarding PHP as a useful and usable language.

Over the years, the biggest gripes are security concerns, and as you explore, logical architecture tiers being intermingled with no regard to maintenance concerns or adaptability. The thing is that all of these complaints are absolutely true. After 7 years developing PHP applications professionally, I have run into these blundering web applications time and time again. I have had to audit some of the worst code I have ever seen in ANY language, and have advised some companies to toss their entire code base out and start from scratch because it was so bad.

PHP is a language that's very easy to learn. This makes it attractive to new web developers. It allows them to put a few dollars in their pockets very quickly as they enter the working world. The clients providing these few dollars don't know whats bad code and what's good, so as long as it works and there aren't too many complaints, they launch their flagship website and start bragging to their friends.

Flash to a year or two later. The site gets hacked, the web developer who wrote it originally has moved on to some company and cannot help. Now the company hires another developer, only this time they realize that you get what you pay for, so they hire someone with a bit more experience. Maybe a journeyman .NET developer, or a Ruby guy or something. They come in, take a look at the code and start running their mouths about the evils of PHP.

I can tell you that I write enterprise level systems using PHP and there is NOT A SINGLE STITCH of PHP embedded in my presentation logic. I use a templating engine I wrote just the same as any other language. I write modular architecture, and love my design patterns just as you do.

The reasons I like PHP are:

1. Because unlike a lot of other languages out there it doesn't hold your hand like a first grader. It expects that YOU know what you are doing. I write alot of closed off apps that work in a vacuum environment. These apps don't need the level of filtering and such that a web based app has, and to have them would just reduce the apps performance and efficiency. Conversely, an input from a web form will most likly need more filtering then a standard embedded feature would do, so whats the use of filtering twice?

2. You're not required to remember a bazillion packages and what they have in them. PHP has a boatload of functions and classes available as a part of it's core language set. There are extensions to take care of special cases, and extensions to improve handling of certain aspects. But the reality of it is, you can code anything any of those extensions does using just the core set of functionality that comes standard with PHP.

3. It's platform independant.

4. It's open source!!!!!!

5. YOU DON'T HAVE TO BUY SOME GREEDY LITTLE SCRIPT KIDDIE'S DLL every time you want to do something other then what's been provided for you by the languages core set.

Posted by Gregory Patmore on May 28, 2009 at 08:38 AM EDT #

Sorry, but I have to draw the conclusion that you're a typical Java snob who has no idea what's out there and doesn't understand you can write crap in any language.

For the record: PHP has plenty of MVC frameworks, ORM's etcetera. Separation of concerns is basic software engineering, and completely language-independent.

The only real weakness PHP has is that it's often inelegant and inconsistent, because it has grown organically rather than having been well designed. However, that organic growth is also one of it's main strengths, because it keeps evolving in response to the needs of its developers.

If there's any typical difference between PHP and Java developers, it's that most professional PHP developers I know have worked with various languages, are respectful of other platforms, and would never make such uninformed statements.

There seem to be a common Java career path that goes straight from CS courses to senior engineering positions without ever having touched any other programming language, or maybe more importantly, without ever having been exposed to any other software development culture.

There's more than one way to write good code. There's also an infinite number of ways to produce crap. Both are largely language-independent.

Posted by Rick on May 29, 2009 at 02:53 AM EDT #

Post a Comment:
Comments are closed for this entry.
 

« April 2024
SunMonTueWedThuFriSat
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today

 
© David R. Heffelfinger