CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Karl Seguin

.NET From Ottawa, Ontario - http://twitter.com/karlseguin/

November 2006 - Posts

  • Is PHP the new VB6?

    Introduction
    In the past, I've come down hard on ASP.NET for trying to be an all-in-one solution. Rather, I've come down hard on ASP.NET developers trying to make it into an all-in-one solution. ASP.NET is an unsurpassed language for enterprise development and other medium-to-large scale projects. Despite significant improvements in 2.0, the simpler your project gets the more you begin to notice .NET's overhead (no, I'm not talking about performance).  In fact, everything that makes ASP.NET a great toolset for large-scale development is what makes it such a bad choice for anything small.

    On the flip side, PHP excels at this type of small/medium work.  It's extremely easy and quick to put something together and supports an incredible amount of kludging before falling apart. Of course, everything that makes PHP such a great toolset for small-scale development is also what makes it such a horrid choice for any large-scale projects or any sized enterprise development.

    I just want to take a little sidetrack to acknowledge that there are in fact tons of large and complex PHP websites out there as well as tons of small ASP.NET sites. In the end, despite everything I'm going to say, the individual developer has far more impact on a project's success and implementation than the tool he or she uses.

    PHP is NOT Object Oriented
    As a language, PHP's object-support is quite rich. Since the introduction of PHP5, developers have had the same level of OO support as most other languages. Of course, there's nothing forcing developers to use OO principals. This is a mixed blessing. For projects with a small domain, I've found that OO can slow down development without brining any substantial benefits.  For projects with a medium/large domain, developers are left on their own to do the right thing. I've heard it said more than once, but PHP doesn't have a culture of objects. The PHP community hasn't bought into the concept of OO the way the Java, Rails and .NET communities have.  Whether that's an issue with PHP or an issue with PHP developers is hard to say. VB6 had decent OO support; most VB6 developers just didn't buy it (or maybe they didn't grok it).

    As a framework, PHP fails to leverage OO in any meaningful way. The fact that PHP itself isn't OO explains why developers don't use PHP's OO features. In .NET everything's an object. Want a textbox? Declaratively create a new textbox objects, programmatically set properties and execute member functions. Most PHP, VB6 or classic ASP programmers just don't get it. Think of it this way, in .NET you create a server-side DOM, manipulate it with a very rich API and an event model then render it out. This is particular useful when you want to extend the built-in objects and achieve greater re-use (within the same project or with completely separate projects).  There are countless other examples, such as user controls, HttpModules, HttpHandlers and the page lifecycle which are completely foreign concepts to PHP developers.

    Beyond the presentation layer, PHP's vast library is also mostly procedural. The contrast between the MySQL and MySQLi libraries is a good example of how PHP is (MySQL) and how it should be (MySQLi). However, almost all of PHP's libraries are procedural: string manipulation, regular expressions, sessions, ftp, sockets, Images, database access (MySQLi is the exception) and so on.

    With PHP's own miserable ue of OO, it's not hard to understand why PHP developers don't leverage OO as much as they could. OO is hard, and PHP neither demands it nor does it bother promoting it.

    Layering
    The only support for layering offered by PHP is the include/require functions.  Like OO, proper layering can greatly increase code readability and maintainability. Much like in classic ASP, most developers tightly intertwine their PHP and HTML. The resulting code is truly worthy of being labeled spaghetti for its incomprehensibility and the difficulty to maintain. "echo" is core to PHP development, while the equivalent Response.Write is scarcely found in ASP.NET. Despite being easily abused, ASP.NET's CodeBehind model and DataBound controls are well ahead of any PHP offering. The lack of OO buy-in often leads to anemic domains.  On the data access front, the .NET community is as far ahead of PHP as Java is ahead of .NET. The introduction of LINQ in the next version of .NET will put it squarely ahead of all if developers adopt it.

    The first example of the MySQLi documentation in the PHP manual highlights the problem perfectly:

    <?php
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    if (mysqli_connect_errno()) {
       printf("Connect failed: %s\n", mysqli_connect_error());
       exit();
    }
    $query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
    if ($result = $mysqli->query($query)) {
       /* fetch associative array */
       while ($row = $result->fetch_assoc()) {
           printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
       }
       /* free result set */
       $result->close();
    }
    /* close connection */
    $mysqli->close();
    ?>

    I can't really think of a worse way to write this. True, this is only a reference document, but most PHP code follows the same pattern.

    Exception Handling
    Exception handling in PHP is like object oriented programming – it's supported but not used. The PHP framework scarcely makes use of structured exception handling as do most of the online samples.  The example which always gets under my skin is the MySQL library. Check the return value to see if a connection failed and echo out mysql_error() . Anyone who thinks using "or die" is any better just doesn't get it. Even the much improved MySQLi library didn't get it right.

    It doesn't really get any simpler than this: a failed database connection should throw an exception and in almost every case developers should let it bubble up to the global handler. I'm sure they exist, but I couldn't find a single standard PHP library which actually uses exceptions.  

    There's a base exception class which you can extend, great! But why bother when you'll still have to do that oh-so-dated return value check all the time. PHP developers are quick to point out that PHP 5 has first class exception handling but slow to actually use it.

    Other Issues
    The fact that PHP is a loosely typed language can lead to significant difficulties for larger projects.  Strongly-typed languages like C#, VB.NET and Java can take full advantages of compile-time checking and design-time tools (IDEs) while loosely-typed ones generally can't. (I have little experience with PHP IDEs, so maybe this isn't as much of a problem as I think it is).  Like everything else we've covered, the lack of strong typing generally makes PHP ideal for smaller projects and problematic for larger ones.

    .NET also has a clear edge when it comes to tool support. Visual Studio has long been considered one of the best IDEs around. It does cost a fair chunk of change (we are talking about medium/large apps though), but there's the free Express line which is quite adequate. For very large projects, PHP doesn't have anything like Team System.  When it comes to profiling, debugging and refactoring ASP.NET is ahead by at least a couple years. VS.NET's projects, solutions and references are all integral to code re-use. PHP largely goes by the copy-n-paste approach.

    Although both PHP and .NET lag behind Java when it comes to Agile methodologies, there's considerably more awareness and support for Test Driven Development, unit testing and Continuous Integration within the .NET community. .NET's tools are more mature and widely used. Searching for information on TDD, unit testing and CI for PHP returns only a handful of useful/relevant hits.

    Conclusion
    VB6 and VBA share a lot more in common with PHP than with either VB.NET or C#. More pointedly, VB6 developers share a lot in common with PHP developers.  Despite having some of the necessary tools, PHP developers (including those working on the actual PHP codebase itself) seem unwilling or unable to make use of core software engineering methodologies.

    From my own experience, and the countless of online tutorials and blogs, many PHP developers are guilty of the same crap code VB developers were once renowned for. OO, N-Tier, exception handling, domain modeling, refactoring and unit testing are all foreign concepts in the PHP world.

    This isn't an anti-PHP rant – it's a using the right tool for the job rant.  There are frameworks out there for PHP which help solve a lot of the inherent issues, but like ASP.NET some of the limitations are built-in (you can't get around C#'s strong-typing and you can't get around PHP's loose-typing). There are MVC  frameworks, O/R mapping frameworks and Agile tools.  Despite those, with the 5 year anniversary of ASP.NET a couple months away, and significant innovation in the next release of .NET, PHP is starting to lag far behind.

  • How to hire a programmer

    I'm always a little amazed when I hear of an employer who doesn't give a written test when hiring someone. I know it might take more time and I've often heard how some people don't work well under interview-pressure – but that's just tough luck. I swear to god, we have higher standards when it comes to hiring a juggler for a kid's birthday than when it comes to hiring programmers – I blame talentless managers.

    That mini-rant aside, my favorite way to perform interview tests is to write a very basic feature description go over it with the person and ask them to actually code it on a COMPUTER with all the tools you're used to using (specifically, google/the internet, VS.NET, SQL Management Studio, …). I never remember what an SQL Server connection string looks like, and I'm not going to expect someone else to remember.

    The type of "feature" I normally ask for is a *very* simple user-management tool where you see a list of users and can edit/add/delete their information (maybe a name, email address and password or something). I'll typically mention that the feature would be part of a larger enterprise application where maintenance is key. The goal is to get to see how they write their code. I'm specifically interested in seeing if they use any N-Tier or OOP concepts and how their codebehind/aspx are done.

    I like to give them about 1 hour to work on this and will let them have up to 30 minutes of "do you want a bit more time?" Hopefully we can all agree that 1.5 hours is more than enough.

    The first time I used this system we interviewed 7 people who did miserable. My manager (the owner of the company) told me the test was obviously too hard and I needed to adjust it. Luckily, before I had time to do so, we interviewed an 8th person who wrote it in 30 minutes and did better than people who worked on it for 2 hours. We hired him and it was well worth it.

    Here are some general recommendations if you do give exams (or want to start) during interviews:

    1. Don't make the exam too hard or too long
    2. Write it yourself, have other coders on your team do it too, and adjust it accordingly to how well you did
    3. ALWAYS let the person know they'll be writing a test prior to the interview and let the person know how long it should take
    4. Don't bother giving the exam unless you think the person is a good fit in all the other important categories
    5. To expand on point 4, consider doing it as part of a follow up
    6. Tell them to fill in any auxiliary parts (like logging) with comments if they want to, and that other comments aren't really necessary
    7. Give them full access to all the tools you use, create a blank database for them and give them a username and password that has full access to that database – for security purposes, you might have to adjust your network a bit
    8. Let them use the internet and any books you have (you might want to mention to them that they can bring any books they like to the interview for the exam)
    9. Treat them like any other employee: don't look at them code more than normal, sit them with your team (if possible)
    10. If you use stored procedures, mention that you'd prefer they used stored procedures if possible. If you don't say anything, don't hold it against them for whatever they use.
    11. At least 2 or 3 times ask them how it's going, what part they are on and if they are having any difficulty. GIVE THEM the answer to any questions they ask. If you don't know the answer, either tell them they are looking at it wrong or help them look it up. Don't be afraid to sit down and type A BIT for them.
    12. Let them know that their time is up and ask if they'd want to spend more time finishing stuff up or reviewing it.
    I'm not going to tell you what you should look for or not (since it'll depend on your own personal prefences), but there are some real tell-tell signs as far as I'm concerned:
    • Datasets and SqlDataSource are very bad (update: the dataset thing didn't go over too well in the comments ;) )
    • Data access shouldn't be in the aspx or codebehind
    • Objects should be properly disposed of (using either try/finally or usings)
    • Check how exceptions are handled: I don't expect anything in the global.asax Application_Error but exception swallowing is also very bad
    • Naming convention might not be a big deal, but I hate Hungarian notation
    • No hard-coded connection string (or at least a //todo, move to config)
    • Caching!
    • Parameterized queries for either sprocs or in-line
  • Internet over Vista is way faster && VS.NET 2005 working fine for me

    Like many others, I got Vista RTM installed over the weekend at home, and just finished 2 test machines installs at work (we've had clients asking about vista compatibility for a couple weeks now...).

    Anyways, at the risk of sounding like a massive fan-boy, the first thing I noticed was the downloading stuff from the internet (and general browsing) is faster. I'm talking 200k/sec to 300-320k/sec at home and 800k/sec to 1000k/sec at work. Maybe it's just because it's a clean install and all that, but if it isn't, this is hands down the best Vista feature.

    Aside from that, I got VS.NET 2005 up and running without a problem, along with Visual Source Safe and SQL Management Studio. Resharper was actually the only thing that really gave me problems - had to right click and "run as administrator" while installing. Also, when I start VS.NET, resharper throws an exception - something to do with permissions and the registry (But it still seems to work). Other than that, I'm able to debug and run my code - including web apps, multithreading services and networking stuff.

    We had some problems with our corporate virus scanner and samba integration (we had the latest version, which you need, but  also had to the LAN manager authentication level). At home, the beta Catalyst drivers seems a little buggy, but games are still playable.

    The security popups aren't nearly as annoying as I thought they would be. But it's amazing how much things have changed. Finding stuff can be a little tricky at times and sometimes you wonder whether the change was really necessary (like c:\document and settings --> c:\users).

    I'm a huge fan of Office 2007 (have been since early betas). If I had to pick between Office 2007 and Vista, I'd probably go with Office (keeping in mind that I spend a lot of time in Word and Outlook). Vista needs developer support before it becomes a truly worthy upgrade (.NET 3.0 and DirectX 10).

  • My 5 most used free utilities

    With Microsoft's release of ProcessMonitor, the next version of SysInternal's FileMon, I thought I'd share the 5 free utilities I use most often in my day to day programming.

    In no particular order (and I realize there's nothing original in my list):

    Reflector
    Reflector is a "class browser", which pretty much it let's you look inside .NET assemblies and figure out how they work. Recently I used Reflector to uncover a [possible] threading bug in MySQL Connector/NET. I also looked inside the SynchronizedQueue class to see exactly how it worked (yes, it was obvious but I wanted to make sure).

    Fiddler
    Fiddler is an HTTP Proxy aimed at helping you debug HTTP requests. It's particularly useful at Fuel because we work with a lot of different data protocols over HTTP. Of the 5 tools, it's probably the one I'd be most screwed without :)

    FileMon
    FileMon is one of those "last resort" tools - you don't use it often, but when it gets to the point where you need it, it comes in really handy. FileMon monitors file/directory access, so it's great when you think their might be permission issues or missing files.

    Snippet Compiler
    Lately I've been using SC less often, but it's still pretty handy. SC is a mini-IDE which lets you quickly compile C#/VB.NET code and run it as a console application. It's great for testing small chunks (snippets) of code.

    Visual Source Safe
    I know a lot of developers hate visual source safe. For the most part I agree with all of you. The truth though is that any source control is better than none (ya ya, not if it corrupts your repository). If you can, consider buying the relatively cheap SourceGear Vault or installing the free Subversion, but we are using VSS here at work and it's doing it's job just fine. 

    Honorable Mentions

    TestDriven.NET
    I haven't had a lot of opportunity to work with TDD.NET lately (doing a lot of planning/and very small projects). I love tools that enhance VS.NET, and this is the best one if you're into unit testing.

    FireFox
    There are a lot of addons that make FireFox a great tool for web developers. JavaScript console, DOM Inspector, Web Dev Bar.

  • Best of both worlds - SessionCache

    A while ago, Sonu of DotNetSlackers asked if I'd be interested in writing for the site. I've been a long time fan of what DotNetSlackers stood for (trying to bring some order to this vast community of ours), so I was more than happy to oblige.

    I'm a huge fan of .NET's cache. I can't think of many good reasons to use either the Application or Session objects, rather than the Cache object. Two things that I've always wanted from the cache is (a) per-item control over where the data is stored and (b) per-session cache.

    Best of both worlds - SessionCache is my brief article on how to achieve a reusable and clean solution to (b). It isn't anything fancy, but I thought it might be of some use.

    Feedback is always welcomed.

    P.S. - I know Sonu has to pay the bills, but IntelliTxt really sucks :) 
     

More Posts

Our Sponsors

Proudly Partnered With