Functions, naming conventions in Python

  • Context: Python 
  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Functions Python
Click For Summary

Discussion Overview

The discussion revolves around naming conventions for local and global variables in Python functions, particularly when the same base name is used for both. Participants explore the implications of such naming practices, referencing PEP 8 guidelines and sharing personal experiences with variable naming challenges.

Discussion Character

  • Debate/contested
  • Conceptual clarification
  • Technical explanation

Main Points Raised

  • Some participants question the necessity of using the same base name for local and global variables, suggesting that clearer naming could enhance code readability.
  • One participant proposes using an underscore prefix for local variables to differentiate them from global variables, but notes that this may conflict with PEP 8 conventions.
  • Another participant expresses a strong preference against using global variables altogether, arguing that it complicates code management.
  • Some participants share their struggles with naming conventions, citing personal difficulties in choosing meaningful variable names.
  • A participant suggests that if a function is intended to be more general, the local variable should have a more descriptive name than the global variable.
  • There is a discussion about the potential for confusion when using similar names across different files, with one participant questioning the need for such naming practices.
  • One participant shares a specific example from their codebase, illustrating how they handle variable naming in the context of a project involving scalar fields.
  • Another participant critiques the naming structure of a class and its attributes, suggesting alternatives that could improve clarity.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best practices for naming variables. While some advocate for clearer, more distinct names, others defend the use of similar names under certain circumstances. The discussion remains unresolved regarding the best approach to naming conventions in this context.

Contextual Notes

Participants express varying levels of familiarity with PEP 8, indicating that interpretations of its guidelines may differ. There are also references to specific coding practices that may not align with conventional standards, highlighting the subjective nature of naming conventions in programming.

  • #31
I'm a minimal commenter as well, but agree with Ibix. You should write comments when you expect other people who will read the code won't understand something.

The problem with comments is that you can write anything in a comment and the code will compile. Each comment introduces a long term vulnerability since it will need to be updated and synchronized with the code as it changes. The more comments you have, the more work that becomes, and the less likely a person is to be diligent in updating them. In general, you cannot rely on them to be accurate. And, if there are too many mundane comments, the reader may just stop reading them and then miss the important ones (like the boy who cried wolf).
 
  • Like
  • Skeptical
Likes   Reactions: PeroK, PeterDonis and jack action
Technology news on Phys.org
  • #32
FactChecker said:
Programming can be a messy struggle, especially if you are working in other people's code. Comments can help a lot.
The bulk of time in the software development life cycle (SDLC) is spent on maintenance, usually done by someone other than the original developer, who is likely off in another job, perhaps in a different company. Code steps that seemed trivial to the original developer might not be so to someone else who is tasked with fixing bugs that the code generated. Comments can help this person, especially if they have been kept up to date and are correct explanations of what the code is doing.
 
  • Like
Likes   Reactions: FactChecker
  • #33
I don't know what other people have experienced, but my experience is that there are far more cases of too little commenting than too much, especially from beginning programmers.
 
  • #34
Jarvis323 said:
You should write comments when you expect other people who will read the code won't understand something.
Including Future You.
 
  • Like
Likes   Reactions: hmmm27, PeroK and FactChecker

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K