An simplified example from a real project:
if (!($this->action[$key] ?? false)) {
However to understand this you must know what the ?? operator means and
note the negation using ! in the beginning.
I would prefer a longer but easier to understand solution like this:
if (!isset($this->statusActions[$key])
|| null === $this->statusActions[$key]
) {
Any thoughts on this?
An simplified example from a real project:s/statusActions/action/g
if (!($this->action[$key] ?? false)) {
What it means:
1) $this->action[$key] ?? false
This return $this->action[$key] if $this->action[$key] exists and is not
null or "false".
2) !( .... )
Will negate the result - so the condition is true if $this->action[$key]
does not exist or is null.
However to understand this you must know what the ?? operator means and
note the negation using ! in the beginning.
When you did not create this code in the first place or try to find a
bug expressions like this are not really helpful.
I would prefer a longer but easier to understand solution like this:
if (!isset($this->statusActions[$key])
|| null === $this->statusActions[$key]
) {
Any thoughts on this?
An simplified example from a real project:
if (!($this->action[$key] ?? false)) {
What it means:
1) $this->action[$key] ?? false
This return $this->action[$key] if $this->action[$key] exists and is not
null or "false".
2) !( .... )
Will negate the result - so the condition is true if $this->action[$key]
does not exist or is null.
However to understand this you must know what the ?? operator means and
note the negation using ! in the beginning.
When you did not create this code in the first place or try to find a
bug expressions like this are not really helpful.
I would prefer a longer but easier to understand solution like this:
if (!isset($this->statusActions[$key])
|| null === $this->statusActions[$key]
) {
Any thoughts on this?
On 23/11/2021 11.52, Arno Welzel wrote:
An simplified example from a real project:
if (!($this->action[$key] ?? false)) {
However to understand this you must know what the ?? operator
means and note the negation using ! in the beginning.
What if you don't know about === then the null check below looks strange
and you would think it has a bug.
if (!isset($this->statusActions[$key])
|| null === $this->statusActions[$key]
) {
I do prefer the short one, but then I'm used to that kind of code
and the code analyzer at work do complain when you have a lot of
if statements with unnecessary and/or operators.
There can also be code execution gains with the short compact code
compared to do the "traditional" way. There been times where I
have managed to make the code execution 1/100 of the original time
using the shorter compact code.
On Tue, 23 Nov 2021 12:17:13 J.O. Aho wrote:
On 23/11/2021 11.52, Arno Welzel wrote:
An simplified example from a real project:
if (!($this->action[$key] ?? false)) {
However to understand this you must know what the ?? operator
means and note the negation using ! in the beginning.
What if you don't know about === then the null check below looks strange
and you would think it has a bug.
Knowledge about === should be more widespread than about ??, and at
least for me the latter is less intuitive to read. But ok, this is
really a matter of personal preference.
I always give preference to array_key_exists as isset($x[$y])
gives my brain the (false) impression that $x[$y] should exist,
while array_key_exists($y, $x) does not do this.
And I prefer is_null() over "null ===" because it cannot be
misinterpreted or misspelled.
If I'd lose a factor 100 (or even a factor 10) of execution time
this would certainly make me use shortcuts. However, I don't think
this is likely to happen. Wherever I've lost one or more magnitues
of execution time it was due to avoidable nested loops or otherwise
poor logic.
An simplified example from a real project:
if (!($this->action[$key] ?? false)) {
What it means:
1) $this->action[$key] ?? false
This return $this->action[$key] if $this->action[$key] exists and is not null or "false".
2) !( .... )
Will negate the result - so the condition is true if $this->action[$key] does not exist or is null.
However to understand this you must know what the ?? operator means and
note the negation using ! in the beginning.
When you did not create this code in the first place or try to find a
bug expressions like this are not really helpful.
I would prefer a longer but easier to understand solution like this:
if (!isset($this->statusActions[$key])
|| null === $this->statusActions[$key]
) {
Any thoughts on this?
--
Arno Welzel
https://arnowelzel.de
1 byte contains only 256 bits of information. Everyone make now Your own conclusions.
☏ : 372 5 3 9 0 0 6 6 0
E-mail : he1983912[@]mail.ee
Arno Welzel kirjutas Teisipäev, 23. november 2021 kl 12:52:33 UTC+2:
An simplified example from a real project:
if (!($this->action[$key] ?? false)) {
What it means:
1) $this->action[$key] ?? false
This return $this->action[$key] if $this->action[$key] exists and is not
null or "false".
2) !( .... )
Will negate the result - so the condition is true if $this->action[$key]
does not exist or is null.
However to understand this you must know what the ?? operator means and
note the negation using ! in the beginning.
When you did not create this code in the first place or try to find a
bug expressions like this are not really helpful.
I would prefer a longer but easier to understand solution like this:
if (!isset($this->statusActions[$key])
|| null === $this->statusActions[$key]
) {
Any thoughts on this?
--
Arno Welzel
https://arnowelzel.de
On Fri, 1 Jul 2022 01:05:37 -0400, Stan Weiss wrote:
Actually 1 byte contains 8 bits and can represent a number from 0 to 255Actually s/number/value/
Actually 1 byte contains 8 bits and can represent a number from 0 to 255
On 7/1/2022 8:18 AM, Allodoxaphobia wrote:
On Fri, 1 Jul 2022 01:05:37 -0400, Stan Weiss wrote:
Actually 1 byte contains 8 bits and can represent a number from 0Actually s/number/value/
to 255
Yes, there are times the value will be looked at as -127 to +127
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 379 |
Nodes: | 16 (2 / 14) |
Uptime: | 70:53:33 |
Calls: | 8,084 |
Calls today: | 2 |
Files: | 13,069 |
Messages: | 5,849,833 |