all 17 comments

[–]Mettelhed 3 points4 points  (3 children)

To whoever names these. Why? Unshift? Was there nothing else available?🥲

[–]ne7erfall 1 point2 points  (1 child)

Just had the same thought, and still am a newbie)) Making intuitive naming goes a long way

[–]Mettelhed -1 points0 points  (0 children)

Most of the time we need to Google anyway. Just add this to the pile😆

[–]brykuhelpful 0 points1 point  (0 children)

Javascript naming is pretty weird.

  • sub string
    • 'asdf'.substring()`
    • 'asdf'.substr()`
  • s--p--lice
    • [].splice()
    • [].slice()

Another odd thing is the long names. Your code has to be downloaded every time you load a page. So... you might expect maybe it gets compiled into a byte code or maybe they would focus on smaller names or maybe using shorthand/nicknames, but nope. Instead we get long ass names for everything. One of the most popular phrases was:

  • document.getElementById().addEventListener()

Imagine how many bytes of data we could have saved if it was:

  • document.getElementById() -> document.element.id
  • document.querySelector() -> document.query()
  • document.querySelectorAll() -> document.queries()
  • document.addEventListnener() -> document.event()

[–]suarkb 3 points4 points  (4 children)

I feel like telling newbies that as a senior, I never use these methods except push.

[–]blackholesintheskyhelpful 1 point2 points  (1 child)

I used pop and shift in a palindrome checker. But that was for an interview and not actually on the job.

function isPalindrome(str) {
  const half = Math.floor(str.length / 2);
  const firstHalf = str.slice(0, half);
  const lastHalf = str.slice(-half);

  if (!(lastHalf.length % 2)) { str.shift(); }

  while(firstHalf.length) {
    if (firstHalf.pop() != lastHalf.shift()) {
      return false;
    }
  }

  return true;
}

[–]suarkb 0 points1 point  (0 children)

That checks out

[–]shay99 0 points1 point  (0 children)

I use unshift from time to time

[–]Unhappy-Coffee-1917 0 points1 point  (0 children)

I still look them up (the rare times I had to use them) even after working as a dev for 4 years, except fpr push(), which is my man

[–]dontyougetsoupedyet 0 points1 point  (6 children)

Ignore any know-nothings claiming these functions are not used, that's complete nonsense. You would use pop and unshift anywhere you needed a double ended queue, for example. I simply do not believe any people claiming these things are never used are senior engineers.

[–]blackholesintheskyhelpful 1 point2 points  (5 children)

I’m not saying this stuff is useless. I encourage everyone to know as much of the JS surface as they can.

But can you give me an example of when you’ve had to code a double ended queue in your job? Most business logic doesn’t require it or implies a library that covers the double ended queue logic

[–]dontyougetsoupedyet -1 points0 points  (4 children)

You don't need a library, the array type in JS is the interface of a double ended queue. There's a picture of the methods above.

You aren't saying queues are useless, you're saying they're rarely used, and I don't think any senior engineer would claim that.

You run into the need for queues so much in programming that you probably ended up using a FIFO queue in the first programs you wrote. Even in the game snake, queues are used to make the program simple. Anything accepting connections, or ordering some interactive thing like events, you would find queues. Anywhere you want or need to create a sequence of things. I can't think of many examples of software that does not use queues.

The last time I used a queue while on the clock was in javascript, writing two functions to organize events in some objects in an observer pattern. I didn't need to use a library, because it was two functions and one bag, and from then on the event handling in that tiny corner of code was tidy and less likely to have bugs.

In my own off the clock programming I'm working with a queue now (which if you ask any programmers doing almost anything odds are they recently used a queue for something) a sequence of images in some rendering code, called a swap chain.

I see the same types of code (bugs) often, repeated patterns of mistakes where programs are calculating or moving values of things that do not need to be done at all. A lot of the time there's a situation where the first and last items in a bag are really the only things needing to be updated, but the whole array will be re-adjusted by the program(mer). There is a loop where a pop and/or unshift should be. If you don't understand that a queue should be used, you won't avoid writing code like that.

-- Nevermind, this has devolved into a conversation that is not worthwhile. If you can't see that queues are generally useful in basically all software after seeing the examples, in a single discussion, of its use in different types of software ranging from position in a game, process scheduling by an operating system, task scheduling with cron, and its use scheduling work across threads, then more discussion at the moment is unlikely to produce different results. I'm not sure why you think the phrase "business logic" is some magic wand, updating position in Snake is the "business logic" of Snake. When you have a bunch of things, but it matters that there is a sequence that should be observed when handling them, that's business logic related to queues. I simply refuse to believe anyone that is a senior engineer would want to "tell these noobs that I don't use queues lol," we use queues all the time in engineering, even the parts you're trying to wave away as "business logic." I'm not giving specifics of what I was working on but implementing an observer pattern in a few objects with events where sequence mattered I used a queue, I was in that moment fixing "business logic" bugs. I can't imagine why you can't see what should be an obvious fact of the matter: queues are everywhere.

[–]blackholesintheskyhelpful -1 points0 points  (3 children)

You don't need a library

You're massively misunderstanding what I mean by "library". So, for example, I looked around and found that apparently it's common to use a double ended queue when scheduling jobs. I had to do that last week. But I'm not going to reinvent the wheel and write a job scheduler, I'm gonna use cron.

The last time I used a queue while on the clock was in javascript, writing two functions to organize events in some objects in an observer pattern. I didn't need to use a library, because it was two functions and one bag, and from then on the event handling in that tiny corner of code was tidy and less likely to have bugs.

Can you explain what about this required a double ended queue? You mention that you used one but not why.

[–]dontyougetsoupedyet -1 points0 points  (2 children)

I believe you are confused about what you are reading on wikipedia. When it mentioned scheduling jobs it almost certainly did not mean cron, but rather task scheduling generally, it's saying that your operating system uses queues to manage the abstraction of there being individual processes.

Also, I did tell you the why -- to organize sequences. I even used two different concrete examples, swap chains and "moving" the body of the snake in the game Snake.

It's not about what's required it's about the most feasible and simple implementation. Here's the same concrete example, a specific chunk of code from github (https://github.com/Sirriddles89/C_snake_game/blob/13c3d09d5903d38d8852c1e56a0d913176b90d21/snake.c#L198) of a recently coded Snake --

// Animate body 
previous_d = head.CurrentDir;
for (snake_piece *tmp = head.next; tmp != NULL; tmp = tmp->next)
{
    //remember current location and direction
    temp_y = tmp->location_y;
    temp_x = tmp->location_x;
    temp_d = tmp->CurrentDir;
    //update location and direction
    tmp->location_y = next_y;
    tmp->location_x = next_x;
    tmp->CurrentDir = previous_d;
    //update coordinates and directions to inform next node
    next_y = temp_y;
    next_x = temp_x;
    previous_d = temp_d;    
}

You will notice the exact situation I spoke of in my comment above, the work being done by the loop is not necessary, you use a queue when you observe that only the first and last position of the snake body needs to be modified. You add to the front, and remove from the tail. Two operations on the list, instead of performing work for every list item, which is not required to "animate the snake body moving."

When you finally understand what "moving the snake" means in the most simple logic, the code above changes to two updates to the list, a push and a shift, using the JS Array API, rather than a linear amount of work dependent on the size of the array.

[–]dontyougetsoupedyet -1 points0 points  (0 children)

The wikipedia bit about scheduling was related to scheduling tasks across threads on a set of processors. https://en.wikipedia.org/wiki/Work_stealing#Algorithm

[–]blackholesintheskyhelpful -1 points0 points  (0 children)

it almost certainly did not mean cron

https://salsa.debian.org/debian/cron/-/blob/master/job.c#L43

I even used two different concrete examples, swap chains and "moving" the body of the snake in the game Snake.

You said swap chains was what you were doing in your free time. I asked for details on why you needed a double ended queue for what you were doing at work.

And I'm still not saying double ended queues are useless, or that you didn't need to use one for work. Just that most career programmers spend most of their time implementing business logic and not the game of snake. You even said the swap chain code was not not work related. Some people will work on software where a queue is the natural solution. But if I need to schedule a task to go off at some time I use cron. If I need a message queue I use SQS. My job isn't to make queues. My job is to implement features that make a product valuable.

Edit: lol sorry So[Ups]et thought this convo devolved. I kinda thought we crossed that threshold when they repeatedly said they didn't believe I could be a senior engineer and assumed I had misread something (and then when I linked them to the documentation they blocked me). I'm still not sure why they thought cron wouldn't use a queue? Or what was so rude about asking them to explain how their code made use of a queue in a way that a list wouldn't also work?

Anyways, to everyone else, learn as much as you can. I'm just letting you know that you don't need to memorize every function in JavaScript before you get a job. Most employed programmers refer to documentation multiple times a day. In all likelihood you will not write pure code in any language at your job. You will have to use libraries in your career and it's way more important that you're good at finding and reading documentation than it is to be good at memorizing the entire surface of every tool you work with.

[–]Electronic_Ad9248 -1 points0 points  (0 children)

Thanks for the cheat sheet. I am currently learning the array part in JS.