Hacker News new | past | comments | ask | show | jobs | submit login
Common Species Of Code (willa.me)
91 points by GotAnyMegadeth on Nov 11, 2013 | hide | past | favorite | 36 comments

The startup one is eerily familiar. I sometimes think the most frequent comment I write is along the lines of "TODO: Should really write a better version of this, but it works for now..."

I've even started writing those comments before I've written the function :(

ADD - Apology-Driven Development

I try to put these in my issue tracker instead of the code. Same thing though. Sometimes my issues look like the rambling ravings of a madman talking to himself. https://github.com/morganherlocker/turf/issues/41

The Enterprise code brings back awful memories of working with a framework architected by someone who had obviously read the Design Patterns book and had the take-away of "Use these as often as they can be wedged in to have a better system."

God forbid I needed to add a single boolean field to a single screen. By the time I submitted the DB change request, then changed the abstract factory interface, then the abstract factory class, then the factory interface that generated, then the factory class, then the abstract DAO interface that generated, then the abstract DAO class, then the parent DAO interface, then the parent DAO class... pauses to catch breath ...then the child DAO interface, then the child DAO class, then the controller object interface, then the controller class, then the actual page, then the unit test, it was 4AM and time to go to sleep.

Let this be a cautionary tale to any future architects out there.. remember that flexibility is a MEANS, with the ends being less time and money spent coding and maintaining code. It's not an ends to itself, and can even be a hindrance to the ends.

Closed-form expression [0], good up to n == 71:

   def fibonacci(n):
        gr = (1 + (5**0.5)) / 2 
        return int((gr**int(n) - (-gr)**int(-n)) / 5**0.5)

[0] http://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_ex...

I wanted to mention this as well. There is actually a closed form (as you indicated). Also good to know: This is or was a interview question at Google. You had to know Moivre-Binet's closed formula from the top of your head to pass.

I don't even know what is Moivre-Binet what the fuck.

> You had to know Moivre-Binet's closed formula from the top of your head to pass.

What's unfortunate about what you just said is that this is utterly irrelevant as a test of coding or problem solving ability.

See my other comment about MIT 6.00.1x

In a similar fashion, "The Evolution of a Haskell Programmer" (I'm sure there are for other langs also) http://www.willamette.edu/~fruehr/haskell/evolution.html :)

Evolution of a Python programmer:


A CS 101 student apparently writes the most readable code.

Has time to think.

It's slower than the mathematician's, and does not handle all error cases. No wonder it looks cleaner.

Except it is not correct.

I like how both the student and the Startup hacker mess up bounds-checking, except the student doesn't realize it in the initial case, and the hacker misses it as the codebase gets progressively larger and he or she gets more tired.

I don't get it, what's incorrect about the code?

Try calling the function with 0 or a negative number.

ignorance is bliss

The Large Company version is way too small. It should have at least 20 code files and is provided to other departments as a hosted service that no one has access to due to security restrictions, and the only documentation for how to use it is a powerpoint that is 3 years out of date.

Don't forget the tests of each. The startup's tests are all commented out because they failed at some point or another, the large company's tests are five times as long as the actual code they test, and all other categories consist of the stubs provided by the IDE.

Why do they all(except Hackaton) use an exponential complexity algorithm for a polynomial time problem? Linear, even.

The math phd one is constant time :)

It's not. Multiplication can't in general be done in constant time (and the exponentiation algorithm is inefficient as well). You might argue that we're working with ints here, so yes it can, but in that case all of these are constant time.

It depends on what you consider an operation, doesn't it? You can consider multiplication to be an operation, or you can count more basic operations, such as MOVs and SUMs. I think for Fibonacci, number of additions is a good measure of complexity.

> It depends on what you consider an operation, doesn't it?

Exactly. I see far too many people lately who want to point out the technically correct (a.k.a. best kind of correct) version of algorithmic complexity where e.g. multiplication is not constant time. Often this is paired with the contrasting complexity analyses having different meanings for the N in the big O notation, e.g. bit complexity of input vs. number of elements input. Note, this is not the case above, but is often what I see happen. It's very sloppy to directly compare the two as they are making wildly different assumptions on what is constant time. The model matters. An example used in a parallel computing class I took was that comparison based sorting on P parallel processors could be done in log N time (less than the Nlog(N) lower bound of comparison based sorting) in the PRAM model where memory access across parallel processors is still O(1).

The exponentiate function is in linear time, so it's O(n).

Too bad, it could be O(log n)...

I've been watching my son go through MIT 6.00.1x. He's working on the first midterm this very minute.

One of the things I've noticed is that, yes, code quality is from OK to horrid sometimes. Rather than stand behind him and correct every little detail I've let him navigate the problem sets and quizzes mostly on his own. When he gets stuck I give him a shove in the right direction and see what happens.

I've reflected on this as I've never really looked at student code. By the time I was a college student taking my first programming course I had already built a number of microprocessor boards, learned how to program them in machine language and moved on to Forth and C. I think my code was reasonably clean by that stage. Still, I know it wasn't professional grade by any measure.

It's easy to forget where we all started from and, as a result, criticize student code as naive. Well, in most cases, it is. That's a fact.

What I have now realized from watching my son learn to code as well as interacting with him through the process is that he is not learning to code. He is learning to think. He is learning about dissecting problems into small conceptual units that he can then translate into code. Sure, he is learning python in the process. However, the problem solving, solution structuring and problem representation skills that are very much necessary to be a good software developer are mostly language agnostic.

Realizing this, the fact that he is writing code that isn't the cleanest or the slickest possible solution to a problem stopped bothering me. I want him to learn how to think first.

Not to say that I've completely ignored the issue. I let him finish the assignments on his own and then we sit down and do a code review session where I point out where and how things could be better. We've had cases of a dozen lines of code replaced by just a couple. And that's OK. That's how he is going to learn.

Oh man these samples are really well done. Really hilarious.

I wrote an explicit-formula fibonacci function once. I think you lose precision before n=100. It was disappointing.

Yes but the mathematician considers the numbers under 100 to be very small fraction of the numbers under, 1000, or 10^12 or something. Or, in math language, a very small portion of the natural numbers.

As some one who rights stats libraries, I can see it now.

"Bug: code does not work with an input of 1"

"1 is of approaching-zero-significance when you consider all of the other test cases we are meeting. 1 is not the responsibility of this lib. Feel free to fork. ======= issue closed."

and the cat writes the most correct code according to most programming best practices - except for giving meaningful names to constants, variables and functions

Many years ago I remember coming across some code to page through a table. One of the variables was "STARTREK". There were a few moments of "WTF?!?", until I realized that the variable should have been named "StartRec" (allowing for the fact that language of the day was case insensitive, and only allowed 10 letter names)

The startup code is the cs101 code with two additional cases handled (and minus the contrived x==2 state), and a lot of documented thought about what could be done to improve.

I think this pretty much nailed the reality. ^^

Applications are open for YC Winter 2022

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact