31 July 2008

Web applications as web services

My running theory is that a typical web application is in reality a web service - distributed and reliant application. It is no surprise , I think all experienced web developers know it. On one side we have the server machine which runs HTTP server that is handles traffic to and from our server side application. The technology that is used to build the server side application is irrelevant. Nowadays you can use almost any programming language known to man to build a server side application. It is a matter of taste, really and of course client expectations. What is important is the fact that our application responds to request trafficked through the web server with code that is transmitted to the client using the HTTP protocol, and which is "compiled" into an client application inside the browser Virtual Machine. That client application is then able to send request back to the server for a new set of code which in turn will replace the existing application with a new one.
Well, that was true in the past, and not so much today (I'm talking about web apps here, not pages - I make a distinction between the two) because we have WEB 2.0 client apps that send request to the server for periodic updates for the client side code without the need for the complete user interface deconstruction we always experienced in the past, with what the ASP.NET guys called the old post-back model.
So if we think about our web apps in this way is no wonder that we need a team to build even the smallest of web apps. Excpecially if we wanted to build them right.
Web application developers experience a host of problems regular developers don't even know about. First there is the whole browser compatibily issue. CSS, HTML and javascript do not render on different browser, different browser verisons or event different operating systems in the same way. A piece of client side functionality that works in Firefox on Windows wont work on IE. If we are building a big app, we need specialized teams for each browser family in order to provide the maximum stabiliy and user coverage for our apps. Thats one problem we all know and hate for the life of us. But one problem that is often overlooked and not discussed out loud is the fact that HTML as a UI generating language was not designed to build UI for web applications. The original goal of the language was to ease the disemminationa and aviability of scientific papers between scientist. Not building applications.
And thats a big problem. If you've ever done javascript heavy applications you often encountered problems and you had to generate tons code to solve them , just because you wanted to implement a functionality that would take in desktop application several lines of code to implement.
I'm really glad that with HTML 5 they are going to fix or attempt to fix the problem. And what Micrsoft did with XAML in Silverlight and WPF and Mozilla with XUL is a real step towards the solutions of the problem. Thats how real web applications should be build in the first place. Why do I need to regenerate the entire UI with every click of the mouse, and why do I have wait so much time for stuff that should be handled on the client. The only thing that should be shuffled across the net is Data, not code to be compile again and again on my computer. It is not practical, it is not functional and it is not friendly. And what really bothers is me is the notion that building web applications is easy, is fast and simple and that kids could do it and its not real software engineering work. And what bothers me the most is that the notion often comes from people who know absolutely nothing about web apps.
First you have to wrap your head around the service generated and absolutely reliant client, than you have to learn and really understand five different technologies , of which four are have several critical difference based on the virtual machine they are executed upon (HTML, CSS, JavaScript and XML). The only simple thing (simple in the fact that you have to know one variation at the time ) is the server side language.
And you can do so much with the web, and it really pains me to see that people don't really understand it and to see so much problems that arise from that ignorance that could be easily solved if the applications in questions were developed with little more understanding what it takes to build a web project.
To build a web product you need to have to following people:
A web designer
A graphical artist
A javascript engineer
A RIA animator
A RIA programmer
A server side engineer
A database administrator
A system administrator
A really, really good architect
And what is important a leader with a vision and understanding what the platform requires of the people that are building atop it.

25 July 2008

Fun at work

It's not all hard work in my life. There is also fun at play. At work, I mean. I have plenty of fun at home with my family . My son , Sven, just started walking and I'm so happy watching him stumble with his first steps. He is so cute. But I digress, my family is not the theme of this post.
My work is.
See, when someone here celebrates a birthday is customary to throw a small party in the office. And since I work in a company that has more then three hundred people is party all the time (well at least once a month, because not all people actually celebrate their birthday or want to throw a party at work- but most do). Since my PM is often away, his desk is empty so people use it as a place to host the food and kick start the party.
This is quite nice, because my appetite is insaciable and having scores of food behind me really is handy when working. Plus is free, which is an even greater bonus.
I've posted a picture of the last party( which was , emm, today not one hour ago.. plenty of food left behind me). The picture shows the desk behind mine littered with cookies and juices:

20 July 2008

My take on MVC

MVC , or Model - Viewer - Controller is very popular on the web framework market. It has become one of the selling points for web based applications and framework. Everybody has or has to have it in order to beat he competition.
It seams if you don't implement the MVC pattern in your application you are not serious. And we all know that the most important thing in software development is being serious. If you are a member of a fringe developing group (PHP, Python, Perl, Ruby and so on... ) you have cerantainly being labeled as not serius as a software developer by your more serious and respectable colleagues in .NET, C++ or Java land. And they are right. Given the fact that all the big, technologically unsavy client go to them to buy software. It really matters. Its a fact of life. If you wanna develop in the big league you must be serious (and therefore program in serious programming languages and use serious developing techniques).
I've being developing with PHP for over four years now. I've started working with PHP 4 and switched over to PHP 5 when it hit mainstream. I loved the OOP capabilities it offered. In the beginning my web apps were plagued by a mirad of simple and annoying problems. Liking issues, navigational issues, code and markup mixed together, character encoding problems and so on. It got better over time, and what really made my apps better, more easier to develop and maintain was the use of the Model - Viewer - Controller design patter.
Every one and his grandmother has his take on MVC . I've developed mine, and I think I got it pretty much right. Everything revolves around one file. That file will be the single point of entrance of the application. In most cases it will be index.php since it's a natural entry point for every PHP base web site.
Beside the index.php I will create a directory structure that will consist of three directories : Contoll, Model , View. And one other file config.php for various configuration options.
It will look like this :
  • /
  • /Control
  • /Model
  • /View
    • /template
  • /config.php
  • /index.php

The code for the index. php file is this:
session_start();
session_regenerate_id();
/**
*
@author Nikola Stjelja ;
*
@copyright GPL2
*/

//Load configuration
include('config.php');


//Get the control
include('Control/control.class.php');

//Get the model
$model=Control::get_model();

//Load the page
$model->load_page();

//load the view
include('view/view.class.php');

//show the template
View::show_template($model->template,$model->data);


?>;

Each request in your application will go through this file. First the configuration will be loaded. The config.php file will contians only configuration options. It is best to set those options as constants. The the Control class will be loaded. It is a static class, e.g. it doesn't have instances. It has only one public method get_model() which will return the model object. The model object has one public method load_page() and two public proprieties template and data, which are loaded as argument to the View static class. The View class handles any templating your application has to do in order to present its content to the client requesting it. The entire entry point of our application has no more that 29 lines , it is simple and readable. It shows you exactly what part of the application does.
The beauty of it is that the application logic can become quite complex but the underlying system will remain simple to use and understand. Needless to say that the most of the work is done inside the model object. Here is the abstract Model class each model object must inherit from:
/**
*
@author Nikola Stjelja ;
*
@copyright GPL2
*/

/**
* Main application logic. This class is inherited by all Model classes.
*/
abstract class Model{
//Template file name
public $template='template.tpl.php';

//Array with the all data viewable as variables inside the template
public $data=Array('title'=>'MVC Application');

/**
* Class consturctor
*/
abstract public function __construct();

/**
* Prepares the page to be displayed.
*/
abstract public function load_page();

/**
* This method prepare super global variables for usage
*/
protected function sterilize($var)
{
$var=(strip_tags($var));
if (get_magic_quotes_gpc()) $var=stripslashes($var);
return $var;
}

/**
* Class destructor
*/
abstract public function __destruct();
}

?>

As you see the class is quite simple. It provides a simple interface that anyone can understand and use. The load_page is the main class method servers as the main class logic controller. The Control static class implement the Factory patter. It's main goal is to create the adequate model object based on the web page query. This is a very simple navigational mechanism. It allows to the developer great freedom in how to set up the site navigation. It's also very simple and short. The Control class may be extended with extra classes that check if the user is logged in or if the user accessing a resource has the right to do so.

/**
*
@author Nikola Stjelja
*
@copyright GPL2
*/

/**
* This is a static class that checks for a specific query string, and
* upon it returns the expected model object.
*/
class Control{
private static $choice;

/**
* Get the choice from the $_GET
*/
private static function get_choice(){
self::$choice=trim(strip_tags(urldecode($_GET['q'])));
}

/**
* Based on what date is contained inside the choice propriety return the
* model object
*/
public function get_model(){

//preuzmi izbor
self::get_choice();

switch(self::$choice){


//default model
default:
include('model/model.class.php');
return new Model();

}
}
}


?>

After both the Control and Model objects have done their job comes than the time for the View class to do its. It views class takes two parameters inside its show_template method. Both parameters come from the Model object, the template name and the data array. The main job the View class is to find the template file, fill it with variables . The rest is upon the template file it self. Normally the developer can implement his or her favorite templating system quite easly. But I like to stick with pure php. With discipline you can write a pretty readable template file just using ifs thens and whiles.

/**
*
@author Nikola Stjelja
*
@copyright GPL2
*/

/**
* Connects the template file with model provided data
*/
class View{

/**
* Loads and shows the chosen template
*/
public static function show_template($template,$data){
while(list($k,$v)=each($data)) $$k=$v;

//Load the template
include("templates/$template");
}


}
?>
The template is a standard php file. It receives its variables from the $data array generated by the model object.

15 July 2008

How I started

I started developing when I was ten years old. I lived in Umag, Istria with my parents and my yunger sister. I just received my first computer , the venerable C64. My father bought it for from a guy who worked in the marina, and smuggled it from abroad at my fathers request. My father has, and had a lot of connections. That will be for a man who for years was the sole insurance seller for a small town in Istria.
It was a grey, silent day. One of many like it in Umag. Winters were always hard. Empty streets, vast empty spaces where no one alive throdded and smiled. Not unlike summers where the streets were full of people, mostly foreigners who came here to enjoy their summer vacations. My father decided to buy ma computer after my constant pestering and nagging to buy me one. My best friend at the time had one , and I always loved to play games at his house whenever we visted his family. Finally my father decided to give in and by me a computer, to be exactly he decided to buy me a Commodore 64, just like the one my friend had. So, here we were, just outside the town marina, a gray winter day dying around us, and my father just gave a slip of deutche marks to a guy I never saw before, and most cerentainly will never see again, and in return he got a brain new commodore 64.
It was a dream come true. My mother, of course disapproved when she found out what had my father done. But it was over. The corner stones of my career as a software developer were set.
I started by playing games. All the classics , bought or copied from pirate collections bought legally at the time from a magazine in Belgrade. It was a different, different time. And I was young and living the life. I never grew over games, but I wanted more. I wanted to create them. I've created several board games at the time, me and the children from the apartment building played them together outside my apartments door, but I wanted to create computer games.
The C64 box came with dutch manual. It explained the basic of BASIC, how to write statements, how load and save programms. It was my mother who helped me out at first. She wrote with me my first 'Hello World' applications. She wrote with me my first 'IF THEN ELSE' structure.
She soon couldn't keep up with me. I always loved to write, and I also became interested in RPGs. I never played them, and never could in my little, redneck town. I read about them in a kids magazine. My imagination became so feverish with the possibilities RPGs offered that I just wanted to play them , write them.
My first games were a cross over between chose your adventure books and rpgs. The were short, written inside a day in BASIC, and morbid. My imagination ran wild. Of course, they are lost now, sacrificed to my adolescence.
The last thing I wrote for my C64 was an arcade. I just figured out how to do primitive graphics reading code from the back of the manual. The game was simple. The hero 'Super Nikola' chased the bad guy who was his dark opposite across the screen. I never finished it.
Times finally got us.
The war came, the famine, the fear. The big fucking scare. I stopped playing with my computer, and made my self busy with playing invisible. I'm from mixed ancestry, and in war time Croatia that was the wrong thing to be. So I survived.
After scary years things normalized in my part of the country. We were near the Italian-Slovenia border, so no war came to us. Only a flood of refugees from all over. My father got fired, and he and my mother decided to start a company. I started working there at the tender age of twelve years old. Just during vacation time. Manning the office, selling insurance and of course learning to use a brand new 486.