question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jdsandifercommented, Sep 22, 2020

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.

0reactions
jdsandifercommented, Jan 21, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A "Gotcha" of JavaScript's Pass-by-Reference
You can "pass" a value into a new variable by using the value to initialize the variable. But even if we give in...
Read more >
Side Effects Of Python Functions | Towards Data Science
Passing arguments to functions in Python. If you have used other programming languages you may have heard the terms pass-by-value and pass-by-reference. Pass-by ......
Read more >
Chapter 5: User-Defined Functions: Participation Activities
Study with Quizlet and memorize flashcards containing terms like Given the PrintFace() function defined above and the following main() function: int main() ...
Read more >
Pass by Reference in Python: Background and Best Practices
You'll implement real use cases of pass-by-reference constructs in Python and learn several best practices to avoid pitfalls with your function arguments.
Read more >
Java is Pass by Value, Not Pass by Reference | DigitalOcean
The method accesses the actual parameter. Often, the confusion around these terms is a result of the concept of the object reference in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found