Posts Tagged ‘C#’

.NET Memory Leak and no solution from Microsoft…

Sunday, September 7th, 2008

You think that living in a managed world eliminates your worries with memory leaks? You think that as long as you don’t touch unmanaged resources you’re safe? Well, think again…

I’ve recently been struggling with .net because of Panels not being destroyed. Since I dynamically create these panels, which hook upon several events in the system, the application started to crawl down after a while due to some thousands/millions of events being fired to panels that should no longer exist. When we started testing the code line-by-line, what we’ve found amazed us: a particular Panel couldn’t be destroyed due to having AllowDrop set to True. WTF?

But there it was: set it to false, and the garbage collector happily destroys the object. Set it to true, and Dispose() is never called, the Panel lives, and the application stalls.

We thought: a bug in .NET? Does this happens in 3.x as well? Yes, it does. But, how is it possible? No one ever found it? After all, Drag’N'Drop is a common feature. Well, apparently, one guy did. But, either I’m misunderstanding the solution, or there is no solution at all.

Help?

Singleton using Generics

Friday, March 28th, 2008

Here goes a tip from my colleague José Vilaça for “elegantly” creating Singletons using Generics in C#2.0+ :

public sealed class Singleton<T> where T: class, new() {
 private static T _instance;
 private Singleton() { }
 public static T Instance {
  get {
   if (null == _instance)
    System.Threading.Interlocked.CompareExchange(ref _instance, new T(), null);
   return _instance;
  }
 }
}

Here’s an example:

Console.Writeline(Singleton<object>.Instance == Singleton<object>.Instance);

Of course, T must be a class with a public constructor without any arguments.

C# Odds, Part IV: Give me usefull Boolean Operators

Tuesday, January 8th, 2008

How many times have you written an ‘if’ statement like this:

if ((a && b) || !a) ...

You can think of condition ‘a’ behaving like a switch. Its like saying, if the switch is on, please take into account condition ‘b’ in order to do something; if it is off, then simply do it*. Now rewrite ‘a’ and ‘b’ as larger, real-word, classes’ fields/properties, mix more logic evaluation, and voila: an unreadable long expression.

Funny thing is, there *exists* a boolean operator that evaluates the very same way: it is called “implication”. Unfortunately, C# doesn’t have a keyword for it (same with Java, Python…). If it had, the code could be written as simple as:

if (a -> b) ...

Far more readable to me. Maybe they didn’t wanted to use the -> symbol because of the C++ syntax heritage? Who knows…

* Real-world example? Easy. You want to override the display of a dialog-box: if the switch is on, the application always asks for user confirmation; if it is off, it simply executes whatever it needs to.

Update: Hugo Ferreira (same name as mine), as pointed out that you can simplify ((a && b)  || !a) to (!a || b); thank you.

C# Odds, Part III: Beware of anonymous delegates

Sunday, December 30th, 2007

Consider the following code:

public class TestEvent {
  public event EventHandler FireUp;
  public void InvokeFireUp() {
    if (FireUp != null) FireUp(this, EventArgs.Empty);
  }
}

class Program {
  static void Main(string[] args) {
    List<TestEvent> fires = new List<TestEvent>();
    for (int i = 0; i < 10; i++) {
      TestEvent t = new TestEvent();
      t.FireUp += delegate { Console.WriteLine(i); };
      fires.Add(t);
    }

    foreach (TestEvent t in fires) t.InvokeFireUp();
    Console.ReadLine();
  }
}

If you are expecting it to print the numbers from 1 to 10 then you’re wrong :-) The thing is, external variables used in anonymous delegates are passed by reference (even value types like int). In the loop, the variable i is not recreated but increased by one. In the end, i will contain the last value and every event fired will simply write down the value 10. To work as intended, you should write the loop as follows:

for (int i = 0; i < 10; i++) {
  int j = i;
  TestEvent t = new TestEvent();
  t.FireUp += delegate { Console.WriteLine(j); };
  fires.Add(t);
}

Because j is defined “inside” the loop, it will always be a new variable (and hence a new reference in memory).

C# Odds, Part II: More about Access Modifiers

Saturday, December 29th, 2007

In my previous post I argued about the behavior of the protected keyword in C# being counter-intuitive. Some people commented on my post (to whom I thank for the participation), with several kind of reactions: there were suggestions to just change the field to public, to make a getter/setter method (property in C#), to go back to university and stay awake in OOP classes (*sigh*), and to use a real language instead of a Microsoft one (eheh). I’ll try to answer to all of those comments here, because I believe that understanding the languages we work with is important to develop correct and sophisticated programs using the Object Oriented paradigm (and not just write C with Classes).

To clarify further the issue, let’s just call stuff by their name: the concept we are discussion is called Access Modifiers. Access Modifiers are also used in C++, Java, Smalltalk and several other OO (arguably more or less) languages. Access Modifiers exist to aid the process of Information Hiding which is part of the Encapsulation strategy used in OO (or separation of concerns). Some argue that Encapsulation and Data Hiding are the same. Personally (and several other people including some from the Python and Java community) I tend to disagree. But the important concepts to stick around can be summed up by the following quote from wikipedia:

“In computer science, the principle of information hiding is the hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. The protection involves providing a stable interface which shields the remainder of the program from the implementation (the details that are most likely to change). In modern programming languages, the principle of information hiding manifests itself in a number of ways, including encapsulation (given the separation of concerns) and polymorphism.”

You may follow the links for further information, but the main reasoning I’m trying to follow here is that using public everywhere around is bad (I’ve seen this countless times). It can appear to be harmless in simple/academic applications, but when designing a full blown framework that will be used by other people, don’t ever underestimate the capacity of users to screw up things (including yourself after you decide to reuse a component you’ve done 1 month ago and don’t remember that, while that field is public, YOU SHOULDN’T TOUCH IT!!).

That said, lets look at the definition given by Microsoft to their Access Modifiers:

  • public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
  • private: The type or member can only be accessed by code in the same class or struct.
  • protected: The type or member can only be accessed by code in the same class or struct, or in a derived class.
  • internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.

Still with me? Great. Everyone knows public well enough. Internal is simple to understand too. But if you think private and protected follow the same rational, guess what: you’re wrong. Compare the following code with the one in the last post:

class A {
  private int protectedField;
  public void doStuff(A a) {
    a.protectedField = 3;
  }
}

class Program {
  static void Main(string[] args) {
    A a = new A();
    A aa = new A();
    aa.doStuff(a);
  }
}

It compiles, as it should! Though members doStuff and protectedField are of different instances, they are defined in the same class. Look at the definition of private: “The type or member can only be accessed by code in the same class or struct”. Are you following me? Private basically says: “I don’t care who tries to access this member, as long as it is within the context of the same *class*.”

Now let’s remember the code using protected:

class A {
  protected int protectedField;
}

class B: A {
  public void doStuff(A a) {
    a.protectedField = 3;
  }
}

It fails to compile, though the definition of protected says: “The type or member can only be accessed by code in the same class or struct, or in a derived class.” Can you spot the differences? The text is the same except that it adds “or in a derived class”. Well, B derives from A. So what is the problem? The problem, it seems, has nothing to do with instances being different. The exact error code the compile gives is: “Cannot access protected member ‘A.protectedField’ via a qualifier of type ‘A’; the qualifier must be of type ‘B’ (or derived from it)”. So in order for the member doStuff to have access to the protected field, a should be typed as B, despite the fact that protectedField is defined in A.

I would personally like to know the person who thinks this is intuitive (and sorry, but I will not even discuss why not just simply cast A to B).

To finalize, I just want to point out that these posts are called C# for a reason: this behavior does not emerge in Java :-) For example, the following code compiles and runs perfectly in JRE6:

public class A {
  protected int protectedField;
}

public class B extends A {
  public void doStuff(A a) {
    a.protectedField = 3;
  }
}

C# Odds, Part I: “Protected” Keyword

Friday, December 28th, 2007

What is wrong with this code?

public class A {
  protected int campoProtegido;
}

public class C: A {
  public void doStuff(A a) {
    a.campoProtegido = 3;
  }
}

Actually, it won’t compile. It seems that the protected keyword only works at the instance context. So, although class C is a subtype of A, because it is a different instance than the instance of A we are trying to access, it fails at compile time in line 7. So much for encapsulation…

Any ideas besides turning the field campoProtegido public?

Why learning Haskell/Python makes you a worse programmer?

Thursday, August 2nd, 2007

Recently, I’ve been having a lot of conversations with a friend of mine about meta-programming, partly due to some development we are working on, and partly due to our coming PhD. Some of the time we incur in largely (perhaps irrelevant) philosophical ruminations about programming languages.

But today, he found a rather peculiar blog-post, where the author teases the readers arguing “Why learning Haskell/Python makes you a worse programmer”.

Why learning Haskell/Python makes you a worse programmer?

The author presents two claims to defend his thesis: a) Demotivation and b) Code Obfuscation. Considering demotivation, he states that he “(…) constantly find (himself) wanting to use idioms from these languages, or noticing how much less code (he would) be able to write if (he) was using one of these languages (…)”. Further, it notes that it finds “(…) C# code very ugly compared to both Python and Haskell (…)”. The conclusion, he remarks, “is to make me very depressed and demoralised. I feel like a human compiler, translating the Haskell or Python in my head into a language which is a whole level lower.”

In what comes to code obfuscation, the author believes that because “C# has begun to get some features that are more friendly to functional style programming”, the programmer “when faced with a very common situation” tends to try a “functional solution”. Adapting this “functional solution” to a language like C#, results in “writing such complex code — using such advanced features as anonymous delegates — when a simple loop would have sufficed”. The overall conclusion is “don’t bother improving yourself, unless you have the freedom to improve your environment accordingly.”

While I tend to sympathize with the author frustrations, I believe this to be a classic example of misunderstanding about programming paradigms and languages. In fact I’ll try to summarize my arguments on “Why learning Haskell/Python makes you a *better* programmer”.

The difference between Paradigm and Syntax

First and foremost one must understand the difference between paradigm and syntax. Each different paradigm usually results in a different algorithm. Though, the same algorithm can be expressed in different syntaxes, which depends on the language one’s using to implement. The fact that a certain syntax is less ugly to a programmer implementing a certain algorithm designed in a certain paradigm qualifies the expressiveness of a language. This expressiveness is dependent on the domain’s problem and the paradigm of the solution, so different algorithms (that solve the same problem) can make the same language seem very beautiful or very ugly.

Expressiveness in Programming Languages

A common metric used to quantify the expressiveness of a language is the number of code lines needed to implement a certain algorithm. It is very common to find advocates of every language, using as arguments single-liners to convince that her/his language is the best: “just compare this one-line quick-sort implementation in X to this 30 line implementation in Y”. This makes the reader believe that X is more expressive than Y, while not noticing that the algorithm paradigm used for X is the same as the native paradigm of X, while the opposite isn’t always true (e.g. see [1] [2] and [3]).

The “other side”

It’s also amusing to see many people learning functional languages, believing they are the other side, and putting C and C# (or Java) in the same bag. Why this happens? Because people tend to learn syntaxes and not paradigms. If a language has a syntax very close to another (like C, C++ and C#), they tend to believe that the paradigm is the same. Even worse, because they find that a certain syntax in a certain language produces expressive code, they incur in the pitfall of trying to apply the same paradigm in a different language (and, surprise! that language wasn’t designed to be expressive in that paradigm). I’ve seen this problem occur over and over, especially in people that already have some programming experience; how many times I’ve seen code written in Java which is plain old C in another syntax?… These authors don’t have a clue what Object-Oriented is all about, and I doubt they also have a clue what Functional Programming or Logic Programming is about too (somehow, Prolog and the Logic Paradigm tends to be forgotten).

Which is the best?

What is the solution? In portuguese we say “Cada macaco no seu galho”, which losely translates to “To each its own”. One must understand that there are several forces to be taken account when choosing a certain language to implement a desired solution. Most of the time this are related to infrastructure features (like available frameworks and tools). But an important force (most of the time marginalized) is related to the problem’s domain; I would argue that functional languages are very expressive in what comes to mathematical defined algorithms and that I would definitely choose C to implement a device-driver (don’t confuse the choice of expressiveness with performance) and SQL to define relational queries.

The author’s mistake, in my point of view, is trying to force-fit a solution in a paradigm that is not natural to the language in question (for the author’s sake, I know that he knows this, but not everyone understand this). Just because a language has the infrastructure to deal with certain paradigms (like C# 2.0 new functional-oriented features), this doesn’t mean the necessary syntax is there to make it expressive in that same paradigm; “while in Rome be Roman”.

What can we do?

One solution for the problem of expressiveness are Domain Specific Languages (DSL). DSLs try to solve this by extending/redefining the original syntax and semantic of a language to better express solutions designed for paradigms more closely related to the problem’s domain (consider LINQ and mathematical sets) while maintaining the concepts horizontal between syntaxes (which differ from scripting or embedded interpreters). DSLs aren’t just syntactic sugar (I will argue about this on a different post).

Why learning Haskell/Python makes you a better programmer?

So why does learning other languages (especially languages that implement other paradigms) makes you a better programmer? a) Because it teaches you different points of view and b) because it gives you the best syntax and semantic for a certain domain. Learning different languages don’t necessarily make you better in other languages. It makes you better in understanding and designing new algorithms, though. But most important, it should make you feel more decoupled from implementation details and more detached from syntax.

There is an analogy that I like to apply when people ask “Is it worth it?”. Scott Ambler, in his book The Object Primer, talks about the difference between the Specialist and the Generalist. He argues that while a specialist is a “Jack-of-all-trades and a master of none”, good developers should try to be generalists, as in a “Jack-of-all-trades and a master of few”.

How I miss Python…

Wednesday, August 1st, 2007

Dear ECMA Technical Committee, please add list comprehension to C#. Here’s a simple snip in python:

', '.join([c.name for c in columns])

And the equivalent in C# (2.0):

public static string FieldList<T>(IEnumerable<T> columns) where T: Column {
    List<string> fields = new List<string>();
    foreach(T c in columns)
        fields.Add(c.name);
    return string.Join(", ", fields.ToArray());
}

Now try to translate the following python snip:

x = [(c.m(y) for (c, y) in zip(columns, ys) where c in primarykey])