1

I need to clean my code and for better coding style i need to pass a value to function and call the result this is my code looks like now

        if (snapshot.hasData) {
          print('Result: ${snapshot.data}');

          double percentage1Calculate() {
            int wouldClick = snapshot.data[index].wouldClick;
            int ratherClick = snapshot.data[index].ratherClick;
            double percentage1 = wouldClick / (wouldClick + ratherClick) * 100;
            return percentage1;
          }

          double percentage2Calculate() {
            int wouldClick = snapshot.data[index].wouldClick;
            int ratherClick = snapshot.data[index].ratherClick;
            double percentage2 = ratherClick / (wouldClick + ratherClick) * 100;
            return percentage2;
          }
        }

What i need to do is instead of writing whole function percentage1Calculate and percentage1Calculate2 here i need to just pass the values in a function

Like this

        if (snapshot.hasData) {
          print('Result: ${snapshot.data}');

          double percentage1Calculate(
           snapshot.data[index].wouldClick, 
           snapshot.data[index].ratherClick);

          double percentage2Calculate(
           snapshot.data[index].wouldClick, 
           snapshot.data[index].ratherClick);
        }

And then in function i can get these values and use

          double percentage1Calculate(receive, recive) {
            int wouldClick = recive.wouldClick;
            int ratherClick = recive.ratherClick;
            double percentage1 = wouldClick / (wouldClick + ratherClick) * 100;
            return percentage1;
          }
2
  • can you specify the result of your trial? Commented May 6, 2020 at 15:06
  • What is your final goal? Calculate the percentages? Commented May 6, 2020 at 15:07

2 Answers 2

1

I don't think you gain anything by defining 2 almost identical functions, especially that the calculations are trivial, all extra code comes from getting access to the data.

Of course you can add parameters to the functions (making them even more similar, or even identical if you pass nominator and denominator as values).

Also, every time you define a function with access to variables in its lexical scope a separate closure needs to be created.

I think you are better off with this:

int wouldClick = snapshot.data[index].wouldClick;
int ratherClick = snapshot.data[index].ratherClick;
int total = wouldClick + ratherClick;

double wouldClickPercentage = wouldClick / total * 100;
double ratherClickPercentage = ratherClick / total * 100;
Sign up to request clarification or add additional context in comments.

Comments

1

You are trying to accomplish two different things with the same function. That's opposite to the very concept of a function or method.

But there is a work around to accomplish this thing. What you can do is add a flag in the parameter of the function so that you can determine one you want to be at numerator position.

If the flag value is 0 then numerator will be wouldClick and if flag will be 1 ot any other integer then numerator will be ratherClick.

double percentage1Calculate(dynamic data, int flag) {
    int wouldClick = data.wouldClick;
    int ratherClick = data.ratherClick;
    int numerator = flag == 1 ? ratherClick : wouldClick;
    double percentage1 = numerator / (wouldClick + ratherClick) * 100;
    return percentage1;
  }

Now when you call the function, you just pass the data and flag to the function like this.

if (snapshot.hasData) {
          print('Result: ${snapshot.data}');

          // to get the percentage with respect to wouldClick
          // as we are passing 0 as flag, numerator will be wouldClick
          double percentage1Calculate(
           snapshot.data[index], 0);

          //to get the percentage with respect to ratherClick
          double percentage2Calculate(
           snapshot.data[index], 1);
        }

Notice that I have dropped your double receive parameters in the function definition as you have wrote there. This is a little optimisation if you are using same index to get both the wouldClick and ratherClick values, otherwise you will have to add another parameter in the function definition to access the values.

1 Comment

But why need to pass flag? We have 2 different functions to calculate so just pass data isn't fine ?

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.