Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect changing value of object passed as parameter

I'm now working with code that looks like this

public String getName(User user) {
     user.setSth(...);
     return user.getName();
}

I think it's bad practice to change objects passed as parameters. Is there a tool that detects that kind of code? I looked at findbugs, pmd and checkstyle, but could not find any check for this.

P.S. sorry for bad example.

like image 812
IAdapter Avatar asked Jan 23 '23 20:01

IAdapter


1 Answers

I think you are already on the right track: your best tool to detect this sort of code is almost certainly Findbugs. However, you will probably need to write your own detector for this pattern. Here is an example of how you would write a detector, though it isn't exactly the detector that you're looking for.

Caveat: I don't really agree that a side-effecting getter is always bad style. However, if you really want to find that sort of thing, I would recommend Findbugs.

like image 52
Bob Cross Avatar answered Feb 15 '23 13:02

Bob Cross