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

The function eval is being used where it is not supposed to be used #247

Open
SeSodesa opened this issue Aug 17, 2023 · 0 comments
Open
Labels
invalid This doesn't seem right

Comments

@SeSodesa
Copy link
Collaborator

SeSodesa commented Aug 17, 2023

Description

Currently, Zeffiro Interface makes extensive use of the Matlab function eval, where it is not needed. As an example, suppose we had a struct named zef in our current workspace/symbol table. The code then does things like

if eval('zef.use_gpu')
    % Initialize a bunch of gpuArrays
end

instead of just

if zef.use_gpu
    % Initialize a bunch of gpuArrays
end

to determine whether a GPU can be utilized in computations. The latter option is the sensible one, as it skips an additional (unnecessary) call to the Matlab parser, making it the faster option.

Steps to reproduce

  1. Navigate to the Zeffiro Interface installation folder with cd /path/to/zeffiro_interface/,
  2. make the function call utilities.lint_mfiles("m") and/or utilities.lint_mfiles("plugins") and
  3. observe the EVLDOT warnings in the function output to see where eval is being used unnecessarily.

Expected behavior

There should be no calls to eval, unless absolutely necessary. It is only a shortcut for building variable names dynamically, but even that use-case can be replaced with the use of dynamic field names.

Example output of utilities.lint_mfiles

Found unacceptable linter message in /Users/soderhos/zeffiro_interface/m/mesh/zef_process_meshes.m:

  ID: EVLDOT
  Severity: warning
  On line: 321
  Description: 'eval' is inefficient and makes code less clear. Use dynamic field names to access structure fields or object properties instead.
Error using utilities.lint_mfiles

Context

Name Value
Version 3b1b2ff Add files via upload
OS Any and all
Shell Any and all

Remark

This issue could be fixed at least partly by running the following fish script in the Zeffiro Interface installation folder:

for filename in (ggrep -lP 'eval\([^ ,]+\)' {m,plugins}/**.m)
    gsed -E "s/eval\('([^ ,]+)'\)/\1/" $filename > $filename.tmp && mv $filename.tmp $filename
end

Here ggrep is GNU's version of the program grep and gsed is GNU's version of sed.

@SeSodesa SeSodesa added the invalid This doesn't seem right label Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

1 participant