you are right, as long as the array is not copied before iterating, n and index match in each iteration - but as you point out, that does not lead to correct code because you're skipping elements. It's a bad practice in general and incorrect in the particular case.
This actually has my preference over .filter as it doesn't allocate a new array.
If you depend on changing the actual array instead of replacing it, that code is fine but it could point to you having implicit dependencies in your code that make your code hard to maintain - but maybe not, that's just my intuition. In any case, it is the kind of data dependency that Rust forbids entirely by default and requires things like Cell or Mutex to opt-in to explicitly.
If you want to mutate in-place purely for performance reasons, then unless you profiled your code, I'd prefer filter.
This actually has my preference over .filter as it doesn't allocate a new array.
This just isn't important really.
Unless the array is expected to be super super huge.
Even then it isn't very important.
The cost of the new array is just so small compared to the improvements from using a modern array function.
Adding and removing elements from anywhere other than the end in JS is costly, so it should be avoided where it doesn't provide performance gains elsewhere, or is strictly necessary.
this is unnecessarily O(n²) - probably worse because of splice
there's an unused trailing parameter
if t instanceof Toast it's not null/undefined, so the ?. is redundant (also in your code) - Typescript should be able to realize t is of type Toast by that point, and ?. is a specialized tool not to be sprinkled around. It doesn't matter with === true, but if it were === undefined (and not in a place where it's completely redundant), it could change the result unintentionally.
in typescript, assuming isExpired has type boolean, I'd skip the whole === true
idx is never -1 in this code; t comes from the array and each t is only remove up to once. If it turned out to be -1 then probably because of a code change with unintended consequences; now the code silently skips elements it should delete. What should be here is an assert of some sort, not cargo-cult "defensive programming" (ok, maybe that was a little callous, but this is badcode, I think I can get away with it)
I think the biggest question is how big n is? This is processing a list of pop-ups. If there's more than a couple there are probably bigger problems. 🤷 And O(n2) is pretty insignificant if n is 1.
True! It could be viewed as premature optimization to care about that - but it's worth noting that not using indexOf in this piece of code would likely make it more readable: this operation requires one iteration, and doing only one iteration makes it easiest for the reader to get the intention.
In the end, it's just one item on a list of reasons why this is inferior to the one-liner that should be there.
Careful, index will be mismatched after the first removal via splice.
This is what I like in languages which use Option and Result, a much more correct way to expose the result.
I agree, but that would not by itself fix the problem. If we talk Rust specifically, the insight is that this is the place to unwrap() instead of using if let or similar.
afaict, this code is functionally correct though. I had a lot to say about it, but I didn't spot an outright bug by trying it in my head. Note that the array being iterated is a copy, and that the indices being removed are correct wrt the array that is being modified.
[...this.popups] effectively copies this.popups: it destructures the array into its elements via ..., and populates a new array with it. see this example:
let a = [1, 2];
let b = [0, a, 3]; // [0, [1, 2], 3]
let c = [0, ...a, 3]; // [0, 1, 2, 3]
Yeah, I was afraid that would be reason. Miss me with that shit. I hate working on a code base where I have to constantly guess if things that are in there actually make a difference or are just superstitious voodoo.
The codebase I inherited calls assert on every parameter of every method and function. Most of the time. If it was the main dev and not one of the contractors they hired.
It makes me fucking crazy. It's the wrong lesson to learn from dealing with people who don't know how to code. People shouldn't be putting nulls in places they don't belong, and when they do and it breaks that's on them. And if you're protecting against yourself that's even worse.
Sounds pretty horrible. At that point, why not just use a statically typed language, at least you get checking at compile time instead of runtime overhead.
Typescript is also annoying as it complains about checking for null when you know that it shouldn't be possible. Usually there's a way to fix it, but often the easiest route is to just throw a ? somewhere.
New language features are shiny and so people abuse them until they get burned.
I like concise and readable code but not at the expense of it being clear about what is going on. Worse is when the author overuses out of habit or inexperience, which is what happened here I think..
If I was just a null check on popups before calling foreach that makes sense, and the spread into a new array is a good idea since they are modifying the original array by removing the entries they are cleaning up. But using both there makes it look like they don't know what's going going.
Also this.popups = this.popups?.filter(isNotExpiredToast);
72
u/kristallnachte Feb 19 '22
So...it can't have an undefined t because that's the thing it's looping, and index is already provided...so checking for the index makes no sense...
And it's all to just do a filter...
16
u/[deleted] Feb 19 '22
No it doesn't belong here. I've seen much worse ones before.
This one clearly:
Loops thru
this.popupsand checks iftis instance ofToastandt.isExpiredproperty equals totrue. Then removes that item fromthis.popupsarray.But it could've been written more "readable" way like:
26
u/whoisrodi Feb 19 '22
what the fuck is even happening
38
u/halfanothersdozen Feb 19 '22
It's not the worst but it does highlight that the optional chaining operator was a mistake.
2
u/MTB-and-beer Feb 20 '22
Wrapping
this.popupsin an array just to spread it is galaxy brain stuff lol2
u/MeatyLabia Feb 20 '22
Can anyone explain the Toast | Message to me? I have never seen that.
3
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 19 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
2
u/positev Feb 20 '22
Anything with <bool> == True belongs here honestly. That will never pass a code review I'm in.
1
u/Tulkdan Feb 19 '22
Happy cake day!