Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deactivate a variable after use

Is there some annotation that can allow me to tell the java compiler that a variable is not supposed to be used after the annotation? Thanks to the autofill and copy paste features of modern IDEs, it is too easy introduce bugs by mistyping variable names. Such annotations will help detect some of those bugs at compile time (or at typing time if one is using smart IDEs like netbeans/eclipse). As an example, I have the following string processing pipeline:

String input = ....
String pass1 = op1 (input);
...
String pass2 = op2 (pass1);
...
String pass3 = op3 (pass1); // typo: pass1 should be pass2
...
return pass3;

If I could say something like disable pass1 just after the line where op2 is called, the typo in the line where op3 is called will be detected as pass1 would be out of scope as the result of the hypothetical disable annotation.

like image 956
Abhishek Anand Avatar asked Oct 19 '14 02:10

Abhishek Anand


People also ask

How do you make a variable unchangeable?

To do this, you must declare the ReadOnly variable at class or structure level. In the constructor for that class or structure, compute the variable's fixed value, and assign it to the variable before returning from the constructor.

How do you destroy a variable?

The correct way to "unset" is to simply set the variable to null . (This enables JavaScript's automatic processes to remove the variable from memory.)

How do you unset a variable in Python?

Using the 'del' command is the most known and easiest method to delete variables in Python. The del keyword deletes the objects. Since everything in Python is an object, therefore lists, tuples and dictionaries can also be deleted using 'del'. It has a really simple syntax.

How do you nullify a variable in PowerShell?

To delete a variable, along with its value, use Remove-Variable or Remove-Item . This cmdlet does not delete the values of variables that are set as constants or owned by the system, even if you use the Force parameter. If the variable that you are clearing does not exist, the cmdlet has no effect.


2 Answers

No, you cannot deactivate variables in Java. As an alternative, reuse the variables, instead of creating new ones:

String input = ....
String currentPass = op1 (input);
...
currentPass = op2 (currentPass);
...
currentPass = op3 (currentPass);
...
return currentPass;

In theory, a sufficiently smart IDE could use java annotations to throw a warning. However, that is a bad idea. The syntax would be ugly, and would tie you to a particular IDE. Additionally, there are no Java IDEs which implement that 'feature'.

like image 141
Joshua Avatar answered Sep 28 '22 05:09

Joshua


You cannot "deactivate" variables in the way that you describe / desire.

However, if you design the block structure appropriately, you can arrange that certain variables are out of scope; e.g.

String input = ....
{
   String pass1 = op1 (input);
   ...
}
String pass2 = op2 (pass1);  // compilation error ... pass1 is out of scope.

It is unclear whether this gives you sufficient control to achieve what you are really trying to do. But if, not then I can't think of any alternatives that would work at compile time ...


Reusing the variables as described by @Joshua is another approach. It avoids the problem rather than solving it directly.

like image 21
Stephen C Avatar answered Sep 28 '22 06:09

Stephen C