r/badcode • • Sep 13 '20

c Found in git's bisect algorithm

Post image
744 Upvotes

17 comments sorted by

246

u/slothefish Sep 14 '20

Is this in fact good code, because they explicitly call out that it's not worth investing the time to make it good, and that decision of time investment is good?

142

u/Catfrogdog2 Sep 14 '20

Yes: perfection is the enemy of good enough.

18

u/Talran #ksh Sep 14 '20

I've been down enough week-long holes to realize that I think..... right after I tweak this shell script to not terminate sessions that started at the same time on a leap day by the same user.

10

u/OOPGeiger Sep 14 '20

Relatable

69

u/[deleted] Sep 14 '20

What makes it bad? Because they say the algorithm is stupid?

30

u/blehmann1 depraved Sep 14 '20

Well it has to be don't it? There's nothing but the comment and the method signature, and the method signature looks fine to me.

The static is the only thing worth noting because it looks like C++, and in C++ you probably shouldn't use raw pointers. But the keyword does actually have a meaning (although a little obscure) in C. Plus I think Linus Torvalds is pretty damn negative on any C++ code making its way into git.

64

u/aismallard Sep 14 '20

It's C, static just means the function is private to the object file. It's common and not a big deal.

The "bad code" aspect here is the comment admitting they use lazy implementation because it's only used for bisecting.

12

u/sunflsks Sep 14 '20

Git doesn’t have any C++ I think. It looks like it’s a whole function that was just cropped out

8

u/CanadianJesus Sep 14 '20

Raw pointers is a bit like raw dogging, if you know what you're fucking around with it's not that dangerous.

86

u/tobyjwebb Sep 13 '20

Quick, someone run git blame, before it gets removed!

15

u/metabun Sep 14 '20

No hurry, we can just run git bisect to find it again.

25

u/caldazar24 Sep 14 '20

I doubt the actual function would belong here. First, what qualifies as “truly stupid” by the standards of the git codebase is probably far better than anything posted here. Second, from the comment I assume they think it’s stupid for performance reasons, but using a naive algorithm (that’s probably easier to follow along when reading than a more optimal one) in an area where you don’t care about performance is good code, especially if the decision is being made very deliberately!

20

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Sep 14 '20

Image Transcription: Code


/*
 * This is a truly stupid algorithm, but it's only
 * used for bisection, and we just don't care enough.
 *
 * We care just barely enough to avoid recursing for
 * non-merge entries.
 */
static int count_distance(struct commit_list *entry)

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

7

u/aharmonicminor sadistic Sep 14 '20

good human :)

9

u/_PM_ME_PANGOLINS_ Sep 14 '20

That’s not code though