tag:blogger.com,1999:blog-22715415490377763582009-06-02T09:26:32.443-04:00Ushankasharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-2271541549037776358.post-3401051235367723122009-02-07T21:40:00.009-05:002009-02-08T16:07:29.194-05:00Windows 7 InternalsThe folks over at <a href="http://channel9.msdn.com">Channel9</a> recently interviewed <a href="http://blogs.technet.com/markrussinovich/">Mark Russinovich</a>, a Technical Fellow at Microsoft. Mark shares some nitty-gritty details of the improvements they've made in the <a href="http://blogs.msdn.com/e7/">Windows 7</a> kernel - particularly with respect to performance. The interview is a bit slow at times but there are enough interesting tidbits to warrant a watch.<br /><br /><a href="mms://mschnlnine.wmod.llnwd.net/a1809/d1/ch9/9/1/1/5/3/4/RussinovichInsideWindows7_s_ch9.wmv"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 239px;" src="http://sharvil.nanavati.net/uploaded_images/russinovich-757957.png" border="0" alt="" /></a><br />Here are some of the improvements that I found particularly interesting:<br /><br /><ul><li>Scalability<ul><li>Finer grained locks on dispatcher queue</li><li>Finer grained locks on PFN database</li><li>Support for up to 256 cores</li></ul></li><li>Power Consumption/Battery Life<ul><li>Core parking: putting cores into deeper sleep states by migrating processes away to more active cores</li><li>Socket parking: putting an entire socket into a low power state by parking cores on the same socket (this is really cool!)</li><li>Timer coalescing API</li></ul></li><li>Virtualization<ul><li>Integrated support for creating/mounting <a href="http://en.wikipedia.org/wiki/VHD_%28file_format%29">VHDs</a></li><li>Boot directly from VHD!</li></ul></li></ul>The timer coalescing API needs an explanation. Suppose you have two timers on your system firing every 5ms, except the first timer was set at t=0ms and the second was set at t=1ms. Then your timer interrupts have to fire at 5ms, 6ms, 10ms, 11ms, ... to service those timers. Since both timers have a period of 5ms, it would be more efficient to reuse the interrupt at t=5ms by advancing the first servicing of the second timer. This way, the CPU has more time between interrupts to go into a deeper sleep state or to execute other code.<br /><br />In short, the timer coalescing API allows timers to share the same interrupt by adjusting the first timer event of a new timer.<br /><br /><script type="text/javascript">var dzone_url = 'http://sharvil.nanavati.net/2009/02/windows-7-internals.html';var dzone_title = 'Windows 7 Internals';var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-340105123536772312?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com0tag:blogger.com,1999:blog-2271541549037776358.post-27639580976620054952009-02-03T14:56:00.004-05:002009-02-03T15:44:06.878-05:00Exhibit A<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://sharvil.nanavati.net/uploaded_images/lunch-721161.PNG"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 240px; height: 320px;" src="http://sharvil.nanavati.net/uploaded_images/lunch-716719.PNG" border="0" alt="" /></a><br />WTF? Yes, this was supposed to be lunch at Google Waterloo yesterday. The black goo is supposedly melted (yes, <i>melted</i>) seaweed. Reminds me of <a href="http://www.telegraph.co.uk/travel/travelnews/4344890/Virgin-the-worlds-best-passenger-complaint-letter.html">another food story</a>.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-2763958097662005495?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com1tag:blogger.com,1999:blog-2271541549037776358.post-69538677286368974402009-01-27T21:13:00.013-05:002009-01-31T15:02:12.740-05:00Policy-Based Component Design<img style="float:left; margin:0 10px 10px 0;" src="http://sharvil.nanavati.net/uploaded_images/policy_of_truth-770068.jpg" border="0" alt="" />It's difficult to predict the exact needs of a client when building reusable software components. Sometimes even your own needs change drastically over the course of a project. One useful technique to deal with the variability is <a href="http://en.wikipedia.org/wiki/Policy-based_design">policy-based component design</a>.<br /><br />The idea is to build a component that implements a core behaviour and applies a client-specified policy to make the key decisions. For example, if I built a <a href="http://www.nist.gov/dads/HTML/hashtab.html">hash table</a> component, the client should be able to specify when and how to resize the table. Some clients may double the size of their hash table when the <a href="http://www.nist.gov/dads/HTML/loadfactor.html">load factor</a> reaches 0.8 whereas others may increase the table size to some fixed value based on expected data growth once they have a good estimate of it. It all depends on the nature of the environment in which the component is used. And only the client knows that.<br /><br />There are many ways to implement a policy-based design. In Java, I could use the <a href="http://en.wikipedia.org/wiki/Strategy_pattern">strategy pattern</a>. In C, I would use <a href="http://www.newty.de/fpt/index.html">function pointers</a> and a context (e.g. void pointer). <a href="http://erdani.org/">Andrei Alexandrescu</a> used C++ templates to achieve this goal in <a href="http://www.amazon.com/Modern-Design-Programming-Patterns-Depth/dp/0201704315">Modern C++ Design</a>. All of those approaches are perfectly valid and result in more reusable components.<br /><br />A final note: policy-based design applies to components at any granularity, not just at the class/function level. <a href="http://www.kernel.org/pub/linux/libs/pam/">PAM</a> is a fine example of a coarse-grained policy-based component which I mentioned in a <a href="http://sharvil.nanavati.net/2008/02/ocamlpam-10-released.html">previous post</a>. Or consider a <a href="http://en.wikipedia.org/wiki/Hyper-V">hypervisor</a> that manages physical resource allocation based on a user's needs (e.g. allocate 512MB of RAM to virtual machine 1).<br /><br /><small>Image from <a href="http://www.amazon.com/Policy-Truth-Depeche-Mode/dp/B000006Y5Y">Amazon</a></small><br /><br /><script type="text/javascript">var dzone_url = 'http://sharvil.nanavati.net/2009/01/policy-based-component-design.html';var dzone_title = 'Policy-Based Component Design';var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-6953867728636897440?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com0tag:blogger.com,1999:blog-2271541549037776358.post-32268323377193655042009-01-26T22:11:00.010-05:002009-01-26T23:11:45.746-05:00CUSEC 2009<img style="float:left; margin:0 10px 10px 0;" src="http://sharvil.nanavati.net/uploaded_images/montreal-766908.jpg" border="0" alt="" />I just attended the <a href="http://2009.cusec.net/">CUSEC 2009</a> conference in Montreal last week(end) and it was incredible. Between the speakers' stories, the crazy evening parties, and meeting so many awesome people, there was little to complain about.<br /><br />Speakers included <a href="http://leahculver.com/">Leah Culver</a> of <a href="http://www.pownce.com/">pownce.com</a>... um, fame?, <a href="http://research.sun.com/people/mybio.php?c=394">Dan Ingalls</a> (best known for his work on Smalltalk), and <a href="http://www.stallman.org/">Richard Stallman</a> who shouldn't need much of an introduction. Each speaker added a different flavour to the conference and despite their diverse backgrounds, they all showed a genuine passion for their work. It's impossible to be in such company and not get fired up.<br /><br />The only downside was the insane focus on web technologies, but I suppose I'll just have to find a systems conference in the area...<br /><br /><small>Picture by <a href="http://www.flickr.com/photos/caribb/">caribb</a></small><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-3226832337719365504?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com4tag:blogger.com,1999:blog-2271541549037776358.post-64074309313884562302008-04-14T09:06:00.002-04:002008-04-14T09:18:02.957-04:00Amazon EC2: Persistent StorageAmazon has just started a private beta program for a new persistent storage API in EC2. According to their documentation, they provide an API to create and manage volumes between 1GB and 1TB in size that behave like unformatted disks. Each volume is persistent and independent of EC2 instances and a single EC2 instance can mount multiple volumes. Their disks are supposed to be low-latency and high throughput with calls to store snapshots onto S3.<br /><br />A lack of persistent storage has been the biggest challenge for developers as EC2 (in my experience) has rather high failure rates. With this persistent storage API (scheduled for public release later this year), Amazon has just made EC2 a dead-easy buy-in.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-6407430931388456230?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com5tag:blogger.com,1999:blog-2271541549037776358.post-73399006799635020952008-03-01T16:31:00.006-05:002008-03-02T13:15:03.686-05:00Amazon EC2: The Potential<img style="float:left; margin:0 10px 10px 0;" src="http://sharvil.nanavati.net/uploaded_images/datacenter-722032.jpg" border="0" alt="" />Amazon's Elastic Compute Cloud (EC2) is easily their most powerful web service offering. With EC2, you get flexible, on-demand computing resources: by launching an <i>instance</i> you get full access to a brand-new machine and its resources. Each CPU-hour costs $0.10, which equates to less than $80 per month if you run an instance 24/7! What's more, you can launch as many instances as you'd like so you can have your own network of machines hosted by Amazon. The clincher? All data transferred between EC2 instances and S3 is free!<br /><br />The default configuration has the following specs:<br /><br /><ul><li>CPU: 32-bit, 1.0-1.2 GHz Opteron/Xeon equivalent</li><li>RAM: 1.7 GB</li><li>Disk: 160 GB</li><li>NIC: 100 MBit</li></ul><br />They have additional configurations if you need more resources on a single machine. For details, see the <a href="http://www.amazon.com/b/ref=sc_fe_l_2?ie=UTF8&node=201590011&no=3435361&me=A36L942TSJ2AJA">EC2 site</a>. To make all of this work, EC2 allocates a <a href="http://en.wikipedia.org/wiki/Virtual_machine">virtual machine</a> running on the <a href="http://en.wikipedia.org/wiki/Xen">Xen hypervisor</a> instead of a physical machine for every instance launched.<br /><br />Amazon designed EC2 primarily to perform many computationally expensive operations - something like batch video encoding or image recognition. Instead of making large hardware investments to perform these (potentially one-shot) tasks, you run the tasks in parallel on a few (hundred?) EC2 instances. Once the tasks are complete, just shut down the instances and your billing stops there. While Amazon's vision for EC2 is pretty sweet, the reality is that there's so much more potential there.<br /><br />EC2 is the next-generation data center.<br /><br />Instead of doing capacity planning as with a traditional data center, with EC2, I could monitor the load on my server and programmatically launch parallel instances once it reaches a threshold utilization. When the utilization drops again, I can terminate the extra instances and go back to a fairly quiescent state. With free traffic between EC2 and S3, I can churn through collected data as many times as I need to as in Amazon's vision. Amazon could even issue hardware updates (e.g. more RAM) to running instances without rebooting! With the inexpensive per-hour prices, any small business can afford to keep an active standby. The flexibility offered by programmatically managing machines running in a virtualized data center is tremendous. Coupled with Amazon's pricing model, this sort of service is poised to take some serious market share away from the traditional, physical data centers.<br /><br />While EC2 has the potential to be all of this and probably much more, it's not currently ready to displace traditional data centers. In a subsequent post, I'll discuss some of the issues preventing EC2 from realizing this dream.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-7339900679963502095?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com1tag:blogger.com,1999:blog-2271541549037776358.post-77809376332645990652008-02-22T17:52:00.004-05:002008-02-24T14:45:20.634-05:00OCamlPAM 1.0 Released!<img style="float:left; margin:0 10px 10px 0" src="http://sharvil.nanavati.net/uploaded_images/passport-783939.jpg" border="0" alt="" /><a href="http://www.kernel.org/pub/linux/libs/pam/">PAM</a> is a slick policy-based authentication mechanism. It abstracts away the method of authentication from applications and makes it possible to change the authentication method for a deployed application/service while running instead of making that decision at compile-time. I've come to love PAM because it makes <a href="http://en.wikipedia.org/wiki/Single_sign_on">single sign-on</a> a possibility and lets me focus on my application logic rather than the details of, say, <a href="http://tldp.org/HOWTO/LDAP-HOWTO/authentication.html">LDAP authentication</a>.<br /><br />Since I've been playing around with <a href="http://caml.inria.fr/ocaml/">Objective Caml</a> lately and I needed to do some authentication, I wrote an OCaml wrapper for PAM. <a href="http://sharvil.nanavati.net/projects/ocamlpam/">Take a look, give it a go, and authenticate away</a>!<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-7780937633264599065?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com0tag:blogger.com,1999:blog-2271541549037776358.post-85621827255726049622008-02-21T23:39:00.003-05:002008-02-22T01:44:52.875-05:00Power Profiling: Saving Energy in Software<img style="float:left; margin:0 10px 10px 0;" src="http://sharvil.nanavati.net/uploaded_images/electricity-772285.jpg" border="0" alt="" />I came across a pretty nifty profiling tool a little while ago called <a href="http://www.lesswatts.org/projects/powertop/">PowerTOP</a>. It gives you a live view of the power consumed by applications running on your Linux machine in an interface that resembles <i>top</i>. As a system-level power profiler, it gives you an idea of which applications or drivers are waking up the CPU or preventing it from entering a sleep state.<br /><br />I think this kind of tool is awesome for both developers and end-users. As a user, it tells me which applications are draining my laptop's battery so I might close those applications if I'm not using them or I might find an alternative altogether. From what I understand, some people have gained significantly higher use-times for their laptops by just killing innocuous-looking apps that don't let the processor enter a deeper sleep state. As a developer, this is good news because it will give me a quantitative measure of my application's power consumption, giving me a means to determine the power-efficiency of different designs. This tool has also managed to <a href="http://www.lesswatts.org/projects/powertop/known.php">expose bugs in a few applications</a>!<br /><br />Check out PowerTOP, fix your applications, and keep this planet green (and my laptop running longer and cooler)!<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-8562182725572604962?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com0tag:blogger.com,1999:blog-2271541549037776358.post-34452867098780719952008-02-10T19:12:00.000-05:002008-02-11T17:47:26.938-05:00A Closer Look: Amazon S3<a href="http://sharvil.nanavati.net/uploaded_images/harddrive-774250.jpg"><img style="FLOAT: left; MARGIN: 0px 10px 10px 0px; CURSOR: hand" alt="" src="http://sharvil.nanavati.net/uploaded_images/harddrive-774243.jpg" border="0" /></a> One of the most mature Amazon web services, Amazon Simple Storage Service (S3) provides a virtually <i>unlimited</i> data storage service. That's right: you can upload as much data as you'd like and it will be held on their machines with all the network capacity you could ever want and with redundancy built-in. Hard drive failures are easily the primary cause of server downtime and Amazon has taken the burden upon themselves to manage all the devices and failures that go along with it. As the name implies, the service is designed to provide simple access so you can't do funky things like mount the virtual filesystem directly.<br /><br />I've been using S3 for over a year and I haven't had any reliability issues with it. Others have had brief outages but they were mostly when the service was first introduced. I'm quite happy with S3 but there's one missing feature that keeps it from being the ultimate simple storage service: range-PUT.<br /><br />Suppose I've got a file on S3 and I want to update a small part of it. Without range-PUT, I would normally have to transfer the entire file again using the HTTP PUT method to store it on the remote host. Using the <i>Content-Range</i> header, I could specify just the range of bytes that have changed within the file and transfer just that portion. This feature would save a lot of bandwidth (and, consequently, money) if files often get modified partially.<br /><br />Of course, supporting <i>Content-Range</i> opens up a can of worms. What happens if the file doesn't exist and the start of my range isn't offset 0? What if the file <b>does</b> exist but the start offset is beyond the end of file (i.e. not a simple append)? I can think of two solutions that seem reasonable: return an error or create the file if it doesn't exist and zero-pad the holes. The former would be easier to implement while the latter would produce a behaviour like Linux sparse files.<br /><br />There are two major application classes that range-PUT would be suited to. The first would be the class of applications where we always append to the end-of-file. Log files would fit into this category but, more importantly, we could resume broken transfers. When uploading large files (S3 supports file sizes of up to 5GB), I've found that my connections often get dropped so if I could just append to an existing file, I could write an upload tool that would auto-resume. The second class of applications would be the ones that only update part of a file. In most cases, I'd imagine this kind of update would take place to change some file metadata. For example, if I modify the metadata for my MP3 file, I'd rather just upload the few changed bytes instead of uploading the whole MP3 again. The music is the same, it's just the metadata that has changed. This problem is even worse when dealing with video files.<br /><br />S3 is a fantastic storage service. It's reliable, it's cheap, and it takes away the hassle of managing your own hardware or creating a highly-available, redundant persistent store. If S3 supported range-PUT, it would save a huge amount of bandwidth resulting in an even lower cost of operation.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-3445286709878071995?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com6tag:blogger.com,1999:blog-2271541549037776358.post-61643856348583713732008-02-10T16:14:00.001-05:002008-02-10T18:38:08.235-05:00A Closer Look: Amazon Web Services<a href="http://www.amazon.com/">Amazon</a> has been doing some pretty nifty stuff lately. They've exposed their computing infrastructure to the rest of the world via <a href="http://en.wikipedia.org/wiki/REST">RESTful</a> web services. I think it's a brilliant move by <a href="http://en.wikipedia.org/wiki/Jeff_Bezos">Jeff Bezos</a> and it realizes some of the technology promises of the last decade or so.<br /><br /><img style="FLOAT: left; MARGIN: 5px 10px 10px 0px;" alt="" src="http://sharvil.nanavati.net/uploaded_images/amazon_web_services-763111.gif" border="0" /><a href="http://www.amazonaws.com/">Amazon's web services</a> have been getting a lot of great reviews from many bloggers and deservedly so. With their pay-as-you-use model, it's amazingly easy to scale up or down based entirely on workload; there's virtually no need for capacity planning and no need to manage physical systems at all! I've been using their web services for over a year now for a variety of tasks and it has been, for the most part, quite pleasant. They've saved me a lot of time and money but their services do have their faults. I'll be talking about some of the issues I've encountered with the design or implementation of their services over the next few posts.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-6164385634858371373?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com0tag:blogger.com,1999:blog-2271541549037776358.post-16193829841194795352008-02-07T18:24:00.000-05:002008-02-10T15:59:38.191-05:00Company Loyalty<a href="http://en.wikipedia.org/wiki/Jerry_Yang">Jerry Yang</a>, CEO of Yahoo!, recently sent out a company-wide memo in response to Microsoft's $44bn hostile bid. <a href="http://daringfireball.net/2008/02/yahoo_translation">Here's a rather hilarious "human readable" version</a> of the memo courtesy of John Gruber.<br /><br />While I don't 100% agree with the translation, I find it amusing that we actually <b>need</b> to translate. You'd think that such an important message would be a bit more... accessible.<br /><br />The memo makes me wonder, though: how often does senior management actually point out the importance of their employees outside of annual company meetings and circumstances such as this? Yang is really asking for loyalty through a difficult time for his organization. Did he build that loyalty or is he just asking for it? I don't know, but I'd be curious to find out.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-1619382984119479535?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com0tag:blogger.com,1999:blog-2271541549037776358.post-38001964620363635462008-02-07T16:26:00.000-05:002008-02-07T16:52:39.978-05:00Protector<div>A while ago, I had come across this Flash game called <a href="http://www.jeannettevejarano.com/games/tower-defence.html">Tower Defense</a>. The idea of the game is to put up guard towers along a path to the castle to prevent enemy attackers from killing your citizens. It was fun but got frustrating as the levels wore on because the income didn't keep up with the increasing strength of the attackers.<br /><br />Then, a couple of days ago, I came across <a href="http://www.kongregate.com/games/undefined/protector">Protector</a>, a game based on the same principles but with an RPG-style twist. It's actually pretty awesome! I have to admit, for a Flash game, it's quite polished with a tutorial mode and everything. Here's a screenshot of the game in action:<br /><center><br /><a href="http://sharvil.nanavati.net/uploaded_images/protector-737901.png"><img style="CURSOR: hand" alt="" src="http://sharvil.nanavati.net/uploaded_images/protector-737876.png" border="0" /></a></center><br />I find that console games have gotten too complex to appreciate. They try to create an immersive experience but most of the time, I just want a quick fix so I can relax. Sure, there are first-person-shooters, but they're all about the same and they get pretty boring pretty fast. I have a feeling that the success of the Wii and its throw-back to the classic, simple games of the 90's show that there's still demand for quick gaming. </div><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-3800196462036363546?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com0tag:blogger.com,1999:blog-2271541549037776358.post-40207857338257600892008-02-06T00:27:00.000-05:002008-02-06T01:53:23.593-05:00Google Web Toolkit: a quick lookWhile writing the last blog post, I had a miserable time trying to get the Java snippets formatted correctly. I suppose the problem is that I'm using Blogger instead of a full-blown publishing platform like <a href="http://www.movabletype.org/">Movable Type</a> which has plugins for code highlighting. Once I got the post up, I decided to build a web service that would take a code snippet in your language of choice and return highlighted HTML code.<br /><br />Along the way, I found a neat package called <a href="http://qbnz.com/highlighter/">GeSHi</a>, a generic code highlighter that would make my life a lot easier. I then got the idea of making an AJAXed front-end to that along with other useful web services... Eventually, I came across the <a href="http://code.google.com/webtoolkit/">Google Web Toolkit</a> and it looks like the Google folks are really on to something!<br /><br />Now, I'm sorry to say that I've done my share of AJAX code before, both client side and server side, and it has been one of the most painful coding experiences of my life. The runtime errors, the lack of decent developer tools, the browser incompatibilites, the ridiculous workarounds... web development has been just one enormous nightmare. The guys and gals at Google managed to take a large part of that away with GWT; they're actually compiling <b>Java</b> code to JavaScript with all of the compile-time checks (hooray, no more typo-bugs rearing their heads at runtime!), development tools (including a <b>debugger</b>!), and patterns kept intact! The really nice bit is that they abstract all of the cross-browser differences via their core library. Now <b>this</b> is a step in the right direction for web development!<br /><br />Of course, it's not a perfect system. They're building this on top of Java 1.4 so we still don't have safe container classes or enumerated values (sigh). What's more, since AJAX is event driven by nature, I think it would be so much easier to use a functional source language so we can pass functions and closures around instead of anonymous inner classes.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-4020785733825760089?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com0tag:blogger.com,1999:blog-2271541549037776358.post-16339285964234779442008-02-03T18:38:00.000-05:002008-02-04T02:34:54.840-05:00When goodbyes aren't enough...The Java core classes have a pretty nasty habit of throwing <em>IOExceptions</em> in <em>close</em> methods. Since <em>IOException</em>s are not derived from <em>RuntimeException</em>, you either have to catch them or declare them as thrown from the method calling <em>close</em>. Now, suppose I have a class that holds a couple of resources, <em>r1</em> and <em>r2</em>. Let's see what happens if I try to release them together:<br /><br /><pre class="java"><span style="color: #000000; font-weight: bold;">try</span><br /><span style="color: #66cc66;">{</span><br /> r1.<span style="color: #006600;">close</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br /> r2.<span style="color: #006600;">close</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #66cc66;">}</span><br /><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #66cc66;">(</span><a href="http://www.google.com/search?hl=en&q=allinurl%3AIOException+java.sun.com&btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> e<span style="color: #66cc66;">)</span><br /><span style="color: #66cc66;">{</span><br /> <span style="color: #808080; font-style: italic;">// Do whatever I need to here...</span><br /><span style="color: #66cc66;">}</span></pre><br />If <em>r1</em>'s <em>close</em> happens to throw, <em>r2</em>'s <em>close</em> will never be called and I might (in a long-running application) leak resources. The same problem occurs if I don't bother catching the exception and declare it thrown from my method. Putting the <em>close</em> calls in the <em>finally</em> clause won't fix the problem either. A working solution could be:<br /><br /><pre class="java"><span style="color: #000000; font-weight: bold;">try</span><br /><span style="color: #66cc66;">{</span><br /> r1.<span style="color: #006600;">close</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #66cc66;">}</span><br /><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #66cc66;">(</span><a href="http://www.google.com/search?hl=en&q=allinurl%3AIOException+java.sun.com&btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> e<span style="color: #66cc66;">)</span><br /><span style="color: #66cc66;">{</span><br /> <span style="color: #808080; font-style: italic;">// Do whatever I need to here...</span><br /><span style="color: #66cc66;">}</span><br /><span style="color: #000000; font-weight: bold;">try</span><br /><span style="color: #66cc66;">{</span><br /> r2.<span style="color: #006600;">close</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #66cc66;">}</span><br /><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #66cc66;">(</span><a href="http://www.google.com/search?hl=en&q=allinurl%3AIOException+java.sun.com&btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> e<span style="color: #66cc66;">)</span><br /><span style="color: #66cc66;">{</span><br /> <span style="color: #808080; font-style: italic;">// Do whatever I need to here...</span><br /><span style="color: #66cc66;">}</span></pre><br />This code looks worse than checking for return codes! What's more, I, as a programmer, am <strong>getting rid of a resource</strong>. I have decided that the given resource is no longer of any use to me. I am throwing it away. And it's coming back!<br /><br />I can't think of a single reason an exception (checked or unchecked) should be thrown from a method that is releasing a resource. In situations like this, a return code should be used instead of exceptions because the programmer has, by the very act of calling your method, declared that she is no longer interested in the resource.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2271541549037776358-1633928596423477944?l=sharvil.nanavati.net'/></div>sharvilhttp://www.blogger.com/profile/10837472563653338726noreply@blogger.com4