Is it improper programming to set a variable equal to the result of a function that accepts the same variable? The languages in question here are c#, javascript and PHP (not sure if this works in C families, personally).
var a = 1;
a = alterData(a);
-
1Can't think of a case where this doesn't just make things more confusing to the reader. Is there a functional need for this?Alternatex– Alternatex2015年12月23日 15:53:52 +00:00Commented Dec 23, 2015 at 15:53
2 Answers 2
"Improper" how?
It is not illegal - that is, it's not a syntax violation in any language with mutable variables that I'm aware of.
It is not broken - that is, the post-condition of the expression will be the result of the function, and barring some weird concurrency effects be nice and consistent.
It's not ugly or non-idiomatic. In pretty much all languages I'm familiar with (that have mutable variables), this sort of thing is considered readable and a fine thing to do as long as a
still means the same thing. Reusing a
for some other semantic concept (like it is velocity for the first half of the function and acceleration for the second half) is not good.
If anything, programmers are moving more towards this form of programming and away from alterData(a)
that uses side effects to change a
under the covers.
So go right ahead.
-
1How could
alterData(a)
changea
under the covers? It's anint
, it is passed by value.JᴀʏMᴇᴇ– JᴀʏMᴇᴇ2015年12月23日 16:07:46 +00:00Commented Dec 23, 2015 at 16:07 -
Great point about it not completely altering the type of the variable (which is all too easy with scripting languages). I don't think it makes sense to extend a primitive just to do some random operation you may only need once.tones31– tones312015年12月23日 16:15:28 +00:00Commented Dec 23, 2015 at 16:15
-
@JᴀʏMᴇᴇ In both C#, PHP and Javascript (using
var
in c#)tones31– tones312015年12月23日 16:15:58 +00:00Commented Dec 23, 2015 at 16:15 -
@JᴀʏMᴇᴇ - I was answering for the general case of the pattern -
int
and its kin of course cannot be mutated by the function.Telastyn– Telastyn2015年12月23日 16:23:15 +00:00Commented Dec 23, 2015 at 16:23 -
1@JᴀʏMᴇᴇ In both PHP and C# the parameter could be passed by reference if that is how alterData defines it.back2dos– back2dos2015年12月23日 16:26:27 +00:00Commented Dec 23, 2015 at 16:26
First, you should try to use meaningful variable names. In this case, you might have original_a and altered_a (where a should be something relevant, of course).
Second, the cost of a local variable is minimal in most programming languages (and sometimes zero), so feel free to use them.
Third, using the functional style of immutable variables (assigned once) has merit. Apple's Swift, for example, is a modern language that is encouraging single variable assignment. Some might argue programs are easier to read/maintain when local variables are not modified; you can see the value of the old and new in the debugger, and if something is amiss, it is easier to see where.
So, I say no, it is better to introduce another variable.
-
+1: Variables should be assigned once unless they are accumulators of some sort.kevin cline– kevin cline2015年12月23日 21:33:54 +00:00Commented Dec 23, 2015 at 21:33