Performance Zone is brought to you in partnership with:

I am a Software Engineer working with the latest in .NET focusing on Web Development. With a burning heart for programming, I blog and author a book about C#. Filip is a DZone MVB and is not an employee of DZone and has posted 53 posts at DZone. You can read more from them at their website. View Full User Profile

Do I need to understand low-level programming and memory management?

09.04.2013
| 6750 views |
  • submit to reddit

There’s been some radio silence here recently, sorry about that, but everything is literally upside down at the moment. Hence, I moved to the other side of the world! So let’s break this radio silence by touching something rather interesting and quite frequently discussed: Do I need to understand low-level programming and memory management?

Let’s start this off with a self-explaining “JIF”:


Well, do I?

Rather frequently you’ll meet developers who praise abstractions with the same enthusiasm that Mr. Ballmer once shouted “developers, developers, developers!” There’s nothing wrong with aiming to perfect abstractions that help you in your daily work, however, it’s important to really understand what the abstraction solves and how it impacts performance, security and, most importantly, manageability. Let’s say that you’re a web developer that has just learned how to use the best abstractions out there. For instance, you know how to set up a data context using EF and how to get some users out of the database. Now your customer asks you not to use EF, but instead to write the SQL yourself. Since you’ve never done this you go on with your day, Google for some rather simple SQL management in C# and end up with something like this:

public int GetUserId(string username, string password)
{
    var command = new SqlCommand(string.Format("select Id from Users where UserName={0} and Password={1}", username, password));
    // and so on..
}

You get the idea, right? The developer just went on with his day because he never had to bother understanding SQL injection since the abstraction took care of all that. Wrong! The developer surely needs to understand everything there is about SQL injections even if the abstraction says it takes care of the most common SQL injections.

Now let’s say that we have another developer that needs to perform some computation and starts doing something like this:

public int Calculate(object data)
{
    var result = 0;
    for(int i = 0; i < 10000; i++)
    {
        result += SomeMagicMethodThatDoesSomething(((int)data) + i);
    }
}

In this scenario, we need to know about value types and reference types and why memory matters. The C# compiler might help us optimize this, other compilers might not. When we cast a value type to a reference type or the other way around, some boxing and unboxing will be performed. This means moving the value in memory, and doing so once might not be a performance penalty, but doing it over and over again in a loop? Well that is just not good. In other scenarios, obscure attempts to beat the compiler on optimization might make code unreadable, which could have been avoided by understanding what happens on a lower level once the code is translated.

So once again, the lack of understanding what happens behind the scenes has lead us to shoot ourselves in the foot. In the examples above the problems might be quite obvious.

So what does this really have to do with low level programming and memory management?

High level languages, such as C# or PHP, can be seen as abstractions on top of something lower level. On the final step of executing a C# program, it will be executed as plain Assembly, which means that understanding how the C# code that we write is translated to Assembly will help us understand how the processor might act on certain operations.

This, in return, will help us write faster and more reliable applications because we understand what happens on a lower level. The same goes with memory management. If we understand how objects are stored in memory and how to avoid memory leaks, this will just as well help us write more reliable applications.

On a personal note, I’ve found that it is easier to transition between different programming languages when understanding lower level programming and memory management. Different people learn differently, for me it’s been a great help to understand low level programming, I even have a weird urge to do some Assembly now and then.

What’s your personal opinion on low-level programming and understanding memory management? Do you think it helps with understanding new programming languages or is it just a waste of time?

 

Published at DZone with permission of Filip Ekberg, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)