r/badcode • • Nov 06 '20

c# I wrote that . . .

Post image
779 Upvotes

27 comments sorted by

73

u/Gortix Nov 07 '20

Isn't c0 incorrect, with the inner method being min instead of max?

This way you're not always gonna get the max value

10

u/LoUnDeRR Nov 07 '20

Oh yea haha. I didn't see that when copy & pasting the previous one. <3

20

u/jeanfrancis Nov 07 '20

Came here the say this (and apparently I said it twice while you were typing your comment πŸ˜‚).

6

u/JJohny394 Nov 07 '20

Obviously there are no good unit tests...

27

u/CW_Waster Nov 06 '20

But what does it do?

30

u/QuantumEternity99 Nov 07 '20

Looks like it just finds the middle value between 3 values. (At least as long as the top is the same result as the bottom)

OP finds the min and max values, so if the min was β€œa” and max was β€œc”, then
b0 = a + b + c - a0 - c0 = a + b + c - a - c = b

18

u/jeanfrancis Nov 07 '20

Not exactly, with the current code, if a < b < c, then:

a0 = min(a, min(b, c)) = min(a, b) = a c0 = max(a, min(b, c)) = max(a, b) = b

b0 = a + b + c - a0 - c0 = a + b + c - a - b = c

But looking at the code in the bottom, I think that the second line is wrong and should use a max instead of a min.

Then your answer would be corrected, and a0, b0 and c0 would be the sorted values of a, b, c.

5

u/QuantumEternity99 Nov 07 '20

Ah right, I must’ve glanced over that but and my mind filled in the details. Good catch!

7

u/jeanfrancis Nov 07 '20

If the second line would be b0 = max(a, max(b, c)) (I think there is a typo!), then the code would be a sorting algorithm for three numbers, where the original values are in a, b, c and the sorted values are in a0, b0, c0.

8

u/SenseiSharkTV Nov 07 '20

Some men aren't looking for anything logical

Some men just want to watch the world burn - Alfred "The Dark Knight"

7

u/SickMoonDoe Nov 07 '20
return 
( ( ( ( a << __builtin_clz( b ) ) && ( a >> __builtinctz( c ) ) ) ||
     ( ( a << __builtin_clz( c ) ) && ( a >> __builtinctz( b ) ) ) 
   ) * a ) +
( ( ( ( b << __builtin_clz( a ) ) && ( b >> __builtinctz( c ) ) ) ||
     ( ( b << __builtin_clz( c ) ) && ( b >> __builtinctz( a ) ) ) 
   ) * b ) +
( ( ( ( c << __builtin_clz( a ) ) && ( c >> __builtinctz( b ) ) ) ||
    ( ( c << __builtin_clz( b ) ) && ( c >> __builtinctz( a ) ) )
   ) * c );

Fight me.

0

u/AutoModerator Nov 07 '20

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/SickMoonDoe, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

5

u/divinealien Nov 07 '20

thank you for writing better solution.

3

u/lunareffect Nov 07 '20

Yes, I wish this was a mandatory format for this sub. Not just posting bad code, but either explaining why it is bad or offering a better approach.

1

u/[deleted] Nov 07 '20

Just curious, doesn't C# support something like a0 = Math.Min(a, b, c); ?

3

u/LoUnDeRR Nov 07 '20

Sadly no. It takes only 2 arguments otherwise it can't compile

1

u/[deleted] Nov 07 '20

that's a shame! I recently used a language that did support it but don't remember which, maybe python

0

u/[deleted] Nov 07 '20

[deleted]

2

u/Keroths Nov 07 '20

that's really not something you would do.

20

u/O1dmanwinter Nov 07 '20

u/Keroths Why wouldn't you do this?

To me it's easier to follow /expand to handle more number (and less error prone - the pic even has a logic error) at the potential expense of performance?

I could be missing something though and this is a genuine question πŸ˜‚

7

u/NoahTheDuke Nov 07 '20

It’s what I would do. Any other method is too error-prone as seen by all of the comments pointing out bugs in both impls.

6

u/stogle1 Nov 07 '20

You wouldn't do this if performance is important because sorting an array usually takes O(n log n) time and you can find the median in O(n) time. https://en.wikipedia.org/wiki/Selection_algorithm

8

u/AngriestSCV Nov 07 '20

Dude. N is 3. Theoretical complexity does not matter here.

Additionally the developer time saved would be worth it in many cases anyway unless you have actually benchmarked and this method is an issue.

1

u/stogle1 Nov 07 '20

Fairy points dude. Instead of saying "if performance is important" I should have said something like "in a real-world scenario where N is large and the benefits of performance out-weigh the cost of implementation and maintenance."

Parent was looking for a legitimate reason why you wouldn't use sort for this and I was trying to provide one. I guess I didn't, but I'm assuming that the smart people that invented faster algorithms to solve this problem must have had some.

1

u/O1dmanwinter Nov 19 '20

Sorry that this is a bit of a slow reply but the points about there being a better algorithm are exactly what I was looking for.

I understood that for the example it's probably wouldn't matter as its very unlikely I would need a function to find the middle value for three numbers but in a more generic world N might be large.

As with all things it would probably be done with the array sort until it actually became an issue and even then it would probably use an already optimised library to solve the issue when it did become a problem πŸ˜‚

Thanks all for your comments

2

u/wikipedia_text_bot Nov 07 '20

Selection Algorithm

In computer science, a selection algorithm is an algorithm for finding the kth smallest number in a list or array; such a number is called the kth order statistic. This includes the cases of finding the minimum, maximum, and median elements. There are O(n)-time (worst-case linear time) selection algorithms, and sublinear performance is possible for structured data; in the extreme, O(1) for an array of sorted data.

2

u/Molehole Nov 07 '20

The solution might seem a little "hacky" and not straightforward. Codes job is usually to explain itself and the simplest code is usually the best.

However if you move this array sort thing into a function that is named approproately I think it's fine.

0

u/[deleted] Nov 07 '20

[deleted]

2

u/Molehole Nov 07 '20

Well the OPs code is plain terrible. That definitely isn't what I'd suggest. There really isn't a simple way to express the problem though so array sort may be the best way.