Wednesday, September 24, 2008

Really Cool Learning Site

I found this site for Linux related learning. I found three pdfs that I really would love to read:
  • Network Administration
  • System Administration
  • Shell Scripting
For my own reference, and for any one else who may be looking for something like this: Linux Learning

Labels: , ,

Saturday, September 20, 2008

Thought: Food and Weight

Today, I had some home made desert made of fruits and condensed and evaporated milk. I believe that the amount of fat in these milks is really high. But the amount of the milk we added to it was little (2 teaspoons I think).

I started thinking - if I eat something thats 20 grams, how can it result in me gaining more that 20 grams of weight. Then this is how I figured it probably is like: a 100kb file in 1 file system when moved to a different file system may take up 110kb in the 2nd file system - because of the file system's structure and layout I am guessing. Similarly, the 20 grams of food is processed and converted to our body's file system, which may result in us putting more weight.

Thinking about it some more, I figured the above would be a violation of conservation of mass; this is what probably happens when we eat high fat things like the condensed milk, and later drink water or other fluids, the body starts retaining it to match the fat level. Thus we put on weight. The key to our weight would then be the amount of water in our body (which - the water retention - would depend on the other things we eat).

Labels: , , ,

Random Thought: Firefox Security

When I opened Firefox today, I saw a link in the home page to "Why is Firefox the safest web browser?" page. I just loved this line that I read :)

An international community of security experts is working around the clock to make your web browsing safer...It's like having your neighborhood watch led by a group of highly trained ninjas...
Hehe...that brought up a smile on my face :)

Labels: ,

Tuesday, September 16, 2008

Presenting the YWeather API

I was designing a site this weekend when I needed some weather data. I started looking for solutions - I found phpweather and I found it to be an excellent tool. However, when I tried to query some "less" known cities, it returned nothing. One of the cities that I wanted the data for did not exist and I was disappointed.

Knowing that I can use sites like the weather network and get the weather data, I figured I might as well write my own class to fill this void. Thus was born the YWeather. YWeather is a php class that provides a class interface to access data from Yahoo Weather. It makes use of the Yahoo weather RSS feed.

The YWeather class uses MagpieRSS to parse through the RSS feed. When a location id is specified, the class parses the feed and gives a class interface to the weather data, making it easy to integrate to any web application.

Here is a test script I created to test the class, this list provides a list of the features of the class (I am too lazy to type this out :P)

$w = new YWeather("CAXX0504");

"URL: ".$w->getURL()
"Title: ".$w->getTitle()
"Skies: ".$w->getSkies()
"Skies Image: getImage($w->getSkies())."' />"
"Temperature: ".$w->getTemperature()
"Latitude: ".$w->getLat()
"Longitude: ".$w->getLong()
"isPM: ".$w->isPM()
"Published date: ".$w->getPublishDate()
"Description: ".$w->getDescription()
"Current Conditions: ".$w->getCurrentConditions()
"Forecasts: ".$w->getForecastsText()

The Yahoo feed provides 2 days forecast:

$forecasts = $w->getForecasts();
"Forecast 0 Day: ".$forecasts[0]['day']
"Forecast 0 Skies: ".$forecasts[0]['skies']
"Forecast 0 Skies Image: getImage($forecasts[0]['skies'])."' />"
"Forecast 0 High: ".$forecasts[0]['high']
"Forecast 0 Low: ".$forecasts[0]['low']
"Forecast 1 Day: ".$forecasts[1]['day']
"Forecast 1 Skies: ".$forecasts[1]['skies']
"Forecast 1 Skies Image: getImage($forecasts[1]['skies'])."' />"
"Forecast 1 High: ".$forecasts[1]['high']
"Forecast 1 Low: ".$forecasts[1]['low']


The class may be downloaded from here.
downloads/YWeather_w_magpie.zip

Labels: , ,

Sunday, September 7, 2008

Strategy Pattern

What?

The strategy pattern is a design pattern designed to accommodate situations when there are multiple ways of doing some/similar thing(s). For instance this might be something along the lines of, if you are designing a media player - you would have different algorithms for playing the different media types. You could use the strategy pattern, as it deals with a common problem of playing a media file, except there are multiple ways it could be done (which algorithm depends on the media type).

How?

Each strategy is made into its own class, in our example above they may be classes like:

Class PlayWAV;
Class PlayMP3;

Each of the above class would implement the IStrategy interface (defined below):

Interface IStrategy {
// This is the function that is called to execute this strategy

public function execute();
}

A pseudo-code (may look similar to PHP code) implementation would look like this:

class PlayWAV implements IStrategy {
// Constructor: Information needed to execute the strategy may be passed through the constructor
public function PlayWAV(File fileObject);
// This needs to be implemented to satisfy the interface; this is the function that would be called to execute the strategy
public function execute();
}

class Context {
public function main() {
...
if (filetype(...) == "WAV") {
$strategyA = new PlayWAV(...);
$strategyA->execute();
} else {
$strategyB = new PlayMP3(...);
$strategyB->execute();
}
...
}
}

Why?

Why the heck would you do this? and not just code everything using an if/else? Well, this provides a clean separation - each strategy is its own entity - this reduces any tight coupling that may be formed knowingly or unknowingly. Since this is build with loose-coupling, you can change each without worrying about affecting the other, and new strategies may be developed independent of the application code and be integrated into the application with ease.

Example

This whole post was explained using an example, another great one can be found here. This one uses the strategy pattern for form validation in PHP.

And now with that I will end this post. Ciao.

Labels:

Observer Pattern

Observer Pattern: When objects have a one to many relationship, i.e. many objects need to be updated when one object changes, and the number of these objects is not known ahead of time, this pattern can be used to implement it.

We begin by implementing 2 interfaces:
  • IObservable: This is the object that gets observed - whose change would affect other objects
  • IObserver: This represents objects that are the observers
Here is a pseudo-code implementation of the interfaces:

Interface IObservable {
// The attachObserver function adds the passed object to an array
public function attachObserver(obj);
// The detachObserver function removes the passed object from the array
public function detachObserver(obj);
// Notify all the objects in the array - loop through the objects and call the update method
// Note: All the elements in the array implement the IObserver interface and hence
// support the "update" method
public function notify();
// Function would be called by the Observers to get state information about the object
public function getState();
}

Interface IObserver {
// The method that performs the action that needs to be done on change of the observable
public function update();
}

The actual observer and observable classes would implement these interfaces.

Two examples of where you might be using this:
  • In a web application, you may register screens with your business model objects (representing underlying data) - hence when the model gets updated, the screens get updated automatically.
  • Another example may be, say you are making a game - and you when 1 object performs a task - such as a commander issuing a fire order, all units within the commanders range should fire (they would have registered with the commander object - and as they observed a change, they perform an action).
We have covered what the observer pattern is, how it is coded, and two examples of where it may be used. The answer for the why is the same as why use design patterns (see the post on Design Patterns in general). With that I will end this post. I hope someone, someday, somewhere (may be myself) will find this useful. :)

Labels:

Design Patterns

Design patterns: are general solutions to commonly occurring software design problems.

Why patterns? Some problems occur often is software design, and these general solutions avoid having to reinvent the wheel over and over again. The patterns have been looked at by so many people and has been refined well that when they are understood and implemented correctly they can save time, will work efficiently and enable more effective/productive discussions among developers.

I have been exploring patterns recently and I thought I would post about them here

Labels:

Saturday, September 6, 2008

Google Chrome

Google's chrome is out and its in beta. My very first impression of chrome, was mediocre. I didn't know why another browser? and what is special about this one? It's new different look did not dazzle me either. I didn't spend much time on it though...there was work to do (I was at work and stumbled upon it when I was going to do a search).

Few hours later...at home I thought I'd look into Chrome and see if I can find some answers, thats when I stumbled upon the Chrome storyboard. It was a little long, but by the end of it I loved the idea behind Chrome. It was spectacular. It was neat. It adds new fire to a somewhat long stale mate in the browser world.

By the way, I highly recommend anyone interested in knowing more about Chrome to visit: http://www.google.com/googlebooks/chrome/. I did learn in the "Intro to Design" course that using a story board to present concept ideas can be very useful. Chrome's storyboard did a really good job of it. And I am impressed by what the medium can do.

For people not interested in going through the storyboard or just don't have the time, here is a summary of whats special about chrome:
  • Chrome supports multiple tabs like other browsers, but the difference is - chrome gives each tab its own process, instead of all the tabs residing in 1 process. Sure, this increases the upfront memory cost of chrome, but this turns out to be very advantageous in terms of long-term memory management (long term = a few minutes - hours).

    Have you ever noticed, you keep working and opening tabs and closing tabs, sometime later, you feel your computer slowing down - you check the memory usage and it is insanely high! you start closing tabs and windows, but it barely drops. :(

    The reason this happens is the browsers allocate a fixed amount of memory and starts fittings tabs into it, as you need more memory another block of memory gets allocated. When you close a tab, that space becomes free and can be used by another tab that is opened later. But that would be the ideal case - in reality, memory corrupts and for other reasons, when a tab is closed, the memory sometimes does not get cleaned completely. And this space becomes unusable, so when new tabs are created, more memory gets allocated, and you end up with a bloated memory usage problem.

    With separate processes, you close a tab, all the resources allocated to the process get cleaned - you get them back. So in Chrome, when you close a tab, you do get back the memory.
  • Each tab is sandboxed - meaning a malicious site can access at most that tab's environment. So everything else (other tabs and processes) is safe.
  • Since each tab has its own process, when there is a crash - only the tab crashes not your whole browser!
  • Chrome uses the V8 javascript engine. Javascript is not interpretted, but gets compiled and executed - leading to faster perfromance and better garbage collection (hence better memory management).
  • After using chrome for a couple of hours, I am beginning to get used to the interface and I feel like i actually like it!
  • It feels very responsive. Firefox is not bad in this aspect and IE7 feels painfully laggy (though it did feel a little better in another computer I worked at recently)
  • Last but not least, the browser has a lot of neat easter eggs like the about:stats, about:histograms pages etc (you can find a complete list of the about: pages - http://googlesystem.blogspot.com/2008/09/google-chromes-about-pages.html)
Chrome's official stand itself is by no means competitive. It doesn't seem like Google wants to engage in a browser war, but is just interested in making browsers better and more suited for the new "media-javascript-rich" web. Supporting the stance, the project is open source as well.

I hope this gets firefox and IE rolling in the right direction in making the browsers better, and helps us the users and developers with a much pleasanter experience in the web.

Labels: