Media Molecule PMoly

Items tagged with 'Tips'

c trick

2010January 27th

I love indexing constant strings, I’m sad like that.

for example,

char c=”0123456789abcdef”[b&15];

converts the low nybble of byte b into its hex character.

oldie, but goldie.

Posted by: Alex

Categories: Code
Tags: c tips

warning signs

2010January 25th

when I’m coding, sometimes I get warning signs that I haven’t understood the problem fully. Normally, they take the form of gross looking code - ‘-1’ or ‘+1’ hanging round in loop limits, even worse, seemingly arbitrary constants, large amounts of unavoidable pointer dereferencing,…

nearlly always this is a sign that either I’m doing it wrong, or I got the data structure wrong. It is ALWAYS better to stop and rethink.

as I’ve got more experienced coding, I’ve continually lowered my tolerance to these warning signs, and it always helps. even if I find myself doing something fairly innocuous, like a nested loop, or if I find an unexpected number of edge cases I need to ‘code around’, I now stop and rethink. I don’t always get the right answer… but I’ve never regretted that mental check.

tl;dr version: develop your spidey coder sense for anything gross, and listen to it. it’s like the human form of -Wall, and it’s always worth it.