Idk I use them for stuff that can be true, false or undefined (mostly in the form of Option[Boolean]-like constructs in functional programming languages)
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/sschr15, 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.
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.
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.
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.
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.)
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.
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.)
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.
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.)
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.
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 ^^
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 check1 is ignored..
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.
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.
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.
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.
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.
234
u/narbuvold Jun 15 '21
All these lines could of course be reduced to:
check243
u/Knuffya Jun 15 '21
the fuck is this
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.
8
u/AvakumaMorgoth Jun 15 '21
Does the last line even do anything? Can the checks have a value other than true and false?
271
u/Immort4lFr0sty Jun 15 '21
Is that just a whole bunch of aliases or does the language actually look that strange?
5
u/grnszgiut Jun 15 '21
Maybe its me but false implies false is true right or
45
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 15 '21
Image Transcription: Code
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!
33
u/UltraPoci Jun 15 '21
At first I thought it was an implementation of the
andoperator, which is stupid, but instead it's literally justcheck2, 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/permanentscrewdriver Jun 15 '21
And...
1
u/SirNapkin1334 Aug 26 '21
Ah yes, the "input number two" logic gate.
3
u/dfmaaa1 Jun 15 '21
Visibly confused*
3
u/maru-chan Jun 15 '21
BWAHAHA that
else -> falsecompletely obliterated me5
u/Not_Sugden Jun 15 '21
what the hell does this even do?!
4
u/brosiedon169 Jun 15 '21
He speak da true true
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/[deleted] Jun 15 '21
this feels like the og assert function
2
u/appeiroon Jun 15 '21
This is top tier code guys stop being jealous
2
u/Tomekske Jun 15 '21
I don't get it
2
u/Humpsel Jun 15 '21
I guess
check1 || check2was too difficult? This hurts my inner object allocation detector as well as my vision XD2
u/[deleted] Jun 15 '21
E L S E
2
u/Voltra_Neo Jun 16 '21
mfw reduces down to
!!check22
u/[deleted] 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
differentiablekeyword which is just too damn long1
u/AliisAce Jun 16 '21
1
u/HerissonMignion Mar 23 '22
Just return check2
-7
u/[deleted] Jun 15 '21
It's Kotlin. That's enough to qualify it as bad code.