Show Mobile Navigation




Thursday, October 21, 2010

Intentional Programming: Whats your intent?

KMan - 1:12 AM
While browsing the wiki some time back, I came across Intentional Programming (IP); as any enthusiantic technologist I wanted to look further into it as to what and how can we use it.

What is it?
Intentional programming is a new way of programming. Easier, quicker, requires less coding; something new in rapidly growing software technology, that allows programming languages to be implemented in a highly extensible manner. In particular, the programmer can specify new abstractions that are specific to his problem domain, while simultaneously recording any domain specific optimizations that may apply to such new abstractions.

Though it is still in research, development, and initial alpha versions of its usage, even at the industry wide level, the core problem that it addresses are the aspects of making developers more productive and software more trustworthy.

It includes programming-language design, compilers, specification and verification, development environments and tools, runtime environments, formal models of systems, performance monitoring and optimization, and quality improvement

Computer programs have been traditionally represented as a stream of characters, usually referred to as code files. The Intentional Programs are represented not only as text but as a database; note that its not the database of the program or database about the program, but rather the database that “is” the program itself.

Lost already? (0: Hold on, following is fairly simple lingo.

Cruel Intentions?
The key to the new representation is the concept of identity - Identity; every object with unique identifier.

The idea of intents is that a representation of the 'intent' of the programmer should be the best way to store the code, and these 'intents' should also be 'viewable' in the language of your choice. In this system, source code as we know it today is just how you view and edit the intents.

Why IP?
Yep, I have thought about this question as well; the key difference with other approaches lies in the way the order of transformation of code that is controlled; in IP, the emphasis is placed on specifying that order in a compositional fashion (like composing an MS Word Document in tree structure - have ever switched to Outline View option?), so that transformations are easily re-used.

"Intents", ultimately, are high-level programming language with tools to automate the it’s writing and translation to lower-level languages, with an attendant loss of information and adding of noise in the process. Noise, that is, may be the additional layers that mediate between frontend and backend.

The independence and self-sufficiency of intentions might well create first a market in abstractions or "language features", followed by the long sought-after dream of a software componentization market.

Some shops (IntentSoft, company owned by Charles Simonyi, a former Microsoftee, who introduced the concept) introduced the IP as a visual tree of boxes editor that stores the abstract syntax tree in a sort of source code database. This concept is critical to power of language-oriented programming, but I believe changing the storage of source code is simply too radical of a change.

Well, actually, usually, source code is routinely stored in databases for IDE features, but the authoritative storage always remains text files, that is, the code files (.cs, .java, etc). All of programmers most trusted tools operate at the string level – at the core – and it is simply impractical to throw everything out at once.

This may be one of the problems of intentional programming, that we're basically creating a new programming language for every domain specific application.

What I'd really like to see is having a core set of abstractions (such as classes, methods, properties, and events) and then applying them in each domain—but in a way that preserves familiarity to existing coding practices.

Some day we may see a new editor which uses an abstract syntax tree box editing model, but preserves the source as text? who knows!

I do realize that visual editors are a bit less efficient than text editors for programming purposes. But the power that comes along with the IDE, Visual Studio, or myEclipse for instance, is far beyond compared to text editors; even those hi-fi text editors that comes with de/attachable compilers options.

Though, to some, boxes may convey less meaning than symbols in a text editor. The boxes and connections are also symbols, but they take up far more screen space than text, so you see much less code simultaneously, which makes the box editor less efficient.

But there are clear benefits to a box editor even in the absence of language-oriented features. For example, navigation of source code by parent, child, or sibling relationships instead of by word or by line; thinking out loud!

Sounds like a tangent?! Well, my thoughts.

References:Following are worthy of reading to get a good grasp of how it works.

Microsoft Research Intentional Programming Video, pretty old but informative. 
Microsoft Research Intentional Programming Article


Saturday, October 16, 2010

How to: create Custom ByteArray class

KMan - 10:26 AM
A friend sent this to me, as an Urgent question,

Write a class called ByteArray that implement allocating, reading and writing to an array of bytes. The runtime environment has a limitation. The maximum continuous memory size that it can allocated is 64k bytes. It can allocate many 64K ( or less) chunks.
The ByteArray class should hide this limitation and support allocating arrays larger than 64K as in the following example :

ByteArray ba = new ByteArray (250000); // Create
ba[150000] = 15; // Write byte
byte x = ba[190000]; // Read byte

This question is really asking for if you know how to implement the index. I responded with the following one minute recipe.

class ByteArray
    byte[] _Byte;
    const int _MaxSizeinKB = 64;
    const int _MaxSizeInBytes = _MaxSizeinKB * 1024;
    public ByteArray(int nLength)
        if (nLength > _MaxSizeInBytes)
            throw new InsufficientMemoryException("Cannot allocate more than " + _MaxSizeinKB.ToString() + "KB");
            _Byte = new byte[nLength];

    public byte this[int nIndex]
        get { return _Byte[nIndex]; }
        set { _Byte[nIndex] = value; }

Usage is as above in the question. Enjoy!

Thanks for pointing out.

I have updated the code the reflect the same.


This is an update based upon the comment that I received. I missed the last sentence; that is,
The ByteArray class should hide this limitation and support allocating arrays larger than 64k
This update adds rest of the functionality; following is the updated ByteArray class:
class ByteArrayEx
    private List lstArray = new List();
    public int _nSize;
    public int _nChunks;

    public const int _MaxSizeLimit = 65536;//3;//65536;//64K
    public ByteArrayEx(int nSize)
        _nSize = nSize;
        if (nSize > _MaxSizeLimit)
            //1. Calculate the number of 64K chunks
            int nChunks = nSize / _MaxSizeLimit;

            //130K; 64K + 64K + 2K
            //64 = 130/
            //2 = 64k x 2 = 128k
            //Remaining = 128 - 130 = 2
            //Remaining = nLengh - AllocatedSize

            int nAllocatedSize = 0;
            //2. Loop through the chunks
            for (int i = 0; i < nChunks; i++)
                //3. Allocate the size
                nAllocatedSize = _MaxSizeLimit + nAllocatedSize;
                lstArray.Add(new byte[_MaxSizeLimit]);


            //4. Allocate remaining bytes
            int nRemainingBytes = nSize - nAllocatedSize;
            if (nRemainingBytes > 0)
                lstArray.Add(new byte[nRemainingBytes]);

            _nChunks = lstArray.Count;
            lstArray.Add(new byte[nSize]);


    public byte this[int nIndex]
            //1. Get the index of array list item
            //2. Convert the incoming index to Index of byte array
            int nByteIndex = nIndex % _MaxSizeLimit;

            return ((byte[])(lstArray[GetBlockIndex(nIndex)]))[nByteIndex];
            //1. Get the index/block of array list
            //2. Convert the incoming index to Index of byte array

            int nBlockIndex = GetBlockIndex(nIndex);
            int nByteIndex = nIndex % _MaxSizeLimit;

            ((byte[])(lstArray[nBlockIndex]))[nByteIndex] = value;

    /// Block, represents the index of List item
    /// Incoming Index    /// Actual Block Index
    private int GetBlockIndex(int nIndex)
        int nAllocatedSize = 0;
        //2. Loop through the chunks
        int nBlockIndex = 0;
        for (int i = 0; i < _nChunks; i++)
            //3. Allocate the size
            nAllocatedSize = _MaxSizeLimit + nAllocatedSize;
            if (nIndex <= nAllocatedSize)
                nBlockIndex = i;
        return nBlockIndex;

Usage is as follows:
ByteArray ba = new ByteArray (250000); // Create
ba[150000] = 15; // Write byte
byte x = ba[190000]; // Read byte

Couple of things to note; the 64K limit that is in the ByteArrayEx class can be anything; for instance:

In case of 64K of it would create single array to contain all the byte array elements.
In case of 128K (64K x 2 = 128K), it will create 2 items in the array list of size 64K each.
In case of 130K (64K x 2 + 2K more = 128K + 2K = 130K) it will create 3 items in the array list; this time, two items of size 64K and third item of size 2K. So array list would contain 3 byte arrays.

Hope you get the idea.

You can change the 64K limit to 32K or 8K or 3K, etc; and it will work as above.

Checkout the output, for the following given input:
class Program
    static void Main(string[] args)
        //Test 1
        ByteArrayEx baTest1 = new ByteArrayEx(133120);// (64 + 64 + 2) * 1024; //130K or 133120 bytes

        baTest1[0] = (byte)'c';//Write
        Console.WriteLine(string.Format("Written byte:[{0}]", Convert.ToChar(baTest1[0])));

        baTest1[133110] = (byte)'d';//Write more
        Console.WriteLine(string.Format("Written byte:[{0}]", Convert.ToChar(baTest1[133110])));

        byte r = baTest1[133110];// be[133110];

        Console.WriteLine(string.Format("Reading byte:[{0}]", Convert.ToChar(r)));
        Console.WriteLine(string.Format("Max Limit: {0}, Size: {1}, Chunks: {2}", ByteArrayEx._MaxSizeLimit,
            baTest1._nSize, baTest1._nChunks));

        //Test 2
        ByteArrayEx byteArray = new ByteArrayEx(250000); // Create
        byteArray[150000] = 15; // Write byte
        byte x = byteArray[190000]; // Read byte
        Console.WriteLine(string.Format("byte:[{0}]", x));

        Console.WriteLine(string.Format("Max Limit: {0}, Size: {1}, Chunks: {2}", ByteArrayEx._MaxSizeLimit,
        byteArray._nSize, byteArray._nChunks));


Note that, I have the List class to demostrate the functionality. You can add any class that provides array like functionality.

How to: GitHub Getting Started

KMan - 6:43 AM
GitHub is a web-based hosting service for projects that use the Git revision control system. It is written using Ruby on Rails by GitHub, Inc. (previously known as Logical Awesome) developers Chris Wanstrath, PJ Hyett, and Tom Preston-Werner. GitHub offers both commercial plans and free accounts for open source projects. According to the Git User's Survey in 2009, GitHub is the most popular Git hosting site. - Source;
It is being used from software miniatures companies to giants.

If you "just" want to setup the Git on your windows, this illustrated guide is of great help; but since I am more of a Tortoise(cvs/svn) fan, so I went an extra mile(ran another search) to get Tortoise client deployed.

But if you hate UIs, and a commandline expert, this is the way to go.


Wednesday, October 6, 2010

How to: DISTINCT, SUM, COUNT the DataTable for a given XML?

KMan - 3:47 AM
Sometimes, you desire that you want something very quick. So you plan on using the Xml as data source and LINQ as a processor.

And then you desire to SELECT DISTINCT, and SUM, and COUNT on the DataTable;

So, for following,



YOu want an output of the sort:

HR 7.7
Marketing 8.8
Admin 8.9

Following is the code to do not just that but alot more, using LINQ.

//Sample xml, taken from my answer@SO[]
string xml = @"
//Load into a reader
System.IO.StringReader readerXml = new System.IO.StringReader(xml);

//Prepare dataset
DataSet ds = new DataSet();

//Read xml reader

DataTable dtTable = ds.Tables[0];

Use linq, the core of the post, to get average/count...

//Use linq to Average/Distinct rows.
var result = from theRow in dtTable.DataSet.Tables[0].AsEnumerable()
             group theRow by new
                 theRow = theRow.Field("category")//Group by category.
             } into Group
             select new
                 Row = Group.Key.theRow,
                 Count = Group.Count(),
                 Average = Group.Average(row => Decimal.Parse(row.Field("performance"))),
                 UniqueRows = (from p in Group
                               select p.Field("performance")).Distinct().Count()

foreach (var res in result)
    MessageBox.Show(string.Format("Row:{0}, Total:{1}, Average Performance: {2}, Unique Records: {3}",
                    res.Row, res.Count, res.Average, res.UniqueRows));



Tuesday, October 5, 2010

How to: Convert string to Time

KMan - 3:40 AM
Generated from this question, I thought it might be helpful for others looking for something similar.

Incoming data: 1000 (string format)
Desired output: 10:00 AM

C# Code:
string str = "1000";//drv.Row("arrival_time")
string[] formats = new string[] { "HHmm" };
DateTime dt = DateTime.ParseExact(str, formats,
string strTime = dt.ToShortTimeString();

DateTime dte = DateTime.Now;
IFormatProvider culture = new System.Globalization.CultureInfo("en-GB", true);

VB.NET Code:
Dim str As String = "1000" 'drv.Row("arrival_time")
Dim formats As String() = New String() {"HHmm"}
Dim dt As DateTime = DateTime.ParseExact(str, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal)
Dim strTime As String = dt.ToShortTimeString()

Dim dte As DateTime = DateTime.Now
Dim culture As IFormatProvider = New System.Globalization.CultureInfo("en-GB", True)

10:00 AM

Note, if you plan on using that frequently, you might want to add that as an extension method.

C# Code:
public static class Extensions
    public static string ToTime(this string str)
        DateTime dt = DateTime.Now;
            string[] formats = new string[] { "HHmm" };
            dt = DateTime.ParseExact(str, formats,
        { throw new Exception("Invalid data"); }
        return dt.ToShortTimeString();

VB.NET Code:
Public NotInheritable Class Extensions
    Private Sub New()
    End Sub
    Public Shared Function ToTime(str As String) As String
        Dim dt As DateTime = DateTime.Now
            Dim formats As String() = New String() {"HHmm"}
            dt = DateTime.ParseExact(str, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal)
            Throw New Exception("Invalid data")
        End Try
        Return dt.ToShortTimeString()
    End Function
End Class

How to call the extension method:
string str = "13000";//SomeDataRowView.Row("TIME_COLUMN")
string strTime = str.ToTime();

VB.NET Code:
Dim str As String = "13000" 'or SomeDataRowView.Row("TIME_COLUMN")
Dim strTime As String = str.ToTime()

Run in debug (VB.NET):

Happy "Dating"! (0;

Sunday, October 3, 2010

Will my .NET 1.x run on .NET 4.0?

KMan - 3:40 AM
Short answer:

Should you?

Read the long answer.

Long answer:

The .NET Framework 4 is backward-compatible with applications that were built with the .NET Framework versions 1.1, 2.0, 3.0, and 3.5. In other words, applications and components built with previous versions of the .NET Framework will work on the .NET Framework 4.

However, in practice, this compatibility can be broken by seemingly inconsequential changes in the .NET Framework and changes in programming techniques. For example, performance improvements in the .NET Framework 4 can expose a race condition that did not occur on earlier versions. Similarly, using a hard-coded path to .NET Framework assemblies, performing an equality comparison with a particular version of the .NET Framework, and getting the value of a private field by using reflection are not backward-compatible practices. In addition, each version of the .NET Framework includes bug fixes and security-related changes that can affect the compatibility of some applications and components.- Source: MSDN
The .NET Framework has two main components:
  • The common language runtime (CLR)
  • The class library (BCL, etc).
The common language runtime is the foundation of the .NET Framework. 3.5 uses CLR version 2.0; and so does .NET Framework 2.0 or 3.0, while .NET framework 1.1 uses v1.1.

The compatibility goal for the .NET framework is that applications and components from previous versions should work smoothly on the latest framework; except for a set of known changes.

So, to make your .NET 1.1 application run properly and stable on the target machine, you would be better to install .NET 1.1 on it to support your application.

Alternatively, you also can try to upgrade your .NET v1.1 application to .NET 3.x.

Here's how.
  • In your IDE, select the Properties-->Application-->Target Framework
  • Change the target framework to a recent one, and rebuild.

Source: MSDN

The compatibility goal for the .NET framework is that applications and components from previous versions should work smoothly on the latest framework; except for a set of known changes.

So, to make your .NET 1.1 application run properly and stable on the target machine, you would be better to install .NET 1.1 on it to support your application.

This means, following may/may-not "properly" with each other:
  • .NET 4.0 will not run "properly" .NET 3.x or earlier applications
  • .NET 3.5 will run .NET 3.0 & .NET 2.0 applications, but not .NET 1.1
  • .NET 3.0 will run .NET 2.0 applications, but not .NET 1.1
  • .NET 2.0 will not run .NET 1.1

Alternatively, you also can try to upgrade your .NET v1.1 application to .NET 3.x.

Here's how:
  • In your IDE, select the Properties-->Application-->Target Framework
  • Change the target framework to a recent one, and rebuild.
Though, any app (web/desktop) can be migrated to the desired framework, here is a step by step indepth article. How to: Upgrade an ASP.NET Web Application to ASP.NET 4.

If you still plan to use the 1.x on 4.0, don't forget to check the .NET Framework 4 Migration Issues.


Saturday, October 2, 2010

ASP.NET Slow on first access

KMan - 9:05 AM
Response is really slow when a page of ASP.NET/MVC web(site or app) is accessed first time.
It takes around(depending?) 4/5 seconds to sometimes a minute for the page to load.

But, interestingly, every following request is fast; as it should be.

The above process(first request slow, and consecutive fast) gets repeated approximately after every few hours of inactivity; and especially on the day start.

1. The first slow response is related to the JIT compilation of the service assemblies.
2. The IIS application pool is shut down after 30 minutes of inactivity.

After that, when a request is made, IIS has to start the website up again, which leads to this behavior.


Workaround 1: Warmup script
Write some code in the Application_Start event of the Global.asax file that calls the main pages or you web.

Here is a well known app startup script available on internet.

Alternatively, you can program it as a separate app and run this script as a scheduled task in Windows Scheduler.

Workaround 2: Remove the recycle option entirely
This could be the recycle worker processes setting on the application pool, check the value for that and either turn it off or make it longer.

You can also configure the shutdown worker process setting, after being idle under performance for the application pool.
  • Type inetmgr.exe in Start-->Run and press enter
  • Select Application Pools; right click and select properties
  • Select Performance tab
  • Uncheck Shutdown worker processes after being idle for (time in minutes): or set it to a higher value or uncheck it entirely.

Workaround 3: Windows service
Create a windows service[] that calls the pages after certain itervals of time.

Workaround 4: AutoStart Feature in .NET4.0
If you are using .NET 4.0, worry not; because there is an Auto-Start feature of the 4.0 framework. This article might interest you.

Workaround 5: IIS7 WarmUp Feature
If you using IIS7, use Application Warmup feature, that does the job for you.

Workaround 6: Use uptime robot?

If this is a production server then why not try adding a website monitor; such as up time robot. It basically asks for your websites headers and gets status codes like 200-OK, 404-NOT FOUND (etc) every scheduled(5, for instance) minutes.

This way your site is always spun up and does not impact log files/analytics as only headers are requested.

Plus, its free for up to 50 sites!

Helpful Resources
Next Previous
Editor's Choice