organized modules
not that important, or maybe it is?
It’s really difficult to parse files when you only rely on your IDE for hopping around. I do blame myself, my IDE setup is dismal. However the point I’m trying to make here is that you should make the purpose of the file/module as apparent as possible from top-to-bottom. You will spend more time reading code than writing it, and this is how people read. I don’t want to pull your branch down and get intimate with what you’re doing.
Take the following example, in python because this is what I live and breathe these days.
from pathlib import Path
def is_audio_file(filename: str) -> bool:
return filename.endswith(('.mp3', '.wav', '.flac'))
def process_audio_file(filename: str) -> None:
print(f"Processing audio file: {filename}")
class DirectoryParser:
def __init__(self):
pass
def parse(self, directory: Path) -> None:
files = directory.iterdir()
for file in files:
if file.is_file():
if is_audio_file(file.name):
process_audio_file(file.name)
elif is_video_file(file.name):
process_video_file(file.name)
elif is_text_file(file.name):
process_text_file(file.name)
else:
print(f"Unknown file type: {file.name}")
def is_video_file(filename: str) -> bool:
return filename.endswith(('.mp4', '.mkv', '.mov'))
def is_text_file(filename: str) -> bool:
return filename.endswith(('.txt', '.md', '.csv'))
def process_video_file(filename: str) -> None:
print(f"Processing video file: {filename}")
def process_text_file(filename: str) -> None:
print(f"Processing text file: {filename}")
It’s confusing. If you don’t think so, I genuinely envy you. Look at the following instead:
from pathlib import Path
class DirectoryParser:
"""Parses a directory and processes files based on their type."""
def parse(self, directory: Path) -> None:
"""Scans the directory and processes files based on type."""
for file in directory.iterdir():
if file.is_file():
if is_audio_file(file.name):
process_audio_file(file.name)
elif is_video_file(file.name):
process_video_file(file.name)
elif is_text_file(file.name):
process_text_file(file.name)
else:
print(f"Unknown file type: {file.name}")
def is_audio_file(filename: str) -> bool:
return filename.endswith(('.mp3', '.wav', '.flac'))
def is_video_file(filename: str) -> bool:
return filename.endswith(('.mp4', '.mkv', '.mov'))
def is_text_file(filename: str) -> bool:
return filename.endswith(('.txt', '.md', '.csv'))
def process_audio_file(filename: str) -> None:
print(f"Processing audio file: {filename}")
def process_video_file(filename: str) -> None:
print(f"Processing video file: {filename}")
def process_text_file(filename: str) -> None:
print(f"Processing text file: {filename}")
I acknowledge this might not mean much entering the era of generated artifacts, but I find the latter so much easier to read. Thanks for coming to my opinionated ted talk.