2

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);
asked Dec 23, 2015 at 15:51
1
  • 1
    Can't think of a case where this doesn't just make things more confusing to the reader. Is there a functional need for this? Commented Dec 23, 2015 at 15:53

2 Answers 2

7

"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.

answered Dec 23, 2015 at 15:58
6
  • 1
    How could alterData(a) change a under the covers? It's an int, it is passed by value. Commented 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. Commented Dec 23, 2015 at 16:15
  • @JᴀʏMᴇᴇ In both C#, PHP and Javascript (using var in c#) Commented 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. Commented 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. Commented Dec 23, 2015 at 16:26
4

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.

answered Dec 23, 2015 at 16:35
1
  • +1: Variables should be assigned once unless they are accumulators of some sort. Commented Dec 23, 2015 at 21:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.