What is this 'modulus' that you speak of?

๐ŸŽ™๏ธ Okkero ยท 205 points ยท Posted at 07:28:46 on March 2, 2016 ยท (Permalink)


What? Modulus? Wuzzat?

public boolean isDivisibleByNine(double t) {
      double answer = 0.0D;
      answer = t / 9.0D;
      String answerString = answer;
      if (answerString.contains(".")) {
          return false;
      }
      return true;
  }
wonderb0lt ยท 45 points ยท Posted at 10:11:52 on March 2, 2016 ยท (Permalink)

Which language is this and why does it allow an implicit cast from double to string?

Moogra2u ยท 26 points ยท Posted at 16:44:29 on March 2, 2016 ยท (Permalink)

The pastebin is labeled Java and this looks like Java syntax and naming conventions so probably Java.

Seriously bad code though! This won't compile and will not work even if it did since something like 9.0/9 = 1.0 which will contain "." making it fail.

hey_aaapple ยท 4 points ยท Posted at 10:37:45 on March 2, 2016 ยท (Permalink)

from double to string

PHP?

mantis8 ยท 25 points ยท Posted at 13:49:35 on March 2, 2016 ยท (Permalink)

Not PHP. Dead giveaway is the lack of the dollar sign in variable names.

hey_aaapple ยท -1 points ยท Posted at 14:36:47 on March 2, 2016 ยท (Permalink)

I was joking about PHP's implicit conversion rules

EccentricalAce ยท -7 points ยท Posted at 10:37:47 on March 2, 2016 ยท (Permalink)

This is Java, and because Java.

[deleted] ยท 11 points ยท Posted at 12:47:00 on March 2, 2016 ยท (Permalink)*

[deleted]

battery_go ยท -2 points ยท Posted at 14:27:06 on March 2, 2016 ยท (Permalink)
 double t = 0.9;
 String s = t + "";

Surely, that will work.

five_hammers_hamming ยท 16 points ยท Posted at 14:44:45 on March 2, 2016 ยท (Permalink)
double thing;
String answerString = thing;`

does not work in Java. That's all that matters.

battery_go ยท -1 points ยท Posted at 16:08:58 on March 2, 2016 ยท (Permalink)

Oh I know that, but that wasn't what I wrote... I think might've missed the point.

Fs0i ยท 4 points ยท Posted at 18:22:56 on March 2, 2016 ยท (Permalink)

Yeah, but that is the code in the OP. You have missed the point.

battery_go ยท 0 points ยท Posted at 19:07:28 on March 2, 2016 ยท (Permalink)

Gotcha buddy!

TheOldTubaroo ยท 2 points ยท Posted at 18:06:10 on March 2, 2016 ยท (Permalink)

The correct way would probably be

 double t = 0.9;
 String s = Double.toString(t);
Jack126Guy ยท 1 points ยท Posted at 13:23:25 on March 2, 2016 ยท (Permalink)

It certainly looks like Java.

fj2010 ยท 30 points ยท Posted at 09:02:13 on March 2, 2016 ยท (Permalink)

Everyone knows the best way to test for divisibility by nine is to add all the digits together.

mothzilla ยท 6 points ยท Posted at 20:19:11 on March 3, 2016 ยท (Permalink)

and then divide by nine.

plahcinski ยท 23 points ยท Posted at 20:12:19 on March 2, 2016 ยท (Permalink)
>>> import sys
>>> def isDivisibleByNine(n):
...    return n in [x*9 for x in xrange(sys.maxint)]
...
>>> isDivisibleByNine(9)

I fixed it!

SuitableDragonfly ยท 6 points ยท Posted at 02:55:35 on March 4, 2016 ยท (Permalink)

That's physically painful

blue_pixel ยท 7 points ยท Posted at 23:17:12 on March 6, 2016 ยท (Permalink)

Yep, that function name doesn't conform to PEP8 standards.

SuitableDragonfly ยท 5 points ยท Posted at 23:44:05 on March 6, 2016 ยท (Permalink)

Man, fuck PEP8. Underscores are so ugly.

thegigaraptor ยท 16 points ยท Posted at 14:06:22 on March 2, 2016 ยท (Permalink)

I hate when conditionals are wrapped in if statements, only to return true || false

[deleted] ยท 19 points ยท Posted at 15:38:38 on March 2, 2016 ยท (Permalink)*

[deleted]

Decker108 ยท 13 points ยท Posted at 19:34:49 on March 2, 2016 ยท (Permalink)
return ((Boolean)!!(condition == true) ? true : false).booleanValue();
thegigaraptor ยท 0 points ยท Posted at 17:45:15 on March 2, 2016 ยท (Permalink)*

Or "return condition"

Edit: to clear the confusion, I find this acceptable, not a bad way of returning a boolean

mjcov ยท 5 points ยท Posted at 17:50:24 on March 2, 2016 ยท (Permalink)

Yeah no it was an example of another thing I hate. Well actually 2 things - using ( spaced parens ) especially around stuff that doesn't need them, and returning true/false from a ternary.

In JS I'd so return !!condition. Since I'm writing PHP right now at work I'll say in PHP while return !!$foo; does work, it's more explicit to do return (bool) $foo;.

thegigaraptor ยท 2 points ยท Posted at 01:53:58 on March 3, 2016 ยท (Permalink)

I don't know you, but I like you

794613825 ยท 1 points ยท Posted at 20:59:55 on March 2, 2016 ยท (Permalink)

What's wrong with that?

thegigaraptor ยท 1 points ยท Posted at 01:55:20 on March 3, 2016 ยท (Permalink)

Assuming the condition is a boolean, you might as well return that.

794613825 ยท 4 points ยท Posted at 02:03:33 on March 3, 2016 ยท (Permalink)

That's what I mean. The way you phrased it, it sounded like that was bad too.

fghjconner ยท 3 points ยท Posted at 18:53:22 on March 3, 2016 ยท (Permalink)

I just had an extended argument with someone over at /r/programmerhumor who thinks that is acceptable code.

thegigaraptor ยท 5 points ยท Posted at 20:48:22 on March 3, 2016 ยท (Permalink)

Like the saying goes, "I learned long ago, never to wrestle with a pig. You get dirty, and besides, the pig likes it."

Svorax ยท 1 points ยท Posted at 18:45:16 on March 4, 2016 ยท (Permalink)

As far as execution goes, it's my understanding the compiler will optimize it for you automatically. That said, shitty readability; I agree.

LEmp_Evrey ยท 8 points ยท Posted at 19:22:47 on March 2, 2016 ยท (Permalink)

Yipp, definitely bad code. Real enterprise code would make the 9 a second parameter, so that this function works for multiple modulo tests.

beigeacid ยท 5 points ยท Posted at 19:58:32 on March 2, 2016 ยท (Permalink)

And this just checks if it's divisible by 9. How many other similar functions exist for all the other numbers?

TrainFan ยท 3 points ยท Posted at 18:40:55 on March 5, 2016 ยท (Permalink)

You could probably generate them.

SquidgyTheWhale ยท 1 points ยท Posted at 21:20:11 on March 2, 2016 ยท (Permalink)

Everyone knows you should just subtract nine until you get zero or negative.

__i_dont_know ยท 1 points ยท Posted at 22:10:15 on March 2, 2016 ยท (Permalink)

Doesn't even compile answer needs to be casted to String.

Dwarfgo ยท 1 points ยท Posted at 07:00:14 on March 8, 2016 ยท (Permalink)

It's like a magic!