Skip to content
36 changes: 36 additions & 0 deletions test_bad_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] SECURITY

API key and database credentials are hardcoded in plain text.

Fix:
Store sensitive information securely using environment variables or a secrets manager.

import sys
import requests

API_KEY = "sk-1234567890abcdef"

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] SECURITY

API key and database credentials are hardcoded in plain text.

Fix:
Store sensitive information securely using environment variables or a secrets manager.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] SECURITY

API key and database credentials are hardcoded in plain text.

Fix:
Store sensitive information securely using environment variables or a secrets manager.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] SECURITY

Hardcoded database URL contains plain text password.

Fix:
Use a secure method to store and retrieve database credentials.

DB_URL = "postgresql://admin:password123@localhost/prod"
MAX = 100

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] NAMING

Function 'p' and variable 'd' have single-letter names. Function 'p' should be renamed to something descriptive like 'process_data' and variable 'd' should be renamed to something like 'data_dict'.

Fix:
def process_data(data_dict):

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] NAMING

Variable 'x' has a single-letter name. It should be renamed to something descriptive like 'data_list'.

Fix:
data_list = data_dict['data']

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] NAMING

Variable 'res' has a misleading name. It should be renamed to something descriptive like 'result_list'.

Fix:
result_list = []

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] NAMING

Variable 'tmp' has a misleading name. It should be renamed to something descriptive like 'doubled_value'.

Fix:
doubled_value = data_list[i] * 2

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] CUSTOM

Function 'p' is missing a docstring. It should have a docstring that describes its purpose and parameters.

Fix:
def process_data(data_dict):
'''Process data and return the result.'''

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] NAMING

Variable 'MAX' is not descriptive and should be renamed to something more meaningful.

Fix:
Rename 'MAX' to a descriptive constant, e.g., 'MAX_DATA_THRESHOLD'.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MEDIUM] MAGIC_VALUES

Magic values (e.g., MAX) are used without explanation.

Fix:
Define these values as named constants with clear explanations.

def p(d):

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] NAMING

Function 'p' has a single-letter name and unclear purpose.

Fix:
Rename the function to something descriptive, such as 'process_data'.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] NAMING

Variable 'd' has a single-letter name and unclear purpose.

Fix:
Rename the variable to something descriptive, such as 'data_dict'.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] NAMING

Variable 'x' has a single-letter name and unclear purpose.

Fix:
Rename the variable to something descriptive, such as 'data_list'.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] NAMING

Variable 'res' has a misleading name.

Fix:
Rename the variable to something descriptive, such as 'processed_data'.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] NAMING

Variable 'tmp' has a misleading name.

Fix:
Rename the variable to something descriptive, such as 'doubled_value'.

x = d['data']

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] NAMING

Function 'p' and variable 'd' have single-letter names and should be renamed for clarity.

Fix:
Rename 'p' to a descriptive function name, e.g., 'process_data', and 'd' to a descriptive variable name, e.g., 'data_dict'.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MEDIUM] COMPLEXITY

Function 'p' can be simplified using list comprehension.

Fix:
Replace the function body with a list comprehension, e.g., 'return [x * 2 for x in d['data']]'.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] CUSTOM

None of the functions have docstrings.

Fix:
Add docstrings to all functions to describe their purpose and behavior.

res = []

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MEDIUM] NAMING

Function p and variable names (d, x, res, tmp) are unclear and do not follow naming conventions.

Fix:
Rename the function and variables to clearly indicate their purpose (e.g., process_data, input_data, result_list).

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] CUSTOM

None of the functions have docstrings.

Fix:
Add docstrings to all functions to describe their purpose and usage.

for i in range(len(x)):
tmp = x[i] * 2

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] COMPLEXITY

Function 'calc' has deeply nested conditionals. It should be refactored to reduce nesting.

Fix:
Consider using a more straightforward conditional structure or breaking the function into smaller functions.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] COMPLEXITY

Function 'calc' has too many parameters. It should be refactored to reduce the number of parameters.

Fix:
Consider passing a dictionary or object instead of individual parameters.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] CUSTOM

Function 'calc' is missing a docstring. It should have a docstring that describes its purpose and parameters.

Fix:
def calc(a, b, c, d, e, f):
'''Calculate the result based on the given parameters.'''

res.append(tmp)
return res

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] COMPLEXITY

Function 'calc' has deeply nested conditionals.

Fix:
Refactor the function to reduce nesting and improve readability.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] NAMING

Function 'calc' has unclear purpose and takes too many arguments.

Fix:
Rename the function to something descriptive and reduce the number of arguments.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] CUSTOM

Function 'calc' contains a print statement, which is not allowed in production code.

Fix:
Remove the print statement and use a logging mechanism instead.

def calc(a,b,c,d,e,f):
if a > 0:
if b > 0:
if c > 0:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] COMPLEXITY

Function 'calc' has deeply nested conditionals and should be refactored.

Fix:
Simplify the conditionals using a more straightforward approach, e.g., 'if all(x > 0 for x in [a, b, c, d]):'.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] COMPLEXITY

The calc function has deeply nested conditionals and does more than one thing.

Fix:
Simplify the conditionals and break the function into smaller, single-purpose functions.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] CUSTOM

The calc function contains a print statement, which is not allowed in production code.

Fix:
Remove the print statement and use a logging mechanism instead.

if d > 0:
result = a+b+c+d+e+f
print("result: " + str(result))
return result

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] CUSTOM

Print statement is used in production code.

Fix:
Replace the print statement with a logging statement or remove it altogether.

return 0

def fetch(u):

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] CUSTOM

Function 'fetch' does not have a docstring.

Fix:
Add a docstring to the function to describe its purpose and behavior.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] ERROR_HANDLING

Function 'fetch' does not handle potential exceptions from the requests library.

Fix:
Add try-except blocks to handle potential exceptions and provide meaningful error messages.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] CUSTOM

Function 'fetch' does not set a timeout for the HTTP request.

Fix:
Add a timeout parameter to the requests.get call to prevent indefinite waits.

r = requests.get(u)
return r.json()

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] CUSTOM

Function 'fetch' does not have a timeout set for the HTTP request.

Fix:
Add a timeout parameter to the requests.get call, e.g., 'requests.get(u, timeout=10)'.

def save(d, f):

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] CUSTOM

Function 'save' does not have a docstring.

Fix:
Add a docstring to the function to describe its purpose and behavior.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] ERROR_HANDLING

Function 'save' does not handle potential exceptions from file operations.

Fix:
Add try-except blocks to handle potential exceptions and provide meaningful error messages.

file = open(f, 'w')

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MEDIUM] CUSTOM

The fetch function does not have a timeout set for the HTTP request.

Fix:
Add a timeout parameter to the requests.get call (e.g., requests.get(u, timeout=10)).

file.write(str(d))

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] ERROR_HANDLING

Function 'save' does not handle potential file I/O errors.

Fix:
Add try-except blocks to handle potential file I/O errors, e.g., 'try: ... except IOError as e: ...'.

file.close()

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] ERROR_HANDLING

The save function does not handle potential file I/O errors.

Fix:
Add try-except blocks to handle potential file I/O errors.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[LOW] DEAD_CODE

Variable 'unused_var' is not used anywhere in the code.

Fix:
Remove the unused variable to improve code readability and maintainability.

unused_var = "hello"