Brainstorming a name for a software company that I’m a part of (well, as soon as we sign an LLC with this name). A post on 10 company name types on TechCrunch is useful.
October 30, 2009
October 24, 2009
October 18, 2009
The complete solution for implementing a deep-copy clone() method in ActionScript 3
When I started out to write a clone method for a few of my AS3 classes, I looked online and didn’t find any official information about cloning classes from Adobe. There were however a few solutions that were hacked together to provide the same functionality (here and here). These solutions have one big problem though, they don’t work with classes which have non-primitive properties.
My solution mostly builds on top of these solutions but it also provides the ability to clone classes which have properties that are custom class. And it also works with inheritance.
I’ve placed all helper methods in a CloneUtility class for better modularity. We’ll be using these methods in the code examples below.
public class CloneUtility /** registerClassAlias(qualifiedClassName, getDefinitionByName(qualifiedClassName) as Class );
{
/**
* This method registers an alias for a class. This registration is required for writing and reading the
object in AMF format. If the object is not registered, a runtime error will be thrown when the object is written
in preparation for a clone.
*
* @param object The object to register.
* @return void
*/
public static function registerClass(object:Object) : void {
var qualifiedClassName : String = getQualifiedClassName(object).replace( "::", "." );
registerClassAlias(qualifiedClassName, getDefinitionByName(qualifiedClassName) as Class );
}
* This method writes the passed object to a byte array. This method is supposed to be used in the
clone() method of the object that is passed.
*
* @param object The object to clone.
* @return void
*/
public static function writeObjectToByteArray(object:Object) : ByteArray {
var qualifiedClassName : String = getQualifiedClassName(object).replace( "::", "." );
var bytes : ByteArray = new ByteArray();
bytes.writeObject(object);
bytes.position = 0;
return bytes;
}
}
For an example of how to make your classes cloneable, I’ll go with the example of class A and its subclass B. (B extends A)
This technique of making a deep copy works by writing the object to a bytestream, and then reading it back in as a new object. To facilitate this process, the super class (A) will need to implement the IExternalizable interface, which requires the implementation of two methods:
public function writeExternal(output:IDataOutput):void
public function readExternal(input:IDataInput):void
The parent class would look something like this (explained with inline comments).
public class A implements IExternalizable
{
private var prop1:String;
private var prop2:int;
public function A()
{
// we need to register class to be able to write and read it back successfully
CloneUtility.registerClass(this);
}
/**
* This method is responsible for returning a deep copy of this object.
*
* @return the cloned copy of this object
*/
public function clone():A {
var bytes : ByteArray = CloneUtility.writeObjectToByteArray(this);
return bytes.readObject() as A;
}
/**
* All properties of the class must be written to output.
*
* @param output The stream to which to write to
* @return void
*/
public function writeExternal(output:IDataOutput):void {
output.writeUTF(prop1);
output.writeInt(prop2);
}
/**
* All properties of the class must be read from the input, in the same order as they were written out.
*
* @param input The stream from which to read from
* @return void
*/
public function readExternal(input:IDataInput):void {
prop1 = input.readUTF();
prop2 = input.readInt();
}
}
Note that the writeExternal() and readExternal() methods will be invoked when the clone method is invoked (by the write and read calls).
The sub-class, B, would look something like this.
public class B extends A
{
private var prop3:uint;
/**
* The constructor registers this class to enable it for cloning.
*/
public function B()
{
super();
// subclasses need to register themselves as well
CloneUtility.registerClass(this);
}
/**
* This method writes all properties of this object to output.
*
* @param output The stream to which to write to
* @return void
*/
public override function writeExternal(output:IDataOutput):void {
super.writeExternal(output);
output.writeUnsignedInt(prop3);
}
/**
* This method reads all properties of this object from input.
*
* @param input The stream from which to read from
* @return void
*/
public override function readExternal(input:IDataInput):void {
super.readExternal(input);
prop3 = input.readUnsignedInt();
}
}
Note that the sub-class doesn’t need to override the clone method, just the writeExternal() and readExternal() methods.
So far it’s been pretty simple. The cool thing however is that you can also easily clone classes that have a custom class (which supports cloning) as a property. Below is code of a class C that has a property of type B.
public class C implements IExternalizable
{
private var customProp:B;
/**
* The constructor registers object to enable cloning.
*/
public function C():void
{
CloneUtility.registerClass(this);
}
/**
* Make a deep copy of this object.
*
* @return A duplicated object of type C
*/
public function clone():C
{
var bytes : ByteArray = CloneUtility.writeObjectToByteArray(this);
return bytes.readObject() as C;
}
/**
* This method writes all properties of this object to output.
*
* @param output The stream to which to write to
* @return void
*/
public function writeExternal(output:IDataOutput):void {
output.writeObject(customProp);
}
/**
* This method reads all properties of this object from input.
*
* @param input The stream from which to read from
* @return void
*/
public function readExternal(input:IDataInput):void {
// note that you should cast as the parent custom class (if there is one)
customProp = input.readObject() as A;
}
}
Note that you will use output.writeObject() and input.readObject() for custom classes. These methods will invoke the writeExternal() and readExternal() methods that are implemented by those custom classes.
Let me know if this works for you or if you have any suggestions for improvements!
March 7, 2009
Evolution of the PC – Where are we headed?
I’m reading In Search of Stupidity: Over 20 Years of High-Tech Marketing Disasters these days which talks about some high profile and some not to high profile blunders in the early days of the tech industry. It feels so unreal to read about the early incarnations of the ‘computer’, which supported at most 640KB and were heavy enough to induce back pains.
I remember seeing and touching some of these old dinosaurs, and it’s amazing how fast we’ve progressed. I was wondering what people 20-30 years down the line will think of our Macbook Pros, our Netbooks and our HP Dragons. I wonder how quaint our operating systems and software will look to them.
Even today we can see change happening as the concept of the PC being the sole repository for everything is becoming antiquated as more and more things are getting pushed to the cloud. I’m really impressed by some of Microsoft’s new Live services, particularly Live Mesh. It will be interesting to see what role Windows 7 plays in this transition of everything to the Web and how Windows as an operating system will evolve over the decades.
Exciting times indeed.
Rant ends here. Now I’m going to enjoy spring break!
November 21, 2008
User Experience Design – Thinking out of the box
GreenWhite recently posted a Google talk, “Don’t make me click“ (sound familiar?) by Aza Raskin. Although his Wikipedia page has all the details, but this amazing guy (apparently a year younger than me) gave his first talk on user interface at the age of 10. I guess it helps if your dad is the visionary behind the Macintosh.
Anyway, the talk was pretty interesting. He spoke about how engineers and designers succumb to the ’seduction of interaction’ in trying to making their UI’s ‘look cool’. The point that he was making was that UI’s should strive for the least bit of interaction possible. No interaction is the best interaction.
This is not something that is really surprising, since this concept was present way back in classics such as Don Norman’s Design of Everyday Things (if you haven’t read this, and you’re a software engineer, read it now before you write another line of code). A good tool is almost invisible. The user doesn’t need to think about the tool when using it.
He showed off some pretty cool interaction concepts, some of which have already been implemented in existing features. I really liked the automatic loading of content in an RSS reader as the user scrolls down (its called ‘river of news’, go ahead and Google it for a demo). I thought it was a pretty good example of doing what the user wants without having them interact with the application, i.e. reducing interaction.
He also showed off Social Helix’s calendar (check it out, it’s pretty cool!), which has a pretty smart interaction design. Effective use of a simple zoom-in/zoom-out allows the user to access all events in the entire 21st century with a few simple mouse movements. He contrasts this with regular implementations of calendars (Google Calendar in specific) in which the number of clicks that you need to make to go to a certain date is directly proportional to how far away that date is from the current date on your calendar (which is sometimes a lot).
During the QA session he mentioned something that I’ve been studying/reading/working on a lot these days, which is to design an effective UI, you need to start really early in the project lifecycle. Usability needs to be addressed right from the requirements phase. I had meant on writing a few posts on this in time for World Usability Day (I’m exactly a week behind :p) for GreenWhite, but yet again I proved to myself how incredibly lazy I can be.
Anyway, check out Aza Raskin’s blog, its got some pretty interesting stuff. I’ve actually read some of his stuff up on ALA without knowing who he was. And I’m pretty sure he’s got a hand behind the cool stuff going on at Mozilla labs.
September 3, 2008
Google ups the Ante with Chrome
Google has officially entered the browser market as of yesterday with their Google Chrome browser. It’s a completely new take on browsers (especially some of the technical aspects unknown to normal users) with a focus on making the browser a faster and safer platform for web applications and to move the focus from the browser to the web site.
I’ve moved to Chrome as my primary browser from the awesome Firefox 3. There are some features lacking in Chrome that I was used to in Firefox 3, but comparing a mature browser like Firefox with a first Beta isn’t really fair. And I’m sure plugins for Chrome will make up for any feature deficiencies, like they did for Firefox 1 and 2.
So far my Chrome experience has been exceptional. The browser loads really quickly and is very responsive. The one feature I absolutely LOVE is the omnibar, great job on that Google! I won’t talk about the features of Chrome in detail, since that’s been done already by a lot of people here (John Resig), here and an article on Internet News.
But before you check out any of those be sure to read the comics introducing Chrome (by Google), they’re very insightful.
It’ll be interesting to see how the browser market share shifts. Will Chrome take the users away from Firefox or Internet Explorer? I’m guessing Firefox, since the user base of Firefox is most likely to consist of early adopters. Majority of the people who use the Internet Explorer family of browsers hardly know of any other browser.
I’m very excited, as a web developer, about the paradigm shift that Chrome will undoubtedly bring in the browser market. And with other exciting projects like Prism, IE8 and the awesome stuff going on at Mozilla labs, we’re all set to change the face of the web as we know it.
P.S. check out these Javascript performance comparisons between Chrome and the other browsers. They’re insane!! The V8 JavaScript Virtual Machine incinerates the competition!
June 16, 2008
Firefox 3 – Why so few pledges from Pakistan?!
Firefox 3 is coming out tomorrow, and this time around Mozilla is going for a Guinness Record for the most software downloads in a day.
So far there have only been 1971 pledges from Pakistan. That puts us in the same category as countries like Ecuador, Belarus, Sri Lanka, Algeria and Egypt. Iran has a whopping 8684 pledges! And they’re right next door to us. (there’s no point in drawing comparisons with India)
Wasn’t Pakistan supposed to have one of the fastest growing tech industries? I thought our software industry and our ‘techies’ in general were a lot more enthusiastic about technology and generally well informed.
And Firefox is more than a web browser, for us web developers it’s a full fledge IDE. I simply can’t imagine web development without Firefox and Firebug. I shudder when I remember the days when we developed applications using Internet Explorer, and pretty much guessed what was going wrong with markup rendering.
So what’s wrong with us? Why are there so few pledges? What factors determine the level of participation in such events? Are we Pakistani’s inherently insular?
UPDATE: At least some Pakistani’s are doing the right thing. The awesome people at WCCFTech have put up a banner and I’m sure they’ve been the source of many pledges.
February 28, 2008
LUMS Comics
Yaay! We finally get our very own comic strip!
Funny stuff from a very gifted artist. The drawing style is somewhat similar to phd comics, which are my all time favorite web comic.
Added to pakdoc as well. Keep up the good work Bilal!
January 26, 2008
GeniDo – Granting your first wish (Offline Basecamp)
It was rightly predicted that the next big thing on the Web would be offline support from the web applications that we know and love. More and more offline apps keep popping up everywhere. The latest one to roll out is GeniDo (click to see demo video), a product by GeniTeam (founded by LUMS alumni).
I think it’s a great product idea, given the huge user base of Basecamp (more details at G&W). It’ll be great to see similar products for other web applications like rememberthemilk, backpack etc.
Regarding offline support for web apps, Flex and Google Gears have helped a lot in providing the infrastructure. I personally am really excited about Firefox 3’s offline app support (demo). Will finally have a reason to consider building Firefox only apps …
January 22, 2008
Writing Semantically correct HTML
“I see dead HTML tags”
Seriously. Sometimes I’m amazed at how the Web is still able to function with the absolute junk with which web pages are built.
CSS is almost 10 years old, but it still doesn’t render uniformly across browsers (even latest ones). And the fact that it is severely crippled doesn’t help either (you can’t even vertically center text without applying half a dozen hacks!).
JavaScript, the poor thing got crippled during its inception due to the infamous browser wars. It’s precariously carrying the burden of Web 2.0 on its worthy yet twisted shoulders.
HTML … well that’s what this post is all about. HTML on the web has gone through many phases. First there was the plain text phase, with newly introduced presentational tags such as <font> and <color>. Then came the Era of the Table. We’d see tables within tables within tables within tables being used to keep the layout of a page in place. Tables were the easiest thing to use for column layouts back then.
But then came CSS with its call to developers to shun tables and use divs and CSS rules for layout. Most of that stuff fell on deaf ears. So now we see a web where a large number of sites are made by developers who are either infected with divitis, or they haven’t looked beyond tables and inline styles. Either everything is a <div> in these sites, or a table element. Headings, lists, blocks of text, you name it, it’s a div/table.
What I want to list in this post are some rules to remember while writing HTML, such that the resulting HTML:
- Has semantic meaning. The next developer to work on your markup will get a good idea of what it represents if your HTML is semantically correct and has ‘meaning’ (rather than a huge table within table within table…)
- Is short and concise
- Is primarily the structure of your page (as opposed to having presentational and structural properties)
The first thing to keep in mind is to use the correct tag at the correct place.
<h1> – <h5>: These tags should be used for your page name and other section headings. You can set these tags to both inline and block displays to suit your needs. They are SEO friendly as well.
<div>: You should use the divider primarily for layout purposes. A basic rule of thumb should be that if a div itself has any content in it, then you should rethink your use of it, and maybe replace it with a list item (if you’re making tabs or a ‘collection’ of items), span (inline text formatting), paragraph (block error messages) or maybe you don’t even need an encompassing tag for your content (think unnecessary boxing of images into divs).
<ul>, <ol>, <li>: Very versatile tags, should be used for both vertical and horizontal (tabs) lists. Also consider using lists for structures of data such as galleries, menus and draggable items. They work really well when floated as well.
<span>: Tag of choice for formatting inline text. It is essential however that you know that you don’t have to use it every time. There are other tags, such as <i>, <pre>, <dfn>, <code>, <strong> etc that you can use in most cases. Remember that you should use these tags according to the context of your site, e.g. <strong> doesn’t necessarily have to be ‘bold’ text. You can give it any style that applies to emphasized text in your page’s layout. Note that these tags will give structure to your data, through CSS you will set their presentational properties, and actually define what, say a <strong> tag will look like.
<label>: I’ve seen many forms that don’t use this tag. I’ve seen JavaScript that is doing what this tag automatically does for you in your forms (click sets focus to related input field). This should not be so. Your forms should always make use of this tag to describe what the user should input in a field. Be sure to use the ‘for’ attribute to link a <label> to its <input> tag.
<p>: This tag should be considered when you’re writing welcome/instructional/advert text on your webpage. Note that usually this text is encompassed in a div or other structure. <p> works just fine in these cases.
<table>: Use for displaying tabular data only. Remember to use the <thead> and <tbody> children tags.
<a>: Use for links. Also work really well in block display, so consider using them where you might be using divs with onclick events and cursor set to pointer.
These are the most commonly used tags out there. Be warned that you will need to swallow the bitter pill of CSS to make full use of some of the above tags, especially divs and list items.
Hopefully this rant makes some sense to you and will help you in structuring your pages better. If you think I got something wrong, comment and be heard!










