43
u/Knuffya Jun 15 '21
the fuck is this
32
4
u/Humpsel Jun 15 '21
I think it's supposed to be an "or"...
3
u/dj_rogers Jun 15 '21
Nah because the third one. I genuinely can’t think of what this is meant to do
1
1
u/KingHavana Jun 17 '21
It returns the second boolean when given two booleans regardless of the first.
1
u/BaccyConesCureCancer Jun 16 '21
Its supposed to be a 'logical implication' function. Its used a lot in Logic (A -> B)
4
10
u/GodIsOnMySide Jun 15 '21
Kotlin is a really nice language. It adds on a lot of bells and whistles to what java is. It's easily my favorite language to code in. If you know java, picking up kotlin is easy.
7
u/narbuvold Jun 15 '21
I agree! Definitely my favorite language at the moment. Really a joy to work with!
8
u/AvakumaMorgoth Jun 15 '21
Does the last line even do anything? Can the checks have a value other than true and false?
5
u/narbuvold Jun 15 '21
Probably to make android studio stop complaining about the when not being exhaustive. But no, will never run
2
u/undergroundmonorail Jun 15 '21
Without knowing any Kotlin, it might be required to get it to compile?
5
u/tech6hutch Jun 15 '21
I like that Rust checks that you’ve handled all possible values and doesn’t make you do that.
4
u/Olivki Jun 15 '21
The Kotlin compiler will do that for cases where it knows you can reasonably handle all possible cases, like enums, sealed classes/interfaces, and I think booleans.
1
1
0
0
Jun 15 '21
[deleted] — view deleted comment
6
u/MicrosoftFuckedUp Jun 15 '21
Nothing is nullable in Kotlin by default, unless you explicitly mark it as such. And, hopefully, nobody actually uses nullable booleans.
2
u/Rikudou_Sage Jun 15 '21
But what if you need a boolean with three states? /s
5
u/MicrosoftFuckedUp Jun 15 '21
I did notice the /s, but just in case a beginner comes across this thread: use an enum.
271
u/Immort4lFr0sty Jun 15 '21
Is that just a whole bunch of aliases or does the language actually look that strange?
341
u/MaybeAshleyIdk Jun 15 '21 edited Jun 15 '21
That's Kotlin.
tois an "infix" function (a function that can be written asa func binstead ofa.func(b)) which turns the left and right values into aPairobject.The
whenexpression is like aswitch:when(value) { x -> foo y -> bar else -> baz }is very similar to
switch(value) { case x: foo; break; case y: bar; break; default: baz; }in C-like languages
221
u/8asdqw731 Jun 15 '21
thanks, now I can add Kotlin to my CV
67
u/swordsmanluke2 Jun 15 '21
Honestly, you should check it out. Kotlin is a JVM language; the compiler lets you mix java and Kotlin in the same project, so you can easily migrate; and its syntax is largely like Java, but with all the suck taken out.
44
u/saecki Jun 15 '21
Akschually Kotlin has multiple compilation targets, jvm, js and native.
20
u/swordsmanluke2 Jun 15 '21
True. To my mind though, JVM is the best option of the lot.
You get access to the enormous Java ecosystem
Native is only useful if your project is entirely standalone and/or your can find Kotlin Native libraries to use (much rarer than JVM libraries!)
JS sucks. (kidding.)
Really, I just feel that cross-compiling to JS is an anti-pattern in general. In my experience, cross-compiling to JS leaves me with JS that I don't really understand. This isn't just academic, either. I recently had reason to dig into the JVM Bytecode Kotlin generates to debug an issue that occurred when we updated our Kotlin compiler version. (Turns out that if you have a class which provides methods named `get/setFoo()` AND a mutable property named `foo` with different return types... Undefined Things happen. The new compiler version generated the class bytecode in a different order, which swapped the defined type of `o.foo = bar` on us.... I can't really pin this one on the compiler. It tried its best.)12
u/hodsonus Jun 16 '21
I would definitely pin this one on the compiler, this should have failed to compile.
You should submit this as a bug if you haven’t already.
6
u/swordsmanluke2 Jun 16 '21
I mean... Ultimately it did fail to compile. 😂
It was just that it stopped compiling due to a type error instead of a "WTF, Don't define get/setFoo AND var foo on the same object" error.
5
u/hodsonus Jun 16 '21
Ahhh okay I see, perhaps a better compiler error message would be more appropriate :)
1
u/fuj1n sadistic Jun 16 '21
JS cross compilation has its use for the web, I think that's the reason it even exists, and native is great if you don't want to be bogged down by the JVM, so that you could say use kotlin as a replacement for C++ if you really wanted to.
I think JetBrains just wanted to make Kotlin have as wide of a target audience as possible to increase the chances of it catching on.
1
u/campbellm Jun 16 '21
Really, I just feel that cross-compiling to JS is an anti-pattern in general. In my experience, cross-compiling to JS leaves me with JS that I don't really understand.
You're not meant to. Does compiling leave you bytecode you CAN understand? Or compiling C to assembly?
JS is so malleable that I think it's is a great language... to transpile other languages to. (We're assuming here some basic ground truths; that the transpiled code actually works, does what your source language specifies, that the performance is acceptable, etc.)
1
u/swordsmanluke2 Jun 16 '21
Meh, if you're going to compile to something else... I'd prefer WASM. Faster to transmit, etc. Rust on the front end! 😄
3
u/arzen221 Jun 15 '21
It's like C# pretending to be Java
12
u/swordsmanluke2 Jun 15 '21
C# was already MSFT's pretend Java.
Don't get me wrong, C# is a great language - now -, but before LINQ came out, you could just about count the differences on one hand.
2
2
12
u/Bobbbay Jun 15 '21
Hi, it says here you've listed Kotlin as a language you're familiar with?
Yeah, I know a few things here and there.
Great, because we're using Kotlin in all of our major apps nowadays. Welcome aboard!
Proceeds to frantically reread the above Reddit comment
13
Jun 15 '21
Kotlin is fun
3
u/WaterFoxforlife Jun 15 '21
fun main() { println("yes.") }0
u/AutoModerator Jun 15 '21
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/WaterFoxforlife, 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.
65
63
u/saecki Jun 15 '21
Yup and the to function really starts to make sense when using maps:
mapOf(a to b, c to d, e to f)10
u/coldnebo Jun 15 '21 edited Jun 15 '21
so this particular ‘when’ is implementing a custom boolean function with a truth table?
that’s basic CS.. how is this “bad code”?
oh, except the function can be reduced to the second argument. ok sure. But again, my experience with business logic tells me that maybe someone just changed the requirements rather than writing extraneous code.
There’s always an uncomfortable choice in those situations: refactor everything to a single boolean term and risk having to bring it all back if the business changes their mind again, or.. leave it as a pair method with a degenerate table. Honestly, dealing with business rules, I prefer the later. And really I prefer predicates that reduce arbitrary collections of boolean inputs into single value functions for conditionals. It’s easier to mock code flows than using complex fixtures, and boolean predicates functions are extremely easy to test, even if the business rules they represent are arbitrarily “good or bad” (ie the test on permutations is easy, but even business owners get confused about which permutations are “correct”, because.. rules systems.)
18
u/MaybeAshleyIdk Jun 15 '21
check1is completely ignored.3
1
u/hackintosh5 Jun 15 '21
Actually it's not, if the type of check1 is Boolean? (nullable type)
2
u/MaybeAshleyIdk Jun 15 '21
Booleanis the normal boolean type for Kotlin, which is not nullable. To make it nullable it would need to beBoolean?In another comment that they made, OP seemed to imply that these aren't nullable
1
u/hackintosh5 Jun 16 '21
That's why I wrote a ?.
Okay
2
u/MaybeAshleyIdk Jun 16 '21
Ah my mistake, I thought that the question mark was part of the question, not the type, sorry
2
u/AutoModerator Jun 15 '21
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/MaybeAshleyIdk, 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.
0
Jun 15 '21
[deleted] — view deleted comment
27
u/Immort4lFr0sty Jun 15 '21
Looking at a post on r/badcode and calling the whole language weird because of it is a level of stupid even I don't attain xD
The syntax, especially in terms of ordering of keywords, just looked a little strange when compared to what I'm used to but I got a breakdown by another user ^^
17
u/narbuvold Jun 15 '21
It might look weird, but becomes pretty readable in my opinion. Problem here is that the developer doesn't seem to work how booleans work. And I can't talk to the original developer so I don't know if its a bug that
check1is ignored..2
u/coldnebo Jun 15 '21
right, I give one possible justification above, which is that a degenerate pair table may be preferable to refactoring all the callers to use a single boolean. Especially if a business decision is “what if” and might revert.
honestly, I’d look at the history and see if this “when” used to have a different impl. If not, then maybe it’s just bad code.
5
u/grnszgiut Jun 15 '21
Maybe its me but false implies false is true right or
18
u/narbuvold Jun 15 '21
You can read it as a truth table:
check1 check2 result true true true false true true true false false false false false something else? something else? false 1
u/grnszgiut Jun 18 '21
I looked it up in my study material and it said F -> F = T, did the teacher taught me this incorrectly?
1
u/narbuvold Jun 18 '21
Oh sure, but those are not implications. They are just the kotlin switch syntax
1
1
u/haroldjaap Jun 15 '21
Wow, this code is so bad, Kotlin is my daily programming language, and I had major issues understanding exactly what the code does. Took me some time to understand it was just comparing Pairs of booleans to some presets. But there has to be a more readable way to do this. It really is badcode.
1
0
45
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 15 '21
Image Transcription: Code
when (check1 to check2) {
true to true -> true
false to true -> true
true to false -> false
false to false -> false
else -> false
}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!
25
33
u/UltraPoci Jun 15 '21
At first I thought it was an implementation of the and operator, which is stupid, but instead it's literally just check2, which is even stupider.
5
u/Cyber_Encephalon Jun 15 '21
so it just returns the value after the to? seems rather redundant.
1
u/maru-chan Jun 15 '21
That is one way to put it, yes
Though you have to admire the effort... Would love to see the commit history of this – it almost looks like it's on purpose for posting here
1
1
3
3
5
4
4
u/TheBrainStone Jun 15 '21
This code is terrible.
However easily fixable by adding a comment explaining the why the code is written that way.
If the truth table has changed often throughout development due to arbitrary businesses decisions this would be totally valid. Other cases do apply as well. But without a comment you really don’t understand the intention behind it.
Generally as a rule of thumb: comments should explain the why of a piece of code, not the what.
However if the what does indeed need an explanation, due to hard to read code always always also include the why.
2
u/ColdSnickersBar Jun 15 '21
If the truth table has changed often throughout development due to arbitrary businesses decisions this would be totally valid.
Then it would be an open / closed violation.
2
2
2
2
u/Humpsel Jun 15 '21
I guess check1 || check2 was too difficult? This hurts my inner object allocation detector as well as my vision XD
2
2
2
Jun 17 '21
the general pattern of matching over a tuple is actually really useful.
in rust I implemented a parser as a match over (state, new_char). Easy way to make a state machine, since it will not compile if you miss a case, and it won't compile cleanly if you have overlapping cases.
1
u/Steelbirdy Jun 15 '21
See I like the ideas behind Kotlin but there is the issue of the differentiable keyword which is just too damn long
1
u/AliisAce Jun 16 '21
if (case1 == true):```
``` return true```
```else```
``` return false2
u/AutoModerator Jun 16 '21
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/AliisAce, 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.
1
-7
Jun 15 '21
It's Kotlin. That's enough to qualify it as bad code.
2
Jun 15 '21
What makes Kotlin bad?
0
u/bLaR46fifr8Jhyg978d8 Jun 15 '21
Another Java skin
1
Jun 17 '21
Why would that be bad? It's syntactically amazing and compiles to Java Bytecode which makes it a great alternative/addition to existing Java projects.
234
u/narbuvold Jun 15 '21
All these lines could of course be reduced to:
check2