# When to use global variable and when to use config\_db?

**URL:** <https://verificationacademy.com/forums/t/when-to-use-global-variable-and-when-to-use-config-db/37366>\
**Category:** UVM\
**Tags:** UVM, config\_db, global-variable\
**Created:** [March 27, 2020, 8:08am UTC](https://verificationacademy.com/forums/t/when-to-use-global-variable-and-when-to-use-config-db/37366 "2020-03-27T08:08:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![supal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@supal](https://verificationacademy.com/forums/u/supal)\
**Post date:** [March 27, 2020, 8:08am UTC](https://verificationacademy.com/forums/t/when-to-use-global-variable-and-when-to-use-config-db/37366/1 "2020-03-27T08:08:33Z")

</div>

Hello All,

I guess we have around three common ways to share variable value across UVM components,

1. Global variable
2. config\_db
3. uvm\_event

In our project, we are interchangeably using these. Some of the places we have used global variable (by declaring in tb\_pkg.sv) and at some places we are utilizing config\_db.

So I wish to know what is the good programming style to share variable values between components. I am aware that “using global variable” is not a good idea but I am not knowing its disadvantages too.

I am aware if we want to set and get variable onetime(static) then config\_db would be the best choice(set/get interface handle,class objects) but if I need to get some variable values per clk (dynamically) then wouldn’t using config\_db be a overhead? What should we use in this situation then?

One common good way I am seeing is to make global\_cfg class and declare all common variables,methods inside it and set it in a config\_db(from the test) and pass the same handle to all the uvm components down the hierarchy without cloning it.

Can anyone help me what should be the good way to share variables based on above situations? (Not sure if this has already been discussed)

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 28, 2020, 6:23am UTC](https://verificationacademy.com/forums/t/when-to-use-global-variable-and-when-to-use-config-db/37366/2 "2020-03-28T06:23:30Z")

</div>

_In reply to [supal](https://verificationacademy.com/forums/t/when-to-use-global-variable-and-when-to-use-config-db/37366):_

The main problem with global variables is scalability. If your design starts growing and you need multiple instances, it becomes difficult to maintain. Many languages discourage the use of global variables. You can search for it and find many articles, such as [this](https://dev.to/mervinsv/why-global-variables-are-bad-4pj).

We recommend using a single config\_object for each agent. If you find many items in your agent config object duplicated, you may want to put them in an env config object.

---

<div class="post-metadata">

**Author:** ![supal.shah](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@supal.shah](https://verificationacademy.com/forums/u/supal.shah)\
**Post date:** [June 30, 2020, 11:30am UTC](https://verificationacademy.com/forums/t/when-to-use-global-variable-and-when-to-use-config-db/37366/3 "2020-06-30T11:30:30Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/when-to-use-global-variable-and-when-to-use-config-db/37366/2):_

Thanks Dave. I understood !
