Why does “a consistent, understandable interface” differentiates database from global states?

  softwareengineering

According to some answers of How are globals any different from a database? that explains how database is different from global state:

https://softwareengineering.stackexchange.com/a/319389/432039
https://softwareengineering.stackexchange.com/a/319403/432039
https://softwareengineering.stackexchange.com/a/319399/432039

as I understand, the answers says accessing to database uses “a consistent, understandable interface” code, which is different from accessing a global variable. However, what I don’t understand is, how a “a consistent, understandable interface” code differentiates from accessing global variables? For example, when accessing global variables:

public void myMethod(){
   AppData.someValue = x;
}

the global state is eval because you can paste “AppData.someValue = x” in any other methods, resulting everyone can modify the state.

When changing the access way from global state to database:

public void myMethod(){
   MyConnector mc=...;
   Session s=mc.beginTransaction();
   AppData appData=mc.getObjectFromTable(...);
   appData.someValue=x;
   s.save(appData);
   s.endTransaction();
   mc.close();
}

When you paste the wall of code:

MyConnector mc=...;
Session s=mc.beginTransaction();
AppData appData=mc.getObjectFromTable(...);
appData.someValue=x;
s.save(appData);
s.endTransaction();
mc.close();

to any other methods, every methods can also change the value of someValue.

Just because the code to trigger “everyone can modify the state” is longer or even has better name than “AppData.someValue = x” doesn’t change the nature of the code that lets everyone can modify the state.

To differentiate database from global states, I think we should wrap and apply dependency injection to the database driver:

public void myMethod(MyConnector mc){
   Session s=mc.beginTransaction();
   AppData appData=mc.getObjectFromTable(...);
   appData.someValue=x;
   s.save(appData);
   s.endTransaction();
   mc.close();
}

so that not every methods can access to database, same as the case of accessing global variables that inject the object only when a method needs to change it.

So my question is, why does “a consistent, understandable interface” differentiates database from global states, even I can paste the “a consistent, understandable interface” code everywhere to let anyone modify the global state, just like global variables?

LEAVE A COMMENT