Oh yes, there is a programming muse! Some people call it "being in the zone". When my muse visits, it's as if the code is writing itself... and doing it well...with meaningful comments!
My friend was skeptical, and said poets have muses. My instant response was that code can be a lot like poetry, albeit a strict form of poetry. It can certainly be elegant, to a reader who understands it's form and meaning.
He remained skeptical, but the conversation planted a seed in the back of my brain. Since then, I've noticed a pattern in my coding preferences. I crave poetic symmetry and pattern. When I have to break pattern to avoid some compiler errors, I'm uncomfortable.
The following code snippet, will not generate compiler errors (in context), but it has an ugliness to it.
Now I know this is just a matter of personal taste, but it would look more symmetrical if I could use "var" in each clause as if the clause was a function and declare that variable in each clause. That way, I could keep "var" in front of each variable. To me, dropping "var" for repeats (like p1 and p2), disrupts the rhythm of the code for me.
if(gen == 1){
// two peaks
var p1 = A.x - T.hz1;
var p2 = A.x + T.hz1;
peaks.push(p1, p2);
} else if(gen == 2){
// 4 peaks
p1 = A.x - T.hz2;
p2 = A.x + T.hz2;
var p3 = A.x + 2*T.hz1 - T.hz2;
var p4 = p3 + 2*T.hz2;
peaks.push(p1, p2, p3, p4);
} else if(gen == 3){
//8 peaks
p1 = A.x - T.hz3;
p2 = A.x + T.hz3;
p3 = A.x + 2*T.hz2 - T.hz3;
p4 = p3 + 2*T.hz3;
var p5 = A.x + 2*T.hz1 - T.hz3;
var p6 = p5 + 2*T.hz3;
var p7 = p5 + 2*T.hz2;
var p8 = p7 + 2*T.hz3;
peaks.push(p1, p2, p3, p4, p5, p6, p7, p8);
}Update: In context, the code above is called in sequence, so that the first time it's run gen=1 and the next time gen=2.
1 comments:
Programming languages and 'human' languages are both governed by rules & context in order to give meaning to the list of letters/glyphs/whatever. Playing with(in) those rules is where the poetry comes into play. Poetry in code can even come in different shapes and forms, just like with 'human' languages. It can come in the form of the actual code, the way a programmer structured the code, the elegance of coded algorithms, the beauty of a well-used conditional ternary operator, etc... Yes, I'm a geek.
But a programming language itself can also have poetic properties, just like for instance French is commonly regarded as a 'romantic' language and German isn't. To me, a good example of a programming language that has poetry built-in is Python. In Python, indentation determines scope. When I first read about Python and read those 3 words, it blew my mind. The idea is so simple, yet so beautiful and a stroke of pure genius. It instantly means code becomes easier to read, easier to identify the current scope, and structure is almost self-enforced (but with a gentle hand on your shoulder, not with a cane). The beauty also lies in that, despite the 'forced' use of indentation, Python still allows for creative use of code, it doesn't get in your way for one second. I should do more Python-coding in my spare time, it's been too long!
Post a Comment