Delphi against the crazy C world

One thing drives me crazy. Why in the today world the ugly C-style syntax became so popular and widespread while beautiful pascal syntax is one on the latest places of popularity. Moreover today many new programming languages appear, and it seems to me the authors of those languages are competing with each other for the price in the category who figures out the most weird way of coding.

Let’s do some math about the SprutCAM project. Today the sources base of SprutCAM is about one and a half million lines of code, and it took 10 years to develop. As I know the effective development team never exceeded 5 men. 1’500’000 lines of code divided by 10 years of 50 weeks with 40 work hours multiplied at 5 men equals to 15 lines of code an hour.

What it looks like:

  result := false;
  at := self.GetArcType;
  case at of
    atL_L: begin
        Result := CalcArcLine_Line(ln1.p, ln1.tt, ln2.p, ln2.tt, s1, s2, fInitRc,
          ap1, ap2, ac, aR);
        if not result and
          IsEqD(1, abs(Vec_mul_Vec(ln1.tt, ln2.tt)), Increment)
        then begin
          result := true;
          ap1 := ProjectPointRay(self.pc, ln1.p, ln1.tt);
          ap2 := ProjectPointRay(self.pc, ln2.p, ln2.tt);
          ac := 0.5*(ap1+ap2);
          aR := VecLen(ap1, ac);
        end;

You can see there are a lot of spaces in each line. Your grandma can type 15 lines of code an hour! This example makes obvious one simple thing. Programming is not about typing. I can approve that programming is much more about reading a code and debugging it than typing it. However the authors of C-style languages think different! They think that by replacing human language words begin, end by curly braces {} and omitting then in the if statement they save hours of programmers lives. Here is an example of a C code.

   stbtt_uint16 format = ttUSHORT(data + index_map + 0);
   if (format == 0) { // apple byte encoding
      stbtt_int32 bytes = ttUSHORT(data + index_map + 2);
      if (unicode_codepoint < bytes-6)
         return ttBYTE(data + index_map + 6 + unicode_codepoint);
      return 0;
   } else if (format == 6) {
      stbtt_uint32 first = ttUSHORT(data + index_map + 6);
      stbtt_uint32 count = ttUSHORT(data + index_map + 8);
      if ((stbtt_uint32) unicode_codepoint >= first && (stbtt_uint32) unicode_codepoint < first+count)
         return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2);
      return 0;
   } else if (format == 2) {
      STBTT_assert(0); // @TODO: high-byte mapping for japanese/chinese/korean
      return 0;
   } else if (format == 4) { // standard mapping for windows fonts: binary search collection of ranges

Just look at those two code snippets! Which one do you prefer?

Advertisements
Delphi against the crazy C world

2 thoughts on “Delphi against the crazy C world

  1. Joseph says:

    I don’t find the “C” code really any less readable although they’re not computing the same problem. Delphi is a language that likes to specify *everything* and as such it’s far more verbose than it needs to be (the Type section, “implementation”, etc.) Way back in the day (and still today for those working on legacy systems) there was a very popular language called COBOL. Do you want to see how verbose COBOL was? Here’s a simple multiplication:

    MULTIPLY PRICE BY UNITS GIVING COST

    Seriously! The argument then was that it was “easier to read” for non-programming managers (who never, ever read source code anyway). I wouldn’t want to ever, ever code in COBOL again. Even the simplest of tasks took forever to write.

    The language “Python” is considered by some to be the most readable and easy to learn language right now and it’s actually far less verbose than Delphi. Lots of words doesn’t equal easy to understand, or else legal contracts would be simple to read. While I share your belief that some languages today are hard to read, and Delphi can be a bit easier than some of them, it’s also slower to code and doesn’t come close to python.

    >15 lines of code an hour.

    That’s the fault of the language and all of the static typing and things you need to explicitly do in Delphi. Python has been found in experiments on non-trivial test cases to allow developers to finish in half the time of the major statically typed languages C, C++ and Java. In fact, there’s some estimates that python users are *5x to 10X* more productive than Java programmers! The famous hacker Eric S. Raymond once wrote an essay about how he ended up “converting” to python as his favorite scripting language and part of it involved himself finding that he was writing code as fast as he can type(!!!) which normally only happened for him after years of practice with a language (this was within days of starting to learn and use python).

    There’s a lot more than could be done with the Delphi language to make it more readable and concise (Barry Kelly made some good moves in that direction, like for…in).

    Let’s say the task is to find all the words in a string that contain “r” regardless of case. It would just look like this:

    sentence = “The quick brown fox jumps over the lazy dog.”

    wordlist = [word for word in sentence.split(‘ ‘) if ‘r’ in word.lower()]

    That’s it! No “Program whatever”, defining variables, types, begins, ends, loops, etc. Speaking of begins and ends, python replaces them with significant whitespace – indentation counts!

    if word == “hello”:
    print(“Hi!”)
    else:
    print(“How are you?”)

    I commend your goal of promoting readability, by Delphi is filled with “if”s, “case”s, “for” loops, “while”s, var sections, type sections, class definitions and implementation sections, etc. Other languages today are using functional programming, “list comprehensions”, type inference, and other techniques to do what needs a page of Delphi code in two or three lines of code (yes, I’ve seen a page of Pascal reduced to three lines of Haskell code). Delphi really isn’t the model today of what an efficient, readable and powerful language should look like.

    1. Hello Joseph, Thank you for your comment. I really appreciate and share your passion about programming languages. Maybe it’s just my subjective opinion that Dephi is the most readable language in the world. Sorry, I don’t know too much about Python. My post was mostly inspired by emergence of such languages as Go and Dart, which inherit many stupid things from C language. The most obvious flaws of C for me are the following:

      1. A type precedes an identifier in variables declarations.
      E.g.
      int a;
      double b;
      This works as long you use only standard types like int, double, etc, because the types are highlighted by the syntax highlighter, but as soon as you start to use custom types like classes or structs, the code becomes messy.

      I think this stupid syntax was the main reason of emergence of dynamic type languages such as JavaScript. They just got rid of types to improve the readability of the code.

      2. Using symbols instead of keywords. E.g. && instead of AND, ! instead of not. It’s just ridicolous for me.

      3. Use of curly braces instead of begin end. Begin end is so much better than {} because they are nice visual delimiters. Just compare

      if SomeCondition then begin
      DoSomething;
      end else begin
      DoSometingElse
      end;

      Vs
      if (SomeCondition) {
      DoSomething();
      } else {
      DoSomethingElse();
      };
      Begin and end split your code into blocks. Curly braces are just too small for that.

      4. Horrible pointer syntax. I think the main reason of emergence of pointerless languages such as Java was the horrible pointer syntax in C. I mean the *, ->, and etc.

      5. And there are many more flaws in C and even more flaws in C++.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s