The global constant pattern

Global variables are generally considered evil. I'm not kidding—they are evil. If you don't believe me, just google it. There are many reasons why they are bad, but in Julia land, they can also be a contributor to poor application performance.

Why do we want to use global variables? In the Julia language, variables are either in the global or local scope. For example, all variable assignments at the top level of a module are considered global. Variables that appear inside functions are local. Consider an application that connects to an external system—a handle object is typically created upon connection. Such handle objects can be kept in a global variable because all functions in the module can access the variable without having to pass it around as a function argument. That's the convenience factor. Also, this handler object only needs to be created once, and then it can be used at any time for subsequent actions.

Unfortunately, global variables also come with a cost. It may not be obvious at first, but it does hurt performanceindeed, quite badly, in some cases. In this section, we will discuss how bad global variables hurt performance and how the problem can be remedied by using global constants.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.157.45