1

In PHP, say if I have code like this:

$aValue = functionThatReturnsAValue(); // the function might return a string or null;
$anotherValue = $aValue ? process($aValue) : null;

only for brievity (IDK is this a good practice or not and also regarding the performance, etc), I used to change the code like so:

$anotherValue = ($aValue = functionThatReturnsAValue()) ? process($aValue) : null;

My questions are:

  1. Is this style even a good practice?
  2. How can I use this with JavaScript? I wrote with same style but got error.

Thank you.

  • 4
    What error? No, this is not good practice. – Sebastian Simon Mar 15 '18 at 08:02
  • you have assignment in if statement, not a value. if you want to keep this in one line it would be better to write it like this : `$anotherValue = functionThatReturnsAValue() ? process( functionThatReturnsAValue()) : null;` – Maksym Shevchenko Mar 15 '18 at 08:02
  • 2
    The ternary operator does not help maintainability – Mawg says reinstate Monica Mar 15 '18 at 08:03
  • @Xufox i have code like this `aValue: (let a = someFunction()) ? a : null,`. ESLint gave me Unexpected Token. – Rahmat Nazali Salimi Mar 15 '18 at 08:10
  • @rahmatNazali That code is completely different from what you posted, and syntactically invalid. – Sebastian Simon Mar 15 '18 at 08:12
  • @MaksymShevchenko I definitely may be wrong, but this will run the `functionThatReturnsAValue()` twice, isn't it? Where we could only call it once and save the result on a tmp variable and use it again later. CMIIW – Rahmat Nazali Salimi Mar 15 '18 at 08:13
  • @Xufox I just tried to change it like so `let aValue = (let a = Constant.projectName) ? a : null` and I still got the same error. CMIIW – Rahmat Nazali Salimi Mar 15 '18 at 08:16
  • well yeah, that's true, it's better to keep value in a variable above, to be honest. By the way, you could check for null in `process()` function, and return null in this case, so you won't need this statement at all. `anotherValue = process(functionThatReturnsAValue());` – Maksym Shevchenko Mar 15 '18 at 08:17
  • @rahmatNazali Well… it’s still syntactically invalid. You can’t put a `let` in expression context. – Sebastian Simon Mar 15 '18 at 08:17
  • @MaksymShevchenko Thankyou for the kind reply. I have learned a lot from this. – Rahmat Nazali Salimi Mar 15 '18 at 08:47
  • @Xufox thankyou for the reply. I tried to change the code to something like this `let a = null; let aValue = (a = Constant.projectName) ? a : null;`. It do works, but it will miss the brievity I am trying to achieve. Anyway thankyou as I also have learned a lot from this. +1 for the duck too! :) – Rahmat Nazali Salimi Mar 15 '18 at 08:50

2 Answers2

0

You could use a logical AND && and process only a given value.

If aValue is null or a falsy value, this value is assigned to anotherValue, otherwise, it takes the value of process(aValue).

anotherValue = (aValue = functionThatReturnsAValue()) && process(aValue);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Since aValue is the value returned from executing functionThatReturnsAValue(), You can try this

var anotherValue = (functionThatReturnsAValue()) ? process(functionThatReturnsAValue()) : null;
Apetu Gideon
  • 136
  • 1
  • 5