Showing posts with label quality. Show all posts
Showing posts with label quality. Show all posts

July 18, 2007

Obscurantism

I may have mentioned before that I have started to use the C# language. Overall it isn't bad - it uses static typing, which I have always found irksome, but the language is reasinably expressive and compact compared with something like Java or C++. Because it is heavily promoted by Microsoft there are any number of tutorial and example web sites, not all of them by programming experts.

Somehow the Java message that it's bad to allow direct access to instance and class attributes appears to have permeated the C# world, I have no idea why. I have just been reading one of the better-written tutorials, which offers the following code as an example if instance creation and manipulation.
static void Main(string[] args)
{
Person Michael = new Person();
Person Mary = new Person();
// Specify some values for the instance variables
Michael.Age = 20;
Michael.HairColor = "Brown";
Mary.Age = 25;
Mary.HairColor = "Black";
// print the console's screen some of the variable's values
Console.WriteLine("Michael's age = {0}, and Mary's age = {1}",
Michael.Age, Mary.Age);
Console.ReadLine();
}
As code goes that isn't bad, and apart from the different comment styles and the declarations it could almost be Python. Unfortnately the author has tasted the Java Kool-Aid, and shortly after this example writes
So each object now contains different data. Note that we directly accessed the variables and we put any values we wanted, right? But wait there is a solution to this problem. We will use properties.

There is no attempt to explain what the "problem" is, and you will probably not be surprised to learn that the "solution" involves making the attributes private to the class and then providing public getter and setter methods for instance users. This turns a five-line class declaration into a 32-line one (though to be fair to the author, he does at least include checking code that demonstrates the value of properties in applying class-based logic during assignment).

I have now read any number of texts where instead of something like
class Person
{
public int Age;
public string HairColor;
}
that allows direct access to the instance variables by client code, readers are encouraged to write horrendous code like
class Person
{
private int age;
private string hairColor;
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public string HairColor
{
get
{
return hairColor;
}
set
{
hairColor = value;
}
}
}
which actually offers no benefit over the short version at all. Python users are used to accessing attributes directly in their code, which clearly has performance benefits, and then implementing properties if and when they are required to add logic to the setting or retrieval of attribute values. I just wish that C# users could see that empty getters and setters offer no measurable benefit over direct access to attributes.

April 23, 2007

Bet Your Business on Python

It is encouraging to see that increasingly businesses are depending on Python to give them a strategic advantage in their marketplaces. The most recent comment of note comes from yet another PyCon sponsor, ITA Software, who use Twisted quite heavily in airline scheduling applications. They are a new phenomenon to the airline business: a company that provides responsive service without the use of mainframe heavy-iron. You can read the whole eWeek article to get more context about ITA's use of Python, but the quote I liked best was from Dan Kelley, ITA's director of application integration:
"So we think, absolutely, that it's ready for prime time. What we're doing is saying to a billion-dollar business, 'Yes, we can write components in this particular programming language, and they will keep your airline running.'"
Note that ITA don't use Python exclusively, and they see one of the advantages of distributed systems as being able to choose appropriate languages for each component.. I don't believe anyone thinks Python is perfect - it's a waste of time to seek perfection in a programming language because the concept is so subjective. Just the same it's nice to see real businesses coming out with this kind of informed opinion about Python in fairly mainstream publications. Quite apart from the general advocacy benefits it is useful, among other things, as ammunition against the dynamic language bigots should any such ammunition be needed.

April 11, 2007

Help Wanted!

Another post from Doug Napoleone says (among many other things) "I need to take the very clean and and elegant code of Steve Holden and stuff in into the somewhat organic and undocumented PyCon-Tech code."

It's strange how other people's perceptions of our code can be so different from our own. I am conscious of several areas where that code (published, by the way, as a part of my PyCon 2007 tutorial) really needs revision. But anyway, thanks for the tip of the hat, Doug.

Anyone wanting to help to improve the software support for PyCon should sign up for the PyCon Tech project. It would be a great way to get started in Django in a supportive environment.