back

by jasonpeacock·10y ago·view on hn ↗
My manager (a long-time-ago former dev) once told me:

"I can write C in any language."

And it's true. He could write beautiful C code in Perl, Java, Ruby, etc - whichever language the current project was using - when he needed to hack something (usually gathering data for business metrics).

3 comments
We used that quote as a joke for not learning new languages well enough. Do you mean that he would write C and link it to each implementation?
He'd just stick to the basics and write C-style code in the given language, he had no knowledge of any of the language idioms and didn't need them if he kept his code simple enough.

Basically only using if/for/while and function/return constructs.

It was strange to open a Perl script and see "C" code, with single-letter variables (declared at the top), etc.

Honestly, writing perl in "C" style is really a good idea. At least if not pushed to extremes. It avoids a lot of unreadability and confusion.

Other things I think make for good perl style:

* Never use unless, especially as the after-statement conditional and/or with a negation. (I once spent a whole day ripping my hair out to figure out that an unless with a negated clause that was itself ambiguously named did the exact opposite of what it seemed to. Think [...] unless not $unregulated;)

* Do use foreach wherever possible.

* Keep reference usage to where it's really needed.

* When in doubt, don't write new OOP code. (The benefit to the caller has to be much greater than the extra complexity in implementation.)

* Lists are the core data-structure and regex are first class construct.

* Avoid the use of implicit $_/@_.

* Avoid the use of explicit $_.

* Use map / grep / sort and similar in pipelines but don't nest too deep on a given line.

> He'd just stick to the basics and write C-style code in the given language

I want to see how he writes C style in Haskell.

In some languages (e.g., R), the performance penalty for such code is enormous.
Yes and no. While typically vectorised code will run faster than a loop (because its a loop in C) i.e. rowSums is faster than a typical apply call, there actually isn't that much difference between a for loop over columns to sum and apply over columns to sum.

The real performance penalty is growing objects as you go. If you preallocate a list or vector of the appropriate length, then there typically isn't much difference.

That being said, you'll pry lapply(x, fun) from my cold dead hands (lapply(x, function (x) g(f(x)) is even better :) )

I have a hard time doing pointer-arithmetic, arbitrary memory writes upon malicious data, and unchecked macros in languages designed for safe programming. :P
The original said "FORTRAN in any language" (http://web.mit.edu/humor/Computers/real.programmers)