0

I'm writing a function in Javascript that will verify the existence of a particular kind of file and if it does not exist, then it will copy the file from a known location in a git repository to the correct location.

To do this, I'm also using a function I wrote that verifies the existence of any file (only at certain paths that we've pre-defined). Also, file.exists is a function prebuilt in our IDE.

That function looks like this:

function verifyFileExistence(file, path, existState)
{
    var result;
    var logMessage;
    var resultMessage;

    if (existState == true)
    {
        logMessage = "Verify that \"\"" + file + "\"\" exists.";

        result = (File.exists(path + file));

        if (result)
        {
            resultMessage = "\"\"" + file + "\"\" exists.";
        }
        else
        {
            resultMessage = "\"\"" + file + "\"\" does not exist.";
        }
    }
    else
    {
        logMessage = "Verify that \"\"" + file + "\"\" does not exist.";

        result = (!File.exists(path + file ));

        if (result)
        {
            resultMessage = "\"\"" + file + "\"\" does not exist.";
        }
        else
        {
            resultMessage = "\"\"" + file + "\"\" exists.";
        }
    }

    resultVP(logMessage, resultMessage, result)
}

Side Note: Each of these functions will write results to a log file which is why the different result/log/message variables appear. I left them in because I think they help to show make the logic clear.

So far, my function to check for the specific file type looks something like this:

import {copyFile,verifyFileExistence} from 'Path\\to\\FileUtilityLibrary.js';

function verifyLoadFile(file, path, existState, inFile, outFile)
{
    var exist;

    exist = (verifyFileExistence(file, path, existState));

    if (exist != true)
    {
        copyFile(inFile,outFile)
    }
}

I feel like having this many parameters in the function is inefficient and that maybe there's a more efficient way of handling them. Can I somehow simply this or is this the best way to handle parameters when calling functions inside a function?

4 Answers 4

2

You can do one object, for example:

const object = {
file,
path,
existState, 
inFile, 
outFile
}

and handle only one parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

What does it look like to use that as a parameter? Do you set the object in the script executing and then simply pass that object to the verifyLoadFile function?
I'm also writing the verifyLoadFile for a function library that will be used by others, so the idea is that it should be easy to implement for many circumstances. Or in other words, I don't know if I like the idea of this function having to have an object declared anytime before it can be used.
0

I'm going to assume you are using javascript ES6 with your import notation, that means you can use the spread operator to do this

import {copyFile,verifyFileExistence} from 'Path\\to\\FileUtilityLibrary.js';

function verifyLoadFile(inFile, outFile, ...fileParams)
{
    var exist;

    exist = (verifyFileExistence(...fileParams));

    if (exist != true)
    {
        copyFile(inFile,outFile)
    }
}

with fileParams being all the params you need to pass to your child function. With this notation you can have a variable number of parameters passed into this function.

Comments

0

As liskaandar's answer points out, an object would be the best way to reduce the number of parameters.

If you don't want to reference the object each time you use those variables, you can destructure the object to give you workable variables.

For example, if passing an object that looks like this:

const object = {
  file,
  path,
  existsState,
  infile,
  outfile
}

you can destructure it within your verifyFileExistence(fileObject) function by doing the below:

var {file,
     path,
     existsState,
     infile,
     outfile} = fileObject;

You can then reference those objects like normal by calling the normal variable names like file and path again.

1 Comment

This function is being written for a function library to be used by different users in different scripts. I'd rather require users to be aware that in order to use it, they have to declare an object before it can be used.
0

I thought in a way to optimize the code and this is what I would use in a scenario like yours:

        // verifyLoadFile function only needs three params (repositoryPath, localPath, fileName) and return an object with the log result and message

        function verifyLoadFile(repositoryPath, localPath, fileName){

            var exists = (!File.exists(localPath+ fileName));  //Check if file exists

            var logMessageHeader = "Verify that ";             //Header of message log
            var messageSuccess =  "\"\" exists.";
            var messageWarning = "\"\" does not exists.";
            var resultMessage = "\"\"" +fileName;              //Initialize resultMessage with redundant parth of message

            if(exists){
                resultMessage = resultMessage + messageSuccess;
            } else {
                resultMessage = resultMessage + messageWarning;
                copyFile(repositoryPath+fileName, localPath+fileName);                                                                       
            }

            return {
                logMessage: resultMessage
                resultMessage: logMessageHeader + resultMessage;
            };
        };


        // This is a function that initialize the sync of repository

        function syncRepository(typeOfRepository){

            var repositoryConfig = getConfig(typeOfRepository);               // In the example I get an array of objects or arrays that contains config needed 


            repositoryConfig.forEach((obj) => {                       
                const {logMessage,resultMessage} = verifyLoadFile(obj.repositoryPath, obj.localPath, obj.fileName);     //Retrieve log values for each file
                resultVP(logMessage, resultMessage);                                                       //Log the result 
            });
        }

In this way, you only need a 14-line function (verifyLoadFile) that verify if file exist, generate log messages and copy the file if it not exists, then only if needed log the result returned in each iteration

So i think, answering your initial question, that the best way to handle parameters in functions is optimize the code.

I hope it help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.