i and j as Variable Names vs Imaginary Unit: What Advice to Give on MATLAB Answers?

87 次查看(过去 30 天)
In a comment to my own comment (advising the OP to not use i or j as loop variable names), Guillaume wrote the following:
Stephen, you say that in every single post that uses i or j as an iterator. I don't agree with it because:
a) it stands very little chance of breaking anything. Matlab uses 1i and 1j to represent the imaginary unit. i and j are just functions that return these values.
b) I'd argue that mathworks was wrong to create these functions in the first place. i and j are used as iterating variables in zillions of computer books. Matlab shouldn't hijack such a common variable name. (Matlab needs namespaces badly).
However, I'd say that in complex code, you should use variable names that explain better their purpose.
Both of a) and b) are good points, but actually my main motivation for making these comments to OP's was based more along the idea of "it is likely the OP is not aware of this information, here is something small and easy to learn about MATLAB". I do feel that it is appropriate on Answers to introduce an OP to new information, or a new way of doing something that they may not be aware of, and indeed occasionally an OP has responded with something along the lines of "oh, thank you, I did not know that".
However I recognize that misusing i or j is unlikely to break as much as an assignment to length or mean or sum (or my favorite false=true), although it does happen, as these answers indicate. For better or worse, MATLAB does allow users to assign these names without warning (YES to namespaces, or atleast some syntax to force this kind of shadowing).
Apparently there used to be a page on the MathWorks website entitled "Avoid Using i and j for Variables", but it does not seem to exist any more. Documentation for older MATLAB versions explicitly recommended not using i and j as variable names, for JIT-related speed reasons.
  1. Is this issue still relevant?
  2. Is it useful to give an OP new information that they likely are not aware of, even if it does not directly affect the outcome of their question?
  3. Should i and j be treated differently to shadowing of other inbuilt functions or variables, even if their posted code does not break?
  4. Is it appropriate to make a comment about their usage?
  5. Under what circumstances, and for which OP's?

采纳的回答

Sean de Wolski
Sean de Wolski 2015-2-9
  • Stephen, I'd say it's a best practice so you're not hurting anything or anyone. Whether it's worth the keystrokes and time for you, that's your own cost function :)
  • The code analyzer does still suggest avoiding i and j for defining sqrt(-1). Though many if not most users ignore the code analyzer:
_
  • The only sure way to check if a function exists is to use
which -all functionname
where functionname is the string function name you want to check. I do this before pretty everything I write. Some checking can be avoided by using namespaces (packages).
When I worked in tech support this was the INDIVIDUAL BIGGEST cause of most calls. Why am I getting a "Too Many Inputs Error" somewhere etc. Pretty much every error message that came from code could be resolved by dbstop if error and which -all, which also tells you about license checkout failures.

更多回答(3 个)

Guillaume
Guillaume 2015-2-5
编辑:Guillaume 2015-2-5
The way I see it is that unless a variable name shadows a very commonly used matlab function (sum, size, max, etc.), I don't think it's worth pointing out. The problem is that, due the lack of namespaces, you don't stand a fighting chance of knowing if a name you use doesn't shadow a function in one of matlab's many toolboxes.
In code that obviously doesn't deal with complex numbers, I certainly don't think it's worth worrying about i. Particularly as, as I said, countless examples in books and on the web use it as the counter variable. Just look at the wikipedia page on the for loop, i everywhere.
Even, with code that deals with complex number, it's only the user's code that can break (I hope that mathwork's own code is resilient) because they explicitly use it as a symbol for the imaginary unit. Therefore, they should already be aware that i means sqrt(-1).
On the other hand, I'd never used i, j, or any single letter variable in any long lasting code. It's probably more beneficial to teach people to use variable names that actually explain their purpose, like fileindex or rownumber, etc. That's certainly what I try to do in my answers (most of the time).
  2 个评论
Guillaume
Guillaume 2015-2-5
Case in point on the fact that you don't stand a chance on knowing whether or not you unwittingly shadow one of matlab's function:
This question where the OP unknowingly shadowed umat, a function of the Robust Control Toolbox.
Stephen23
Stephen23 2015-2-6
编辑:Stephen23 2015-2-9
So new users should be allowed to, but "I'd never used i, j, or any single letter variable in any long lasting code" ?
I thought one of the points of MATLAB Central Answers was to give some of that experience and knowledge to new users, or do we expect them to discover the wheel for themselves every time? Certainly the experiences related here indicate that using i and j as variable names really can be a bit buggy, and it seems that most more seasoned users tend to avoid them, so why try to hide this from new users? Perhaps they are also trying to write "long lasting code", of which we only see a small extract.

请先登录,再进行评论。


Star Strider
Star Strider 2015-2-5
My suggestion to an OP when I see them in code is to not use ‘i’ or ‘j’ simply because it could be confusing in the context of complex variables in the same code. (For loop counters, if I don’t need them to be more descriptive, I just use ‘k1’ for the outer loop, ‘k2’ for inner loops, etc., if I cannot avoid loops.)
As far as shadowing a function name, one quick method I use to check (in the MATLAB Editor) is to right-click on a name that I am concerned could shadow a function. This brings up a ‘Help’ window that either shows the documentation for a function of that name, or ‘No help found for [name]’ if no function of that name — either built-in or user-written — exists (in my installation). Right-clicking on ‘i’ or ‘j’ brings up ‘Help’ documentation for ‘imaginary unit’.
Guillaume and David Young both make valid additional points.
  8 个评论
Guillaume
Guillaume 2015-2-9
@Sean, the way packages are implemented in matlab, they actually don't help much. Instead of a clash of function name, you've got a clash of package name. Granted, it's less likely, but it's not something the user can resolve (other than with path manipulation). The package/namespace name needs to be under the control of the user, not the author.
DGM
DGM 2023-6-26
I know this is extending a tangent, but checking for the existence of a function/method with a particular name is one of the other things for which I've found when() to be convenient. Sometimes it's to check for the possibility of conflict in code that's to be shared on the FEX; sometimes it's to figure out if some unknown function in someone else's code even came from MATLAB.
Anything that exists in webdocs should be identifiable, so it doesn't depend on what's installed (i.e. the version or specific toolboxes), and it doesn't depend on the local documentation being installed either (relevant to +R2023a).
Of course, checking for conflicts today doesn't prevent TMW from making a new function in the next version, and there are always other things that can slip through the cracks. I just think it's a fair balance of sufficiency and convenience, especially compared to relying on what's in a particular installation.

请先登录,再进行评论。


David Young
David Young 2015-2-5
I'm pretty much with Guillaume on this.
I think there's a bigger issue. I feel that one of the things beginners get wrong is making too little use of functions. Breaking code into small functions has, of course, all sorts of advantages, and localizing variables is one of them. Using i or j as a variable in a function cannot wreak global havoc, and if the function is small it's much easier to see what is wrong if there is a problem.

类别

Help CenterFile Exchange 中查找有关 Install Products 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by