Archive for the ‘Languages’ Category

Frameworks Make Reading New Code Hard

Sunday, January 25th, 2009

In an effort to increase my knowledge of both Ruby and Rails, I have been keeping my eyes open for interesting Ruby/Rails apps to peek at the source code of. Whilst looking at @j_stirk’s website, I noticed it was running on Radiant CMS, a Ruby CMS. So, I grabbed a copy of the source code.

Whilst I was having a peek at the source, I began to think that I was in a little over my head. I managed to muddle my way through the config/routes.rb file and figure out (with judicious use of Google) what requests were being directed to what controllers. The problems started happening when I hit the controller, and started seeing filters and symbols and statements that I didn’t know where to go next to find out about.

Confusion

At first I thought it was to do with the fact that I don’t really know Ruby. I know enough to recognise it, and muddle through basic syntax, but I don’t know any tricks, any coding conventions and the nuances of Ruby programming. However, upon reflecting on my dilemma, I realised this isn’t something that was restricted to Ruby, Rails and Radiant CMS.

I remember last year looking into Symfony, before deciding that PHP was too god-awful ugly to waste my time on. I recall that too had a fairly high learning curve, and looking through an existing Symfony app, it was very hard to figure out what exactly was going on. There was much API browsing.

A few years ago when I first learnt ASP.NET, I was in a similar situation. Server tags and user controls and page events and assemblies were all very new and very confusing to this ex-PHP & ASP 3.0 developer. I stormed through a book on ASP.NET and was thrown head first into a contracting gig before catching my stride again.

I love frameworks. Ever since I heard about this thing called “Rails” a few years back, and realised there were a few similar frameworks written in PHP, I’ve been in love with frameworks. And I’ve always learnt them from the ground up, creating my own application. Last night was the first time I’ve ever tried to learn a framework from an application.

And it’s hard.

While frameworks increase programmer productivity and make maintenance easier for programmers who already are in the know, they make things really hard for a new maintainer who is not familiar with the code to dive in and figure out what’s going on. Not only do they have to learn the individual nuances of the programmer who wrote the application, they also have to figure out how the framework works.

PHP Has An Identity Crisis

Sunday, January 11th, 2009

I have been using PHP for quie a while. I first learnt PHP 4 when I was 18-19, building a dynamic art gallery type site for my drawing attempts. I thought it was wonderful that I could programmatically display new images without having to update the HTML file. I also used PHP for my final year project in university, and by the time I graduated, I was quite proficient in it.

However, as I slowly got more and more interested in programming, and the principles of programming, I learn more and more languages, and learnt OOP practices that I never applied to PHP, simply because I was self taught, and at the time PHP wasn’t pushing itself as an OOP language.

However, times change, and the PHP community tends to push the fact that PHP is an OOP language. Firstly, this isn’t entirely accurate. PHP is a language that supports OOP, however it isn’t an OOP language. There is no string object – I cannot take a substring of a string by calling a method on the string variable. Instead I must use a global function substr() to do it. That’s not OOP, it’s procedural wrapped in OOP.

However, PHP does support OOP. You can define custom classes, you can create objects and you can call methods on that object. In a hostile world full of more OOP languages like Ruby and Python that are gaining traction in the web development world, PHP must push more and more it’s OOP support. If you talk with serious developers in the world of PHP, they will insist that although PHP is not inherently an OOP language, it’s close enough.

Today I found out that from PHP has introduced in PHP 5.3 the GOTO statement.

For those of you who are unaware of the specifics of the GOTO function, this post, also the post that I learnt this fact from, outlines it. While experienced programmers know enough to avoid the GOTO statement, new programmers may not, and PHP can be abused enough already without the GOTO statement.

So, what is it PHP? Are you an OOP language, or are you procedural? The PHP community pushes it’s support for OOP, and then it introduces a control statement that characterises the worst of procedural code. PHP needs to clarify it’s future as a language, and decide whether to push itself as an OOP language or as a procedural language.

Encapsulation in Python

Saturday, January 3rd, 2009

Or, “I see London, I see France, I see Python’s underpants”

I come from a primarily C# background, where I have fairly tight control over how visible my properties and methods are. I can choose to make them private to the class, protected to the class and it’s children, internal to the assembly or public for everything.

In my course of learning Python, I’ve discovered awesome things like docstrings, multiple inheritance and sequence unpacking. However, I’ve also been shocked and made more than a little uncomfortable by the realisation that Python doesn’t implement encapsulation. I don’t trust other people not to abuse access to methods that I’ve intended to be private – I’ve seen too much bad code for that. While I know there are ways to get around this, I don’t feel I should have to perform code gymnastics to implement what I feel is an essential part of an OO language.

Another thing that makes me cringe a little about Python is the need to pass in the self parameter for all object methods. I realise that this is required to differentiate between an instance method and a class/static method, however I just can’t shake the idea that it seems a little clunky, even though I know it’s no different from writing a static at the start of my function definition.

However, to wrap up the last two weeks, I feel confident in saying that from what I’ve seen I like Ruby more than I like Python. That’s not to say these are bad languages; I first learnt web based programming with PHP, and I’m sure that I’m going to leave PHP in the dust for one of these two languages. I will also disclose that I did not learn Python 3.0, and haven’t really looked into it whatsoever, so if 3.0 changes some of the stuff here, forgive me.

Having said that, I will investigate both languages more, and definitely work in both more. I’ve found PHP doesn’t do it for me anymore, I’ve seen a better side of programming where I don’t have to live with ugly warts in my languages, and PHP is ugly. Long live Python and Ruby.

Passing Parameters in Actionscript

Monday, December 29th, 2008

While in the process of trying to learn Python, I decided it would be a good idea to attempt to learn some AI and AI related concepts at the same time using the PyGame framework, as advised by AIGameDev.

For a while, I’ve had this really nice steering demo/white paper in my Delicious account, something that I’ve meant to try implementing myself. Because this was entirely new territory, and I know my physics/math isn’t up to scratch, I decided to try to implement some of the behaviors outlined in the previous paper in Actionscript 3.0 first, then when I got the hang of it, port it to Python.

After about 8 hours or so, I had finally puzzled out the seek/flee behavior, and tried to put together a demo as in the above page, when I hit a strange bug. If I put the seek behavior vehicle on the stage on it’s own, it performed correctly. If I placed the flee behavior vehicle on the stage on it’s own, it performed correctly. If I had both on the stage at once, they freaked out and opposed one another, and the two vehicles often got caught in a state of equilibrium, their velocities canceling each other out.

It seemed as if my little vehicle views were sharing the same vehicle model, and the two different behaviors were acting on the same vehicle model. Which didn’t make any sense, considering they were two separate instances of the same class.

After a lot of bug testing and fiddling around, I finally realise I had been caught by the way Actionscript passes parameters into functions/constructors: unless you’re passing a primitive data type, Actionscript passes by reference.

I was instantiating my vehicles with the same velocity and starting position, and I was using arrays to pass in this information into the constructors, little knowing that the actual array each class was manipulating was the same array.
You can easily bypass this by importing

mx.utils.ObjectUtil

and using the

ObjectUtil.copy

method (remembering to cast it back to your desired data type, in my case an array).
So, if you find your instances seem to be sharing private variables they shouldn’t be, check your parameters and make sure you’re not passing things by reference to multiple instances and manipulating them.

Ruby Wrap Up

Friday, December 26th, 2008

Today marks the last day of my Ruby portion of my Ruby/Python Bootcamp. Unfortunately with the Christmas and related Christmas stresses, and an impending move, I didn’t get to spend a lot of time with the language, beyond the fundamentals.

I did a dumb thing, and started to try to get a website up and going on Rails 2.2. Rails is a great framework, although I’ve found the learning curve beyond your basic scaffolding and MVC set up to be fairly high. I made extensive use of this Rails API guide and it certainly helped a lot, as did the Ruby on Rails guides portal. The reason this was as dumb idea had nothing to do with Rails or Ruby, and everything to do with the fact I hate making websites.

Seriously, I’m on holiday. I should be staying away from websites for a while.

So, I didn’t end up doing much on the website I promised I’d show, and there’s not really anything to show, yet. I did manage to set up an excellent workflow with SVN and my web-host, which will eliminate the need for an FTP client, and Ruby did help me complete my partial understanding of RESTful design.

So, I walk away after a week of Ruby with a rudimentary knowledge of Ruby syntax, which is enough to let me read Ruby code and fill in the gaps myself, a stronger knowledge of RESTful design and a much smarter development workflow.

Thanks, Ruby.

So, tomorrow, or tonight, I’ll kick off Python, and I’ll also see if I can’t use it while investigating a few other programming related interests, like OpenGL programming or some AI stuff, if I can figure it out. Hopefully I’ll be able to get as much out of Python as I got out of Ruby.

First Impressions Of Ruby

Saturday, December 20th, 2008

I like it.

A lot.

But I always knew I would. I’m so far impressed with how much effort Ruby goes to at making itself understood at a casual glance, and just how easy it is to write self documenting code. Writing code that’s free of braces and parenthesis is refreshing, and it’s nice to know the safety blanket is there (with regards to parenthesis for functions, conditional statements, etc) is there if I need it.

I started last night by flicking through Why’s (Poignant) Guide, and found it pretty good for the basics. I quickly picked up the general concepts and basic syntax fast, and Why’s Guide was pretty whimsical and kept things interesting. However I quickly bored of the absurdity in chapter 5 where the majority of its content is fluff.

I tried switching to SaphireSteel’s The Book of Ruby, however right at the start when the author stated:

if (subtotal < 0.0) then subtotal = 0.0 end

Putting everything on one line like this adds nothing to the clarity of the code, which is why I prefer to avoid it

I knew that this book would not help me. I already know many languages that require simple statements like that to be spread over multiple lines. I want a language to do this:

subtotal = 0.0 if subtotal < 0.0

Hi, Ruby.

Then I browsed over to Ruby in 20 Minutes and quickly flicked through the pages, and it concreted my knowledge. I like iterators, I like respond_to?, I like how everything seems to make sense. I also am pleased to see how many concepts map onto similar things in C#. Modules are like namespaces, but better. attr_accessor is like { get; set;}. attr_reader is like {get; private set;}.

I feel I’m ready to go find some code in the wild and attempt to read it. I’ll also see if I can come up with a post that maps Ruby concepts to C# concepts for other C# programmers interested in Ruby (and with IronRuby, there’s no reason why you shouldn’t be).

Ruby & Python Bootcamp

Friday, December 19th, 2008

It’s common advice in the programming industry to learn one new language every year to stay on top of things. It’s something I honestly believe in as well. Unfortunately I can’t say I’ve learnt a new language this year.

Last year, I learnt Actionscript 3.0, which is different enough from Actionscript 2.0 to be called a new language. I did this because of work, I did it in December and I had to do it fast. That worked pretty well for me. I got thrown in the deep end and had to learn or drown.

This year, with this being my last day at work for 2 weeks, I’m going to do the same thing, but I’m going to make it a little more interesting. I consider myself a reasonably competent programmer, and fairly solid in my knowledge of OOP paradigms. I think I can learn a language pretty quick. So, during my holidays, I’m going to:

  1. See how fast I really can learn a language at least to the point where I know what to search for online and can find my own way around reasonably confidently.
  2. See just how easily various concepts from different languages transfer across syntax
  3. Build some stuff I’ve been meaning to build for a while but kept putting off

With that in mind, this year I’m going to learn both Ruby and Python in 2 weeks, dedicating a week to both languages. In the process of learning these languages, I will also build a base for 2 websites for 2 of my domains that I have been meaning to build. I won’t spend any time designing them, just building the code.

So, over the next 2 weeks, the majority of posts on here will be reflecting my thoughts on Ruby and Python, and linking to resources that I plan to use in my learning, with a final post being the unveiling of the websites I manage to build.

Wish me luck!

Compile a User Control as a DLL

Monday, December 8th, 2008

At work, I’m redeveloping the old, ASP 3.0 CMS application and replacing it with a shiny new .NET 3.5 implementation. The software is designed to be run as a virtual directory from all websites, and thus shares the same from installation to installation.

The old CMS is a mutant. It started as an ASP 3.0 application, and then had extra bits made of .NET 1.0 & .NET 2.0 tacked on to it, much in the same manner that Frankenstein’s monster was created. The core ASP 3.0 application handled the page/news creation and publishing, and served as the interface.

The additions were mostly in the form of user controls (.ascx files) that were placed in the content for a page that needed one. These user controls took care of things like random image rotators, contact forms, bread crumb menus, etc. Various sites used a various sub-section of a pool of controls.

The modus operandi with the current system is that each installation has it’s own collection of controls which are inserted into pages by us on behalf of the client, because it involves putting ASP.NET markup into the content areas.

I’ve decided this isn’t such a great idea, and as such, I’m centralising the user controls as a library, seperate from the CMS, so that each site accesses the single pool of controls, and if a client leaves, it will be relatively simple to recompile a new library with the controls they use.

Anyway, that’s the back story. Essentially what I need to do is compile a whole load of .ascx files into a dll, and load them into pages. I’ve spent all morning looking on Google for tips on how to do this. And frankly the results were less than helpful, however I have a history of not reading documentation properly.

What I found out you need to do, is to add a class library project to your solution, and add the .ascx file as per normal. However, you need to do several steps to get this working properly:

  • Go to the properties of your .ascx file and change the build action to Embedded Resource.
  • From your control declaration on the .ascx file, delete AutoEventWireup. We need to wire these things up ourselves. Personally I cleared it of everything except the language attribute.
  • In the old code behind (now unattached class file) for the .ascx, you need to override the OnLoad event handler, and put the following code:
    string content = String.Empty;
    Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(GetType(), "example.ascx");
    using (StreamReader reader = new StreamReader(stream))
    {
    content = reader.ReadToEnd();
    }
    Control userControl = Page.ParseControl(content);
    this.Controls.Add(control);
    

    This source will use reflection to open the currently executing dll (in this case, our .ascx library) and find the content for the .ascx, parse it as a new control and add it to the currently executing context.

  • For every control on the .ascx, in our now orphaned .ascx.cs you need to declare a private/protected variable of the same time, with the same variable name as the ID on the control (for sanity’s sake). You need to find this control in our newly loaded .ascx and assign a reference:
    labelInAscx = (Label)control.FindControl("labelInAscx");
    
  • The above two steps is exactly what the AutoWireUp attribute on the control declaration does for you. We do it manually because we have to use reflection to extract the content of the ascx file, which is compiled into our dll.

After taking these steps, compile your project, and in another project, on a regular aspx page, register the assembly and use as you would any normal .ascx and hopefully the content will render out fine.

Compile .ascx as DLL solution for Visual Studio 2008

Adobe AIR 1.5 Update Framework

Thursday, December 4th, 2008

Lately I’ve been working on a small Adobe AIR application, and one of the last things I had to do for this application was build in auto-update functionality. So, naturally, I hit the all knowing sage with my question, and found many blog posts referencing the official Adobe AIR Update Framework, an additional swc library you can download and include in your project.

Last night I found out that this framework has been rolled into the latest AIR 1.5 SDK, so I checked my version and downloaded the update. My experience with the Flex SDK told me I’d have to find the SDK path in Flex Builder and point it to my new SDK to get the auto-update goodness.

This, however, was not the case – there was no variable. So I searched more online for the answers to my question, and finally found some official Adobe documentation. After a quick browse through, I deemed this useless. I still could not find how to get access to the ApplicationUpdaterUI class. 

Eventually I re-read that documentation, and a section that I thought was incorrectly copied from the old update framework download actually held the key.

You still need to copy the swc library to your project

Only, the swc is in the AIR SDK package, not available as a separate download. Having copied the library to my project, I then had access to the auto-update goodness, but no thanks to Adobe’s confusing documentation.