Sometimes I've been trying to come up with a good variable name for minutes, when I realize that it isn't worth the effort for this tiny loop. Is there any situation where it would be justified to call a variable "temp"?
Update: I should have been more clear since you are all programmers! I'm not looking for cases where "temp" actually describes the function of the variable, but cases where it means absolutely nothing at all.
Sure, if you do swapping by value;
int temp = a;
a = b;
b = temp;
Or if you use variable notation such as calc for calculation etc you should use temp for temperature ;)
Yes; if the variable has no real semantic meaning, then I would say that in the situation you describe, it's perfectly acceptable.
Sure. When modeling weather:
pressure = 3
temp = 21
pressure_tommorow, temp_tommorow = model(pressure, temp, 1.day.from.now)
However if I had the misfortune of programming in a language that would not support this and it was totally one off (e.g. temporary) then why not:
WeatherModel temp = MyNotSoAbstractModellingClass.giveMeTheDamnModel(TimeManager.getTommorow());
PressureEstimate tommorowsEstimatedPressure = temp.getPressure();
TemperatureWhyMakeThisPointleslyShortEstimate tommorowsEstimatedTemperature = temp.getTemperature();
// like someone can pretend that it's not painfully evident what's going on
Whenever you need an intermediate variable, intended to temporarily hold data during manipulation of something else, IMO the best name is temp - it clearly describes the variables function.
Samples:
char Temp[32]; sprintf(Temp, ...); RealVar += Temp;
int temp = a; a = b; b = temp;
Yes I think there are a couple:
var temp = "35°C";
var temp = Path.GetTempPath();
If that's the most descriptive name you can think of. Sometimes you can extend it to "temp<something>", in which case the "<something>" is also a candidate for a name.
You can use only first letter of it's meaning for a short function body, in this case "m" would be well understood considering function name which gets/sets minutes has a self describing name eg
var m = currentDate.Minute;
As someone else described, name "temp" is good for swapping variable values or probably as a name for temporary file path, nothing more.
No, it isn't.
IMHO:
tempPerson
tempLoopVariable
tempWhatever
or even
temporaryWhatever
are better.
Related
I'm trying to return a pair from a function and assign it to already defined variables, in Kotlin.
The way I've seen pairs received from a function until now is:
val/var (thing1, thing2) = FunctionReturningPair()
Is it possible to assign already defined variables to the pair? Something like:
var thing1: String
var thing2:int
//do other things here
(thing1, thing2) = FunctionReturningPair()
//note that thing1 and thing2 were ALREADY DEFINED.
Unfortunately that's not allowed as far as I know. The current Kotlin syntax allows for destructuring declarations, but that only works if you declare the variables at that time. In your example you declared the variables above and just want to assign a value.
Looking at the grammar makes it clear that assignment only accepts a directlyAssignableExpression (such as a simpleIdentifier). A declaration instead accepts a multiVariableDeclaration.
I don't see a reason of why that would not work, but apparently it does not. In the docs, I don't see any mention of doing that as well. Usually features are added if there is a consensus that 1. people consider that they are needed. And 2. they are not going to break other things / or be confusing. So I guess that one of those conditions has not been met, or nobody ask for it.
If you really want it, it might be worth checking youtrack to see if somebody else requested it. If they have vote for it, and if not, write a feature request.
Until then, I guess that you are stuck with one of these ways:
val p = functionReturningPair()
thing1 = p.first
thing2 = p.second
OR
functionReturningPair().let { (first, second) -> thing1 = first; thing2 = second }
I'm trying to write a for loop to generate multiple functions. I would like to iterate on the name of the functions as well but I can't seem to make it work. Here's what I want to do:
for i = 1:n
h_[i](x) = i*x
end
I know this doesn't work but I would like something like that.
Thanks!
As #laborg mentioned, quite often you don't need/want to use metaprogramming. Since you already try to use array indexing syntax (h_[i]) perhaps you should simply create a vector of functions, for example like so:
h_ = [x->i*x for i in 1:n]
Here, x->i*x is an anonymous function - a function that we don't care to give a name. Afterwards you can use actual array indexing to access these different functions and call them.
Demo:
julia> n = 3;
julia> h_ = [x->i*x for i in 1:n];
julia> h_[1](3)
3
julia> h_[2](3)
6
julia> h_[3](3)
9
No metaprogramming involved.
(On a side note, in this particular example a single function h(x;i) = i*x with a keyword argument i would probably the best choice. But I assume this is a simplified example.)
You have to evaluate the name of your function:
for i = 1:n
f = Symbol(:h_,i)
#eval $f(x) = $i*x
end
More on this:
https://docs.julialang.org/en/v1/manual/metaprogramming/
As a general note on meta programming in Julia: It really helps to think twice if meta programming is the best solution, especially as Julia offers a lot of other cool features, e.g. multiple dispatch.
So I'm trying to do an assignment for an ml course, the issue is that the function requires a set type: int * int -> int for example, and the way that I see to solve the problem is to use another function (say for iteration) to solve the problem.
I believe that lisp has some kind of way of having a function be in scope for only one other function.
I think that this could be done:
fun a (x, y) =
let
fun b (i,j) = ...;
in
...;
[Not sure of exact syntax for this but I remember reading something like this only it was for temporary variables (which could be functions?]
but please correct me if this is wrong.
In ML, functions are first class citizens (i.e. values). You can bind them via let just like any other value.
Therefore, your idea is correct. It is especially a good design for functions passed as "iterators" (i.e. to map/fold/iter). Your question is too vague however for any further advise.
Whenever i put up a code for review from professional programmers they tend to point out that "using a variable named temp is bad" but no one seems to know why.
Why is it considered bad ?
temp indeed doesn't mean anything useful. A better question is: does it have to?
Without knowing the context (how is it used in your code?), it's hard to say what it's for, and whether temp is a good name. If you use a variable often or non-locally, the name must be descriptive. A name like temp can be fine if you use it, say, three times in three adjacent lines.
void swapIntPointers(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
It's immediately obvious what this function should do (from its name) and what it actually does (from its structure). In this specific case, I strongly prefer short (and automatically nondescript) names. I'd even say that temp may be a little too long here ;)
However:
If you use the variable often, it's apparently important, and 'deserves' a better name.
If you use the same variable in places in the function that are far apart (non-local), it helps programmers to 'remember' the meaning when you give it a recognizable name.
It's because temp suggests something about the longevity of the variable (temporary) but nothing about the meaning or significance of its content. Variables are generally best named to reflect what their underlying value is intended to represent.
I more than once saw code of the form:
DiceThrower dt = new DiceThrower();
dt.throw(); //this is a void method
int result = dt.getResult();
instead of
DiceThrower dt = new DiceThrower();
int result = dt.throw();
My question is...why? Isn't it better to have the throw method returning the result? By not doing so, I could even incurr in sometimes forgetting about calling throw() before getResult(), accessing always old values of getResult(). Having both the operation and the result in the same method would circumvent that.
What is your oppinion on the matter?
Thanks
I'd use this pattern if you would need to use the data often, and it's expensive to (re)generate. But then memoization would probably be better, so the caller doesn't have to care.
You could even support both. Throw could return the value but it would also be available via getResult(). As noted above, if it's expensive to throw you want to cache the value in case it's needed more than once.