Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add W_X_i and W_v_Wi function parameters in process_point_kinematics #201

Closed

Conversation

xela-95
Copy link
Member

@xela-95 xela-95 commented Jul 9, 2024

I've noted that in this function of collidable_points.py the two variables were used directly from the outer scope, with potential undefined behavior.


📚 Documentation preview 📚: https://jaxsim--201.org.readthedocs.build//201/

Copy link
Collaborator

@flferretti flferretti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @xela-95! LGTM

Copy link
Member

@diegoferigo diegoferigo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pattern that is often used in lambda functions (and, generally, closures). In this specific case, those variables are already available when the function is created, and the function body is closed over these two variables that are used as read-only.

I don't see any problem in this case resulting from the spurious access of global variables (as it happened in your scripts recently). Anyway, this change does not affect performance, if it enhances readability, let's proceed merging. Just note that this pattern was used on purpose and it is also used similarly elsewhere.

@xela-95
Copy link
Member Author

xela-95 commented Jul 9, 2024

This is a pattern that is often used in lambda functions (and, generally, closures). In this specific case, those variables are already available when the function is created, and the function body is closed over these two variables that are used as read-only.

I don't see any problem in this case resulting from the spurious access of global variables (as it happened in your scripts recently). Anyway, this change does not affect performance, if it enhances readability, let's proceed merging. Just note that this pattern was used on purpose and it is also used similarly elsewhere.

Thanks, in this case I didn't know if it was used on purpose or it was a code smell, and I opened this PR for this reason. I leave to you to decide (or not) to merge it :)

@diegoferigo
Copy link
Member

I think I'll close this PR for the time being. It's a pattern used thoroughly in JaxSim (often on model and data, but sometimes on other variables).

I want to remark again that scoping problems may occur only when global variables are involved, and this is pretty much the reason why there is the consensus that global variables should be used the least possible. Of course, in Python scripts, it's often quicker to just develop all global (note that also using if __name__ == "__main__" is global), the real solution is to wrap the content in a def main() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants