Peter Toth
Peter Toth

Reputation: 770

Effective Dart Usage: decleare variables with same value and same type

How could I make the following code shorter?:

String foo = "same";
String foo1 = "same";

One approach I guess would be:

String foo = "same", foo1 = "same";

However I don't find this satisfactory as the values I give to both variables are the same.

I'm looking for something like (pseudo code):

String foo & foo1 = "same";

so that both foo and foo1 are of type String and both have the same value.

Upvotes: 0

Views: 507

Answers (1)

julemand101
julemand101

Reputation: 31299

You can do something like this:

void main() {
  String foo = 'same', bar = foo;

  print(foo); // same
  print(bar); // same
}

I would say that the readability is not that great here and I personally would prefer to just declare each variable with the value it should have. Or do something like this:

void main() {
  String foo = 'same';
  String bar = foo;

  print(foo); // same
  print(bar); // same
}

Again, short code is not necessary readable code. Sometimes, it is better to make it clear what happens even if it takes more lines to do it. :)

Upvotes: 1

Related Questions