How do you name single output functions?

14 ビュー (過去 30 日間)
Maximilian Schönau
Maximilian Schönau 2021 年 2 月 5 日
コメント済み: Walter Roberson 2021 年 2 月 7 日
The matlab style book reccomends to name single output functions like their output, which is consistent with the MALTAB functions...
I dont like this very much, because I want the variable of the output of my function be called like the output...
How do you name your single output or single output function?
I would probably use something like
function result = calculateResult(input)
% calculateResult calculates Result ;D
altough I dont like it that much


Jan 2021 年 2 月 6 日
I use CamelCase for my funtions to reduce the danger a collision with toolbox functions of Matlab. For frequently used functions, compact names are nice. A leading "Calculate..." would introduce too much redundancy, because most functions calculate something. Compare:
CalculateMean(CalculateSin(CalculateLinSpace(0, 2*CalculatePI, 1000)))
mean(sin(linspace(0, 2*pi, 1000)))
How the output of a function is called, does not matter, when the function is called. Then the code of the function is not visible and the naming does not influence the usability.
Sometimes, I use the name of the procedure, sometimes the name of the output, and sometimes I add an initial "Set", "Get", "Write", "Read" etc. An example for different approachs:
MD5 = GetMD5(File)
MD5 = MD5Hash(File)
Hash = MD5(File)
Some fixed styles for choosing names of functions and variables are useful. But as long as you keep the overview and hte names do not get too long or too cryptic, it remains a question of taste. Then spending time and energy in the naming conventions is a wasted optimization.
On the other hand I'm using conventions for all names of variables:
  • sulkingCamelCase for internally used variables, CamelCase for onput and output.
  • All names in singular, trailing "List", "Vec" etc. if it matters, that they are arrays.
  • Leading "is..." or "do..." for logical flags.
  • "n..." is a number of elements or objects, "i..." is a loop counter.
This reduces the chance of e.g. confusing "nFile" and "nFiles" anywhere in the code. In opposite to this, I see less problems with confused names of functions.
  1 件のコメント
Maximilian Schönau
Maximilian Schönau 2021 年 2 月 6 日
Thank you!
I guess I won’t use calculate anymore, and think of a different Name for the same thing within the function it is calculated...


その他の回答 (2 件)

madhan ravi
madhan ravi 2021 年 2 月 5 日
result = @(in) in.^2;
result = result(1 : 10)
result = 1×10
1 4 9 16 25 36 49 64 81 100
  4 件のコメント
Maximilian Schönau
Maximilian Schönau 2021 年 2 月 6 日
Hi, I probably didn’t made it clear, but I was referring to the variable name within the function...
And a function declaration of
function mean = mean(y)
Would not be possible.
As to your last Statement, I would always write:
x = result();
I also do this when calling methods with no inputs on my class.


Steven Lord
Steven Lord 2021 年 2 月 6 日
function result = calculateResult(input)
% calculateResult calculates Result ;D
I have some feedback on this.
First, the identifier input already has a meaning in MATLAB so I wouldn't use it as a variable name.
Second, I dislike that function name because it's so bland. It offers no hint of what the function does. "calculate" and "Result" in this context are both plain filler words. In my opinion reading the name of the function should give me a reasonable idea of what the function actually does and ideally I should not need to read its help text or its documentation unless I need details about some specific nuance of the function. [Yes, I know that not all functions in MathWorks products satisfy this. I did use the word "ideally" in my description. Plus we've learned a bunch about designing our software over the past couple decades.]
Think of it like Burger King, Kentucky Fried Chicken, or Pizza Hut. Even if you've never heard of one of these businesses before, likely you could guess just from the names what they are (restaurants) and what they sell (burgers, fried chicken, and pizza respectively.) As a MATLAB example, consider the islocalmax function. Without reading that function's help text or documentation, what would you guess it does?
Third, it shouldn't matter what you name the output variable inside your function. Ideally the user of your function should not care how the sausage is made, just that the sausage they ordered is the sausage they received. [The health inspector should care, but in this metaphor the health inspector is played by the white-box tests of your function. And even the health inspector probably shouldn't care what nicknames you have for the pieces of equipment in your kitchen unless you call the meat grinder "The Salmonella Factory."] There's nothing tying the name of the variable in your function that you assign as the first output to the name of the variable to which that output is assigned in your user's code. There's effectively no difference between (using your calculateResult function)
y = calculateResult(1:10)
abracadabra = calculateResult(1:10)
qwerty{1} = calculateResult(1:10)
% Assuming the caller called sin on one of the above
z = sin(calculateResult(1:10)) % the output of calculateResult is stored in an unnamed temporary
  4 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 7 日
I have seen programming languages in which the method to return a value was to assign to a variable with the same name as the function. However, the name could not otherwise be used as a variable inside the program, and could not be indexed. So for example it would have been valid to do the equivalent of
double mean(double x) {mean = sum(x)./numel(x);}
However, such languages have challenges. For example,
double mean(double x) {mean = 0; mean = mean + mean(1) + mean(x./2);}
Is the mean(1) indexing? Is it function evaluation?
double mean(double x) { memzero(&mean, sizeof(mean)); }
is memzero being passed the address of the output variable for the function, or is it being passed a pointer to the function ?



Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by