How much impact does sending unnecessary objects have?
I often find myself in a situation where I need to have a lot of objects as parameters for a method, but all or almost all of those objects can be referenced from another object. In my case, I've got loads of objects such as particle managers and projectile managers which are all attributes of a screen object. Sometimes I just give the entire screen object as the parameter to save time, getting whatever objects I need.
Is this good practice or not? On the one hand, it saves me time, but I don't know the impact of the extra (unnecessary) information also within the screen object when I send it as an argument. Is this inefficient?
java performance
|
show 4 more comments
I often find myself in a situation where I need to have a lot of objects as parameters for a method, but all or almost all of those objects can be referenced from another object. In my case, I've got loads of objects such as particle managers and projectile managers which are all attributes of a screen object. Sometimes I just give the entire screen object as the parameter to save time, getting whatever objects I need.
Is this good practice or not? On the one hand, it saves me time, but I don't know the impact of the extra (unnecessary) information also within the screen object when I send it as an argument. Is this inefficient?
java performance
It's no inefficient, because no copy is made. It's just a reference. However, such large objects are called "god objects" and thus that's a code smell.
– Thomas Weller
Nov 24 '18 at 20:58
Shotgun programming is never a good idea, but passing arguments (per se) isn't inefficient.
– Elliott Frisch
Nov 24 '18 at 20:58
You're only sending references to that data, so the overhead is negligible. What I'd be more worried about is that you haven't thought out the right abstractions, and therefore have code where everything needs access to everything, which can make your code hard to test and maintain.
– yshavit
Nov 24 '18 at 20:58
1
It doesn't make any difference. You don't "send objects". You pass references to objects. I.e. passing 3 references instead of 1 means that the JVM has to copy 8 more bytes (or 16 if 64-bit references are used). It's peanuts.
– JB Nizet
Nov 24 '18 at 20:59
1
There's a different kind of impact besides performance to talk about. Providing a single object as a param that allows access to all of the things the method needs can future-proof your interface thus allowing you to add new "params" by simply adding to the object and avoid having to change your method signature.
– Michael Krause
Nov 24 '18 at 21:06
|
show 4 more comments
I often find myself in a situation where I need to have a lot of objects as parameters for a method, but all or almost all of those objects can be referenced from another object. In my case, I've got loads of objects such as particle managers and projectile managers which are all attributes of a screen object. Sometimes I just give the entire screen object as the parameter to save time, getting whatever objects I need.
Is this good practice or not? On the one hand, it saves me time, but I don't know the impact of the extra (unnecessary) information also within the screen object when I send it as an argument. Is this inefficient?
java performance
I often find myself in a situation where I need to have a lot of objects as parameters for a method, but all or almost all of those objects can be referenced from another object. In my case, I've got loads of objects such as particle managers and projectile managers which are all attributes of a screen object. Sometimes I just give the entire screen object as the parameter to save time, getting whatever objects I need.
Is this good practice or not? On the one hand, it saves me time, but I don't know the impact of the extra (unnecessary) information also within the screen object when I send it as an argument. Is this inefficient?
java performance
java performance
edited Nov 24 '18 at 21:02
Robin Green
22.5k876156
22.5k876156
asked Nov 24 '18 at 20:53
BlueCPBlueCP
3613
3613
It's no inefficient, because no copy is made. It's just a reference. However, such large objects are called "god objects" and thus that's a code smell.
– Thomas Weller
Nov 24 '18 at 20:58
Shotgun programming is never a good idea, but passing arguments (per se) isn't inefficient.
– Elliott Frisch
Nov 24 '18 at 20:58
You're only sending references to that data, so the overhead is negligible. What I'd be more worried about is that you haven't thought out the right abstractions, and therefore have code where everything needs access to everything, which can make your code hard to test and maintain.
– yshavit
Nov 24 '18 at 20:58
1
It doesn't make any difference. You don't "send objects". You pass references to objects. I.e. passing 3 references instead of 1 means that the JVM has to copy 8 more bytes (or 16 if 64-bit references are used). It's peanuts.
– JB Nizet
Nov 24 '18 at 20:59
1
There's a different kind of impact besides performance to talk about. Providing a single object as a param that allows access to all of the things the method needs can future-proof your interface thus allowing you to add new "params" by simply adding to the object and avoid having to change your method signature.
– Michael Krause
Nov 24 '18 at 21:06
|
show 4 more comments
It's no inefficient, because no copy is made. It's just a reference. However, such large objects are called "god objects" and thus that's a code smell.
– Thomas Weller
Nov 24 '18 at 20:58
Shotgun programming is never a good idea, but passing arguments (per se) isn't inefficient.
– Elliott Frisch
Nov 24 '18 at 20:58
You're only sending references to that data, so the overhead is negligible. What I'd be more worried about is that you haven't thought out the right abstractions, and therefore have code where everything needs access to everything, which can make your code hard to test and maintain.
– yshavit
Nov 24 '18 at 20:58
1
It doesn't make any difference. You don't "send objects". You pass references to objects. I.e. passing 3 references instead of 1 means that the JVM has to copy 8 more bytes (or 16 if 64-bit references are used). It's peanuts.
– JB Nizet
Nov 24 '18 at 20:59
1
There's a different kind of impact besides performance to talk about. Providing a single object as a param that allows access to all of the things the method needs can future-proof your interface thus allowing you to add new "params" by simply adding to the object and avoid having to change your method signature.
– Michael Krause
Nov 24 '18 at 21:06
It's no inefficient, because no copy is made. It's just a reference. However, such large objects are called "god objects" and thus that's a code smell.
– Thomas Weller
Nov 24 '18 at 20:58
It's no inefficient, because no copy is made. It's just a reference. However, such large objects are called "god objects" and thus that's a code smell.
– Thomas Weller
Nov 24 '18 at 20:58
Shotgun programming is never a good idea, but passing arguments (per se) isn't inefficient.
– Elliott Frisch
Nov 24 '18 at 20:58
Shotgun programming is never a good idea, but passing arguments (per se) isn't inefficient.
– Elliott Frisch
Nov 24 '18 at 20:58
You're only sending references to that data, so the overhead is negligible. What I'd be more worried about is that you haven't thought out the right abstractions, and therefore have code where everything needs access to everything, which can make your code hard to test and maintain.
– yshavit
Nov 24 '18 at 20:58
You're only sending references to that data, so the overhead is negligible. What I'd be more worried about is that you haven't thought out the right abstractions, and therefore have code where everything needs access to everything, which can make your code hard to test and maintain.
– yshavit
Nov 24 '18 at 20:58
1
1
It doesn't make any difference. You don't "send objects". You pass references to objects. I.e. passing 3 references instead of 1 means that the JVM has to copy 8 more bytes (or 16 if 64-bit references are used). It's peanuts.
– JB Nizet
Nov 24 '18 at 20:59
It doesn't make any difference. You don't "send objects". You pass references to objects. I.e. passing 3 references instead of 1 means that the JVM has to copy 8 more bytes (or 16 if 64-bit references are used). It's peanuts.
– JB Nizet
Nov 24 '18 at 20:59
1
1
There's a different kind of impact besides performance to talk about. Providing a single object as a param that allows access to all of the things the method needs can future-proof your interface thus allowing you to add new "params" by simply adding to the object and avoid having to change your method signature.
– Michael Krause
Nov 24 '18 at 21:06
There's a different kind of impact besides performance to talk about. Providing a single object as a param that allows access to all of the things the method needs can future-proof your interface thus allowing you to add new "params" by simply adding to the object and avoid having to change your method signature.
– Michael Krause
Nov 24 '18 at 21:06
|
show 4 more comments
3 Answers
3
active
oldest
votes
So you have a Screen object that has lots of useful fields that your other methods need, and you are too lazy to pass those fields to those methods, so you made the methods all accept a single Screen object, then you can just pass your Screen.
Hopefully I have understood your situation correctly.
This doesn't actually mean that you are moving unnecessary data around in memory. Objects themselves aren't copied when they are passed as parameters. Only their addresses are copied, which would be the same size of data as if you pass the individual fields. Addresses are all of the same size. Therefore, this probably won't cause a performance problem.
However, this might be bad design. By passing Screen to your methods, you are making your methods dependent on Screen. If your methods has nothing to do with the UI, they should not depend on Screen, right? They should work without a Screen as well.
Also, your Screen might be a god class and break the Single Responsibility Principle. You might want to refactor that.
add a comment |
It's hard to say in general. Adding an extra parameter means an extra push on the stack, which is generally compiled down to... an extra push on the stack. So regardless of whether it's running in interpreted mode or in compiled mode, you will have one more push per call. But that's fairly cheap. By comparison, getting that same object by making another method call or just dereferencing a variable, could be fast or it could be a little slower - it depends on what happens to be in the L1 and L2 caches at the time, which could vary from call to call. But either way, the object is never copied (unlike in C++). So in summary... it depends, and you probably shouldn't worry about it too much in general. First profile your code if it's slow, and focus only on the parts of the system that are much slower than they could be.
1
Saying things like "fairly cheap" and "it depends" is hedging more than you need to. It'll be slower in an only-technically-true way; the impact would probably be sub-nanosecond. It's fair to say there won't be any measurable impact.
– yshavit
Nov 24 '18 at 21:03
On one call, yes. But on billions or trillions or quadrillions of calls?
– Robin Green
Nov 24 '18 at 21:04
2
Let's say it's a whole nanosecond (and like I said, it's likely less). In a billion of calls, you've added one whole second. Unless your calls are something as trivial as++i, that section of code is going to take long enough that one second is (a) insignificant, and (b) lost in the noise of other activity, like GC, so it'll be hard to even measure. If you really do need to account for and optimize every second from a billion calls, Java is probably not the language for you anyway. All of this to say that I still stand by my assertion that you're hedging more than needed.
– yshavit
Nov 24 '18 at 21:10
add a comment |
To me, passing a single context - this is how I call that "transfer" object, is appropriate. As said, if you want to support future additions or removals of some parts of that object - this is easy to do.
But there are cases when I am only pro-passing separate individual pieces. This is the case when you want to test a method and if some more objects are needed for a method - I want tests to fail.
// context contains first and second
public void testMe(Context context) {
}
vs
public void testMe(String first, String second){
}
Adding one more parameter to the context, like String third - will most probably make your test pass; but adding one more parameter to a method that expect two... It's easy to ignore writing a unit test, it's a lot harder to ignore a compilation error. So sometimes, we have code that takes more arguments for this reason.
As far as performance, I doubt you will ever feel a difference (even if one exists), after all, passing objects is actually passing a reference.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462267%2fhow-much-impact-does-sending-unnecessary-objects-have%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
So you have a Screen object that has lots of useful fields that your other methods need, and you are too lazy to pass those fields to those methods, so you made the methods all accept a single Screen object, then you can just pass your Screen.
Hopefully I have understood your situation correctly.
This doesn't actually mean that you are moving unnecessary data around in memory. Objects themselves aren't copied when they are passed as parameters. Only their addresses are copied, which would be the same size of data as if you pass the individual fields. Addresses are all of the same size. Therefore, this probably won't cause a performance problem.
However, this might be bad design. By passing Screen to your methods, you are making your methods dependent on Screen. If your methods has nothing to do with the UI, they should not depend on Screen, right? They should work without a Screen as well.
Also, your Screen might be a god class and break the Single Responsibility Principle. You might want to refactor that.
add a comment |
So you have a Screen object that has lots of useful fields that your other methods need, and you are too lazy to pass those fields to those methods, so you made the methods all accept a single Screen object, then you can just pass your Screen.
Hopefully I have understood your situation correctly.
This doesn't actually mean that you are moving unnecessary data around in memory. Objects themselves aren't copied when they are passed as parameters. Only their addresses are copied, which would be the same size of data as if you pass the individual fields. Addresses are all of the same size. Therefore, this probably won't cause a performance problem.
However, this might be bad design. By passing Screen to your methods, you are making your methods dependent on Screen. If your methods has nothing to do with the UI, they should not depend on Screen, right? They should work without a Screen as well.
Also, your Screen might be a god class and break the Single Responsibility Principle. You might want to refactor that.
add a comment |
So you have a Screen object that has lots of useful fields that your other methods need, and you are too lazy to pass those fields to those methods, so you made the methods all accept a single Screen object, then you can just pass your Screen.
Hopefully I have understood your situation correctly.
This doesn't actually mean that you are moving unnecessary data around in memory. Objects themselves aren't copied when they are passed as parameters. Only their addresses are copied, which would be the same size of data as if you pass the individual fields. Addresses are all of the same size. Therefore, this probably won't cause a performance problem.
However, this might be bad design. By passing Screen to your methods, you are making your methods dependent on Screen. If your methods has nothing to do with the UI, they should not depend on Screen, right? They should work without a Screen as well.
Also, your Screen might be a god class and break the Single Responsibility Principle. You might want to refactor that.
So you have a Screen object that has lots of useful fields that your other methods need, and you are too lazy to pass those fields to those methods, so you made the methods all accept a single Screen object, then you can just pass your Screen.
Hopefully I have understood your situation correctly.
This doesn't actually mean that you are moving unnecessary data around in memory. Objects themselves aren't copied when they are passed as parameters. Only their addresses are copied, which would be the same size of data as if you pass the individual fields. Addresses are all of the same size. Therefore, this probably won't cause a performance problem.
However, this might be bad design. By passing Screen to your methods, you are making your methods dependent on Screen. If your methods has nothing to do with the UI, they should not depend on Screen, right? They should work without a Screen as well.
Also, your Screen might be a god class and break the Single Responsibility Principle. You might want to refactor that.
answered Nov 24 '18 at 21:04
SweeperSweeper
69.3k1074140
69.3k1074140
add a comment |
add a comment |
It's hard to say in general. Adding an extra parameter means an extra push on the stack, which is generally compiled down to... an extra push on the stack. So regardless of whether it's running in interpreted mode or in compiled mode, you will have one more push per call. But that's fairly cheap. By comparison, getting that same object by making another method call or just dereferencing a variable, could be fast or it could be a little slower - it depends on what happens to be in the L1 and L2 caches at the time, which could vary from call to call. But either way, the object is never copied (unlike in C++). So in summary... it depends, and you probably shouldn't worry about it too much in general. First profile your code if it's slow, and focus only on the parts of the system that are much slower than they could be.
1
Saying things like "fairly cheap" and "it depends" is hedging more than you need to. It'll be slower in an only-technically-true way; the impact would probably be sub-nanosecond. It's fair to say there won't be any measurable impact.
– yshavit
Nov 24 '18 at 21:03
On one call, yes. But on billions or trillions or quadrillions of calls?
– Robin Green
Nov 24 '18 at 21:04
2
Let's say it's a whole nanosecond (and like I said, it's likely less). In a billion of calls, you've added one whole second. Unless your calls are something as trivial as++i, that section of code is going to take long enough that one second is (a) insignificant, and (b) lost in the noise of other activity, like GC, so it'll be hard to even measure. If you really do need to account for and optimize every second from a billion calls, Java is probably not the language for you anyway. All of this to say that I still stand by my assertion that you're hedging more than needed.
– yshavit
Nov 24 '18 at 21:10
add a comment |
It's hard to say in general. Adding an extra parameter means an extra push on the stack, which is generally compiled down to... an extra push on the stack. So regardless of whether it's running in interpreted mode or in compiled mode, you will have one more push per call. But that's fairly cheap. By comparison, getting that same object by making another method call or just dereferencing a variable, could be fast or it could be a little slower - it depends on what happens to be in the L1 and L2 caches at the time, which could vary from call to call. But either way, the object is never copied (unlike in C++). So in summary... it depends, and you probably shouldn't worry about it too much in general. First profile your code if it's slow, and focus only on the parts of the system that are much slower than they could be.
1
Saying things like "fairly cheap" and "it depends" is hedging more than you need to. It'll be slower in an only-technically-true way; the impact would probably be sub-nanosecond. It's fair to say there won't be any measurable impact.
– yshavit
Nov 24 '18 at 21:03
On one call, yes. But on billions or trillions or quadrillions of calls?
– Robin Green
Nov 24 '18 at 21:04
2
Let's say it's a whole nanosecond (and like I said, it's likely less). In a billion of calls, you've added one whole second. Unless your calls are something as trivial as++i, that section of code is going to take long enough that one second is (a) insignificant, and (b) lost in the noise of other activity, like GC, so it'll be hard to even measure. If you really do need to account for and optimize every second from a billion calls, Java is probably not the language for you anyway. All of this to say that I still stand by my assertion that you're hedging more than needed.
– yshavit
Nov 24 '18 at 21:10
add a comment |
It's hard to say in general. Adding an extra parameter means an extra push on the stack, which is generally compiled down to... an extra push on the stack. So regardless of whether it's running in interpreted mode or in compiled mode, you will have one more push per call. But that's fairly cheap. By comparison, getting that same object by making another method call or just dereferencing a variable, could be fast or it could be a little slower - it depends on what happens to be in the L1 and L2 caches at the time, which could vary from call to call. But either way, the object is never copied (unlike in C++). So in summary... it depends, and you probably shouldn't worry about it too much in general. First profile your code if it's slow, and focus only on the parts of the system that are much slower than they could be.
It's hard to say in general. Adding an extra parameter means an extra push on the stack, which is generally compiled down to... an extra push on the stack. So regardless of whether it's running in interpreted mode or in compiled mode, you will have one more push per call. But that's fairly cheap. By comparison, getting that same object by making another method call or just dereferencing a variable, could be fast or it could be a little slower - it depends on what happens to be in the L1 and L2 caches at the time, which could vary from call to call. But either way, the object is never copied (unlike in C++). So in summary... it depends, and you probably shouldn't worry about it too much in general. First profile your code if it's slow, and focus only on the parts of the system that are much slower than they could be.
answered Nov 24 '18 at 21:01
Robin GreenRobin Green
22.5k876156
22.5k876156
1
Saying things like "fairly cheap" and "it depends" is hedging more than you need to. It'll be slower in an only-technically-true way; the impact would probably be sub-nanosecond. It's fair to say there won't be any measurable impact.
– yshavit
Nov 24 '18 at 21:03
On one call, yes. But on billions or trillions or quadrillions of calls?
– Robin Green
Nov 24 '18 at 21:04
2
Let's say it's a whole nanosecond (and like I said, it's likely less). In a billion of calls, you've added one whole second. Unless your calls are something as trivial as++i, that section of code is going to take long enough that one second is (a) insignificant, and (b) lost in the noise of other activity, like GC, so it'll be hard to even measure. If you really do need to account for and optimize every second from a billion calls, Java is probably not the language for you anyway. All of this to say that I still stand by my assertion that you're hedging more than needed.
– yshavit
Nov 24 '18 at 21:10
add a comment |
1
Saying things like "fairly cheap" and "it depends" is hedging more than you need to. It'll be slower in an only-technically-true way; the impact would probably be sub-nanosecond. It's fair to say there won't be any measurable impact.
– yshavit
Nov 24 '18 at 21:03
On one call, yes. But on billions or trillions or quadrillions of calls?
– Robin Green
Nov 24 '18 at 21:04
2
Let's say it's a whole nanosecond (and like I said, it's likely less). In a billion of calls, you've added one whole second. Unless your calls are something as trivial as++i, that section of code is going to take long enough that one second is (a) insignificant, and (b) lost in the noise of other activity, like GC, so it'll be hard to even measure. If you really do need to account for and optimize every second from a billion calls, Java is probably not the language for you anyway. All of this to say that I still stand by my assertion that you're hedging more than needed.
– yshavit
Nov 24 '18 at 21:10
1
1
Saying things like "fairly cheap" and "it depends" is hedging more than you need to. It'll be slower in an only-technically-true way; the impact would probably be sub-nanosecond. It's fair to say there won't be any measurable impact.
– yshavit
Nov 24 '18 at 21:03
Saying things like "fairly cheap" and "it depends" is hedging more than you need to. It'll be slower in an only-technically-true way; the impact would probably be sub-nanosecond. It's fair to say there won't be any measurable impact.
– yshavit
Nov 24 '18 at 21:03
On one call, yes. But on billions or trillions or quadrillions of calls?
– Robin Green
Nov 24 '18 at 21:04
On one call, yes. But on billions or trillions or quadrillions of calls?
– Robin Green
Nov 24 '18 at 21:04
2
2
Let's say it's a whole nanosecond (and like I said, it's likely less). In a billion of calls, you've added one whole second. Unless your calls are something as trivial as
++i, that section of code is going to take long enough that one second is (a) insignificant, and (b) lost in the noise of other activity, like GC, so it'll be hard to even measure. If you really do need to account for and optimize every second from a billion calls, Java is probably not the language for you anyway. All of this to say that I still stand by my assertion that you're hedging more than needed.– yshavit
Nov 24 '18 at 21:10
Let's say it's a whole nanosecond (and like I said, it's likely less). In a billion of calls, you've added one whole second. Unless your calls are something as trivial as
++i, that section of code is going to take long enough that one second is (a) insignificant, and (b) lost in the noise of other activity, like GC, so it'll be hard to even measure. If you really do need to account for and optimize every second from a billion calls, Java is probably not the language for you anyway. All of this to say that I still stand by my assertion that you're hedging more than needed.– yshavit
Nov 24 '18 at 21:10
add a comment |
To me, passing a single context - this is how I call that "transfer" object, is appropriate. As said, if you want to support future additions or removals of some parts of that object - this is easy to do.
But there are cases when I am only pro-passing separate individual pieces. This is the case when you want to test a method and if some more objects are needed for a method - I want tests to fail.
// context contains first and second
public void testMe(Context context) {
}
vs
public void testMe(String first, String second){
}
Adding one more parameter to the context, like String third - will most probably make your test pass; but adding one more parameter to a method that expect two... It's easy to ignore writing a unit test, it's a lot harder to ignore a compilation error. So sometimes, we have code that takes more arguments for this reason.
As far as performance, I doubt you will ever feel a difference (even if one exists), after all, passing objects is actually passing a reference.
add a comment |
To me, passing a single context - this is how I call that "transfer" object, is appropriate. As said, if you want to support future additions or removals of some parts of that object - this is easy to do.
But there are cases when I am only pro-passing separate individual pieces. This is the case when you want to test a method and if some more objects are needed for a method - I want tests to fail.
// context contains first and second
public void testMe(Context context) {
}
vs
public void testMe(String first, String second){
}
Adding one more parameter to the context, like String third - will most probably make your test pass; but adding one more parameter to a method that expect two... It's easy to ignore writing a unit test, it's a lot harder to ignore a compilation error. So sometimes, we have code that takes more arguments for this reason.
As far as performance, I doubt you will ever feel a difference (even if one exists), after all, passing objects is actually passing a reference.
add a comment |
To me, passing a single context - this is how I call that "transfer" object, is appropriate. As said, if you want to support future additions or removals of some parts of that object - this is easy to do.
But there are cases when I am only pro-passing separate individual pieces. This is the case when you want to test a method and if some more objects are needed for a method - I want tests to fail.
// context contains first and second
public void testMe(Context context) {
}
vs
public void testMe(String first, String second){
}
Adding one more parameter to the context, like String third - will most probably make your test pass; but adding one more parameter to a method that expect two... It's easy to ignore writing a unit test, it's a lot harder to ignore a compilation error. So sometimes, we have code that takes more arguments for this reason.
As far as performance, I doubt you will ever feel a difference (even if one exists), after all, passing objects is actually passing a reference.
To me, passing a single context - this is how I call that "transfer" object, is appropriate. As said, if you want to support future additions or removals of some parts of that object - this is easy to do.
But there are cases when I am only pro-passing separate individual pieces. This is the case when you want to test a method and if some more objects are needed for a method - I want tests to fail.
// context contains first and second
public void testMe(Context context) {
}
vs
public void testMe(String first, String second){
}
Adding one more parameter to the context, like String third - will most probably make your test pass; but adding one more parameter to a method that expect two... It's easy to ignore writing a unit test, it's a lot harder to ignore a compilation error. So sometimes, we have code that takes more arguments for this reason.
As far as performance, I doubt you will ever feel a difference (even if one exists), after all, passing objects is actually passing a reference.
answered Nov 24 '18 at 21:40
EugeneEugene
70.8k9102169
70.8k9102169
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462267%2fhow-much-impact-does-sending-unnecessary-objects-have%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It's no inefficient, because no copy is made. It's just a reference. However, such large objects are called "god objects" and thus that's a code smell.
– Thomas Weller
Nov 24 '18 at 20:58
Shotgun programming is never a good idea, but passing arguments (per se) isn't inefficient.
– Elliott Frisch
Nov 24 '18 at 20:58
You're only sending references to that data, so the overhead is negligible. What I'd be more worried about is that you haven't thought out the right abstractions, and therefore have code where everything needs access to everything, which can make your code hard to test and maintain.
– yshavit
Nov 24 '18 at 20:58
1
It doesn't make any difference. You don't "send objects". You pass references to objects. I.e. passing 3 references instead of 1 means that the JVM has to copy 8 more bytes (or 16 if 64-bit references are used). It's peanuts.
– JB Nizet
Nov 24 '18 at 20:59
1
There's a different kind of impact besides performance to talk about. Providing a single object as a param that allows access to all of the things the method needs can future-proof your interface thus allowing you to add new "params" by simply adding to the object and avoid having to change your method signature.
– Michael Krause
Nov 24 '18 at 21:06