ryanmcdermott / clean-code-javascript

:bathtub: Clean Code concepts adapted for JavaScript
MIT License
91.12k stars 12.21k forks source link

Avoid Side Effects (part 2) - Uses confusing pass-by-reference terminology #321

Closed jdsandifer closed 2 years ago

jdsandifer commented 3 years ago

Pass by reference is an older concept that doesn't truly exist in most modern programming languages. Because of this, it's meaning has become confused and twisted. It's also generally discouraged where it does exist. I recommend using different terms to avoid this confusion.

To understand true pass by reference, please look at this example code from the PHP docs here: https://www.php.net/manual/en/language.references.pass.php.

<?php
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
// $a is 6 here
?>

Note how the how the original variable $a has it's value reassigned to be 6 because $var becomes an alias to that variable - it's like they're the same variable.

This is not possible in JavaScript.

Even with objects and arrays in JavaScript, you can't assign a new object to a variable outside a function by assigning the new object to a parameter of that function. (You can only change the original object using the parameter.)

Because of this, I recommend using the concept of Mutability to convey the information about how objects and arrays can be changed when passed to a function. Null, undefined, booleans, numbers, and strings are all immutable values and we don't need to worry about a function changing them. Objects and arrays are mutable values and thus we need to take care that we don't unintentionally change their contents/properties inside a function.

I want to credit Dan Abramov and his Just JavaScript course with introducing me to this idea. I've used JS for years and have only recently solidified my understanding of pass by value and this newer way of thinking about it recently thanks to him.

jdsandifer commented 3 years ago

Or alternatively, I think the sentence about pass by reference could simply be removed and the section would still make sense.

I'd be happy to submit a PR for either option.

ryanmcdermott commented 3 years ago

Thanks! Good explanation. I would be more than happy to accept a PR. ---------------------------- Ryan McDermott github.com/ryanmcdermott https://github.com/ryanmcdermott

On Tue, Sep 22, 2020 at 2:15 PM J.D. Sandifer notifications@github.com wrote:

Or alternatively, I think the sentence about pass by reference could simply be removed and the section would still make sense.

I'd be happy to submit a PR for either option.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ryanmcdermott/clean-code-javascript/issues/321#issuecomment-696985871, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHAWKWFMOCFBIC6TMAHB3LSHEHWNANCNFSM4RWH5HHQ .

Kahumasolomon commented 2 years ago

I here I have tried programing now its 8 years gone into basics from basic, html, css, Javascript, php, python, c, Java but always but always stuck in the middle, I can do some console and cmd programs but have failed to get further please help me with some advice where to start with and which language I should.....

RiskySaptra commented 2 years ago

I here I have tried programing now its 8 years gone into basics from basic, html, css, Javascript, php, python, c, Java but always but always stuck in the middle, I can do some console and cmd programs but have failed to get further please help me with some advice where to start with and which language I should.....

jdsandifer commented 2 years ago

This isn't the best place to ask about learning/career advice, but I know it can be hard to find sometimes so here's a quick answer:

freeCodeCamp

That's really the only advice I think you need. Go through the certification programs in order - they're completely FREE - and do the projects completely. This is key - make real things and don't start the next thing until you've got the current one working and in a finished state (something you'd be happy to show off in an interview or to your family and friends).

I would also recommend not skipping any of the challenges or projects. If you've already learned the material, you can skip the intros and just do the steps to get the tests to pass. (It'll either go really quickly or you'll realize you didn't understand it as well as you thought and can go back and review the information.) They also have a great forum if you need help on the projects.