Spot any errors? let me know, but Unleash your pedant politely please.

Saturday 7 May 2016

Reassuring

A few years ago, when was getting into Python, I wrote an XML parser.  It's slow and fairly terrible at namespaces and doesn't have any proper unit tests, but it's pretty easy to use.  Of course, I shouldn't have written it at all, but I learned to code before Stack Exchange was a thing. I didn't know that I could find all the answers from smarter people there. Or even that there were some built-in Python things for XML.

I'm still using that parser, which has its own sort-of-XPATH-ish searching, which was written before I  knew that XPATH was a thing.

When I wrote it, I'd never really written a parser for anything particularly complex or vague.

To parse, it uses a class called StringReader.  I've never been happy with the name, but haven't spent any time trying to figure out what it should be called.

Today swift-name-demangling popped up in Reeder. It talks about a 'Scanner' class, and shows the .cpp header:

class NameSource {
  StringRef Text;
public:
  NameSource(StringRef text);

  bool hasAtLeast(size_t len);
  bool isEmpty();
  explicit operator bool();
  char peek();
  char next();
  bool nextIf(char c);
  bool nextIf(StringRef str);
  StringRef slice(size_t len);
  StringRef str();
  void advanceOffset(size_t len);
  StringRef getString();
  bool readUntil(char c, std::string &result);
};

If Python had headers, this is what mine would look like:

class StringReader(object):
    def __init__(self, string):            
    def fwd(self):
    def rev(self):
    def next(self):
    def prev(self):
    def peek(self):
    def eos(self):
    def sos(self):
    def reset(self):
    def find(self, value):
    def checkFor(self, string):
    def skipAny(self, valueToSkip):
    def skipPast(self, valueToSkip):
    def peekAhead(self, length=20):
    def getUntil(self, value):
    def getUntilSequence(self, value):
    def rememberPosition(self):
    def restorePosition(self):
    def remainder(self):

It's really reassuring that even though I wrote this in the dark (metaphorically at least) there's a significant overlap with same/similar names (peek, next, getString/remainder, readUntil/getUntil, isEmpty/eos).  I have more methods. Some look like unnecessary duplicates. I hadn't discovered how to indicate private methods, hadn't discovered properties and hadn't discovered Python naming conventions. I'll have to look into that default length of 20. That looks really odd.

Given that I this grew organically as I needed methods in my XMLElement class, I'm pretty pleased with it today.  I think I'll rename it StringScanner.


Monday 25 April 2016

Prince. Sadface. What an idiot!

The untimely death of Prince has me saddened and confused. Saddened for obvious reasons. Confused by own stupid neglect of my erstwhile fan status. I'm kicking myself that I never quite got around to seeing him live. I'm annoyed that I stopped listening to his music.

I was a fan from Around The World in a Day to Diamonds and Pearls.  I remember rushing from work to Our Price in a lunch hour the day that Diamonds and Pearls came out, getting back to the office a little late and plying it on my CD Walkman for the rest of the week.

This span, from 1985 to 1991 was what I considered my metal years. I'd buy lots of terrible hair metal on Roadrunner Records. I was into guitars and guitarists that were written about in Guitarist magazine. Nuno Bettencourt and Steve Vai and Joe Satriani and Jennifer Batten and Paul Gilbert.

Astonishingly, I'd have probably bought Project Driver at about the same time as Parade. 

You've probably seen the video of Prince's astonishing playing on 'While my guitar gently weeps' with some rock dinosaurs. I don't know if it's true, but I read that this performance was shortly after, and perhaps a response to being absent from a Rolling Stone list of the 100 greatest guitarists. If true, what a fabulous "Fuck You!"

But here's the weird thing: I really liked Prince. I was really into guitarists. I never put these things together. It seems utterly bonkers in hindsight, but I think I know why…

Prince wrote some great songs, arranging and producing them, often playing most of the instruments himself.  Guitar was just a part of what he did. Just part of a song.  Maybe exemplified by When Doves Cry, where it's pretty much just an intro to the intro. Not a run of the mill guitar solo in the middle of a song in the middle of an album of other songs full of guitar solos.  In Paisley Park, it's kind of easy to miss the moments of guitar virtuosity, or forget that it's Prince himself playing them, because they're part of a whole.

Maybe it was a compliment to his song writing and production skills that Rolling Stone overlooked his guitar playing.







Friday 1 April 2016

Tabs, spaces and alignment.

I just read this...
http://cocoawithlove.com/blog/2016/04/01/neither-tabs-nor-spaces.html

I agree with it all completely, except the parts I disagree with.

I agree that I t doesn't matter whether you tabs or spaces as long as you and your team are consistent.

I think my preferences come from coding on actual VT100s an VT220s in the 80s. Physically 80/132x24 monochrome. 

I prefer spaces. A space is a fixed unit and a tab is unpredictable. You need spaces in your code, but you don't need tabs. It makes sense to pick just one, and that can't be tabs. 

Where I really disagree though, is that manually formatting and aligning to make the code pretty is a waste of time and effort.  Readability is really fucking important. Code is read far more than it is written. To not make the code pretty will annoy or inconvenience readers, including future me. The most valuable tool in making code readable is space. We see this most obviously in left indentation implying or defining scope. It's also good for indicating arbitrary blocks. Or giving a complex line room to breath. 

The other thing that really, really bugs me about this drive for efficiency while coding. I'm not 100% creative coding at full speed the whole time. It's more like crafting or interval training. There's a creative burst, then a period of recuperation. This is when I prettify the code I've just written. There's low cognitive load, pride in a job well done, and I'm ready for the next creative burst. 

In monochrome, there's no syntax highlighting. Keys and values, formal parameters and their default values or actual values, names and literals. They all look the same. Maybe pretty code yields more readability in that context.

While I'm at it… Comments that add value are fine. Yeah, yeah, they can atrophy, and code should be self-documenting, but a little explanation can make skimming the code a lot easier. Anyone maintaining or reviewing code needs to take responsibility for the comments.

Lastly, I was intrigued by this post, in which an editor would present code like an outliner rather than just text: http://inessential.com/2015/12/21/how_to_solve_tabs_vs_spaces_forever





Monday 28 March 2016

Misfits and Heroes

I've been trying to figure out why a lot of reasonably well made TV is unwatchable crap. The main problems are poor dialogue and too much exposition (often the source of poor dialogue). i consider both to be dumbing down or condescending. Sometimes it's just poor music (I find a lot of BBC productions have poor/predictable incidental music). There most be more though...

Rob Delaney was on BBC Breakfast recently. He explains differences between UK and US humour.  For professional comedians, there's no difference. We're both as funny as each other.  On an every day level though, Brits are funnier than Yanks.

I wonder if this is why I consider New Tricks to be watchable crap,  CSI pretty much unwatchable and CSI Miami made me shout at the TV when I tried to watch it. Perhaps the American stuff really does represent relatively humourless everyday interactions and this is simply a cultural difference.

Perhaps this is the difference between Misfits and Heroes. The difference between Sheldon and Howard?