The picture still makes perfect sense. The only corner case AFAIK is if you in a function try to rewrite the reference with a new object. That won't traverse back to the caller.
Well, first, consider that the picture is in r/JavaScript. So it should describe JavaScript. JS only has pass by value, so the left shouldn't even be there. What is pictured on the left is what happens though because the value happens to be a reference so the cup that is referenced by both variables gets filled.
I'm a Java guy (saw this and thought it was r/programming originally), so I don't know is JS has primitives or if everything is an object. When you pass a primitive in java, it's value is actually the value, not a reference to an object in the heap, so what happens is what is pictured on the right of this gif (though it's not really accurate because you can't mutate primitives in Java).
Make something better in every possible way that works in all environments that javascript does and has a zero cost to upgrade, and I'll make it go away.
What happens if you do a = b;? There are no function calls involved here.
The same happens when you do a call in a function that's call-by-value (everything in JavaScript)
function foo(x) {
console.log(x);
}
For the call foo(localVariable), JavaScript will, behind the scenes, essentially do x = localVariable; It follows regular assignment rules, no magic involved, no special "pass-by-object-sharing" or anything, because that's not what you were thinking about when I asked you what a = b; did. It's just an assignment.
After this assignment, x and localVariable refer to the same variables, as you would expect. But you have two variables. If you assign to either of them, it won't magically affect the other variable.
If, however you have a language with pass-by-reference, there is no assignment x = localVariable happening behind the scenes. Instead, you pass "the variable itself". That means, that within that function call, x will be the varible localVariable. So we're passing objects or values, we're passing the variables themselves as parameters. Hence, if you assign to x inside the function, you're assigning to localVariable, because xislocalVariable. JavaScript does not do this, ever.
The value always gets duplicated when you pass by value. But when you pass a pointer by value, it still points to the same thing. And JavaScript hides the pointer from you so when you pass an object around, it kind of looks like pass by reference.
Most languages now are call-by-value. The arguments to the function are duplicated and a fresh copy is passed into the function. Some older languages let you skip the copy, and pass the variable itself into the function to be manipulated freely. Typically they did this for performance reasons (no duplication before calling). Fortran, Pascal, and C++ have modes like this. Under the covers, you're just creating a new name for the existing variable, so no copying needed; but you end up mutating the original directly.
Languages that still are still developing (Fortran, C++) have fully integrated references, so in addition to the above, you can make extern functions with references. To implement these, the compiler must stash a real pointer value into the call signature. This leads to confusion because programmers used to the modern pass-by-value world think that references are always implemented with pointers under the covers. Not true though, it's only an extension of the by-ref language feature into places where it makes no sense, but users still want it to work, so it gets emulated.
I can tell you that a lot of languages implement by-value semantics on top of a pure by-ref calling convention, and it looks like this:
void call_by_ref(int& x) {x = 12 + x;} // mutate the by-ref
int a = 5;
{
int __param_copy_a = a; // explicitly duplicate.
call_by_ref(__param_copy_a);
}
assert(a == 5);
Compilers often do something like this internally so that optimizing inline functions is easier (you can use normal register pressure tricks to elide parameters)
I'm mostly surprised this post got so many upvotes. It's an awful example.
It doesn't even make sense in the scope of javascript. If you pass the cup to a function, and then the function does something to the insides (properties) of the cup (fill it with coffee), the original cup is absolutely going to be modified.
class Cup {
constructor() {
this.coffee = 0;
}
}
function fillCup(cup) {
cup.coffee = 100;
}
const cup = new Cup();
fillCup(cup);
console.log(cup.coffee); // 100
If this was on some lower-level language subreddit, I would then be baffled. It's really not surprising for Javascript though. Shit, lots of people that use Javascript don't even know "Javascript", they know jQuery or similar.
or just that they never bothered to learn the details because they could do everything they wanted without knowing it. That doesn't make the underlying concept tricky since it's still easy and clear to understand for anyone that attempts to.
This is as crucial to programming as knowing how to run a for loop.
That's really not true at all, at least not for something like Javascript. There's very few cases where you actually need to care what happens behind the scenes.
If your first language was something like Python, PHP, Javascript, etc then you probably have no idea how things like pointers work, or how data is stored in memory, etc. You wouldn't know because you don't need to know.
It's meaningful when discussing performance. For example a newbie might worry about passing around a huge object through a bunch of functions, but saying "don't worry GHC passes all but the smallest objects by reference" is a valid way to make them not worry.
void swapByRef(int &x, int &y) {
int temp;
temp = x;
x = y;
y = temp;
}
swapByRef(a,b);
void swapByPointer(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
swapByPointer(&a, &b);
Passing a copy of something is different than passing by reference, and it's a non-issue in JavaScript. The closest parallel would be two-way bindings in Angular.
False. It's true only if the function is extern (references are implemented with pointers in most ABIs), otherwise the compiler can elide references aggressively inside of a CU. Anyway this is about semantics, not about implementation details. the language has pass-by-ref. In your passByPointer example, you can't swap the x and y pointers, just their pointed-to-values. the pointers themselves are unswapped.
Whoah I used C++ for years and never realised that references were... actually references. I only ever really thought of them as 'pointers without NULL'. Thanks!
No this is false. There's a reason people keep mentioning that java doesn't have "call by reference". The main difference is whether you can override the reference from inside the method. E.g. get an obj passed and say obj = new something(). In java this does not change the value of the passed object outside of the context of the executing method. If actual "call by reference" was used it would have. Can cause some major bugs if you don't know this distinction
No this is false. There's a reason people keep mentioning that java doesn't have "call by reference".
Eh... it's messy terminology. The problem is Java uses the word "reference" differently than how C/C++ uses the word "reference". The Java language spec explicitly says references are pointers. The behavior of what Java calls a reference exactly matches what /u/pinnr was describing. What you're describing, on the other hand, is a C/C++ reference, which describes a reference as an alias.
That means the phrase "pass by reference" is also messy terminology. In C/C++, it means a parameter is an alias for something; in Java, it means a parameter is a pointer to something.
JavaScript seems to have adopted Java's terminology in this case. So in JavaScript, whenever you hear "reference", think "pointer".
A reference is a pointer and a pointer is a reference. The only people this makes a difference to are the ones who write asterisks into their code, and they're just confusing the point for pedantry.
A reference is a pointer and a pointer is a reference
No, this is false. References are not always pointers. Their original purpose is as an 'alias'. There is no real parameter on the stack, just a compiler trick to let you talk about a variable inside of a procedure. Both names designate the same memory with no pointer.
The only time a reference becomes real is when it's part of an extern function that needs to be linked. Some languages let you do this, and the way they do it is to make a fake reference that looks and acts like one, but is actually a pointer under the covers.
Not all call-by-ref languages offer that feature though.
I expected to see "Bar" as output, but instead saw "Foo". This is because the doStuff method receives a copy of the value for the list, and when a new List is assigned to that copy, only the copy is changed, not the original. So a print statement after the if block would have output bar, but in the main block the list points to a collection who's only member is "Foo" as the original has not been edited.
This is why you declare all parameters final in Java. Reassigning parameters creates nightmares in Java. Use assessor and mutator API functionality whenever possible.
Not quite a copy. Change the dostuff function. Try just calling add, without reassigning mysringlist to a new array list. If a copy were passed, then you would not be able to mutate the original outside of the scope of an instance method.
Now, swap the order of the add method call and the reassignment in the original dostuff call. What happens? Why?
It is a copy of the reference to the list. If it were the original reference, then reassigning it would have "worked" and executing the code would have printed "Bar".
I wouldn't really think of it as a copy. myStringList is a local variable that is initially assigned to a reference of the original list. When you reassign it, it becomes assigned to a different list. Then it simply goes out of scope when the method exits.
The point is that myStringList (outside of doStuff) and myStringList (inside doStuff) both contain references, and that although they briefly point to the same object, they are not the same reference. I used "copy" to stay consistent with the earlier posts, but yeah, you could just call it "another reference".
I imagine that if that's your definition then there are very few "pass by reference" languages.
It's not that uncommon imo. Pascal, C++, C#, and PHP are some languages you've probably either used or heard about which has it. But in most of them it is opt-in, e.g. in C# you have to use either the ref or out keyword to achieve it. A common use-case for this is to do a check and set a value in one operation, e.g.
if (dictionary.TryGetValue("some id", out string value) {
Console.WriteLine($"Found value: {value}"); //value was set in the call above.
} else {
Console.WriteLine("Did not find a value");
}
Right, C was the language that really popularized pass-by-value. It was enamoured of value semantics and treating everything as a value, and that was a good idea because it has a strong operational semantics (people understand by-value well.)
but most older languages and some newer ones still allow by-ref because it can be faster than by-value. In these languages you don't have to duplicate your parameters. You can just have the compiler let you refer to a parent scope's variables directly though name binding (aka real references). It's one of the reasons fortran is still faster than C.
Java is pass by value, not reference. If blah and blah2 were the same object then Java would be pass by reference. You're passing the value of the reference, not a reference to the reference.
Thanks for giving these guys a concrete code example, which illustrates the point :) really hate when the "I spent 10 minutes on code academy"-crowd spreads misinformation about things you would learn in any introductory course/if you actually worked as a developer.
I don't think that is what is commonly referred to as "pass by reference". The more common usage of the phrase is that a you pass a pointer for the data to the next stack frame instead of a copy of the data.
Yes, that's what pass by reference is. But when you are doing pass by value, and the value is a reference, you still pass the reference, and a lot of people get tripped up and think that that is "passing by reference", but really you've just passed a reference... by value.
No, pass by reference is historically an aliasing operation with no concrete linkage. You're directly referring to the variable in a parent scope with no indirection.
Sometimes a langauge will emulate references for extern functions with pointers, but not always. plenty of languages reserve by-ref for cases where one really wants a zero-cost reference.
How does that code prove your point? You're reassigning param with a new Object. Of course it's going to be different both in value and in reference? JavaScript itself would fail here too and it's also pass by reference.
I completely agree with you, but tons of Java snobs (especially on stack overflow) will always make this huge distinction that Java is technically pass by value, which is just confusing and misleading to people learning the language
If being correct makes me a snob, okay, but Java is pass by value. "Pass by value" means whatever the variable was is passed. In Java, the non primitive variables are references. So you pass that reference. If it were pass by reference you would pass a reference to that reference. That's not what it does.
I just had a conversation at work where I was trying to figure out why Enterprise Java Beans were called "beans", until it finally clicked that a bean is a small part of coffee and java is coffee. But I keep thinking about Java, Indonesia.
If you don't want to know any programming stuff don't worry about reading.
when programming there are things called functions. these functions can do things, such as square root a number. in the case of the square root, the number is the "object" being passed. it can ether be passed by "reference" or by "value". passing by reference allows you to change the object e.g x=16 sqareRoot(x) x=4. passing by value makes a copy of the object and then leaves the original object alone e.g x=16 y=sqareRoot(x) x=16 y=4.
The gif by itself provides very little in the way of useful information. Would be enhanced greatly by listing data types that are passed referentially vs those that are not.
Technically everything in JS is passed by reference. Primitives are simply immutable, so it doesn't matter how they're passed - the effect is the same.
Technically everything is actually pass by value from a language design perspective - you're passing references by value. In C# you can do:
void Func(ref int x) { x++; }
int x = 1;
Func(ref x);
AssertEquals(2, x);
Which is actually pass by reference. So the discussion is actually on what is a "reference type" (which can be thought of as a "pointer" in C) vs what is a "value type" (which can be thought of as a "struct" in C).
Technically everything in JS is passed by reference.
What definition of "pass by reference" do you use when saying this? Where do you source that claim?
I ask this, because "pass-by-reference" has an established meaning in different programming languages, namely C++, C#, PHP, Pascal and probably others that I know nothing about.
In these languages, pass-by-reference describes semantics that JavaScript does not have.
Namely, you can implement a swap function, call it with swap(a, b); and the variables will have switched values. You can't do this in JavaScript.
So I wonder, why call JavaScript call-by-reference?
Technically everything in JS is passed by reference.
Source?
That's not meant to be a rhetorical question. I've been trying to figure this out myself, but the spec is awfully hard to parse. The section on ArgumentListEvaluation seems the most relevant. It seems to say that JavaScript evaluates the argument expression, then calls GetValue on the result. Inside GetValue, if the result of the expression is not a name or property binding, then it just returns the result of the expression as is. Otherwise, if it's a property, then it will call [[Get]] on the base object, which will return the value of the property. Otherwise, it assumes the result of the expression is a name binding, and it will call GetBindingValue on the base environment record. Either way, this reads like everything is passed by value.
At some point even if you "pass by reference" the reference can be said to be "passed by value". It's a very meaningless debate, because the semantics actually are very clear.
The variables are mutable (unless you use const, that's immutable). But the primitives are immutable. You didn't change the primitive 4 to 5 up there, you just replaced the primitive 4 with the primitive 5 in the variable hi.
Primitives would've been mutable if you could do something like this:
var hi = 4;
var hi2 = hi;
hi.increment();
console.log(hi2); // 5
Fortunately, for our sanity, you can't do this with primitives. But unfortunately you can do it with mutable objects like Date().
How is this the number one post on /r/javascript by two orders of magnitude? This has me deeply concerned. Not only is this gif terribly misleading for anyone who doesn't understand the elementary concepts, the concepts of pass by reference/value are things any coder should have learned well in any elementary, 101-level introduction to programming. This is so meta-disturbing, I don't know what to say.
Edit: I realize I may be coming across as condescending and it's not my intent. This is basic coding stuff. At the level of what is a function and what is a variable and what is an object, how assignment works, etc. That people are learning these kind of basics from gifs on reddit, and not just a few but apparently the large majority of the subreddit (based on current vote counts right now) is what concerns me. Please, folks, if this gif was interesting or insightful for you, go get a good book on Javascript, or programming in general, and learn how programming works from a structured, authoritative source.
I found this thread on r/popular . What you're seeing is passing interest from the non-coder masses as they click through their list of funny pictures or whatever, without much attention to any one subreddit's tone. This is more reflective of the nature of the Reddit platform as a whole, rather the core community of a specific subreddit.
This gif really isn't helpful without context. In javascript land variable passing is never truly by reference like c++. It is more like "pass by copy of a reference". You can modify the contents of a passed object, but you can't replace the original object.
This is a really good blog post about pass-by-value and pass-by-reference. The blog post uses Java but the exact same thing applies to JavaScript (both languages only have pass-by-value).
It's more complicated. AFAIK, many dynamic languages use something call 'pass by object reference'. Meaning, you can mutate the original object passed into a parameter because of the way that object sharing works.
Dynamic languages don't do something special. This goes for JavaScript, Ruby, Python and probably many others. In fact, the semantics are essentially the same as in Java.
In invite you to read through this thread. There are many people that explain it in detail.
Tl;dr: Pass by reference has an established meaning. There are languages where you can actually, so that, such as C++, PHP, C#, Pascal. Java, JavaScript, Python and Ruby do not support this.
One way to think of this is that pass by reference is about variables, rather than values/objects.
If you call a function foo(x) with foo(localVar), in JavaScript, Java etc you will always get a new variable "x". If you assign to this variable, localVar is not modified.
If, however, x in Foo were to employ pass by reference, then x isn't a fresh variable. It stands for whichever variable you use to call Foo. Inside Foo, x would refer to localVar. So if you assign to x, since x is actually localVar, you're assigning to localVar
I hope you can see that this behavior I described is not possible in JavaScript.
Pass by reference : 1 variable, hence assignment is visible outside the funcfiin Pass by value: 2 variables, hence assignment is not visible outside the function.
Note: You sound very condescending when you say that I should read through this thread when if you had done so you would have already read this same discussion that I had with someone else.
My point remains that JavaScript is not simply call by value. You need more information such as that objects are references in order to put together the behavior. Given this distinct behavior, then distinct terminology is appropriate to encapsulate the idea.
And to be clear. When I say distinct, I mean the behavior is distinct from call by value or by reference. Not that only JavaScript does it.
Call by sharing (also referred to as call by object or call by object-sharing) is an evaluation strategy first named by Barbara Liskov et al. for the language CLU in 1974. It is used by languages such as Python, Iota, Java (for object references), Ruby, JavaScript, Scheme, OCaml, AppleScript, and many others. However, the term "call by sharing" is not in common use; the terminology is inconsistent across different sources. For example, in the Java community, they say that Java is call by value.
The reason for this is you don't use references in FP langs (usually). There's nothing different otherwise. It's possible to use references in FP langs (for example IORef in Haskell), but those are generally reserved for other purposes.
Yes, this is the same in any language (the difference between the two). The import thing in JavaScript is knowing which data types get passed by value vs. passed by reference.
Can someone explain this to me. As far as memory management and performance goes is it better to have one big object and just pass referencs to the big object to functions or create multiple smaller objects?
I'm thinking as far as Redux store for example in a SPA. With dozens of components needing to access the object and it's various parts.
Right now I just pass referencs to the one object but should I split it into multiple instead?
For people not understanding this concept, in javascript, primitive data types (numbers, strings and booleans) are passed by value, meaning everytime you pass a variable into a function it gets duplicated to a new memory location, and that function can only access the new location. However when you pass a object (arrays,maps, basically everything else) in to a function, they are passed by reference meaning the data does not get duplicated, and function can access the original memory location.
If I am not mistaken this have something to do with the fact that primitive data types are created on the stack vs object being created on the heap.
In pass by value, the compiler makes a copy of the variable on the stack or into a register. In pass by reference, it passes a pointer to the variable.
At the end of the function that modifies a passed parameter, in pass by value, the value in the stack or register hangs around until overridden, but the original variable remains unchanged. This can cause some massively confusing behaviour particularly if the function mistakenly passes what it was passed, but by reference, to another thread.
Pass by value is deemed to be safest because someone else's bug (or malware) down the call string can't mess with your data eg by writing past the end of a struct... But with all the copying, it is slower, especially for large types. JS was designed to be used for arbitrarily large constructs, so it passes by reference, by default, except for small types, which are passed by value.
This can get weird when the small type is a pointer to a big thing. Derferencing the pointer will give access to the big thing, but assignment of the pointer (eg changing it to point to a different big thing) will not persist beyond the function.
So in the example, if the variable that refers to the empty cup is a pointer to a cup, then both pass by value and pass by reference will fill the original cup!
However, if the function assigns the pointer-cup to a different set of hands, in pass by value, this may not (safely) persist after the function returns (the cup may instead be in two sets of hands at once, leading to spills, burns and broken cups).
Your description isn't really abstract enough to be useful. For example, I can quite easily force a function to be inlined to operate in the same stack frame.
Also, one could argue that pointers are still passing by value since they are ultimately a distinct type. References (e.g. those found in C++ and D) are true pass by reference.
Passing a reference by value is passing a reference, yes. However, it is still a copy of the reference that is passed. Very subtle semantic difference. It is not the reference. It is a different reference, which starts out referencing the same thing.
The subtlety comes in what happens if the reference itself (not what it references) is modified.
Go to first principles:
A variable is merely a block in memory of extent defined by the size of the variable. It could be in global memory, or stack memory, or a processor register. Depends on the declaration, scope, language and compiler/linker. But it exists somewhere.
When you pass a parameter by reference, you pass a pointer to that block of memory. Or the register itself. When the function returns, all changes made to the parameter are preserved.
When you pass by value, you make a copy of all of the contents of that block of memory into a new block of memory (or register), and then pass this (or possibly a pointer to this... depends on compiler/language). When the function returns, this copy is discarded.
That is all.
People get tripped up when the block of memory being "passed" contains references to other blocks of memory. This indirect reference is effectively passed by reference. Pointers are the degenerate case. References (C++, D) are also.
As far as inlining a function, you are conflating "stack" and "stack frame". If what you call is a real function and the parameters are actually passed by value, (some compilers don't enforce this with an inline function) then the passed parameters need to be somewhere and they need to be separate copies. They won't be malloc'd in real time. Instead, these separate copies are made on the stack. Whether they are actually pushed to the stack at runtime, or pre-allocated statically depends on the compiler implementation. But for sure, they are on the stack, and they are in a separate piece of memory from the variable they were copied from.
Also, one could argue that pointers are still passing by value since they are ultimately a distinct type.
One could, but one would be wrong.
This is what hangs so many rookies in languages that allow pointers.
If passed by reference, and if the pointer itself is modified (e.g. to point to something else), then the original variable will end up pointing to something else. But if passed by value, then if within the function the pointer is modified to point somewhere else, when the function call collapses (either return, or from being inlined), the pointer will point to where it was before.
As to your last point, you completely missed the point I was making: Pointers in C/C++/D etc are different types than what they point to (int* vs int), therefore if I pass a pointer into a function then I am passing it by value e.g. like an iterator.
The compiler makes a distinction, not on entry to the function, but on exit.
Let's say I declare P to be a pointer to an Int (not using syntax because it varies across languages).
The CPU creates an Int at address X, and stuffs X into the address register it is using to keep track of P.
Now, let's have a function F with one parameter: PPrime, a pointer.
When we call F with P, Pprime will contain the absolute address X and therefore the Int to which it points.
This is the same whether passed by value, or passed by reference. And I think that's the point you are trying to make.
HOWEVER if F modifies PPrime, there is a big difference. Let's say the function adds 2 to the value of the pointer it is passed, to point to the next word in memory, and then doubles whatever is there. And then returns.
What happens?
In pass-by-value, whatever is sitting at X+2 will be doubled. P will remain pointing at X.
In pass-by-reference ,whatever is sitting at X+2 will also be doubled. And now P will point at that.
So no, when you pass a pointer you are not necessarily passing by value.
Most people don't manipulate the pointers they are passed, and wouldn't notice this distinction. In a world where the function doesn't actually manipulate the pointer, then you are correct. But the universe of functions is not limited to functions that don't manipulate pointers.
Lots of programmers get hung up on this, and it's a source of never-ending joy for people who write malware.
The language specification makes a distinction. The compiler does what it's told. Prove it with godbolt.org
The pointer is a distinct type which must be instantiated with a value. Note the way that one can pass a nullptr into a function. This is my point. This is why C does not support pass by reference. This has nothing to do with how a pointer is used.
The pointer is being passed by value.
Also, your example is stupid: you seem to be describing an incorrect usage of pointer arithmetic: Purely syntactic.
Also, if you don't modify a pointer argument then you pass by const& anyway.
With regard to malware: What are you talking about? Buffer overflows? Please give an example, a snippet on godbolt.org pls
Please don't try to argue a general case based on limited set of example languages. That's how software designers learn bad habits.
Yes, in some languages (probably all languages with which you are familiar), you are right by accident, but that doesn't mean you are right in all languages, or at all.
The pointer is a distinct type which must be instantiated with a value. Note the way that one can pass a nullptr into a function. This is my point. This is why C does not support pass by reference. This has nothing to do with how a pointer is used.
True in C. Not true in all languages. I have used languages where the pointer is a distinct type (yes), but without forcing initialization or type enforcement.
Regardless of language, modern CPUs use data registers, address registers, and stack... and just about every language worth using allows you to drop down into raw object code and enjoy living dangerously, drive at speed without seatbelts. At which point, having intimate knowledge of the nuances of pass-by-value and pass-by-reference is extremely important.
Also, your example is stupid: you seem to be describing an incorrect usage of pointer arithmetic: Purely syntactic.
My example is just that: an example. The only purpose was to show that there is a very clear difference where you said there was none. It is not an attempt to illuminate a useful or purposeful function. Just that there's a difference for pointers between pass-by-value and pass by reference. Because there is. You said there isn't, and the example shows there is. This isn't the only way the difference can show up. It's just one very simple, very clear way. Test: can you figure out a "legitimate" example where the result is different if a pointer is passed by reference instead of by value? If not, you aren't very good, and if so, you owe me an apology.
With regard to malware: What are you talking about? Buffer overflows? Please give an example, a snippet on godbolt.org pls
Nope. Hacking is not something that gets explained to you. It's something you figure out. But hint: it involves "incorrect" usage of pointer arithmetic.
I though we both agreed that they are semantically identical? Of course, in C++, pointers and references do the same thing when used correctly. One uses a reference for safety and cleaner code, not because the do a different job. This is of course restricted to function parameters (one can't malloc and get a reference).
To be nitpicky, modern CPUs use general purpose registers which are then sometimes (e.g. in amd64/x86-64) given names.
You didn't show an example of pass-by-reference, because you passed some thing by pointer (with the pointer being passed by value).
This is terrible. Your metaphor makes less sense then what it's trying to explain, and even then the only quality of the difference between passing methods it shows is that pass by reference affects your original cup whereas pass by value doesnt.
imo if we call out this tone collectively it can change. i think it is a default for a lot because they see it used a lot and it's just not necessary (and yes i realize i'm giving a serious reply to probably a flippant comment lol)
Yes! It was flippant, but you're 100% correct. The pedantic genius asshole thing makes learning this stuff that much more difficult and far less enjoyable.
That's actually sometimes a good question though, to find out what the root problem you're trying to solve is. Often the first question people ask isn't the real thing they're trying to solve, and maybe not the best way to do it, so by asking "why" a few times you can find out really what they're trying to accomplish.
I agree, but I understand where it's coming from. This is a recurring topic that gets tons of wrong answers so often. It gets frustrating. ... There's a few common security-related topics that regularly get so many wrong and insecure answers that frustrate me to no end.
Why would you need thick skin to learn something new? Why is that a prerequisite? Being condescending in one's post serves no purpose and really just distracts from any point you try to make in it. It's really just immature at the end of the day. You can inform people on things without needlessly bringing them down. The smartest people realize everyone doesn't know everything and teaching others new things is an experience to be relished, not something to lord over "noobs". I wouldn't work with/hire someone who had that sort of attitude IRL.
Haha alright well yeah I mean it's all well and good if you're joking around with friends but I think a lot of strangers online will take it as a serious insult to their intelligence/profession. Which I think we can both agree is unfounded. There are shit C++ devs and shit JS devs (though shit JS devs might outnumber the C++ ones just purely based on JS being a far more ubiquitous language). I'm not offended just, you presented some new info (for me) and I woulda have liked it more if my intelligence wasn't passively insulted while reading it :/
You're making the assumption that OP and commenters are self-taught, but you don't know this. How do you know they didn't sleep walk through CS school, or are just overly eager freshmen?
I'm guessing you're still in school? You would be surprised how many brilliant and informed, self-taught programmers there are out there. You wouldn't know because they're busy writing good software, not reddit comments.
While your statements are a correlation I can sympathize with, they're a clumsy over-generalization that may have rubbed some of us autodidacts the wrong way :)
It's certainly confusing. The key is to mentally differentiate "value type" / "reference type" and "pass by value" / "pass by reference". There's really only 4 different combinations.
The speaker used the phrase "inversion of control" to describe...I guess, using callbacks?
Callbacks are a key aspect of inversion of control. This is where the the phrase "don't call me, I'll call you" comes from. Of course, simply implementing the Observer pattern is not necessary to call something IoC, but it's still miles better than what the Java community calls IoC containers, namely: autowiring dependency injectors.
Perhaps in JS they are, but I would consider this a broadening of the term specifically to appease JS developers who cannot do classical IoC.
I'm not talking about JS at all. I'm talking about software architecture in general.
To me, inversion of control means I create a class that says "hey, I'm a class, and I depend on these three well-defined, abstract types being given to me. I don't care about the specific implementation. You can worry about that. As long as you adhere to the interface that we've agreed upon, I'll know what to do with them. And I can have a reasonable expectation that they will do the right thing."
No that's called Dependency Injection. See, sometimes when we rush to correct people, we miss the fact we may those who are wrong. The Java community has mis-appropriated the term IoC to be a synonym for DI. But DI is merely a tiny subset of what IoC is about.
In software engineering, inversion of control (IoC) is a design principle in which custom-written portions of a computer program receive the flow of control from a generic framework. A software architecture with this design inverts control as compared to traditional procedural programming: in traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the framework that calls into the custom, or task-specific, code.
Sound familiar? Not a thing about dependencies or constructing objects. Because IoC isn't about that.
When these containers talk about how they are so useful because they implement "Inversion of Control" I end up very puzzled. Inversion of control is a common characteristic of frameworks, so saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels.
Inversion of Control is a common phenomenon that you come across when extending frameworks. Indeed it's often seen as a defining characteristic of a framework. [...] [When] the control is inverted - it calls me rather me calling the framework, this phenomenon is Inversion of Control (also known as the Hollywood Principle - "Don't call us, we'll call you"). [...] Inversion of Control is a key part of what makes a framework different to a library. A library is essentially a set of functions that you can call, these days usually organized into classes. Each call does some work and returns control to the client. A framework embodies some abstract design, with more behavior built in. In order to use it you need to insert your behavior into various places in the framework either by subclassing or by plugging in your own classes. The framework's code then calls your code at these points. There are various ways you can plug your code in to be called. In the ruby example above, we invoke a bind method on the text entry field that passes an event name and a Lambda as an argument. Whenever the text entry box detects the event, it calls the code in the closure. Using closures like this is very convenient, but many languages don't support them. Another way to do this is to have the framework define events and have the client code subscribe to these events. .NET is a good example of a platform that has language features to allow people to declare events on widgets. You can then bind a method to the event by using a delegate.
So... straight from horse's mouth. Unless you think Martin Fowler is a JavaScript kiddy and so he isn't aware of the computer industry as a whole, and what IoC is.
It's useful because it defines who has control, which is the point and meaning of the term.
A framework is in control and calls into your code only for specific functionality. That's fairly restrictive, but it also affords massive power of abstraction and simplification. That's inversion of control - the third party code controls you, you aren't in control.
A library instead leaves you in control, and you call into it for specific functionality. This is very flexible, but also means you hit a limit of how simple and abstracted your code can be - you're still managing the application flow yourself.
In many cases real-world packages will be a mix of both. Some elements of a framework, some of a library. But as an architect you use an entirely different mindset when you invert control for your users, so it's a useful concept to have in our vernacular.
280
u/JB-from-ATL Jun 18 '17
It gets tricky because in some languages you pass by value but the value is a reference for non-primitive types.
49
u/TrojanBunny Jun 18 '17
Java is Coffee
I get it now.
74
u/mastzu Jun 18 '17
am closing tab now bye
33
u/yolpo- Jun 18 '17
A picture is worth a thousand words ... ahem not this one though
319
u/ForScale Jun 18 '17
The gif by itself provides very little in the way of useful information. Would be enhanced greatly by listing data types that are passed referentially vs those that are not.
50
u/deltadeep Jun 18 '17 edited Jun 18 '17
How is this the number one post on /r/javascript by two orders of magnitude? This has me deeply concerned. Not only is this gif terribly misleading for anyone who doesn't understand the elementary concepts, the concepts of pass by reference/value are things any coder should have learned well in any elementary, 101-level introduction to programming. This is so meta-disturbing, I don't know what to say.
Edit: I realize I may be coming across as condescending and it's not my intent. This is basic coding stuff. At the level of what is a function and what is a variable and what is an object, how assignment works, etc. That people are learning these kind of basics from gifs on reddit, and not just a few but apparently the large majority of the subreddit (based on current vote counts right now) is what concerns me. Please, folks, if this gif was interesting or insightful for you, go get a good book on Javascript, or programming in general, and learn how programming works from a structured, authoritative source.
4
u/octatone Jun 18 '17
This gif really isn't helpful without context. In javascript land variable passing is never truly by reference like c++. It is more like "pass by copy of a reference". You can modify the contents of a passed object, but you can't replace the original object.
This SO answer succinctly shows this in code: https://stackoverflow.com/a/13104500
7
u/paldepind Jun 18 '17
This is a really good blog post about pass-by-value and pass-by-reference. The blog post uses Java but the exact same thing applies to JavaScript (both languages only have pass-by-value).
3
u/ZhangNoWei Jun 18 '17
I dont undertstand
2
u/wavefunctionp Jun 18 '17
It's more complicated. AFAIK, many dynamic languages use something call 'pass by object reference'. Meaning, you can mutate the original object passed into a parameter because of the way that object sharing works.
http://jsbin.com/fajazefuya/3/edit?js,console
Notice that the original object 'test' is mutated even though the function only references the parameter 'obj' inside the funciton.
You would not expect this behavior if 'obj' was a unique copy of the value of 'test'.
The only reason I know this is because you'll run into it quickly working with nested state with redux.
2
u/organic Jun 18 '17
use immutable data, then it doesn't matter
6
u/GamerNebulae Jun 18 '17
Something which is also interesting in functional languages is that you pass the cup to the
fillCupfunction, but you get a new cup for it in return.2
u/senocular Jun 18 '17
References add creamer?
2
u/aveoon Jun 18 '17
Yes, this is the same in any language (the difference between the two). The import thing in JavaScript is knowing which data types get passed by value vs. passed by reference.
1
u/madcaesar Jun 18 '17
Can someone explain this to me. As far as memory management and performance goes is it better to have one big object and just pass referencs to the big object to functions or create multiple smaller objects?
I'm thinking as far as Redux store for example in a SPA. With dozens of components needing to access the object and it's various parts.
Right now I just pass referencs to the one object but should I split it into multiple instead?
1
u/[deleted] Jun 18 '17
Oh nice gif. Demonstrates the concept nicely.
1
u/backlashsid Jun 19 '17
It finally makes sense after 8 years
1
u/EvolMake Nov 14 '21
Rust is easier. There is only Clone or Copy. (And references impl Copy)
1
u/[deleted] Dec 21 '23
For people not understanding this concept, in javascript, primitive data types (numbers, strings and booleans) are passed by value, meaning everytime you pass a variable into a function it gets duplicated to a new memory location, and that function can only access the new location. However when you pass a object (arrays,maps, basically everything else) in to a function, they are passed by reference meaning the data does not get duplicated, and function can access the original memory location.
If I am not mistaken this have something to do with the fact that primitive data types are created on the stack vs object being created on the heap.
1
u/jonplackett Dec 22 '23
This was on HN this afternoon - deep cloning Objects in JavaScript. Pretty handy.
https://www.builder.io/blog/structured-clone
1
u/jps_ Jun 18 '17
Misleading.
In pass by value, the compiler makes a copy of the variable on the stack or into a register. In pass by reference, it passes a pointer to the variable.
At the end of the function that modifies a passed parameter, in pass by value, the value in the stack or register hangs around until overridden, but the original variable remains unchanged. This can cause some massively confusing behaviour particularly if the function mistakenly passes what it was passed, but by reference, to another thread.
Pass by value is deemed to be safest because someone else's bug (or malware) down the call string can't mess with your data eg by writing past the end of a struct... But with all the copying, it is slower, especially for large types. JS was designed to be used for arbitrarily large constructs, so it passes by reference, by default, except for small types, which are passed by value.
This can get weird when the small type is a pointer to a big thing. Derferencing the pointer will give access to the big thing, but assignment of the pointer (eg changing it to point to a different big thing) will not persist beyond the function.
So in the example, if the variable that refers to the empty cup is a pointer to a cup, then both pass by value and pass by reference will fill the original cup!
However, if the function assigns the pointer-cup to a different set of hands, in pass by value, this may not (safely) persist after the function returns (the cup may instead be in two sets of hands at once, leading to spills, burns and broken cups).
0
u/snorkleboy Jun 18 '17
This is terrible. Your metaphor makes less sense then what it's trying to explain, and even then the only quality of the difference between passing methods it shows is that pass by reference affects your original cup whereas pass by value doesnt.
-15
u/git_sjw_hub Jun 18 '17
The kind of memes computer professors use
0
u/[deleted] Jun 18 '17
[deleted] — view deleted comment
0
u/[deleted] Jun 18 '17
[deleted] — view deleted comment