JavaScript What is a typical class of issues that is caused by mutable data types in a single-threaded environment? what is the typical class size at texas a&m,

JS models concurrency by an event loop. As a result there are no race conditions. So what are the drawbacks of the following type safe operation in the main scope of a program that would justify any caveat:

const m = new Map([["foo", true]]);

//...

m.set("bar", false);

Even if I'd empty m this shouldn't raise any issues, since every operation that depends on m should consider the empty case anyway.

Maybe someone can illustrate a typical class of issues that comes along with mutable data types.

I know that this question might be too opinion based so please feel free to close it if you think it doesn't fit SO.

Thanks in advance!

Answer:1

[...] since every operation that depends on m should consider the empty case anyway

Naming things is difficult. Assuming things can be dangerous.

I consider myself a pragmatic developer and sometimes mutation just has to happen. However it is your job to know what could go wrong and educate the people around you about the danger so that they can make informed decisions.

I once pointed out during a code review that this function was mutating its parameter: (it roughly looked like this)

function foo(bar) {
  const baz = {...bar};
  baz.a.b.c = 10;
  return baz;
}

To which the author of the function replied "Nope, I've cloned it before. So the function is 'pure'".

Had I not spent the time to sit down with that person we could have had a major production issue. It turned out that they were mutating the application state and we had several false positive tests as a result.

This to me is the worse that can possibly happen when mutating data: confusion.

Bugs caused by mutation can be difficult to trace back.

I always encourage people in my team to not code for the "impossible case" because that often leads to code cluttered with "just in case" checks which increases the effort of maintainance and undermines our confidence in the code.

However the "impossible case" is just waiting around the corner if you allow uncontrolled access to your data.

I've already demonstrated that people do mutate things without realising it. When you've got people with different level of experience in your team the best you can do is:

  1. Never assume anything
  2. Teach them
  3. Use a library to enforce immutability

Perhaps not the "academic" answer you were possibly expecting but I thought I could share some tips.

Answer:2

I need to add class to input parent if input is not empty or focus because I want to change label position. <div class="input-field"> <input type="email" name="email" ng-model="email" ...

I need to add class to input parent if input is not empty or focus because I want to change label position. <div class="input-field"> <input type="email" name="email" ng-model="email" ...

I am trying to change custom icon of video, when video is toggled(Play/Pause). ngAfterViewInit() { const vdoCont = document.querySelector('.video-player'); const vdo = vdoCont.querySelector('video')...

I am trying to change custom icon of video, when video is toggled(Play/Pause). ngAfterViewInit() { const vdoCont = document.querySelector('.video-player'); const vdo = vdoCont.querySelector('video')...

  1. javascript snake
  2. javascript foreach
  3. javascript map
  4. javascript for loop
  5. javascript array
  6. javascript substring
  7. javascript switch
  8. javascript reduce
  9. javascript date
  10. javascript regex
  11. javascript filter
  12. javascript string contains
  13. javascript function
  14. javascript replace
  15. javascript slice
  16. javascript split
  17. javascript settimeout
  18. javascript download
  19. javascript interview questions
  20. javascript promise

I'm trying to implement an image preview. It works fine until i came to a problem where i need to prevent the input file to not change. JSFIDDLE When trying to change to image its fine. After ...

I'm trying to implement an image preview. It works fine until i came to a problem where i need to prevent the input file to not change. JSFIDDLE When trying to change to image its fine. After ...

  1. jquery preventdefault return false
  2. preventdefault jquery return
  3. jquery preventdefault vs return false
  4. jquery event.preventdefault vs return false
  5. jquery event.preventdefault return false

This is my data with 5 arrays. What I wish to achieve is to combine id and name and the new array should have 5 different playname values. It can be in either an array or new key like playername1. [ ...

This is my data with 5 arrays. What I wish to achieve is to combine id and name and the new array should have 5 different playname values. It can be in either an array or new key like playername1. [ ...

  1. combine array objects javascript
  2. concat array objects javascript
  3. concat array objects
  4. combine array of objects
  5. combine array of objects js
  6. combine two array objects
  7. combine arrays into objects
  8. lodash combine array of objects
  9. combine two array of objects javascript
  10. combine 2 array of objects javascript
  11. es6 combine array of objects
  12. underscore combine array of objects
  13. powershell combine array of objects
  14. php combine array of objects
  15. jq combine array of objects