Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
My ternary example is also structured in a similar way to Ramda's cond.
// from: http://ramdajs.com/docs/#condvarfn=R.cond([[R.equals(0),R.always('water freezes at 0°C')],[R.equals(100),R.always('water boils at 100°C')],[R.T,temp=>'nothing special happens at '+temp+'°C']]);fn(0);//=> 'water freezes at 0°C'fn(50);//=> 'nothing special happens at 50°C'fn(100);//=> 'water boils at 100°C'
Readability is very subjective and depends on what you are already previously familiar with (aka familiarity bias).
For example, which code is more "readable" here, VB or C#?
Dim count As Integer = 0
Dim message As String
If count = 0 Then
message = "There are no items."
ElseIf count = 1 Then
message = "There is 1 item."
Else
message = "There are " & count & " items."
End If
intcount=0;stringmessage;if(count==0){message="There are no items.";}elseif(count==1){message="There is 1 item.";}else{message="There are "&count&" items.";}
I would say the C# is more readable, but that is because I have spent more time in this land. Someone who spends their whole day in VB land will say the opposite.
To me, when done right, the ternary is much more readable.
I would argue that the ternary isn't any more or less readable than an if statement, but instead only less familiar.
In your first example it is clean, readable and perfectly fine to use it in that way.
In your comparison of C# and VB both are perfectly understandable to any developer of almost any modern programming language.
The ternary is familiar to any moderately experienced developer. In the examples in your article they do not in any way improve upon the standard if statement or aid in functional programming.
The norm is to use if..else if..else for multiple clauses. Deviating from that norm needs a good reason. The examples in your article are not good reasons.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
The reasons being that when you start to substitute statements and blocks with expressions, your code will be much easier to write functionally. It it hard to see the benefit from just this one article, but it's an important (small) step towards creating functional (or pre-functional) code.
The same arguments could be said about a for loop or switch statement, which like if are also imperative (non-functional) constructs.
I also remove for and switch for more functional code.
Replacing a single if else statement with the ternary operator in a lambda function does enable you to write the logic in one terse expression on one line. I agree that this is a good thing.
This does not make the ternary operator functional and the if...else block not. It's just a, in this use case, more appropriate syntax for exactly the same result.
You could rewrite a single line arrow function that uses a ternary operator as a multiline arrow function using if..else and the function would still be functional, it would behave in exactly the same way.
The ternary operator is great for a single one line piece of logic. It is less great for multiple if..else if logic - just use an if block, it's easier.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You are absolutely correct about eliminating if statements not being "functional".
Eliminating the if statement is a pre-functional step that will make it easier to create functional code.
The key difference being the transition from statements and blocks into expressions.
This minor change can allow you to use it in function compositions:
My ternary example is also structured in a similar way to Ramda's cond.
Readability is very subjective and depends on what you are already previously familiar with (aka familiarity bias).
For example, which code is more "readable" here, VB or C#?
I would say the C# is more readable, but that is because I have spent more time in this land. Someone who spends their whole day in VB land will say the opposite.
To me, when done right, the ternary is much more readable.
I would argue that the ternary isn't any more or less readable than an if statement, but instead only less familiar.
In your first example it is clean, readable and perfectly fine to use it in that way.
In your comparison of C# and VB both are perfectly understandable to any developer of almost any modern programming language.
The ternary is familiar to any moderately experienced developer. In the examples in your article they do not in any way improve upon the standard if statement or aid in functional programming.
The norm is to use if..else if..else for multiple clauses. Deviating from that norm needs a good reason. The examples in your article are not good reasons.
The reasons being that when you start to substitute statements and blocks with expressions, your code will be much easier to write functionally. It it hard to see the benefit from just this one article, but it's an important (small) step towards creating functional (or pre-functional) code.
The same arguments could be said about a
for
loop orswitch
statement, which likeif
are also imperative (non-functional) constructs.I also remove
for
andswitch
for more functional code.hackernoon.com/rethinking-javascri...
hackernoon.com/rethinking-javascri...
Replacing a single if else statement with the ternary operator in a lambda function does enable you to write the logic in one terse expression on one line. I agree that this is a good thing.
This does not make the ternary operator functional and the if...else block not. It's just a, in this use case, more appropriate syntax for exactly the same result.
You could rewrite a single line arrow function that uses a ternary operator as a multiline arrow function using if..else and the function would still be functional, it would behave in exactly the same way.
The ternary operator is great for a single one line piece of logic. It is less great for multiple if..else if logic - just use an if block, it's easier.