News for hackers. Stuff that matters.
Re:Algorithms in PHP: Deques by pluc on Tuesday September 13, 2022 @07:39AM (#32823007)
That code is so bad. Who does this?!

    return ($this->head === 0 && $this->tail === $this->size - 1) || $this->head === $this->tail + 1;
Or this?

     $return = $this->next?->resetPrevious();

Or this!!!

    $this->tail =
            $this->tail?->setNext(new Node($this->tail, null, $value))->next
            ?? $this->head = new Node(null, null, $value);

Or leaves typos in their code?

    elseif ($this->tail === $this->size - 1) {
            $this->tail = 0;just a tiny
        }


I get that PHP has a bad rep but this ain't helping.
Re:Algorithms in PHP: Deques by withinboredom on Tuesday September 13, 2022 @08:14AM (#32823296)
> who does this

Well, I’m doing this for fun, mostly in an illustrative manner. I’m not getting paid for this, nor is anyone reviewing my work. So thanks for volunteering, though your critical analysis could be more constructive.

As to the typos, I suspect it comes from the WordPress editor just being its weird self and perhaps me starting to type in the wrong place, or Grammarly updating the wrong thing. Who knows, but it’s not in the markdown version of this draft.

Re:Algorithms in PHP: Deques by nkozyra on Tuesday September 13, 2022 @08:51AM (#32823597)
I also wonder what the purpose of wrapping native PHP array mutation in an object is and what overhead that introduces.

I'm with you on being kind in code reviews but one of the canonical examples of sticky bad PHP code is the addslashes top answer on SO for preventing SQL injection. It stayed there for years/decades and hundreds if not thousands of junior devs copied and pasted it.

So there's some risk when we share code that Google can index and possibly elevate.

I think consulting an algorithms book first would give a more formal example that you could work from.

Re:Algorithms in PHP: Deques by withinboredom on Tuesday September 13, 2022 @11:10AM (#32825317)
> purpose of wrapping native PHP array mutation in an object is and what overhead that introduces.

The purpose is to illustrate what we are doing and setting the stage for later implementations. The overhead is practically unmeasurable unless we are filling the object table with these things (we are not).

Property lookup and function calls are pretty darn fast in PHP, especially with JIT enabled.

Re:Algorithms in PHP: Deques by nkozyra on Tuesday September 13, 2022 @11:16AM (#32825408)
> The overhead is practically unmeasurable unless we are filling the object table with these things

I think that's typically how one would measure complexity and regardless of JIT I think it introduces some artificial cost.

Re:Algorithms in PHP: Deques by withinboredom on Tuesday September 13, 2022 @12:29PM (#32826495)
Sure, it adds a cost, but it’s infinitesimal compared to the cost of what we are measuring.
Re:Algorithms in PHP: Deques by P5fRxh5kUvp2th on Tuesday September 13, 2022 @08:55AM (#32823638)
or maybe you're being overly critical.

PHP aint pretty, but that code doesn't seem unreasonable to me. I certainly wouldn't blink at it outside of the typo.

Re:Algorithms in PHP: Deques by bkanber on Tuesday September 13, 2022 @08:53AM (#32823617)
> I get that PHP has a bad rep

Have you ever tried reading perl code?

Re:Algorithms in PHP: Deques by rinze on Tuesday September 13, 2022 @09:40AM (#32824159)
I learned that Perl is a write-only language back in the day, and that really helped.
Re:Algorithms in PHP: Deques by firasd on Tuesday September 13, 2022 @10:01AM (#32824400)
Actually this type of over-usage of ternary operators is not a PHP thing.. it's very prevalent in the JavaScript world
Re:Algorithms in PHP: Deques by jorams on Tuesday September 13, 2022 @10:39AM (#32824861)
None of those snippets contain a ternary operator.

?? is the Null Coalescing Operator[1]

?-> is the nullsafe operator[2]

[1]: https://www.php.net/manual/en/language.operators.comparison.... [2]: https://www.php.net/manual/en/language.oop5.basic.php#langua...

Re:Algorithms in PHP: Deques by firasd on Tuesday September 13, 2022 @01:32PM (#32827529)
Oh my mistake. Still stuff like 'return a || b' is the kind of thing I see in JS way more than in PHP codebases
Re:Algorithms in PHP: Deques by deergomoo on Tuesday September 13, 2022 @05:47PM (#32830565)
Probably because the Boolean operators in JS return the left or right side of the expression, rather than a Boolean.

It was an okay-ish way to get a default/fallback value before JS got a proper coalescing operator.

Re:Algorithms in PHP: Deques by goshx on Tuesday September 13, 2022 @09:20AM (#32823892)
You are not helping either. Show how it is supposed to be :)
Re:Algorithms in PHP: Deques by vanviegen on Tuesday September 13, 2022 @11:02AM (#32825207)
That's kind of the whole issue, isn't? Who knows what that kind of code is supposed to mean?