Friday, 8 May 2015

Code monkey like you

It started with Facebook. First, there was a post about Singapore’s PM Lee Hsien Loong talking about wanting to learn Haskell. Then, there was this lovely blog post by one of
Lux's colleagues - Lakshmi Kumaraswami (Does that make her another Lux?) - about her experiences with learning Python. Python is cool. So is Perl. Or Ruby. Over the course of the last few years, I've downloaded a variety of programming languages - swearing that I would spend the time getting to learn them, but never have gone beyond opening the documentation.Erlang. Nimrod. Haskell. Haxe. Clojure.

Maybe it's a nostalgia thing.

Maybe it's about the feeling of power you get. I downloaded a PDF of good old Kernighan and Ritchie. And started my way back up from "Hello, world".

It was like a kick in the nuts.

Like many bad programmers, I had an inflated opinion of my own programming skills. It would be a breeze, doing those old programs again, I thought.

I couldn't have been more wrong.

One of the exercise programs in the first chapter read: "Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank"

I was fucked.

I was looking for ways to assign inputs to string objects and call replace() functions. string objects don't exist in K & R C.

Objects don't exist in C.

In the end, the solution is so simple. If the previous character is a space and the current character is a space, don't do anything. Otherwise, write the current character to the output.

I had to google it.

It wasn't just the code. Setting up the development environment required more thinking than I had done over the past four years combined. My office network administrator treats every user as one with the skills and the intentions of a Russian hacker, which means that I can't install anything that requires changes to the
registry. 
So I downloaded MinGW. Download, unzip and extract works better than an
installer. 


Then I wrote "Hello, world".

/*
* Most of what follows is probably humblebragging, about setting up an editor with
* C syntax support. 
*/


I thought, "I need syntax highlighting. Notepad doesn't cut it." 

The usual suspects - Textpad, Multiedit, Notepad++ - were either no longer free or
used installers. 


Then I thought of Scintilla. Scite was a nice and easy to use Notepad substitute, with syntax colouring support for every language I'd downloaded -plus several that I hadn't. 

Downloaded it, unpacked it and found there was no executable file. 

Opened the readme file and it began with


"Scintilla can be built by itself. To build SciTE, Scintilla must first be built."


I needed either a Visual C++ compiler (recommended). The readme also grudgingly informed me that TDM Mingw32 4.7.1 was also supported. 

Oh Good. I had MinGW.

I followed the instructions 
To build Scintilla, make in the scintilla/win32 directory

cd scintilla\win32
GCC:mingw32-make

Of course, the make failed. 
The compiler spat this message out and refused to proceed.

In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\cmath:44:0,
from main.cpp:1:
c:\mingw\include\math.h: In function 'float hypotf(float, float)':
c:\mingw\include\math.h:635:30: error: '_hypot' was not declared in this scope
{ return (float)(_hypot (x, y)); }
^
Googled and found it was known issue with the MinGW compiler. Bug 2250, no less

The support guy's response was:

"I can guess that the problem is unwarranted occlusion of the
prototype for _hypot() in the presence of __STRICT_ANSI__,
while still exposing an inline function implementation which
requires it, but, as I say, I don't have time to pursue it at
present. The work-around is to add -D__NO_INLINE__ to the
compile time options, to hide the offending inline function
implementation."
That made as much sense to me as you can imagine.
But I latched on to the word "work-around" and figured that I needed to change the compiler options. 

Right. 

So where were the compiler options? 

I knew the GNU C++ compiler was gcc.exe. So I searched within the Scite folder for all files containing the words gcc. The search turned up a bunch of files - none of which seemed to fit. The only one that seemed to have any relevance was a file called scite.mak. 

Which began with the comment
# Make file for SciTE on Windows Visual C++ version.

But helpfully, it also contained this line

# The main makefile uses mingw32 gcc and may be more current than this file.

And yes, there was a file called makefile in the folder.
There was no use of the word "gcc" in it, but at some point, memory kicked in and I modified the CXXFLAGS to read  $(CFLAGS) -pedantic -fno-rtti  -D__NO_INLINE__

And what do you know, it worked.

And that was a rush. It was the equivalent of untying an irritating knot.


Any one who has pretensions of being a writer has only one thing on his or her minds. To connect. To move the reader. To make the reader feel. Making the reader think requires another set of skills altogether - something that's frankly beyond my ken.

And that's why writing code is such an experience. You can move an inanimate object. Say the right words, and the CPU will do anything you want. Say the wrong words, and the compiler will shit on you, until you get things right. And you're not home, even if your code compiles. A misplaced bracket, and the whole purpose of your program is lost.

Can you imagine how liberating that is? Here is a reader with rules! Here is a reader who gives you honest feedback. No more worrying about whether your friends 'like' the stuff you write - "ooh poor Nari, looking for approval again.  Let's like his post".
When you code, you either use le mot juste or you don't. And if you don't, your reader tells you where you've fucked up. To the line and the position in the line.

It's hard to explain to non-programmers the lure of programming. It shouldn't be. If you like writing, you should like coding. If you like reading, you should like
coding. If you like prose, you should like coding. If you like poetry, you should fucking love coding.

When I was in college - which is shorthand for "when I was young, innocent and romantic", I thought a great deal of poetry. And the best poet, to my mind, was Stephen Crane.

There's this poem by Crane called "A man said to the universe". It's just 24 words long, but it conveys so much. Crane would have been a great programmer, I think. He did so much with so little. And that's the glory of code. What you make of it is what you can make of a severely limited vocabulary - but that vocabulary is enough to send you to the moon and back.