Program Efficiently
Use declarative programming to streamline your work.
October 30, 2009
aspnetNOW
ProgramEfficiently
Usedeclarative programming to streamline your work.
By JeffreyRichter
Programmingis hard work and often can be quite tedious. I can't count the number of timesI've written a linked list (or some other algorithm) over and over again andadded just a few tweaks to the code here and there. Obviously I'm not alonebecause many new technologies have appeared over the years to help developers.I'm sure you're familiar with object-oriented programming as a technology thatallows one developer to design and implement an algorithm in a generic fashion;then, other developers can tweak the generic algorithm for a specific situation(by deriving from a base class and overriding virtual methods). No developerquestions the incredible productivity boost object-oriented programmingprovides - it has proven to be a huge benefit, especially when you design andimplement big applications.
ForWindows programming, C# has made object-oriented programming mainstream becauseit makes building Windows applications and components more efficient. C#brings, however, another technology that, for some reason, doesn't enjoy asmuch of the limelight as object-oriented programming. This other technology isknown as declarative programming, which is when you instruct your applicationor component to do something using data rather than by writing source code.(The act of writing source code is sometimes called imperative programming.)
GoMainstream
Anexample of declarative programming is the production of an HTML Web page. Inthis process, the "programmer" defines how the Internet browser applicationshould process the HTML tags and text to lay out the page in a window. Manyhard-core programmers don't consider HTML programming to be "real programming."But I do, and I think this kind of declarative programming is going to becomemuch more mainstream - thanks to C#.
C#offers many forms of declarative programming. First, there's metadata - dataproduced by the C# compiler as it processes your source code and is embedded inthe resulting .exe or .dll file. This declarative data is used by severalfacilities that are part of the .NET Framework and its Framework Class Library(FCL). For example, the serialization formatters - BinaryFormatter andSoapFormatter - classes can discover automatically the fields within an objectand fields that refer to other objects by examining the object type's metadata.From this declarative information, the formatters easily can walk an entireobject graph spitting out a stream of binary bytes or a stream of SOAP.
Imaginehow you would have to implement serialization if metadata didn't exist. Withoutmetadata, Microsoft wouldn't have been able to create the BinaryFormatter orSoapFormatter classes because there would be no way for these classes to obtainenough information about an object graph to do anything useful with it.Instead, if you needed serialization in your own application, you would have toimplement the serialization code manually and the implementation probably wouldbe specific to the types of objects your application needs to serialize.Maintaining the code for this application as your application's types changewould be quite an undertaking.
Inaddition, without metadata, the code to serialize the object graph would besprinkled through your application's types; there's no "serializationalgorithm," per se. The code to serialize one type is in one source-code file,the code to serialize another type is in another source-code file, and so on.This means similar code frequently is copied from one place to another and,therefore, the "algorithm" has multiple maintenance points.