Monday, February 25, 2008

80's The Limit

I'm currently "studying" the course "Introduction to computer science" at the university.

Why "studying"? Because I have to, and because I don't really go to lectures because they are boring as hell and useless.

Anyhow, the course has its own coding conventions and style. Most of the conventions are totally fine and agreeable, but there's one that's driving me crazy - "Thou shall not write lines that include more than 80 characters.".
The reason is not specified (as usual here - why specify why to do something when you can just tell the students to do A and B), but I may assume it concerns the 1980's DOS/Amiga/Mainframes 80 chars limit, when working in textual video mode.

So let's count how many graphical operating systems that can display and print lines longer than 80 chars in one line got released by Microsoft(TM) 'till now:
  1. Microsoft(TM) Windows(R) 3.1
  2. Microsoft(TM) Windows(R) NT 4
  3. Microsoft(TM) Windows(R) 95
  4. Microsoft(TM) Windows(R) 98
  5. Microsoft(TM) Windows(R) ME
  6. Microsoft(TM) Windows(R) 2000
  7. Microsoft(TM) Windows(R) XP
  8. Microsoft(TM) Windows(R) 2003
  9. Microsoft(TM) Windows(R) Vista

And they still haven't found the "Change font size" button... too sad.

Limiting line length is good, but why limit it to the ridiculous number of 80? When programming in an object oriented language, creating a line which calls a class method and specifying 2-3 parameters will exceed this limit. So to avoid losing points an evil linefeed needs to be inserted.

The huge problem here, as I see it, is that most of the people studying with me haven't programmed in a real programming language (not Pascal) and doesn't know how a real-life code should look like. This is one of some bad habits they learn in a place that should theoretically teach them everything they should know about programming.

So what's the moral here? Read Code Complete and forget about people who know nothing about programming.


lorg said...

The 80 chars/line limit is actually a pretty standard coding convention.
For example, check out Python's style guidelines in PEP 8.
I saw other places that had this limit and explained it as a readability issue.

Also, I don't like changing font-sizes to read code.

Of course, I don't adhere to this rule strictly as well. If there's some code that will look better with 82 chars, it will have 82 chars.

Another point of interest:
My sister was studying C++ at the college, and I helped her a little bit.

At first, she mostly came to me with programs and asked 'why doesn't this work?'. Well, before even agreeing to actually read the code, I demanded she indent it properly, put all the curly braces where they should be, etc...

Their C++ teacher didn't teach them *anything* about style, and so their code looked really bad. I even heard stories of accepted assignments that used only one variable for all of the program. Ugh.

Last thing:
When I studied into to comp. sci. in TAU, they taught scheme, which was cool. It was the first time I learned lisp. I mostly sat in the back and read the presentations instead of listening.
Still, when software 1 came around and they taught C, I skipped all the classes.

Assaf said...

Lorg, so what if there's a PEP that's saying you should do it? Just look at all the brain damage the Python team has had to fix in the passing years, and was brandished happily in early PEPs.

Just open up your favorite code editor, and ask it to mark the 80th column.

For me, on a puny 13" laptop, that's a bit more than half the screen. It's called "modern screen resolution."

Are you seriously expecting me to just trash half my screen real estate, for someone reading my code on a TTY or PDP11?

I'm sorry, but the 80 character limit comes from VT100 capabilities -- a standard from 1978.
And even VT100 supports 132 characters per line with AVO.

Do you seriously want to base your coding style on some TECHNICAL limitation from 30 years ago?

So instead of enforcing proper rules (like indentation or proper code structure), you're wasting machine cycles on something that's counter-productive.
That's asinine, plain and simple.

StatusReport said...

I truly agree with Assaf. One more thing I didn't noted is that even in the course that is being held near a famous furniture store (and we all know what course I am talking about) this convention has been removed ~3 years ago because there has been a global understanding about this topic, with the reasons specified above.

StatusReport said...

Ami reminded me to check what Steve McConnell says about line length, so here's a quote from Code Complete (2nd Edition):

"With larger screens, narrow typefaces, laser printers, and landscape mode, the arguments for the 80-character limit aren’t as compelling as they used to be. A single 90-character-long line is usually more readable than one that has been broken in two just to avoid spilling over the 80th column. With modern technology, it’s probably all right to exceed 80 columns occasionally."

lorg said...

I never said you should strictly adhere to the the 80 chars rule. It's a style guideline. Not the most important one at that.
I like my code when without too long lines. It's easier for me to read it that way. I also have a wide screen, and I like my editor window to take half of it (so that I can put another window in the other half). When I had two regular screens, I had my window take just one of them, not both.

Just to check, I did some line counts on my scripts dir. Most lines are short, but I have the occasional 100 chars, or even 160 (really few of those :).

My point is, as a general rule, it's healthy to keep the lines on the short side. Makes for easier reading.

StatusReport said...

I think it's healthy too - lines of 160 chars are too long, but what about 100-120? It fits every modern screen resolution.

By the way - it's funny you actually counted script lines, because when I wrote the post I thought about asking you to write a script that gathers statistics about this issue from random google code or projects :)

lorg said...

I do base a lot of the things I do on technical limitations from years ago.
Why, do you type Dvorak?