Why Programming is Hard

I am passionate about teaching people to program. I feel that the more people will know programming, the better off the world will be. Therefore, it makes me sad that programming is inherently hard. Here is an easy example to show something that is inherently hard, no matter what solution you find.

You choose to teach new people language X. What features should X have? Ideally, X should not be a language fit for only new programmers — otherwise, there will never be good support for X. You want people who work on X implementations to be using X for their day-to-day jobs, at least partially.

Now, for the next question: what should be the result of “1/2”?

Here are some possible answers:

  • 0 (C, Java) — seriously guys? seriously? people learned in school something about 1/2, but that ain’t even close
  • Raise exception (nothing I know) — that would not work *at all* for real programmers
  • 1/2 (ABC, some Schemes) — Rational numbers. Except after real calculations, these tend to explode space-wise, and teach the new programmers about the wonders of OutOfMemoryException
  • 0.5 (Perl) — floating point numbers. The misnamed floating point numbers. If you take the inverse of 303743984349384034343743945., and multiply it by 303743984349384034343743945., you do not get 1. You get 0.99999999999999989 (on my Mac’s floating point HW) I must have forgotten that lesson in math class, that sometimes x/x is just really close to 1…

I could not think of any others. Now, this is fairly basic. You have to have numbers like 1 and 2 to appeal to, well, anyone. If you have 1,2 and division, you already screwed yourself up — you’re going to explain to someone new to programming why they this is 0 (“why? and how do I represent freakin’ one-half? the point does WHAT?”), why they occasionally need to apply .compress(max_error) (“space? I’m on earth.”) or why x/x is really really close to 1 (“oh, that’s what those pentium jokes mean — programmers are insane”)

[You think this only hurts you if you’re doing super-complicated science stuff? I once had floating point approximation bugs in a Perl program designed to calculate the size of generated images. I actually had to use a rational-number module.]

Advertisements

3 Responses to Why Programming is Hard

  1. Ben says:

    If you think of / as being context specific, then 0 is a perfectly good answer to 1/2. Since 1 and 2 are integers, / is integer division. The answer is 0 remainder 1, and the remainder gets dropped. In this case, it’s probably good to discuss the % operator at the same time.

  2. Ben N says:

    I’m not buying this as a general cause of programming being hard. Programming is the art of saying what you want, but first you have to want something. What is the application? Why are we dividing 1 by 2?

    I should think that demonstrating the need for both behaviors should be easy — present a case where integer division is obviously what you want, present a case where float division is obviously what you want, point out the obvious fact that both behaviors can’t be the default, and then show how to get the non-default behavior.

    Division forces you to actually decide what number system you’re working in, and that introduces a new topic, but I don’t think it’s “inherently hard”, either from a pedagogical or language design perspective — a good language should provide a variety of numeric types, and a good introductory programming course (that’s not specifically aimed at scientific or numeric computation) should start with exercises which avoid this issue until students have the basics down.

  3. c9y says:

    Programming being hard…
    For me programming is a new language… You have to study every letter that mixed up together and analyse their function as a word/code before putting up together those codes to create a group of codes (or a sentence) to become a function or a command…

    studying every codes and mastering how to combine them… Just like studying English language and grammar… You have to combine them perfectly …
    Its hard…

    Help me… I want to create a program… game application… I don’t know where and how to start…

    THANKS

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

%d bloggers like this: