?

Log in

No account? Create an account
Who, me? [userpic]

Clean code

May 21st, 2008 (11:25 am)
current mood: smug

OK, this is a bit of obscure geek bragging:

I've been working on a language interpreter (originally just a chance to experiment writing a garbage collector), in C++. I finally got around to turning on -Wall ("warn me about anything that might be a problem"), and did a build, expecting to see a slew of warnings.

I got two. One was an unused variable and one was comparing a signed int to unsigned. The first can be a symptom of bad logic, but in this case was just a variable I forgot to delete when I deleted the code that used it; the second is bad logic, but only if there's a chance of the signed one being negative, or the unsigned one being 231 or more. (Context: I was looping through a string, to parse it as an integer. So the unsigned int was the length of the string; the signed one will become negative only if the unsigned one is 231 or more...which would mean someone handed me a 2GB string. Unlikely, but still worth fixing.)

Admittedly, this is in only 4600 lines of code; but it's still a pleasant feeling. I suspect this is an effect of my time at Endeca, where everything was built with -Wall. My high-level fingers apparently incorporated all the warnings I saw in that time.

Comments

(Deleted comment)
Posted by: metahacker (metahacker)
Posted at: May 21st, 2008 05:08 pm (UTC)
keys

which would mean someone handed me a 2GB string. Unlikely, but still worth fixing.

"Who'd ever need more than 640KB memory? I mean 2GB memory? I mean..."
"Who'd ever need a VARCHAR more than 64K long?"
"Who'd ever request more space than they need and overwrite the executable to hack our program?"

:D

Seems very, very likely to me, in the long run...

Posted by: Who, me? (metageek)
Posted at: May 21st, 2008 05:22 pm (UTC)

Well, yeah. And, admittedly, this signed/unsigned one is a mistake I make fairly regularly.

...and, admittedly, "someday" may not be that far away, since my desktop does now have 4GB of RAM, and a 64-bit CPU. :-)

Posted by: robertdfeinman (robertdfeinman)
Posted at: May 21st, 2008 06:18 pm (UTC)

Not to rain on your parade, but this is like relying on spell checkers. There (oops They're ) no good at finding wrong words spelled write (oops right).

After being involved in computer coding since the 1960's I find it discouraging that anyone still has to use procedural languages. In fact I don't see the rash of clever languages that have been created over the past 20 years as a step forward. Fortran and Cobol may be excessively wordy, but there (oops their) syntax is at least similar to English.

Any language which has contests to see who can create the smallest statement to do something or who can create the most obscure syntax is not doing its practitioners any favors. With so many smart people in the field I just don't know why more effort isn't put into developing more abstract tools that write code.

I don't think the integrated environments are really the same thing, it's just like using a sharper saw blade to make a cut, but it's still the same cut that needs to be made. (OK, so I'm an old curmudgeon...)

Posted by: Who, me? (metageek)
Posted at: May 21st, 2008 06:51 pm (UTC)

Not to rain on your parade, but this is like relying on spell checkers.

True. But the compiler warnings are usually there for a good reason; and most code I see has lots of them if you turn on -Wall.

I just don't know why more effort isn't put into developing more abstract tools that write code.

It's been tried, many times. It never works, because a tool that's sufficiently powerful to write any code you need is a programming language. The best you can do is pick the most powerful language you can. I would normally be using something more powerful than C++, but, in this case, I wanted to do some lower-level work to learn about language implementation.

Posted by: Justin du Coeur (jducoeur)
Posted at: May 22nd, 2008 11:01 pm (UTC)

It never works, because a tool that's sufficiently powerful to write any code you need is a programming language.

And usually a pretty bad one. I've had the misfortune of writing serious systems in a couple of these supposedly higher-level "languages"; they've generally taken more work than it would have taken in a conventional one.

There are good trends in language design, but most attempts to replace languages with tools so far have been real wastes of time...

Posted by: Who, me? (metageek)
Posted at: May 23rd, 2008 02:36 pm (UTC)
Too-high-level languages

I've had the misfortune of writing serious systems in a couple of these supposedly higher-level "languages"; they've generally taken more work than it would have taken in a conventional one.

Yup. My first full-time job was working in one such language, a system for writing CAI courses. The original idea had been to create a simple programming language so that teachers could write the courses themselves. When using teachers didn't work out, they tried hiring "low-skill coders" to do the work. When the coders started producing crap, they hired "enhancers" to clean up after them. These enhancers were full-fledged programmers, and generally found that it took them just as long to clean up a unit of code as it would have taken them to write it in the first place. They often wanted to just throw out what they got from the coders, but it wasn't allowed.

It occurred to me this morning that the fallacy here is basically that people don't really understand the Church thesis (loosely, "a Turing-complete language can be used for anything"). If you turn the Church thesis around, you realize that a system that can be used for anything is necessarily a Turing-complete language, and using it is necessarily programming. But the only people who really internalize the Church thesis are those that have programmed in lots and lots of different languages. So managers dream up these systems to replace good programmers, and hire bad programmers to work on them—since good programmers know better.

7 Read Comments