Your Ad Here

Thursday, February 5, 2009

Die, You Gravy Sucking Pig Dog!


In the C programming language, you're regularly forced to deal with the painful, dangerous concepts of pointers and explicit memory allocation.



b1 = (double *)malloc(m*sizeof(double));


In modern garbage collected programming languages, life is much simpler; you simply new up whatever object or variable you need.



Double[] b1 = new Double[m];


Use your objects, and just walk away when you're done. The garbage collector will cruise by periodically, and when he sees stuff you're not using any more, he'll clean up behind you and deal with all that nasty pointer and memory allocation stuff on your behalf. It's totally automatic.


Pretty awesome, right? I'd wager the majority of programmers alive today have never once worried about malloc(). I call this progress, as does Jamie Zawinski:



Based on my experience using both kinds of languages, for years at a stretch, I claim that a good garbage collector always beats doing explicit malloc/free in both computational efficiency and programmer time.


However, I also claim that, because of the amount of programmer time that is saved by using GC rather than explicit malloc/free, as well as the dramatic reduction in hard-to-debug storage-management problems, even using a mediocre garbage collector will still result in your ending up with better software faster.


Most of the time, throwing memory and CPU at the problem is still cheaper than throwing programmer time at the problem, even when you multiply the CPUs/memory by the number of users. This isn't true all the time, but it's probably true more often than you think, because Worse is Better.



But even for programmers who have enjoyed automatic garbage collection their whole careers, there are still some.. oddities. See if you can spot one here:



sqlConnection.Close();
sqlConnection.Dispose();
sqlConnection = null;


That is one hellaciously closed database connection. Why don't you take it out back and shoot it, while you're at it?


Even with your friendly neighborhood garbage collector making regular rounds on commodity desktops/servers where many gigabytes of main memory are commonplace, there are still times when you need to release precious resources right now. Not at some unspecified point in the future, whenever the GC gets around to it. Like, say, a database connection. Sure, your database server may be powerful, but it doesn't support an infinitely large number of concurrent connections, either.


The confusing choice between setting an object to null and calling the Dispose method doesn't help matters any. Is it even clear what state the connection is in after Close is called? Could the connection be reused at that point?


Personally, I view explicit disposal as more of an optimization than anything else, but it can be a pretty important optimization on a heavily loaded webserver, or a performance intensive desktop application plowing through gigabytes of data.


Of course, your average obsessive-compulsive developer sees that he's dealing with a semi-precious system resource, and immediately takes matters into his own hands, because he can do a better job than the garbage collector. K. Scott Allen proposes a solution that might mollify both camps in Disposal Anxiety:



What the IDisposable interface needs is a method that promotes self-efficacy in a developer. A method name that can stir up primal urges as the developer types. What we need is a method like the one in BSD?s shutdown.c module.



die_you_gravy_sucking_pig_dog()
{
char *empty_environ[] = { NULL };

syslog(LOG_NOTICE, "%s by %s: %s",
doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
"shutdown", whom, mbuf);
(void)sleep(2);

(void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
if (killflg) {
(void)printf("\rbut you'll have to do it yourself\r\n");
exit(0);
}


Now, I know this function was written back in the days when steam engines still ruled the world, but we could modernize the function by applying some .NET naming standards.


sqlConnection.DieYouGravySuckingPigDog();


Can you feel the passion behind this statement? This statement carries the emotion that is hard to find in today's code. I hope you?ll support this proposal. Good people will be able to sleep at night once again.



So the next time you feel anxious about letting objects fall out of scope, remember: you could always terminate them with extreme prejudice, if you feel it's necessary.


But it probably isn't.






[advertisement] Tired of restoring deleted files? Get PA File Sight and track down the culprit. PA File Sight ? file auditing made easy. Download the Free Trial!



Read More...

[Source: Coding Horror - Posted by Kishore Vengala]

Your Ad Here

No comments: