ProgrammerHumor

checkOutMyCode

checkOutMyCode
https://i.redd.it/vch87332zqdf1.jpeg
Reddit

Discussion

InsideBSI
:c::cp::cs::js::ts::py:

java code that looks like python yeah, nice

20 hours ago
legendLC

Must add this comment:

// Do not reformat the braces {}. It breaks the production.

15 hours ago
RiceBroad4552
:s:

"Java" code (or actually JVM code) that looks like Python would be more this here:

object Permuter:
   private def permute(n: Int, a: Array[Char]) =
      if n == 0 then
         println(String.valueOf(a))
      else
         for i <- Range(0, n+1) do
            permute(n-1, a)
            swap(a, if n % 2 == 0 then i else 0, n)
   private def swap(a: Array[Char], i: Int, j: Int) =
      val saved = a(i)
      a(i) = a(j)
      a(j) = saved

That's Scala, a kind of Python + Haskell for the JVM.

This code wouldn't be useful of course for obvious reasons: The static methods (methods on an object in Scala) are private, so you can't call them from the outside…

Also the Range(0, n+1) expression isn't idiomatic Scala. You would usually use the 0 to n syntax sugar instead. But the Range looks more like Python so I've written it like that.

Beside that, that's anyway not idiomatic Scala as it uses mutable values and imperative loops. Also Array is just the "naked" Java Array, and not a Scala collection, and one does usually not use Array directly. Actually also not in Java!

Writing that above code makes also no sense in general as Scala comes with a permutations method on collections (and Array through extension methods).

But syntactical the code above is pretty close to Python, imho.

8 hours ago
No_Definition2246

It is now looking more like ruby lol :D

7 hours ago
DigitalJedi850
:cs:

I hate that my brain made me sort out what this does…

20 hours ago
jungle

Funny how all (or most) comments are about the formatting and not the horrific implementation of permute. I can't even figure out if it works.

16 hours ago
suskio4
:c::cp::py::asm::lua:

This is why its so good at permuting

16 hours ago
SinsOfTheAether

It's the permuta triangle algorithm

11 hours ago
Anaxamander57
:py::rust:

Depression is iteratively calling a recursive algorithm inside of itself.

11 hours ago
rruusu

It does nothing, as that class only has two methods and both are private. (The closing brace for the class is at the end of the last line.)

Whatever its permute method would do, if anyone were allowed to call it, it would have a time and console output complexity of O((n+1)!) (factorial time), unless n > a.length - 1, in which case it'll throw an ArrayIndexOutOfBoundsException.

Edit: Off by one in the time complexity.

15 hours ago
SovereignPhobia

Doesn't it also just not have a termination case? The case presented is a print and not a return.

9 hours ago
rruusu

The recursion is in the else clause, so it does eventually terminate. Also, for negative values of n, the for loop makes zero iterations.

7 hours ago
SovereignPhobia

Oh, that's awful.

6 hours ago
hawkwolfe

I’m responding after your edit and if it was to edit your time complexity to add the “+1”, that’s unnecessary. Big O notation is concerned with the asymptotic growth of the function relative to n, and as n approaches infinity the difference in the function output due to any constant factor approaches 0.

9 hours ago
rruusu

That’s what I thought initially, but (n+1)!/n! tends to n+1 as n tends to infinity, so it's not a constant factor. Instead of the +1, it should perhaps rather be expressed as O(n(n!)) to be more idiomatic.

7 hours ago
sammy-taylor
:js::elixir-vertical_4::cp:

I was like “what friggin language is this” until I saw the horrifying brackets 🤢

20 hours ago
Ahazveroz

Jathon? Pyva?

20 hours ago
NobodyPrime8

is this the "Jason" every web developer seems to be so obsessed over?

20 hours ago
ohmywtff

Jython probably, since there's the cython

16 hours ago
RiceBroad4552
:s:

Jython is already taken…

https://www.jython.org/

8 hours ago
anotherbutterflyacc

I was like “is this python…? I’m so confused” and then saw the brackets and physically startled lol

20 hours ago
Boris-Lip

IRL depression doesn't really look like anything, though

20 hours ago
TooSoonForThePelle

True. I should post a selfie.

20 hours ago
giantrhino

I was so confused until I finally looked in the right margin.

Kill it. Kill it with fire.

19 hours ago
SPAMTON____G_SPAMTON
:unity:

This isn't depression. This is heresy.

19 hours ago
ghstber

Brother, get the flamer... the heavy flamer. 

18 hours ago
spac1al

kris…this code is not a [BIG SHOT]

9 hours ago
giantrhino

Tfw you find out you’re gonna have to put your friend down.

19 hours ago
Deep__sip
:js::j::c:

This is autism

20 hours ago
EldritchEne

I- really can't tell if I love or hate this

20 hours ago
ZunoJ
:cs: :asm: :c:

Aside from the obvious, why are the methods static and not extension methods? Or just injected as a singleton?

18 hours ago
SKabanov

Everything in JVM-based languages needs to be encased within a class, even if you just need to define a collection of pure functions. Kotlin allows you to create "classless" files in which you define these pure functions, but that's ultimately syntactic sugar.

10 hours ago
ZunoJ
:cs: :asm: :c:

Sure but static functions like this are an anti pattern. It bypasses DI and makes the code less testable

9 hours ago
SKabanov

Pure static functions are not per se an anti-pattern, and forcing everything into classes for the sake of DI and testability can be just as much of an anti-pattern itself. That being said, I'll admit that I misread the class and that the actual issue is that the functions aren't pure: it's got a stealth dependency of a PrintStream instance where it's printing out the permutation result. The class should be rewritten to include a PrintStream member field that gets used in the System.out.println() call, and the functions would then become instance functions instead of static - there's the DI that you'd want.

9 hours ago
RiceBroad4552
:s:

Because Java still doesn't have extension methods…

The singleton code would be awful complex, especially if it needs to be thread safe.

8 hours ago
ZunoJ
:cs: :asm: :c:

Didn't know Java doesn't have extension methods. How would it be more complex to make this thread safe in a di context than it would be with this static method. I mean it isn't thread safe right now

7 hours ago
RiceBroad4552
:s:

I've just meant that a fully written out singleton, which is thread safe, is quite some code in Java. You asked about injected singletons.

A static class is not a singleton. You can't inject different static classes by DI. Using static stuff means it's hard coded to the concrete type name.

And yes, this doesn't look thread safe in the current state…

52 minutes ago
SuitableDragonfly
:cp:py:clj:g:

No, I think that's the manic stage.

19 hours ago
Corelianer

We write code in a readable form for humans not machines.

18 hours ago
Elijah629YT-Real
:ts::js::c::cp::cs::rust:

It’s not even good code

17 hours ago
IceColdFresh

Variable width font coders BTFO

20 hours ago
RiceBroad4552
:s:

I'm not sure what you mean.

The chars align perfectly in columns, so this isn't a variable with font used for that code.

8 hours ago
baltimooree

The RHS is killing me bro 😶

18 hours ago
drsimonz
:py::cp::cs::re::ts:

Anything other than 1TBS is mental illness, change my mind.

18 hours ago
RiceBroad4552
:s:

What's "1TBS"?

8 hours ago
drsimonz
:py::cp::cs::re::ts:

There's a decent explanation on wikipedia.

1 hour ago
Sad_Welcome3776

LOL the code segment is the most accurate depiction of how my brain feels on a daily basis 😂 #ProgrammerLife

18 hours ago
Etheo
:bash::py::table_flip::redditplatinum:

Hi police? I'd like to report an assault.

ON MY EYES

17 hours ago
Debopam77

This isn't depression, it's a cry for help.

17 hours ago
RiceBroad4552
:s:

Indeed! At the moment someone makes you use some language that requires useless syntax noise like braces and semicolons one starts to cry for help, that's true.

8 hours ago
WinkAndWithdrawn

Lmao, both are accurate, but that Java code hits a bit too close to home! Anyone else debugging till 3 AM feeling like a part of their soul is being permuted? 😅

17 hours ago
Ineeddramainmylife13

Ugh reminds me of the time I accidentally took the harder coding class that was required. One of the worst classes ever (teacher sucked)

17 hours ago
sakkara

When a python dev tries have for the first time :D

16 hours ago
korneev123123
:py:

Most of my programming experience is python, so I never understood what is wrong until comment section. My only guess was "Java surely must have built-in function for that, like itertools.permutations in python. Maybe it's the joke, that depressed person rewrites library functions"

11 hours ago
RiceBroad4552
:s:

Java surely must have built-in function for that, like itertools.permutations in python

LOL, no. That's Java.

You do such things in Java like so:

https://www.baeldung.com/java-array-permutations 😂

The language you're looking for is Scala (see my other comment).

8 hours ago
milboldi

I had my C++ segfault in the inplementation of GTest on a random ass move. We debugged it for 3 days, than we came to the conclusion, that my linux distro is fucked, and the problem isn't in the code.

11 hours ago
RiceBroad4552
:s:

This sounds scary!

Which Linux distri was this, and what exactly was fucked?

8 hours ago
P-h-a-n-t-a

Took me a while to see wtf

10 hours ago
AibofobicRacecar6996

That's not depression, that's insanity

8 hours ago
Still_Explorer

You wanted a python job but got hired for java.

Gotta make it work somehow... 🤙

8 hours ago
billybobsdickhole

Set up default code format.

Autoformat on Save.

Save.

Problem Fixed.

8 hours ago
joxay

Tru depression is if you have spent two days debugging this shit before having to ask someone to help you

6 hours ago
meolla_reio

Depression? Homicide more like.

3 hours ago
Subject_Try_5973

I will find you and I will k1ll you! 😂

3 hours ago
AStripe

So nobody remembers c++?

17 hours ago
Select_Blackberry543

Yep, that make my readability drop to -1

17 hours ago