Wednesday, April 13, 2011

Writing a Bare-Minimum PHP Application

PHP has a pretty bad rep for being cluttered, inconsistent, and ugly. Why would any language use \ as a namespace delimiter? Or –> for dereferencing object members? Well, because, . is reserved strictly for string concatenation… HA! But although PHP has many shortcomings, it remains the most ubiquitous language for web development today. Perhaps one of the most distressing things about PHP is that it makes it very easy to write bad code. The gentle learning curve assures that PHP is heavily used by beginners and is the server-side language of choice for almost every shared web hosting service. It also assures that there is a lot of bad, bad code out there.

When I first learned PHP, I remember wondering why anyone would want to write a class or a function when you could drop a bunch of PHP inside a bunch of HTML and get instant gratification. I wrote a fan-finder application for a fan site that my friends and I ran at the time this way. Users of our fan site would write a little bio and upload a picture and they would be added to an interactive world map of all users who used the service (a few hundred). There was no database, everything was saved into a pipe-delimited text file, which needed to be read and written by nearly every page in this application. It got completely out of control! The same code for reading/writing the data file existed in at least twelve different places, all written slightly differently. It was riddled with bugs. I barely managed to keep it alive—I actually had to maintain the data file by hand at times. I learned very quickly how important it was to keep my code minimal and clean. Some languages attempt to force you to do this, to a degree—but PHP made it so easy to do everything the wrong way.

Nowadays I would just use one of the many excellent PHP frameworks out there, such as Code Igniter. But sometimes it's nice to build everything yourself. So I spent some time designing an ultra-light, barebones PHP app which I would consider to be the minimum required amount of structure for a small PHP application, in order for it to be able to survive and evolve. This is intended mainly for PHP beginners--my "framework" is by no means an optimal solution, and many other designs would be sufficient also, but here's what I would do:

Wednesday, April 6, 2011

Vectors, Projectiles, and More: Doing Physics in .net Part III

Having just started getting into C#, I can already say that it’s a pleasure to work with – especially when used with its tightly coupled IDE, Visual Studio. Of course, being a physics/astronomy/computer science geek, I got straight to work doing some projectile physics in C# to see how it turned out. To get myself started on this blog, I’m going to write a couple of posts about doing physics in .net:

In part 2 I started out an F# physics library containing the units we want to use, and some unit-safe math functions called Units.fs (note the edit added to part 2 regarding the sqrt function – you don't need to write one, F# has its own built-in, unit-safe sqrt). This will be used by the rest of the library and should remain the first source file in your project (in Visual Studio, the order the files appear in is the order they are compiled in. Now we want to start fleshing out our library and making it into something useful. I'll start by moving the whole projectile class into the F# library.

But wait. I'm pretty much just writing this whole thing over again in F#, right? The point of this is to interoperate between the two languages, after all. Really, this is the proper thing to do – we're going to write the projectile physics as a class in F#. The class can be extended by C# or other .net languages if you want to add qualitative properties to the projectile (such as, making a red ball that you want to send flying through the air – you just attach the projectile physics to your otherwise normal red ball object).

So here's our C# class, translated into F#:

//Projectiles.fs
#light

namespace FsPhysicsDemo

module Projectiles =
    [<Literal>]
    let g = -9.808<m/s^2>

open Projectiles
open UnitSafeFunctions

type Projectile =
    val mutable xi : float<m>
    val mutable yi : float<m>
    val mutable vxi : float<m/s>
    val mutable vyi : float<m/s>

    new() = {
        xi = 0.0<m>; yi = 0.0<m>;
        vxi = 0.0<m/s>; vyi = 0.0<m/s>;
    }
    
    member this.xf (t : float<s>) =
        this.xi + this.vxi * t
    member this.yf (t : float<s>) =
        this.yi + this.vyi * t + g * sqr t

Here we've moved our g constant out of the class, and into a module. As for the Projectile class, you see that we opened the Projectiles and UnitSafeFunctions modules just above them. This is because we use the g constant, and our unit-safe square function in the last member function in the class. We don't have to open anything in order to use the units however, since those weren't defined in a module, but rather just in the namespace (our Projectile class is also defined at namespace level – types can be in a namespace).

That's not bad, now we have our C# class, but a little more condensed and unit-aware. We're not really harnessing much power from functional programming right now though—but what more can we do? Well, those x and y values all belong together. In C# you'd probably use the Point class in System.Drawing, or make your own class. In a functional programming language (or one that supports them nicely, such as Python) you could use a Tuple: