lundi 22 février 2021

How to optimize the structure of a sequence of functions?

i want to improve my javascript code and to optimize a frequent coding situation i have often to use. It is a sequence of function, which are collecting data from server or databases and as a result do something with it like draw a chart or fill a table. At the moment this block is not extremly good coded:

main();
 
function main () {  drawChart ();  }
 
function drawChart(){  getHistoricalTemp(); }
 
 
function getHistoricalTemp ()
{
  let Temperature = 38.1;
  getConversionfactor (Temperature);
}
 
function getConversionfactor (T)
{
  let Conversion = 1;
  getServerTime (T, Conversion);
}
 
function getServerTime (T, c)
{
  let time = 0;
  loadUsersettings (T, c, time);
}
 
function loadUsersettings ( T,  c,  z)
{
  let bgcolor = 255;
  initializeChart (T, c, z, bgcolor);
}
 
 
function initializeChart ( T,  c,  z,  bgc)
{
  console.info (" initializeChart: " + c + " | Temperature" + T + " Time: " + z + " color: " + bgc );
}

For the first i tried to improve this block to style it like a c++ class:


    let Temperature = null;
    let Conversion = null;
    let time = null;
    let bgcolor = null;

    function getHistoricalTemp() {  Temperature = 38.1;  }

    function getConversionfactor() {   Conversion = 1; }

    function getServerTime() {  time = 0; }

    function loadUsersettings() { bgcolor = 255;}

    function init() {
        console.log(`Opening database #`);

    }

    function draw_chart() { 
        
        getHistoricalTemp();
        getConversionfactor();
        getServerTime();
        loadUsersettings();
        console.info ("draw_chart with Temperature: " + Temperature + " | Conversion " + Conversion + " | time " + time + " | bgcolor " + bgcolor ); 
        }

    return {
        draw_chart: draw_chart,
        //close: init,
        //note: operations.note
    }
}

var chart = drawChart();
chart.draw_chart();
```

Is there a better way to code this wanted procedure? Whats the best practice to start all these function enclosed in the class?

Aucun commentaire:

Enregistrer un commentaire