News for hackers. Stuff that matters.
Re:JavaScript fundamentals before learning React by Swizec on Thursday July 19, 2018 @05:46PM (#17570588)
Beginners don't know about references. What you say is exactly what the author meant.
Re:JavaScript fundamentals before learning React by rwieruch on Thursday July 19, 2018 @05:54PM (#17570672)
Author here {: Yes, what you are saying here is what I meant. I didn't want to mix in the whole topic around "passing by value or reference" in this short teaser.

Hi Swizec :wave: :)

Re:JavaScript fundamentals before learning React by pygy_ on Thursday July 19, 2018 @06:02PM (#17570721)
Hi, Robin.

Then you may want to say that `const` guarantees that the name will remain bound to that object.

The great-GP is correct in that `const` has nothing to do with immutability besides that. It doesn't necessarily conveys an intent of inner immutability. It just states that whatever is in the variable will stay there until it goes out of scope.

Using `const` whenever possible is good advice, off course.

Re:JavaScript fundamentals before learning React by ravenstine on Thursday July 19, 2018 @09:47PM (#17571846)
It's been astonishing to me how people switched from var to let, but seem confused as to when to use const. I use const wherever I can since it's more appropriate for non-changing variables, and it helps me more easily visualize how a variable is being used in a block. But in other people's code I see a lot of letting all over the place. Maybe it's because of those articles saying to use let instead of var, but glossing over const.
Re:JavaScript fundamentals before learning React by jwdunne on Friday July 20, 2018 @03:42AM (#17573176)
I tend to go by the following rules:

Use const by default. If I need let, see if I can refactor to using const.

I've found that actually I can go far without using let, improving my code along the way.

I see a pattern from colleagues. They initialise a variable using let and then branch to figure out what should go inside it. That can be replaced by a pure function.

For loops tend to be the next thing. In that case, they can be replaced by higher order functions/methods without detriment to clarity (usually an improvement).

Perhaps the move towards iterators, with async support, will make it more common and legitimate. I haven't yet used those in a codebase but perhaps let in that instance leads to clearer code.

Re:JavaScript fundamentals before learning React by dotancohen on Saturday July 21, 2018 @08:03AM (#17581722)
> They initialise a variable using let and then branch to figure out > what should go inside it. That can be replaced by a pure function.

I don't want to detail the discussion, but I would love to see some of your code. I contend with this antipattern often but solve it in different ways often depending on language features. If you're doing anything other than simply calling an auxiliary method with a ton of parameters, I would appreciate seeing your solution in Javascript, a language that I'm learning but not proficient in.

Re:JavaScript fundamentals before learning React by BigJono on Thursday July 19, 2018 @10:40PM (#17572045)
It's trivial to learn as well. You use const for everything until you get an error for trying to reassign it.
Re:JavaScript fundamentals before learning React by vga805 on Thursday July 19, 2018 @11:01PM (#17572127)
the only way to go
Re:JavaScript fundamentals before learning React by mygo on Thursday July 19, 2018 @11:59PM (#17572345)
maybe it’s because const is 2 more letters to type.

if “let” was “letitbe” I bet const would be more popular

seriously though, I wish they could have chosen a 3-letter word for const in keeping with var and let. I know const exists in other languages, but it doesn’t even mean the exact same thing as some other languages anyway.

Re:JavaScript fundamentals before learning React by dpwm on Friday July 20, 2018 @04:49AM (#17573441)
> I wish they could have chosen a 3-letter word for const in keeping with var and let.

"set" or "fix" would be quite nice, given what const actually does.

Re:JavaScript fundamentals before learning React by DonHopkins on Friday July 20, 2018 @07:34AM (#17574130)
I'm having one of those days when variables won't and constants aren't.
Re:JavaScript fundamentals before learning React by pygy_ on Friday July 20, 2018 @04:51AM (#17573445)
It would have been nice to have `let` work like `const` does, and something else for what is currently `let` (`mut`?).

The TC39 was constrained by the list of reserved words though. `let` and `const` were reserved in ES5, and there was nothing that could have conveyed `mut`.

https://mathiasbynens.be/notes/reserved-keywords#ecmascript-...

Re:JavaScript fundamentals before learning React by eip on Friday July 20, 2018 @05:33PM (#17579140)
Unrelated but didn't know how to contact you: https://www.youtube.com/watch?v=TdzIKD0oMzo
Re:JavaScript fundamentals before learning React by DonHopkins on Friday July 20, 2018 @07:33AM (#17574119)
How about balancing out every "let" with a "forbid" at the end of the block to mark the end of the scope?
Re:JavaScript fundamentals before learning React by rwieruch on Thursday July 19, 2018 @06:12PM (#17570790)
Good point, thank you for the clarification! Somehow I have always thought about showing the intent of keeping the data structure immutable when using const. I have changed it in the article. One more learning for me today :)
Re:JavaScript fundamentals before learning React by Silhouette on Friday July 20, 2018 @07:46AM (#17574191)
I've found this is often a problem in languages that pass simple values by value but compound values like objects and arrays by reference. The meaning of a qualifier like const and the inference a reader can draw when they see it can fundamentally change depending on what type of value it is applied to.

My experience has been that both the change in value/reference behaviour and the lack of total immutability guarantees when using a const qualifier on a reference type can be a source of bugs.

Languages like C and C++ distinguish more explicitly between value and reference semantics, and likewise between constant pointers and [mutable] pointers to constant data. To some extent that removes those sources of bugs, at the expense of having to write more explicit but verbose types like `const SomeType &` instead of `SomeType` all over the place.

Re:JavaScript fundamentals before learning React by chocolatemario on Thursday July 19, 2018 @09:55PM (#17571883)
If you don't know about references, I feel like you shouldn't be touching react. React is quite easy to pickup if you have an understanding of JavaScript and the Dom. The tooling surrounding react is an entirely different story.
Re:JavaScript fundamentals before learning React by BigJono on Thursday July 19, 2018 @10:28PM (#17571994)
To be fair, beginners should stay far away from most of the tooling around React. In-fact a lot of veterans should too. Most people only use stuff like Redux or Immutable out of habit rather than necessity, or as a crutch to constrain themselves away from bad habits that they shouldn't have picked up in the first place.