Fremus.co.za

Demistifying Life and Web Development

Archive for October, 2009

Growing into Learning LINQ to XML

So I forced myself last night to spend time learning LINQ to XML, both from a technical perspective and a theoretical perspective. My aim is to not only know what LINQ to XML is but also to understand why and how. My goal for the next six months is to not skim over source code, but to actually spend time going through it and trying to understand it, and the same applies to LINQ to XML.

I started off with the MSDN article, and my goal is to go through all of the material and to get a good overview of the technology as well as knowing the code that goes with it. So I will try and learn as much of code as possible.

From what I read last night I found a few facts about LINQ to XML interesting:

  • It is a built-in language feature. I already knew this actually. And because it is a built-in language feature it gets to use debugging features as well as be strongly typed
  • LINQ to XML is equivalent to a redesigned XML DOM. You write query expressions that are equivalent to XQuery and XPath in functionality (not syntax).
  • It uses something called functional construction
  • You can create and manipulate XML by saving it, serializing it or sending it over the net
  • The query expression are SQL-like:
    IEnumerable items = from items in purchaseOrder.Descendants(“Items”)
    where (int)items.Element(“Quantity”) * (decimal)items.Element(“USPrice”) > 100
    orderby (string)items.Element(“PartNumber”)
    select items;
    The above sample is taken directly from MSDN
  • Share/Bookmark
posted by fr3dr1k in C#,LINQ to XML and have No Comments

More code vs less code vs readability vs efficiency

After my blog post yesterday on File.ReadAllLines() vs StreamReader I have been thinking that more code does not necessarily mean more, or does it? I mean syntactically the first one is a one liner and the second one is a few lines, but I do feel that the latter adds more value. Why? Well firstly because streams seem to be an important part and concept in the .NET framework and secondly the streamreader class seems to dispose of resources a bit better. It also seems as if my implementation of the streamreader class deals with something the first option has still to deal with. I mean you have to assign a string array and then somewhere you have to iterate through the content of the array, whereas with the streamreader you can do all of that once. Looping through the contents that gets read by the streamreader class instance can be achieved by either appending the results to a stringbuilder instance or by using a function that implements the IEnumerable interface and uses the yield keyword. Using a function that implements IEnumerable means that you can run a foreach against it. I do honestly find this approach a lot more expressive. Its also interesting to note that the streamreader class performs better in some cases.

  • Share/Bookmark
Tags:
posted by fr3dr1k in Application Development and have No Comments

Do we spend enough time designing?

In my current work environment it seems as if I spend way more time pushing out code than spending time making sure the quality of the code is good. Quality in this context means that you actually spend some time before you code planning the structure of the program you are about to create. In its simplest terms this is simply penciling some ideas down and drawing a few basic flowcharts. Doing this enables you to see things unfold a bit clearer, because I have found myself in a situation where I started coding and I got to a stage where the logic of the code went sour and it meant recoding everything or parts of it. With a bit of planning and design beforehand this can be avoided.

On a deeper, more complicated level, detailed specification building that incorporates requirements definitions and the full SDLC type-of-thing, obviously provides a different approach. But I think when you get to a deep and complex level you are addressing a big group of developers. Or can the same principles be applied to smaller projects?

I do find that I draw a lot of contextual diagrams in my current work, because I like having a contextual overview of what I am doing and what the system I am busy with attempts to achieve. A contextual diagram is usually a very basic diagram that just shows the system on a high level, with the idea being that you drill down to deeper detail from there. I remember using an analysis methodology called SSADM (Structured System Analysis and Design Methodology) and remember it as being very, very structured. You would start your diagram at the contextual level and drill down until the very lowest level. I do find OOAD (Object Oriented Analysis and Design) easier in a sense though, because you can directly translate objects in the real world to objects in the system. But I still draw contextual diagrams for OOAD.

The point though is that planning a piece of code is as important as the code itself and is a discipline.

  • Share/Bookmark
posted by fr3dr1k in Application Development and have No Comments

File.ReadAllLines() vs StreamReader

A couple of days ago I forced myself to memorize the C# code for reading from a file (text, html), and for it I used the StreamReader class (on MSDN):

try
{
    using(StreamReader reader = new StreamReader('filename'))
    {
            string s;
            while((s = reader.ReadLine()) != null)
            {
                   Console.WriteLine(s);
            }
     }
}
catch(Exception ex)
{
      Console.WriteLine(ex.Message);
}

This is in comparison to the File class which has a method called ReadAllLines, and its biggest benefit is that it results in less code. Is less more? I’m not sure yet. With the StreamReader class you can create a method that implements IEnumerable which allows you to use the foreach on it. I found the sample for this on http://stackoverflow.com/questions/286533/filestream-streamreader-problem-in-c.

public static IEnumerable ReadLines(string path)
    {
        using (StreamReader reader = File.OpenText(path))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
        }
    }

foreach(string line in ReadLines(file))
{
    Console.WriteLine(line);
}

It is also mentioned that the StreamReader approach works nicely with Linq. Another thing that was mentioned is that the StreamReader uses a concept called lazy evaluation and this increases performance.

Does the less code option result in better performance just because it uses less lines of code? I’m a bit undecided. Maybe I will have a better opinion in the future.

  • Share/Bookmark
Tags:
posted by fr3dr1k in Application Development,C# and have No Comments

Are the big names bad for us?

Earlier today I spoke to some colleagues and none of them knew who Steve Jobs was, well one did, the rest just weren’t informed. Its not a bad thing not to know, its not good either, because I believe you have to know about the people who make visible contributions to technology and lifestyle. Apple, for instance, is not a PC that I work with everyday but I have respect for the brand, and I understand what the Apple brand has done. Just think about the IPhone and the IPod, two outstanding devices that have changed the way we look at cellphones and MP3 players.

In the same conversation I asked the people around me who Eric Schmidt was, and none knew. I wonder if I ask them who Sergei Brin and Larry Page if they would know. Google has come to not only revolutionise internet search but have been pioneering all kinds of development, from browsers to cellphone operating systems. True movers and shakers in every sense, the same goes with Steve Jobs, Bill Gates, Mark Zuckerberg, Linus Torvalds, Bjarne Stroustrup, and the list can go on and on.

Coming back to the question though, are these ‘big names’ bad for us? I don’t think so because its the populace that determine the extent of the influence that these ‘big names’ have.

  • Share/Bookmark
posted by fr3dr1k in General and have No Comments

Is Firefox slowly dying?

I mean really, the performance sucks! The Firebug add-on is a 600Kb+ download and it feels as if it really, really slows it down. I have for a while now switched over to Google Chrome, because it is such a fast and responsive browser. I have found one or two issues when using Facebook with Chrome, but other than that its a pretty cool browser. The thing I dislike the most about Firefox is that its process does not terminate completely sometimes, and if you start a new instance of the browser the OS complains that Firefox is still running. Even IE is way quicker than Firefox. I used Firefox for development, and specifically the Firebug and Web Developer plugins, but I have decided to switch over to Chrome, because it does have a developer plugin. IE 8 also has a developer plugin. Thats my two cents for now.

  • Share/Bookmark
posted by fr3dr1k in Browsers,Chrome,Firefox,Internet Explorer and have No Comments
Get Adobe Flash playerPlugin by wpburn.com wordpress themes