Reputation: 691
Please read the Note at the bottom before commenting suggestions. The question is not about strongly typed, dynamically typed, static typed languages etc. It is about starting to use variables anywhere in the program and change one not the other will result in a huge behavioral change.
if PyLint
or flake8
can help, please suggest the configuration with an example so it becomes easy for me to implement it.
It would be great if I can do something like type hinting that looks like syntax error than pylint or flake 8 which is done by running later to check like tests.
In Python a variable is used like below :
def func():
is_name_appropriate = False
while not is_name_appropriate:
print("Hello World")
is_name_appropriate = True
func()
**Output :**
Hello World
In C++ a variable is used like below :
void func()
{
bool is_name_appropriate = false;
while (is_name_appropriate != True)
{
cout<<"Hello World";
is_name_appropriate = true;
}
}
func();
**Output :**
Hello World
There is a compile time error and after correcting the mistake obviously the behavior of the program is not changed.
void func()
{
bool is_name_refactored_appropriate = false; // changed variable name
while (is_name_refactored_appropriate != True) // changed variable name
{
cout<<"Hello World";
is_name_appropriate = true; // Forgot to change this. It will give me compile time error.
}
}
func();
**Output :**
Hello World|
Hello World
Hello World
Hello World
.
.
.
and infinite
is_name_appropriate
is now a new variable and the program run and the behavior is changed. This is my issue. How to I tell the python compiler that is_name_appropriate
variable does not exist and it is a error. Something like the c++ code.
def func():
is_name_refactored_appropriate = False # changed variable name
while not is_name_refactored_appropriate: # change variable name initialized above
print("Hello World")
is_name_appropriate = True # forgot to change this. I don't use IDE. terminal is great!!!
func()
**Output :**
Hello World|
Hello World
Hello World
Hello World
.
.
.
and infinite
My program has 30,000 lines of code and it is difficult to remember where a variable is used. Refactoring is highly necessary, so no no to refactoring.
I use PyCharm Profressional Edition as an IDE but it is still difficult to refactor a name that is used more than 50 times.
Note : The code above is just an example and actual code is pretty complex to be posted here. Please don't suggest removing the while loop above. Understand the question. I also cant change the language as python is specifically need in the program, so please don't suggest that too. Unit testing is not helpful here. Sorry folks. BDD is used here.
Upvotes: 1
Views: 485
Reputation: 24140
I would suggest adding type hints to your code and using MyPy to do static type checking. I've found this to be a game changer when maintaining and refactoring big Python code bases. See this article for an example of how this works in detail.
Admittedly, type hints won't solve the specific problem you're asking about where you're renaming a local variable:
is_name_refactored_appropriate = False # changed variable name
while not is_name_refactored_appropriate: # change variable name initialized above
print("Hello World")
is_name_appropriate = True # forgot to change this. I don't use IDE. terminal is great!!!
Type hints are optional, which is by design. It allows you to omit them where MyPy can infer the type, and it also allows you to introduce type hints gradually to an existing codebase. This does, however, mean that even if you put a type hint on is_name_refactored_appropriate
, MyPy would not give you an error on the line where you assign to is_name_appropriate
because it thinks you want to create a new variable.
However, renaming local variables isn't generally a big refactoring obstacle because they can only be referenced from the function they're used in. If you rename a local variable, the only thing that can break is the function it's being used in. This means that even a simple textual search-and-replace is an effective method for changing the name of a local varaible.
Renaming properties or methods, for example, is much more problematic because they can potentially be referenced anywhere in your program. MyPy will give you errors if, for example, you try to access a property or call a method that doesn't exist. These are the really important errors to catch because they are global in nature: If you rename a property or method, any part of your program can potentially break.
To add type hints to an existing project, I would take the approach of introducing them to areas that you're about to refactor and that therefore need to be protected against breaking. This ensures that the work you're doing by introducing type hints has an immediate payoff.
Upvotes: 1