Tuesday, March 22, 2005

Loopy Syntax - Delphi vs. C#

There is a guy who I work with who is taking some introduction to programming classes. He came by to ask us (the developers) some questions about loop syntax, not specific to any language. They are just working in pseudo code at this point. A co-worker wrote out some C# code and he was a little bit confused even though it was written to be very easy to follow. I decided to write it all out in Delphi for him. He found it much easier to read, specifically the for statement.

He pointed to the C# for statement and said "Why would anyone want to use this syntax when the Delphi syntax is so much simpler?" I told him that the C# for statement was more flexible, but that I agreed Delphi was a simpler language to read.

Julian Bucknall was recently ranting about code readability in response to Nick Hodges' comments about the superior readability of Delphi. Julain said readability has nothing to do the language and everything to do with what the reader is familiar with and how the code is written. While those are both important factors I disagree that the language is irrelevant. APL and ASM are not readily apparent with what they are doing, even to people who know the language. Don't get me started about binary machine code! Delphi is more readable then C#.

From Julian's examples of the for syntax used:

Delphi
for i := 0 to pred(List.Count) do

C#
for (int i = 0; i < list.Count; i++)

The Delphi for statement reads like English:

[For] [variable] [gets the value of] [starting value] [to] [ending value] [do]

Where each square bracket represents each part or token of the statement. The only things that are even the slightest confusing is the := for [gets the value of] and using pred(List.Count) for [ending value], with the latter being expressed other ways just as frequently. For great readability store the ending value in a variable before the for statement is called.

The C# is considerably more confusing:

[for] [required open parenthesis] [type of variable] [variable] [starts with] [value] [required separator] [variable] [check condition] [required separator] [increment variable] [required closing parenthesis]

The ++ syntax is more confusing then the := syntax (before the variable or after the variable?) but it could be expressed other ways (i.e. variable = variable + 1) but rarely is. I realize that the ++ syntax is holy war for some people, and I will admit it is a nice short-cut. When you are talking about readability, short-cuts usually mean less readable.

The C# for statement is more flexible and powerful then the Delphi one is natively. You can do everything you can in C# with Delphi, but Delphi might take a little more code. The difference is the resulting code will be easier to read in Delphi then in C#.

1 comment:

Esteban Pacheco said...

I think that the point of view of an outsider (let call it a fresh/pure mind) is more important.

If you talked with an ASM expert, he will give you a totally different point of view. Same with C#, etc.

The idea behind Nick's comment was on how easy to read or to understand is each of the languages.

I couldn't agree more on the fact that by nature Pascal is the ideal language for understanding programming principles. (reason why its used vastly in universities for starters)

The additional power Object Pascal has (Delphi) is a different discussion.