CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Raymond Lewallen

Professional Learner

December 2004 - Posts

  • Catching up on your feeds during the holidays

    I follow 163 feeds these days, and if I don't go in and check them at least 4 or 5 times a day, it gets to be overwhelming trying to keep up with them. There are just too many good and interesting posts floating around. Holidays are especially bad. I like to spend time with my family and daughter and don't check my feeds during weekends or vacation periods. I always come back from 3 day weekends with 250 posts to catch up on, and actually read a decent portion of them. It takes a good two hours to catch up once I follow everybody's links and post comments and whatnot, while still marking many of them as read even when not reading them (I hate having unread posts. Those bold posts hanging around bug the crap out of me!!)

    Anyways, Happy New Year to all and good luck catching up on your posts over the holidays, its a lot of work! Thankfully, the holidays also keep many of us from posting very much anyways, so I won't be posting again until next week sometime, to save you the hassle of having to do a Mark All As Read on my blog, if you subscribe.
  • How to Write Unmaintainable Code (Humorous Article)

    A funny article on how to write unmaintainable code, which spells out job security, as long as you do your own code reviews ;)
    Quidquid latine dictum sit, altum videtur.
    Whatever is said in Latin sounds profound.
  • SuperBowl Predictions 2004


    Click here for the NEW 2005 Preseason Superbowl Prediction!


    Patriots over Philadelphia
    Final score 35 - 17

    What's your prediction?

    Yes, all of last years comments have been removed.  Nothing I could do about that, sorry.
  • Constructors for Exception types won't take an inner exception parameter

    I haven't looked at this in 2.0 yet, and hope somebody can answer this for me.

    One of the biggest frustrations I have with 1.0/1.1 is that the FCL exception types do not have constructors that allow you to give it an exception object that sets the inner exception. Take the following example:
    C#
    public Int32 Foo(string value)
    {
      try {
        return value.Length;
        } catch (NullReferenceException e) {
          throw new ArgumentNullException("value", "Cannot pass Nothing as a value.", e);
        }
    }

    VB
    Public Function Foo(ByVal value As String) As Int32
      Try
        Return value.Length
        Catch e As NullReferenceException
          Throw New ArgumentNullException("value","Cannot pass Nothing as a value.", e)
      End Try
    End Function

    The above won't compile because most exception types don't have constructors that take exception objects as parameters to set their inner exception property. Somebody please tell me this is fixed in 2.0. Or I can just go look for it myself sometime during the upcoming holidays ;)
  • Generics in VB.Net

    Generics is a new feature of the .Net 2.0 Framework. I've seen many articles on C# using generics, but don't recall seeing many on done for the VB.Net developer. I haven't looked in awhile, so there may be quite a few out there, but I've been meaning to post this for awhile now because its been written and just sitting on my hard drive, so here we go...

    Let's consider this overly simplified example below (Note that an array is not really the way you would want to implement an orders collection of sorts, but it works for the concept of generics in this example. This isn't something I would actually implement.)
    Public Class Orders
      Private myarray(0) As String
      Private currentSize As Int32

      Public Sub New()
        currentSize = 0
      End Sub

      Public Sub Add(ByVal itemName As String)
        If Not myarray.Contains(itemName) Then
          If currentSize > UBound(myarray) Then ReDim Preserve myarray(currentSize)
          myarray.SetValue(itemName,currentsize)
          currentSize += 1
        End If
      End Sub

    End Class

    So there we go. You can add orders based on the item name. But what if you wanted to also have a way to add items based on the item number, which is an integer datatype? You would have to create a new class to do so. Now, you could, in the above code, change the Strings to Objects, but that uses late-binding and leaves you open to passing any object type, such as a DateTime into the Add method. This compiles fine, but will throw a runtime exception.

    So here is where generics comes in. The Of keyword becomes your best friend now. The Of keyword is the same as <T> in C#. It identifies the data type that should be implemented for the class. So now lets look at our code with generics implemented.
    Public Class Orders(Of orderType)
      Private myarray(0) As orderType
      Private currentSize As Int32

      Public Sub New()
        currentSize = 0
      End Sub

      Public Sub Add(ByVal itemName As orderType)
        If Not myarray.Contains(itemName) Then
          If currentSize > UBound(myarray) Then ReDim Preserve myarray(currentSize)
          myarray.SetValue(itemName,currentsize)
          currentSize += 1
        End If
      End Sub

    End Class

    Now we have a way of creating the orders to class so that we can add items of any type simply by doing the following
    Dim itemNameOrders As New Orders(Of String) ' Creates a new orders class to store item names

    ' Or you can use the next line instead

    Dim itemNumberOrders As New Orders(Of Int32) ' Creates a new orders class to store item numbers

    And we didn't have to write an entire new class to accomplish this.
  • Pains of changing rss feed readers

    I've been using the same rss reader for quite awhile now called RssReader. I've enjoyed using this reader, but now I would like to change to a new reader. I've looked at SharpReader and OMEA Reader. I've downloaded and installed both and compared all three that I now have and have decided I would like to use OMEA Reader.

    First, RssReader doesn't export feed subscriptions into OPML format, only XML. OMEA only imports feed subscriptions that are in OPML format. So I went out and I found a converter for XML to OPML. I did that to my XML feed subscription file I exported from RssReader, but OMEA complains the file is corrupt. I went in and exported the 2 feeds I added to OMEA to compare the OPML files, and of course they are different, way different. The program I downloaded doesn't appear to be up-to-date with its OPML formatting. Holy cow, I have over 165 feeds that I subscribe too, and I certainly don't want to go and subscribe to each feed one by one.

    But hey, I have over 1500 blog posts saved on my hard drive via RssReader. If I switch to another reader, the new reader isn't going to download all the feed posts dating back to the beginning of the blog. So far, its seems OMEA only pulls the last 35 posts for dotnetjunkies, which is not the readers fault, that just happens to be the number of posts kept in the aggregation file. I continuously use the filter and search functions of RssReader to find old posts I want to go back and refer to. Without being able to move all those historical posts to a new reader, I have to keep the old reader so I have access to the historical posts, many of which are well over a year old. So now I will be stuck using 2 readers, and I really would like to switch to OMEA, but at what cost?

    Has anyone else changed your reader after using a particular reader for an extended period of time? What did you do about all those historical posts you have saved? Does anyone know of an XML-OPML converter that actually works? Any suggestions? Comments? Are there other news readers out there, for free, that you prefer? What is everybody using and doing about these issues? I've almost decided on just sticking with RssReader for the single reason of the historical posts.
  • Don't forget the T-SQL Enhancements in SQL Server 2005 chat today

    Today, December 16th from 2:30 - 3:30 PM PST.

    In this chat, we shall be discussing the enhancements in T-SQL implementation of SQL Server 2005, like Exception Handling, Pivoting, XML specific and more.


    You can find more information by clicking here.
  • A Simple Walkthrough for deploying a SQLCLR Stored Procedure

    Brad over on the VS Data Team has posted a walkthrough for deploying a SQLCLR stored procedure.

    Honestly, using .Net to create stored procedures to me is just a bad idea, in my opinion. Now you have application developers creating simple stored procedures and want their assembly added to the database, when they may not have the first clue about how performance may be impacted on the server. The developer may have no knowledge of index structures, triggers, foreign keys etc, and could very well be causing a bottleneck with the code they write when its simply doing nothing more than inserting, updating, deleting or selecting data. Leave the database and its purpose to the DBA's. Just because you can create a stored procedure in C# and put it in the database doesn't mean you should. Database programming is more than just creating a stored procedure to update data. If you understand the database architecture and the performance impacts your code will have and can avoid those bottlenecks when implementing your code, then by all means code your heart out, no harm done. I'm going to leave my simple stored procs in the database, written in T-Sql.

    Now, to take the other side of the argument. There are some very complex business logic models out there in the world, and sometimes Sql server just can't put that data together cleanly enough, and sometimes, not at all. You end up with a stored procedure or a view that is gigantic, has case statements all throughout, has dozens of subqueries, even more joins and is just a nightmare to have to deal with. In these cases, this is where SQLCLR becomes useful to me. C#/VB is much more capable and just downright easier to code those complex business models when you need some data structures put together, and I am very much looking forward to migrating code already in .Net that handles these situations over to SQLCLR to be executed on the database server where it can become more closely knitted with the database itself.
  • Pdfs Hate Modal Dialog Windows

    Well, after wasting much time and developing a nice headache, there seems to be no solution for getting a pdf to open in a modal dialog.

    Now you would think something like this would work:

    myWindow.caller = window.showModalDialog("/myReport.pdf")


    but it doesn't work. All you get it a blank page. So lets try something else:

    myWindow.caller = window.showModalDialog("/showReport.aspx")


    and lets say we have the following in our System.Web.UI.Page.Load event handler:

    Response.ClearContent()
    Response.ClearHeaders()
    Response.ContentType = "application/pdf"
    Response.WriteFile(MapPath("myReport.pdf"))
    Response.Flush()
    Response.Close()


    Ok, that doesn't do anything either. You still get a blank page. So after hours and hours of trying to make this work and looking at other peoples solutions, there is no solution. There is only a workaround. The work around is to create an IFrame on showReport.aspx, and the source of the IFrame would be the location of your pdf file. Viola! That works! But yeah, it sucks. Now you have to scroll the IFrame instead of the window itself, and printing becomes a worry. Not only that, but I need this to work with another third party software program that most likey won't work with an IFrame, and I don't even want to go through the troubles of seeing if thats going to work out alright. For those of you who don't know and use the product (sorry, can't tell you which one it is), you might think "We'll of course it will work. Its just an IFrame, anyways". We'll, the product, as awesome as it is, doesn't quite work like that.

    So, in conclusion, what was my solution? window.open. Didn't want to do it this way, but maybe if I set the window size properties from screen.width and screen.height and take up the full screen, they'll be more inclined to close down the window rather than just minimize it out of the way.
  • Coping with Burnout

    Eric talks about burnout on this blog post, and I hope to see quite a few more comments about it. No doubt, burnout hits each and every developer at one point or another. Its tough to deal with. We all do so much reading, writing, typing, reviewing, discussing etc that its easy to get burned out on it. Being a developer is very hard on a person's mental state over a period of time, it you have to get away from it. I would love to see some more comments on how people deal with avoiding and recovering from burnout. I've only had to seriously recover once, and it involved dropping an ongoing contract of 3 years, and lots of alcohol. This, my friends, is not the way to recover from burnout. How do I avoid it these days? I play with my daughter, I play my guitars, and I play Enemy Territory online with my friends. You'll notice the word play in each of those. Do something you enjoy every day that doesn't involve work. I enjoy my work, yes, but you have to do other things you enjoy too. Make time for the other things that make you who you are.
  • Performance Issues with exception handling

    An often discussed topic in the developer world is the performance of handling exceptions. Some argue that exception handling is too expensive. Yes, there is a performance hit when handling exceptions, but when you look at what you get out of exception handling, in my opinion, that is much more important and outweighs the fact that there is going to be a performance hit associated with the exception handling.

    A big plus in the world of .Net and exception handling is the garbage collector. Managed code has its objects stored in the managed heap, and the garbage collector takes care of the managed heap for us. When we create objects in our code and then throw an exception, we know that the GC is going to eventually clean up those objects. Managed code compilers don't have to keep track of this information at compile time like some other compilers, and this helps out with the performance. The wonderful little Finally block in managed code allows us to clean up some of these resources ourselves, even. An unmanaged code compiler (unmanaged C++ is the best example) has to know what's going on with its objects. When an exception occurs, all the deconstructors for these objects must be called for all the objects that were created successfully prior to the exception. There is a lot of tracking going on there with the compiler, and it increases code size and creates a performance hit in order to handle all of this in unmanaged code. Managed code, thankfully, the compiler doesn't care anymore. Managed code compilers know that big brother GC will clean up any mess that gets made along the way, making our code smaller and probably (I haven't tested this yet, and honestly probably won't) performing better when an exception is thrown, given certain circumstances, than the same code written and compiled with an unmanaged code compiler. The reason I probably won't test this is because the JIT and your platform can greatly impact the performance of exception handling. Code JITted for an Alpha processor is going to perform differently than code produced for an x86 processor, and I don't have the resources to do all the testing necessary to get solid numbers. Just take my word for it, exception handling is worth any performance hit you might incur, so implement exception handling.

    What's the alternative to exception handling for you skeptics? You write code to check the input values and return values of each one of your methods and pass a return value back up to the caller. This in itself is quite a performance hit, not to mention a coding and maintenance nightmare due to all the extra code that has to be written to do this. You also introduce more possible points of failure, which quite honestly, you need to wrap in Try blocks to handle an exception that may occur in your custom i-don't-like-exceptions-they-are-too-expensive-i'm-going-to-do-it-my-way code. Why do all that when it can be handled with a simple Try..Catch..Finally...End Try? I would have thought this argument would have gone away by now, but I still hear and see it lingering around. In my opinion, use exception handling and use it out the wazoo, where appropriate. What you gain is far better and more important than what you lose.

    Exception handling is a very deep and diverse topic in managed code. Most managed code books probably have an entire chapter devoted just to exceptions. I encourage you to pick up one and read about them, if you haven't done so already.
  • DataRowCollection, Red-Black Tree and a simple explanation.

    Last week I posted about the DataRowCollection and the arraylist that supports it was replaced in Beta2. At that time I didn't know what the implementation was going to be. Turns out, an internal implementation of a red-black tree is the current method used to replace the arraylist. I was also told this could change at any time.

    For those of you who don't know, a red-black tree is a type of binary tree with a height of no more than 2log(n+1). Standard operations take O log(n) time. Each node of the tree contains fields for color, key, parent, left, right and rank. The biggest rank of a tree occurs at the root and goes down to zero, so the rank is the height of a node. Two consecutive nodes differ in rank by either zero or one. Every leaf node is black (also a NULL pointer). If a node is red, both its children must be black. Most imporantly, to ensure the balance of the tree, each path from the root node to a leaf contains the exact number of black nodes.

    A red-black tree has great performance, and will not suffer the linear performance degredation that a standard binary tree will encounter, due to the number of elements in the standard binary tree. Red-black trees are always balanced, so searching, inserting and deleting all are guaranteed to be O log(n). Re-balancing a tree can be a bit costly, but that still occurs in O log(n) time.

    So what does this mean? It means the performance of deletions in a DataRowCollection will be substantially faster, if the DataWorks team continues to use their internal implementation of the red-black tree instead of an arraylist.
  • From Microsoft: Research Survey for leading edge developers

    Via Kimberly Tripp, Microsoft has put out a research survey to get feedback on just how satisfied you are with your current development environments and data platforms.

    It took me about 10 minutes to fill out.
  • Fields and constants for VB Beginners

    Here's a simple VB test anybody who programs in .Net should understand, yet you might be suprised at the number of people who don't understand how values and references work (explained here), and also don't understand how fields and constants work. I decided to put this up when a friend emailed me asking why something wouldn't work the way he was expecting.

    Note: This test does not apply to C#, and actually can't be converted to C# as is, and I'll explain why later, along with the C# equivalent of the test.

    Public Class Test

    Private A As Int32 = 5
    Const B As Int32 = 20
    Private ReadOnly C As Int32 = 10

    Public Sub New()
    Go()
    End Sub

    Private Sub Go()
    Console.WriteLine(Bar(A))
    Console.WriteLine(Bar(B))
    Console.WriteLine(Bar(C))

    Foo(A)
    Foo(B)
    Foo(C)

    Console.WriteLine(A)
    Console.WriteLine(B)
    Console.WriteLine(C)
    Console.WriteLine(Bar(A))
    Console.WriteLine(Bar(B))
    Console.WriteLine(Bar(C))
    End Sub

    Private Sub Foo(ByRef x As Int32)
    x = x + 5
    End Sub

    Private Function Bar(ByVal x As Int32) As Int32
    Return x + 5
    End Function

    End Class

    The output would look like the following:
    10
    25
    15
    10
    20
    10
    15
    25
    15

    Console.WriteLine(Bar(A)) outputs 10
    Console.WriteLine(Bar(B)) outputs 25
    Console.WriteLine(Bar(C)) outputs 15
    Then we call Foo() with each of the fields and the constant.
    Console.WriteLine(A) outputs 10
    Console.WriteLine(B) outputs 20
    Console.WriteLine(C) outputs 10
    Console.WriteLine(Bar(A)) outputs 15
    Console.WriteLine(Bar(B)) outputs 25
    Console.WriteLine(Bar(C)) outputs 15

    So, the only thing here that I see beginners not understanding is what goes one when calling Foo() and what gets output in the following 3 lines following Foo() calls. When calling Foo(), each one of A, B and C is still the value that they were at the time they were initialized. Bar() doesn't change any of the values, because Bar() takes its parameters by value, which does a copy of the fields in memory, thus preserving the values of the passed types. The tricky part is the calls to Foo(). Foo() takes its parameters by reference, which really just passes the address of the type being passed, and not a copy of the type. This means any change to value of the passed parameter will change the value of the initial type itself. So, that explains why A was 10 after the call to Foo(), but hey, shouldn't B and C have been 25 and 15, respectivly? Ah, there's the trick that most beginners miss. B is a constant, and cannot have its value changed after initialization (there are exceptions to this in VB, but we'll save that for another time). C is a ReadOnly field. The definition of a ReadOnly field specifies that its value can only be changed from the instance constructor or the type constructor, so Foo() cannot change its value either. The only value we can change when accessing the types memory address, thus the type itself, is A, our private read/write field.

    I'll explain the differences in this code in C#, and why it doesn't work the same way, but I'm going to have to save that for later.
  • ByVal and ByRef for beginners

    I like to provide help to the beginning programmers, and understanding the difference in ByVal and ByRef is something I see many of them ignore. So here's a tidbit of information, again, for the beginners out there.

    You need to understand and grasp this simple concept of value types being passed ByVal and ByRef. Soon, we'll talk about what happens when passing reference types ByVal and ByRef. This will also lead us into discussions about boxing and unboxing. For now, lets just concentrate on value types. When you pass an object by value, a copy is made of that object's state in memory and allocated to another memory space, so you have two copies of the object. Look at the following example in VB:

    Class Foo

    Public x As Int32

    Public Sub New()
    x = 5
    Main(x)
    End Sub

    Private Sub Main(ByVal y As Int32)
    y = 6
    Console.WriteLine(x)
    Console.WriteLine(y)
    End Sub

    End Class

    Now in this example, the output will be the following:
    5
    6

    This is because when we passed x to Main by value, we made a copy of the state of x in memory and that copy is used in Main. When Main is finished executing, the copy made for y goes away and we no longer have access to it.

    Now look at this code:

    Class Foo

    Public x As Int32

    Public Sub New()
    x = 5
    Main(x)
    End Sub

    Private Sub Main(ByRef y As Int32)
    y = 6
    Console.WriteLine(x)
    Console.WriteLine(y)
    End Sub

    End Class

    The output for this code is:
    6
    6

    The reason x is 6 and not 5 is because we actually passed to Main a pointer that tells Main where in memory it can find x; passing by reference. No copy is made and anything done to y also changes the state of x, because y is only a reference to where x is located in memory, not a new copy of the state of x. For this reason, you will never write code such as the following (which I found in some friends code and is what lead to this topic)
    Class Foo

    Public x As Int32

    Public Sub New()
    x = 5
    x = Main(x)
    End Sub

    Private Function Main(ByRef y As Int32) As Int32
    y = 6
    Return y
    End Sub

    End Class
    There is no need to return the value of y, because you were given y as a pointer to x. When you change y, you are changing x, as discussed above.

    Now, the concept of ByVal and ByRef goes much, much deeper, but we'll save that for a later time.
More Posts Next page »

Our Sponsors

Free Tech Publications