References in Java and C#

1
2
Whatever  blah= new Whatever();
blah= utility.GetANewBlah();

Today is the second time I get a piece of code like this and I have to stand this huge misunderstanding about references. This time was done in C# but the first was in Java. Anyway, they behave in the same way for this matter.

Java and C# behave with variables that point to objects as placeholders for references. Using the example code as a guide, we execute the following steps in order:

  1. Define a variable called blah
  2. Create a new instance of Whatever
  3. Assign to the variable a reference to the new instance we’ve just created.
  4. Call a method in the object utility. The utility variable in turn holds a reference to another object.
  5. The method returns a reference to an object of type Whatever. We assign it to blah loosing the previous reference.

There is the mistake. We create a new object for the fun of typing code and then we forget about it. This behavior should be avoided in 99% of the cases because when the programmer does that he believes he is assigning the contents to the first generated object, when in fact he is forgetting about it and assigning a new one. This, if done in C++, will make your application eat your memory for breakfast.

This misunderstanding can lead to the following snippet I saw in a previous project.

1
2
3
4
5
6
7
List<Url> blah= new List<Url>();
...
blah.Add(icanhascheezburger);
....
blah= utility.GetANewBlah();
...
this.useBlah(blah);

Of course icanhascheezburger was lost and the coder was wondering why. In this case the previous coder assigned to that project did the same, and the new coder, not being used to the Java language, followed his example copying the behavior. From this snippet we have to understand that in line 5 we loose the reference to the object and if we don’t have a copy of that reference whatever we did before is lost.

C# notes.

  • No, the equal operator cannot be overloaded in C#.
  • Doesn’t apply on properties. A property can replace the behaviour of the equal operator and, for example, on assignment copy the content. But we have to code that behavior.

Use with caution.

We can use this behavior in some restricted cases. For example when the new assignment is “exception prone” and we want to continue no matter the result.

1
2
3
4
5
6
Whatever blah= new Whatever();
try {
blah = utility.GetPreviousBlahs();
}
catch () {}
this.DoSomething(blah);

In this case perhaps GetPreviousBlahs should return an empty list. Anyway, in case it fails, we can continue and operate with blah because it’s initialized.

Summary

Do not waste your time,others time, memory and CPU time. Object variables are references and the = operator removes the previous reference and assigns a new one.

Tags:, , , »
Comments (2) »

A day with C# and jQuery

jQuery Write Less, Do More

Days ago I tried  ASP.NET AJAX for an autocomplete text field in a form. The results were automatic, in no time you could have the most common and famous features you see in your favourite sites. But after dragging and dropping some controls here and there, and also adding a bunch of .dll’s to the project, a question appeared: what if I want a small modification?

Well, the answer wasn’t clear for me. Perhaps because my experience with ASP.NET is not so big enough to make some changes to a library that packs functionality in other language. Some times I want the things straightforward: from A to B.

So today I decided to reimplement the system using a javascript framework/library. I took a look to some of the libraries available on the net. I did a fast preview of these little amazing libraries and I liked them all. For my tests I chose jQuery.

Results: In one day not only was able to reproduce what I did with ASP.NET AJAX. I personalized the interface adding the functionality I wanted: calendars, creating new sections on the fly. And all this without a call to a piece of server code (except for the autocomplete part).

Before this change, even the javascript code was served using an aspx file server. For god shake! they’re static files! Do you really need to execute an aspx to serve a static file? Now I was able to setup the interface using XHTML, CSS and javascript.

But don’t think that .NET is the bad boy. I like a lot the “webservice” implementation it has. It can output JSON to feed your hungry AJAX/AJAJ applications. With a good web client for fast and quick tests.

Some other links I found interesting:

Tags:, , , , , »
No comment »