DEV Community

Zepher Ashe
Zepher Ashe

Posted on

Stop Writing Python Like JavaScript: Common Language-Switching Mistakes


Overview

When moving between languages, the main risks are forcing habits from one language onto another, misusing idioms, or forgetting environment-specific behaviours. The following practices help minimise friction and avoid subtle bugs.


1. Respect Language Idioms

  • Don’t force a main() everywhere.

    • Bash, Perl, PHP, JavaScript → natural to write procedural top-down code.
    • Python, Java, Go, Rust, C/C++ → explicit entry point is expected.
  • Follow the ecosystem’s coding style:

    • Python → snake_case for variables/functions, CamelCase for classes.
    • JavaScript → camelCase for variables/functions, PascalCase for classes.
    • PowerShell → Verb-Noun (Get-Process).
    • Java → CamelCase everywhere, verbose OOP structure.

2. Mind the Type System

  • Dynamic languages (Python, Bash, Perl, PHP, JavaScript):

    • Expect runtime type errors; validate inputs early.
    • Use linters/type checkers if available (mypy for Python, tsc for TypeScript).
  • Static languages (C, C++, Java, Go, Rust):

    • Embrace compiler feedback; it saves runtime pain.
    • Prefer explicit over inferred types when readability matters.
  • Hybrid (PowerShell):

    • Strongly typed but still allows implicit conversions → always declare expected types in Param() blocks.

3. Handle Errors the Right Way

  • Bash/Perl: check $? or $! exit codes; use set -euo pipefail in Bash.
  • Python/Java/JavaScript/PHP: structured try/except or try/catch.
  • Rust: pattern-match on Result/Option.
  • Go: check returned err explicitly (if err != nil { ... }).
  • PowerShell: try/catch/finally with terminating errors.

4. Manage Dependencies Correctly

  • Python → pip/poetry/venv.
  • JavaScript → npm/yarn.
  • PHP → composer.
  • Go → go mod.
  • Rust → cargo.
  • Perl → CPAN/cpanm.
  • Avoid hardcoding library paths; use ecosystem tools for portability.

5. Be Conscious of Execution Context

  • CLI scripts: Bash, Perl, Python — make sure you handle arguments ($1, sys.argv, @ARGV).
  • Web scripts: PHP, JavaScript — expect interaction with HTTP state, superglobals, or events.
  • Compiled binaries: Go, Rust, C/C++ — distribute with care; consider static builds for portability.
  • Mixed models: PowerShell, Python, Node.js → scripts can be interactive or part of automation pipelines.

6. Security Practices Don’t Always Translate

  • String handling:

    • Perl, Bash, PHP → watch out for injection risks (shell eval, SQL injection).
    • Python, Go, Rust → encourage safer libraries by default.
  • Secrets handling:

    • Never hardcode credentials; environment variables or vaults are standard.
  • Input validation:

    • JavaScript/PHP → sanitize all external input (XSS/SQLi risks).
    • Bash/Perl → validate before using in system commands.

7. Testing & Tooling

  • Always use linters/formatters when switching (helps enforce idioms):

    • Python → black, flake8.
    • JavaScript → eslint, prettier.
    • Go → gofmt.
    • Rust → clippy, rustfmt.
    • PowerShell → PSScriptAnalyzer.
  • Write small unit tests in the ecosystem’s framework:

    • Python → pytest.
    • Java → JUnit.
    • PHP → PHPUnit.
    • Go → built-in go test.
    • Rust → built-in cargo test.

Switching isn’t just syntax — it’s mindset. Learn the idioms, adopt ecosystem tools, and adjust error-handling and security practices to the language’s expectations.

Top comments (0)