10

Variable assignment as declaration is stupid. Looking at you, Python 😑

You can never be sure whether you accidentally overwrite a previous value and you have to fuck around with global because it doesn't know what's an assignment to a global var and what's a declaration. It's just not as satisfying as doing it explicitly and only leads to errors

Comments
  • 4
    I hate that python variables exist out of scope

    if cond:
    X = 1
    else:
    X = 3
    print(X)

    Is valid for example. I hate that so fucking much. Absolute trash.
  • 0
    @FrodoSwaggins I didn't even know that...

    So would variables be in global scope when declaring them in the body of 'if __name__ == "__main__":'
  • 1
    Same exact behavior worked in C in 90s. I doubt it suddenly became bad.
  • 0
    @12bitfloat that if body is in the global scope.
  • 1
    @irene thats not correct, C was never like this ever. Even B wasn’t like this.
  • 1
    @FrodoSwaggins oh, I thought it was about introducing new operator in python. Which behaves sorta like assignment in C.
  • 0
    @irene ah ok. C++ you can overload operators. I don’t think you can do it in c. Is that what you mean or am I still misunderstanding?
  • 0
    @FrodoSwaggins no, I mean the := (walrus?) operator which can be used as assignment in C:

    if (X := something())
  • 0
    @FrodoSwaggins i always thought the reason for this has to do with the way Python deals with and treats files(as modules) and thus a variable defined in the if statement that you mentioned is scoped to the innermost funcion, module or class.

    I get that, i still think it is stupid and error prone. Talk about polluting the global scope of the file.
  • 1
    @irene that’s not valid c; I don’t believe any such operator has ever existed in c. I can’t compile it with c89 or any of my prestandard compilers either
  • 1
    @irene &= |= += -= etc are all real things
  • 0
    @FrodoSwaggins it's a valid Python. In C it should be without ":".
  • 1
  • 0
    @irene Variable declaration should be ambiguous from assignment like it is in most languages. E.g. "let a = 1; b = 2;"
  • 0
  • 0
    @irene Because having "a = 1;" being declaration and assignment leads to errors and is ambiguous. It can't warn you when you accidentally use the same name for two distinct variables because it thinks it's just another assignment and you have to declare variables as global to be able to use variables from global scope. It's not explicit enough and thus fragile
  • 0
    Also when you have a typo when writing to a variable it can't give you a clear error message because it just implicitly declares a new variable
  • 0
    @12bitfloat don't do big stuff with something not intended for big stuff. break the stuff in small chunks. no ambiguity will be that way
  • 1
    People do mistakes. Why design a language in a way that computers can't help you when you inevitably do something wrong. Expliciteness is a good thing (as long as it doen't become too verbose).

    The walrus operator is fine btw because it explicitely declares a variable
  • 2
    If you think that, you're using it wrong. You're just not used to python. I started with python, then learnt VBA and C++ and thought the exact inverse.
    "wtf why do I have to declare this, it should know it exists because I assigned it"
  • 1
    @retnikt Maybe. But I have objective arguments that it is worse than using 'let' or 'var'. I mostly don't like Python because it's dynamically typed, these little things are just the cherry on top. I'm definitely not using it wrong though (how would that be possible?)
  • 1
    @retnikt It's the other way around that's the problem. When the interpreter knows that the variable is already declared but you don't. This translates into bug that a declared language would have found without manual (i.e. expensive) debugging.
  • 1
    @12bitfloat you say it like any other language treats you like kindergartner. If you fuck up, you fuck up. Computer is a stupid thing, it just does what was told.
  • 2
    @irene Real world example: you have to implement shit in existing code you didn't write and where you don't know every variable by heart. A declared language will complain if you redefine a variable, and the compiler will tell you which line the bug is in.

    An undeclared language will instead fuck up the program logic at runtime, and not necessarily in an obvious way.

    Quiz question: in which of the scenarios do you think you will be faster fixing the bug?

    Computers may be stupid, but they are damn good at automatic checks. That's why it doesn't make sense to do manual work that a compiler could have done as well.
  • 1
    @irene The computer is a tool, I want it to work with me not just sit around passively doing nothing. I'm still the one doing the work but my human brain can't remember everything perfectly without having to double check like my IDE can
  • 0
    @Fast-Nop okay, fair
  • 0
    @Fast-Nop use... an... IDE?
  • 1
    @retnikt Even under Windows where good IDEs are available and common, you shouldn't use a build system that depends on any IDE.

    Also, this idea is flawed because it works only for small projects. Already medium sized projects have several devs working on the same codebase and merging with Git, SVN or whatever, but not with the IDE. That's why the build chain has to refuse the code, and that's why it has to be a language feature, not an IDE feature.
  • 0
    @Fast-Nop when did I say anything about building? You notice that this variable name is already used while you're writing the code, then rename it. So yes it should be an IDE feature.
  • 0
    @Fast-Nop and when you merge code, why would variable declaration pose any problems?
  • 0
    @retnikt Variable declaration doesn't put problems, it uncovers them. As to what problems that are, read back the thread.
  • 0
    @Fast-Nop I don't understand
  • 0
    @retnikt The IDE cannot help you when you accidentially redefine a variable because it has no idea whether you meant to declare a new variable or just assign a new value to the already existing one. It can't show you an error because it isn't one. Except it is
  • 0
    @12bitfloat why do you have globals? Even if you have globals why aren't they named in uppercase to indicate special status?

    Python is not much of a problem. Users are.
Your Job Suck?
Get a Better Job
Add Comment