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
(C++) What if I am refactoring the code and change the variable name?
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
(Python) What if I am refactoring the code and change the variable name?
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.
pylintshould pick up errors like that, and perhapsPyCharmcan also be configured to spot that?