Javascript – Implementing Settings/Preferences in JavaScript

javascriptpreferencessettings

I am making a simple web app mostly in JavaScript. I was wondering as to how do I deploy settings/preferences? Do I just store user preferences somewhere and make use of if…else… statements all over the code?

I think that there must be a better alternative. I am know JS, jQuery & PHP and willing to learn anything new if at all required.

I have already made the app, only the settings are remaining. I know what options to give users and how to program them in js.

What's the most optimal way? How is it done in professional web apps and software made by companies (I am independent Student Developer – this is my first "BIG" project)?

EDIT:

For all the modifications that the settings are supposed to make (in this particular app), the whole js code base would litterally be filled with many branches of if…else…statements and I think that would make the code a lot harder to read and maintain.

In my app, the whole database table to be fetched from the database and the number and types of HTML DOM manipulation to be done, new elements to be added to HTML dom and whether some are visible or not would change. How do I deal with all that?

Best Answer

I think it depends on the nature of the settings.

Some settings are basically numbers, like number of results to display or a zoom factor or such like. These settings can be read from your settings structure (or API) as needed and plugged in as parameters to functions in your code. I think these are straightforward.

Other settings control functionality, like whether entire blocks of information are displayed. These naturally lead to branches in the logic of the app (if/else statements). If you want to eliminate these, use the strategy or delegate design patterns. You basically have a function preconfigured to follow one path or the other, and you pass that function as a parameter. The main code then becomes simple, and complexity is pushed to the set-up code that composes and selects the desired implementation functions.

If the logic that you think you need to generate your page is complex, and the content of the page is complex, some logic in the final implementation might be unavoidable. What you can do to keep it manageable is to rethink the logic and see if you can simplify it. I find this is often quite possible. Factoring out implementation from the logic that engages it can also make your code more manageable. In the main body you will have some logic that reads settings values and that invokes subroutines as needed. Well-selected parameters to those subroutines, including function parameters, might result in fewer of them overall. JavaScript lends itself to this model. Take advantage of it.

For example:

function addTable(tablename)
{
    while (readFromDatabase(tablename))
    {
        writeTableLine(...);
    }
}

function addGraph(tablename)
{
    while (readFromDatabase(tablename))
    {
        buildGraph(...);
    }
}

// in the main code:
var table1 = settings.getTable1name();
var table2 = settings.getTable2name();
var outputFunction = settings.shouldShowGraph() ? addGraph : addTable;

outputFunction(table1);
// more stuff
outputFunction(table2);

The code above can output data from any number of tables in either table or graph form with no if statements.

Related Topic