Shubham Sawant
Shubham Sawant

Reputation: 95

Flutter: Is there a better way to write the following code?

This is a Flutter Code which i tried to write...but since I am new to coding I need few help with code such as any shorter version i can make of the following code? or this is the best? This is the code...Also u can help with naming convention if u have better ones ;)

splitvar = comma seperated array String fetched from php-sql database

  static haversine() async {
    String splitvar = await Latlong1.latlong();
    List<String> splittedString = splitvar.split(",");
    int j = splittedString.length;
    List<List<double>> moblatlong = List();
    splittedString.removeLast();

    List<double> splittedInt = splittedString.map(double.parse).toList();

    for (int i = 0; i < j - 2; i = i + 3) {
      List<double> row = List();
      row.add(splittedInt[i]);
      row.add(splittedInt[i + 1]);
      row.add(splittedInt[i + 2]);
      moblatlong.add(row);
    }
    print(moblatlong);

    var usersInRange = new List();
    final lat1 = x.894165;
    final lon1 = y.0544283;

    for (int i = 0; i < moblatlong.length; i++) {
      var p = 0.017453292519943295; // Math.PI / 180
      var a = 0.5 -
          cos((moblatlong[i][2] - lat1) * p) / 2 +
          cos(lat1 * p) *
              cos(moblatlong[i][2] * p) *
              (1 - cos((moblatlong[i][1] - lon1) * p)) /
              2;

      var k = 12742 * asin(sqrt(a));
      print(k);
      if (k <= 3) {
        usersInRange.add(moblatlong[i][0].ceil());
        // newlong.add(moblatlong[i]);
      }
    }
    print(usersInRange);
  }

  //add haversine formula and check the radius
  //filter out mobile numbers
  //
  //moblatlong = moblatlong[nth user][1]//Longitude = mobllatlong[nth user][2]

}```

Upvotes: 2

Views: 131

Answers (1)

Gabe
Gabe

Reputation: 6865

This looks like good code, but since you posted this I will nitpicket a few things that could be shortened.

  1. You create a variable for you list's length: int j = splittedString.length; and then reference it later in your for loop. The variable j is rather obscure, and I would not even create that variable. I would just use splittedString.length in your for loop as it clears some confusion.

row.add(splittedInt[i]);
row.add(splittedInt[i + 1]);
row.add(splittedInt[i + 2]);

can be simplified to:

row.addAll([splittedInt[i], splittedInt[i + 1], splittedInt[i + 2]])
  1. var usersInRange = new List(); you are never re-assigning userInRange simply just mutating it so you can change it to final and the construction new List() can be shortened to []. The end result looks like: final usersInRange = [];.

  2. var p = 0.017453292519943295; // Math.PI / 180 I like that you show in your comments what that number is, but again I would change it to final and initialize it outside of your for loop instead of inside your for loop simply because it will save performance if you list is very long instead of the Dart compiler having to initialize space for p every time the loop runs it just does it once at the beginning.

  3. // newlong.add(moblatlong[i]); It's good practice to never comment out code (or if you do delete it very quickly afterwards). With tools like Git this is unnecessary and can quickly lead to cluttered code as your code changes over time.

This is just a few things but I hope it helps. Overall very nice code.

Upvotes: 2

Related Questions