Re: issue with multiple variables in if statement

@25
To eliminate the globals in your example, take this part:

default_duration=3000
auto_disappear=True
disable_auto_for_message=False
extend_time=3000
request_extra=False
def dialog(message):

And turn it into:

def dialog(message,
    default_duration=3000
    auto_disappear=True
    disable_auto_for_message=False
    extend_time=3000
    request_extra=False):

Which will work exactly the same except that the caller of the function can now do things like:

dialog("hi", auto_disappear = False)

It also makes sure that something in one file that's trying to change the dialog settings doesn't mess with something else in another file trying to change the dialog settings, because by putting them in the function you just get the defaults unless they're specified.

Then you rename default_duration to duration because it's juskt duration now.  These are called keyword parameters, if you want to be able to Google this.

But more generally, at your level of programming globals.py is probably your best bet, just because you are still having trouble with even small stuff.  But the way you get rid of globals is via default function parameters like the above or putting them in a class instead.  You can for example write a Game class, then pass the Game class to all the things in the game when they get created.  Like this:

class Game:
    def __init__(self):
        self.global_thing = 5

class Mob:
    def __init__(self, game):
        self.game = game

g = Game()
m = Mob(g)

This is called dependency injection.  The mob depends on the things stored in the game, so we give it the game, because that's the thing it depends on to exist.  This seems useless, but the thing it lets you do is tell what can modify the globals without having to read the entire program: either something has the ability to touch a game or it doesn't, and if it doesn't it can't go behind the reader's back and do it anyway.  There are more advanced patterns than this, but this is a really basic one to get started with.

I think it's also important to distinguish between settings and globals.  If you have a conf.py with a bunch of settings in it that you set before you run it, that's fine, and all the other languages call those constants.  The reason globals are problematic is that when you start writing to them, it becomes literally impossible to figure out what can and can't touch them without reading literally the whole program.

To put this in perspective, work's codebase is probably around 100000 lines.  We easily have less than 100 globals that are used between multiple files, most of those are hidden behind functions that tightly control access.  Of the variables that aren't used between multiple files, 99% of those are for integrating with our metrics provider.  This isn't some work trade secret either, it's just how big codebases are, because anyone who is using globals all the time didn't get to 100000 lines before they went out of business due to all the bugs.

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : ironcross32 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector

Reply via email to