https://kotlinlang.org logo
Title
k

karelpeeters

09/27/2019, 1:01 PM
I feel like a broken record but just use if!
☝️ 4
:thread-please: 1
💯 14
4
r

Ruckus

09/27/2019, 2:00 PM
But, but... that's imperative 😱 How dare you even suggest such a thing when there is an over-engineered, needlessly complicated functional alternative.
❤️ 7
😆 4
k

karelpeeters

09/27/2019, 2:06 PM
I have to admit I sometimes find myself getting stuck in a mindset like that, sometimes you need to snap out of it 😒imple_smile:
m

Matteo Mirk

09/27/2019, 3:04 PM
this story reminds me of when programmers in Java want to use
Optional
everywhere, even if a plain old if/else is more readable
l

Lou Morda

09/27/2019, 4:26 PM
me everytime i try to read a line of code "val x question mark colon, wait thats an elvis, val x elvis.." googles elvis 😂
t

tDnamein

09/27/2019, 7:34 PM
@Ruckus Using an if-else expression is not an imperative style of programming. In fact it's pretty functional . Using an if-statement is imperative. That being said: I totally agree otherwise: an if-else expression is simply the better solution.
r

Ruckus

09/27/2019, 7:43 PM
No, it isn't "pretty functional". It's basic flow control (part of any language, regardless of paradigm). It's quite common to refer to any programming concepts not championed by a specific paradigm as imperative, even if it's not technically accurate.
t

tDnamein

09/27/2019, 9:06 PM
The short version: Using "if-else" as an expression is pretty functional in that sense that you hardly will find it in imperative languages. The longer version: This
if(true)
   println("true")
else
   println("false")
would be an imperative style of programming in that sense that you won't find this in "pure functional" languages (most likely not even in those who are solely "functional" but not "pure functional") On the other hand this:
val result: String =
    if(true)
       "TRUE"
   else
       "FALSE"
is a functional style of programming in a sense that you will find this in functional languages (even in those, which call themselves "pure functional" And while you will find an "if-else" in any language (at least in any that I one of) you have to differentiate if it can be used as a statement or can only be used as an expression. In the first case that language is most likely not solely functional (although it may be a multi-paradigm one). In the second case it is most likely a solely functional language That's why your claim basic-control flow can be found in any language and because it is so, it's common to call it imperative is just not correct- as (shortly) explained above there is a huge difference between an "if-else" in Java and an "if-else" in - let's say - Elm. Even when on the surface both look similar there are not the same. Additionally: Despite reading blogs, or chats like this one about Java-Programming, Kotlin-programming or programming in general all the time since 11 years, it's the first time for me hearing that language features common to all languages can be called imperative. Furthermore: Such usage of the term "imperative" isn't helpful even if it was wide spread. If everything common to all languages can be called imperative then it is much harder to understand what the difference is between "imperative" or "declarative" or "functional". But we as programmers need this understanding to make conscious decisions while coding. There is a difference between an "if-statement" and an "if-expression" which has implications and we should be aware of them. This awareness lets us as programmers appreciate what's special to the paradigms or programming styles and enables us to make the right choice when solving a problem.
r

Ruckus

09/27/2019, 9:09 PM
Pretty much every language has some sort of ternary short hand. Kotlin is
if (t) a else b
, java is
t ? a : b
, Python is
a if (t) else b
(I really dislike that one), etc. so your claim that
In the second case it is most likely a solely functional language
is fundamentally wrong.
t

tDnamein

09/27/2019, 9:16 PM
You have misread, that's why I will explain again. if an "if-else" construct can be used as a statement => most likely not a solely functional language If an "if-else" construct can only be used as an expression => most likely a solely functional language
since in all your examples the first condition is true we can deduce that Kotlin, Java and Python are most likely not solely functional languages Edit: we can also see that the second condtion does not hold for any of the mentioned languaes
the usage of if-else as an expression only is typical for functional languages.
And since the simple solution for Andreas Unterwegers problem would be an if-else-expression we can call this solution "functional" since it is typical a for functional languages.
r

Ruckus

09/27/2019, 10:12 PM
You seem really defensive about this for some reason, so I'll stop discussing it. I'm sorry if I somehow said something to offend you.
t

tDnamein

09/27/2019, 10:22 PM
@Ruckus I am by no means offended - you didn't say anything personal. Your statement from above is just incorrect, that's all. I hope this little discussion is helpful for others, since I truly believe that we as programmers have to be correct and be able to distinct between those subtle differences.
n

napperley

09/29/2019, 1:55 AM
As Torsten has mentioned Kotlin's if statement can be used as an expression, which I would recommend using. The code would look like the following:
val i: String = if (isEmpty()) "other" else "ssss"
The code above is much more readable than the
elseIf
function version.