Junior — Middle
Why is it considered good practice to limit the length of a line in code to about 79 characters?
sobes.tech AI
Answer from AI
Limiting the length of a line of code to about 79 characters is a recommendation from PEP 8, the official Python coding style. The main reasons:
- Readability: shorter lines are easier to perceive visually, especially when viewing code on different devices.
- Tool compatibility: many editors and terminals default to a width of about 80 characters.
- Simplified code comparison: when using version control systems and viewing diffs, long lines can complicate change analysis.
- Support for multiple windows and panels: developers often work with multiple files or windows, and limiting line length helps to comfortably place code.
Example:
# Good
if user.is_active and user.has_permission:
perform_action()
# Bad (too long line)
if user.is_active and user.has_permission and user.account_balance > 0 and user.subscription.is_valid:
perform_action()