Resolving Checkmarx issues reported
So, here we are using input variable String[] args without any validation…
July 03, 2018
While doing code review of one of junior, I encountered following code:
class Test:
logger = MyLogger.get_instance()
def __init__(self):
client = Jira.get_instance()
def do_process(self):
self.logger.info('Hey test')
# some other code
What bothered me was
Lets consider below code:
class Test:
a = 20
def __init__(self):
self.a = 10
test = Test()
print(test.a)
test.a = 10
Test.a = 30
print(test.a)
print(Test.a)
### Output
10
10
30
These are the variables that are associated with class not with the instance of class. Which means, static or class variables are shared across all instances of the class. There can be an instance variable with the same name as static one. But, both will have a different identifier and can have different values.
Now, you should always refer class or static variable with class name. Although, you can also refere a static variable with class instance. But, it will result into nighmares of your debug life if you declare same name instance variable. Also, if some new developer joins your team and try to read your code. If you have not refered class or static variable with class name, he might consider it an instance variable which it is not.
Always refere statics like:
Test.a
So, here we are using input variable String[] args without any validation…
MongoDB CRUD Operations Mongoose provides a simple schema based solution to…
Introduction VS code is an excellent free IDE for maany types of programming…
This post some useful tips of using strings, and some issues while dealing with…
While writing JUnit test cases, we encounter cases like we want to initialize a…
Listing down the commonly used Elastic Search queries. You can get search…
In this post, we will see some of the frequently used concepts/vocabulary in…
System design interview is pretty common these days, specially if you are having…
Introduction You are given an array of integers with size N, and a number K…
Graph Topological Sorting This is a well known problem in graph world…
Problem Statement Given a Binary tree, print out nodes in level order traversal…
Problem Statement Given an array nums of n integers and an integer target, are…