In my view the insight that is missing in this article is the key difference between both conditional constructs:
An if statement is - well, a statement.
The ternary operator is part of an expression.
So what?
An if statement routes the flow of execution - based on the outcome of the condition, program execution is routed to a specific conditional block of statements.
A ternary has to produce value - so a ternary expression will always have to have two possible outcomes (values). (BTW - what else returns a value: a function).
If you have an imperative programming style you will be thinking and programming in statements. As such you will default to if statements (me - programming C decades ago and going "yech" every-time I came across a ternary).
Once you start using a functional language (say Erlang, OCaml, Haskell, Elixir, Clojure, etc.) statements go out the window (even Rust, though imperative, is largely expression-based). You have to deal with everything in terms of expressions producing values (not statements mutating values; PLOP - Place-Oriented Programming) because expressions - just like functions - take some values as inputs and produce a new value. There is no if statement - only conditional expressions.
Scheme is in JavaScript's history which is evidenced by its function-oriented nature so while it isn't possible to dump statements entirely (they have their uses 😏) one can develop a more expression-based style - and using ternaries (with tuples and destructuring assignments) is a big part of that.
// ...functioncategorize(ch){return'a'<=ch&&ch<='z'?CHAR_CATEGORY.LowerCase:'A'<=ch&&ch<='Z'?CHAR_CATEGORY.UpperCase:SEPARATORS.includes(ch)?CHAR_CATEGORY.Delimiter:CHAR_CATEGORY.Other;}constisAlphabetic=(c)=>c===CHAR_CATEGORY.LowerCase||c===CHAR_CATEGORY.UpperCase;functionreducer([last,acronym],ch){constcurrent=categorize(ch);return(last===CHAR_CATEGORY.LowerCase&¤t===CHAR_CATEGORY.UpperCase)||(last===CHAR_CATEGORY.Delimiter&&isAlphabetic(current))?[current,acronym+ch.toUpperCase()]:[current,acronym];}// beginning of phrase is equivalent to a "delimiter"constINIT_COLLECT=Object.freeze([CHAR_CATEGORY.Delimiter,'']);functionacronym(phrase){const[_last,result]=phrase.split('').reduce(reducer,INIT_COLLECT);returnresult;}
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.
In my view the insight that is missing in this article is the key difference between both conditional constructs:
if
statement is - well, a statement.So what?
An
if
statement routes the flow of execution - based on the outcome of the condition, program execution is routed to a specific conditional block of statements.A ternary has to produce value - so a ternary expression will always have to have two possible outcomes (values). (BTW - what else returns a value: a function).
If you have an imperative programming style you will be thinking and programming in statements. As such you will default to
if
statements (me - programming C decades ago and going "yech" every-time I came across a ternary).Once you start using a functional language (say Erlang, OCaml, Haskell, Elixir, Clojure, etc.) statements go out the window (even Rust, though imperative, is largely expression-based). You have to deal with everything in terms of expressions producing values (not statements mutating values; PLOP - Place-Oriented Programming) because expressions - just like functions - take some values as inputs and produce a new value. There is no
if
statement - only conditional expressions.Scheme is in JavaScript's history which is evidenced by its function-oriented nature so while it isn't possible to dump statements entirely (they have their uses 😏) one can develop a more expression-based style - and using ternaries (with tuples and destructuring assignments) is a big part of that.