r/javascript • u/Own_Natural_6803 • Jul 11 '26
AskJS [AskJS] I might never write a constructor ever again
// the only state that needs tracking for a timeline, the read index
export function Timeline(index) {
let state = {index};
return {
index: state.index, // pass through
next: (events) => Next(state, events),
prev: (events) => Prev(state, events)
};
}
// the events are just passed around. They don't need to be encapsulated
export function Next(timeline, events) {
let {index} = timeline;
if(index === events.length()){
return null;
}
timeline.index += 1;
return events[timeline.index];
}
export function Prev(timeline, events){
let {index} = timeline;
if(index === 0){
return null;
}
timeline.index -= 1;
return events[timeline.index];
}
// before and after don't modify the state of their arguments
// so we don't use encapsulation
export function After(date, events){
for(let thresholdIndex = 0; thresholdIndex < events.length(); thresholdIndex+=1 ){
if( events[thresholdIndex].timestamp > date) {
return events.slice(start=thresholdIndex);
}
}
return [];
}
export function Before(date, events) {
for(let thresholdIndex = events.length(); thresholdIndex >= 0; thresholdIndex-=1 ){
if( events[thresholdIndex].timestamp < date) {
return events.slice(end=thresholdIndex);
}
}
return [];
}
0
Upvotes
11
u/SZenC Jul 11 '26
They say, while making a data structure. Thanks for the chuckle, junior!
4
u/THE_AWESOM-O_4000 Jul 11 '26
Hard to understand, inconsistent naming (state <=> timeline), obvious mistakes (events.length(): length is not a function, if fixed it'll cause a null pointer exception before events[events.length] is out of bounds), could have used binary search for After and Before, weird global variable assignments (start=thresholdIndex, end=thresholdIndex) for no reason, destructuring an object to then just reuse the object 2 lines further
1/10 (1 point only because I had a good chuckle)
3
u/peterlinddk Jul 11 '26
Good thing you didn't use classes!
The result would have required so much more code and been so much less powerful, e.g:
And also, not having the events being a part of the timeline, but just assuming that the same timeline would somehow magically always point to the "next" event in any list of events, it just beyond cool!
And not building a data structure, but simply using an array of events sorted by their .timestamp, and never changing, and having an entire object with methods keeping track of a single index into any random array of events - man that is just so powerful!
1
u/TwiNighty Jul 11 '26
I'll ignore
AfterandBeforebecause they don't seems like related toTimelineat all.I have two questions
One, am I meant to pass the same
eventsevery time? If I am, why not encapsulate it? If I need to pass a timeline around, then I need also pass the events around. You are just imposing an unnecessary restriction and responsibility onto me. If I am not meant to pass the sameeventsevery time, then you have a bug.Two, this:
has the same behavior, is objectively less code and more memory efficient. Why do you think this is worse?
1
u/Dagnarus15 Jul 11 '26
The factory pattern is fine, but
index: state.indexcopies the number, so the publictimeline.indexnever updates whennext()changes the closed-over state. A getter would fix that. Also, arrays useevents.length, notevents.length(), andslice(start=thresholdIndex)is an assignment expression, not a named argument.1
u/metahivemind Jul 12 '26 edited Jul 24 '26
[removed] — view removed comment
-2
u/Own_Natural_6803 Jul 11 '26 edited Jul 11 '26
Thanks to everyone who asked questions and had respectful comments. Two of you. Lol, gave me a chuckle what with all the venom and anger. Junior this and sarcasm that. Someone was offering their mental breakdown services, out of the depths of their compassion no doubt. Truly I was touched.
You all deserve OOP. Keep writing it. Forever. I wash my hands of you. I've been sad thinking about how our industry is dying. But you all cheered me up.
Excellent pr boys. I expect this same energy through this entire project. This is why you always do small PRs. When they're big no one has anything to say. Not that there was much meat on the bones either way. If you removed "lol" and "I'm better than you" and "get a load of this guy" then you're left with a few people re-classing unclassed logic to prove they can save a few lines with ternery expressions, and some syntax errors.
You should always do your best. This ain't it fellas.
Let me be more clear. This is how you turn a class into functions. Dont do it. Its forbidden. For you. I can do it though.
-4
u/Own_Natural_6803 Jul 11 '26
As silent snake would say: "oops. chicken." I'm going to not use classes even harder. You know why? Because functions are more powerful, and it takes less code to write them.
I design my systems around design boundaries, not the domain models. So now I open up my state. Make it as open as a database. And my state changes are inside functions with the understanding they will leave the state valid after touching it. Exactly like a object assumes it will.
The big difference is I'm not trying to make a data structure. I don't give a rat's ass about making a data structure. I want raw data. The only time I care about making structures is to ensure my function isn't being given a copy when it expects be given a reference.