[TIPS] Refactoring - Clean Code - Tip 5 - Consistent Coding Style
By JoeVu, at: July 19, 2024, 5:44 p.m.
Refactoring Tip 5: Consistent Coding Style
-
Junior: Coding style might be inconsistent.
-
Senior: Adheres to PEP 8 and uses tools like
black
andflake8
to ensure consistent style.
Consistent coding style is essential for readability and maintainability of the code. Here's an example to illustrate the difference between how a junior and a senior developer might approach this principle.
Example 1: Inconsistent Naming Conventions and Formatting
Junior Developer's Approach
A junior developer might have inconsistent naming conventions and formatting in their code:
import requests
def fetchData(URL):
response = requests.get(URL)
if response.status_code == 200:
return response.json()
else:
return None
def processData(data):
processed_data = []
for item in data:
if item['value'] > 10:
processed_data.append(item)
return processed_data
data = fetchData('https://api.example.com/data')
if data:
processedData = processData(data)
print(processedData)
Senior Developer's Approach
A senior developer would follow PEP 8 guidelines and use tools like black
and flake8
to ensure consistent style:
import requests
def fetch_data(url):
response = requests.get(url)
if response.status_code == 200:
return response.json()
return None
def process_data(data):
processed_data = [
item for item in data if item['value'] > 10
]
return processed_data
data = fetch_data('https://api.example.com/data')
if data:
processed_data = process_data(data)
print(processed_data)
Example 2: Inconsistent Indentation and Spacing
Junior Developer's Approach
A junior developer might have inconsistent indentation and spacing in their code:
def calculate_total(items):
total=0
for item in items:
total+=item['price']
return total
items = [{'price': 10}, {'price': 20}, {'price': 30}]
total = calculate_total(items)
print ( total )
Senior Developer's Approach
A senior developer would ensure consistent indentation and spacing, following PEP 8:
def calculate_total(items):
total = 0
for item in items:
total += item['price']
return total
items = [{'price': 10}, {'price': 20}, {'price': 30}]
total = calculate_total(items)
print(total)
Example 3: Using Tools for Consistency
Junior Developer's Approach
A junior developer might not use tools to enforce coding style, leading to inconsistencies:
import requests
def get_data(url): response = requests.get(url); if response.status_code == 200: return response.json(); else: return Non
Senior Developer's Approach
A senior developer would use tools like black
and flake8
to format the code consistently:
# Run black to format the code
black script.py
# Run flake8 to check for style violations
flake8 script.py
Before running black
and flake8
:
import requests
def get_data(url):
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
return None
After running black
:
import requests
def get_data(url):
response = requests.get(url)
if response.status_code == 200:
return response.json()
return None
other great tools are https://pre-commit.com/ and https://github.com/4Catalyzer/fourmat.
Key Improvements:
- Consistent Naming Conventions: Follow PEP 8 guidelines for naming variables and functions.
- Consistent Indentation and Spacing: Ensure code is uniformly indented and spaced.
- Use of Formatting Tools: Utilize tools like
black
for automatic formatting andflake8
for style checks.
Consistent coding style improves code readability and maintainability. Using tools like black
and flake8
helps enforce these standards, ensuring that the codebase remains clean and consistent.