Dart is an object oriented programming language borrowing from both Java and Javascript. What general tips do you have for golfing in Dart? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Dart (e.g. "remove comments" is not an answer). Please post one tip per answer.

If a tip is similar to Java/JS, please link to the answer in the original language's thread as well if you can.

Taken mostly from Joey's Tips for Powershell


5 Answers 5


Variable declaration

Regular variable declaration takes 4 bytes (var) + 2 bytes/variable (i,). You can declare variables as optional named parameters for your function and shave a few bytes.

f(){var i,j,k;} //Takes 15 bytes
g({i,j,k}){}  //Takes 12 bytes

Note: This doesn't work for non-constant values, for example:

f({i=List}){}  //Works
g({i=[]}){}  //Doesn't work

f({i=0,j=0}){}  //Works
g({i=0,j=i}){}  //Doesn't work

Implicit parameter passing

In some cases where Dart expects a function to be declared, you can code the function elsewhere, and then just pass its name. Dart will take care of passing the value automatically. Let me illustrate :

[0,1,2,3,4,5].forEach((i) => print(i)); //Using a lambda
[0,1,2,3,4,5].forEach(print); //Using implicit parameter passing since print() expects a similar parameter

You can also use your own functions

f(List i){
    i.forEach(print); //Prints each number on a new line

[[0,1], [1,2]].forEach(f); //Prints 0 \n 1 \n 1 \n 2\n

If you know a lambda might be used in multiple places, for example a map(), it can be useful to make it its own function and pass it that way instead of declaring it multiple times.

  • \$\begingroup\$ Is this not just a consequence of first-class functions? (I don't know Dart) \$\endgroup\$
    – Quelklef
    Nov 9, 2018 at 12:48
  • \$\begingroup\$ Looks like it's the case. I didn't know this was more widely spread dartlang.org/guides/language/… \$\endgroup\$
    – Elcan
    Nov 9, 2018 at 12:51

String conversion and concatenation

Dart doesn't allow for concatenation for types other than String and doesn't implicitly convert to String like C# does for example.

It however includes a very useful way to concatenate variables without needing to use any addition operator or explicitly casting to String (using the .toString() method).

var i=0,j=1,k=2;
var s0='$j'; //'1'
var s1='$i$j$k'; //'012'
var s2='i=$i'; //'i=0'

You can also perform operations directly in the ${} section and save a few temporary variable declarations.

var i=0,j=1,k=2;
var s='${i+j+k}'; //'3'

Creating range of consecutive integers

If you want to create a list of integers from start and end values, you can use below options:

final int s = 0; // start value
final int e = 5; // end value

List.generate(e-s,(i)=>i+s);  // 28 bytes
List.generate(e,(i)=>i);      // 24 bytes, if start is guaranteed to be zero
[for(int i=s;i<e;i++)i];      // 24 bytes
[for(int i=s;i<e;]i++];       // 23 bytes
[for(int i=s;i<e;]++i];       // 23 bytes, if start is exclusive
[for(;s<e;)s++];              // 16 bytes, if start can be changed in-place

Return inside loops

Let's suppose in your code there is a return inside a loop. You can use the following idiom to reduce bytes:

// indexOf method
f(v,x,[i=0]){for(;i<v.length;i++)if(v[i]==x)return i;return-1;}   // 63 bytes
f(v,x)=>([i=0]){for(;i<v.length;i++)if(v[i]==x)return i;}()??-1;  // 64 bytes
f(v,x)=>(i){for(;i<v.length;i++)if(v[i]==x)return i;}(0)??-1;     // 61 bytes

The more variables to be initialized, the more bytes can be reduced.

// "index of integral"
g(v,x,[s=0,i=0]){for(;i<v.length;i++){s+=v[i];if(s>=x)return i;}return-1};  // 74 bytes
g(v,x)=>(s,i){for(;i<v.length;i++){s+=v[i];if(s>=x)return i;}(0,0)??-1;     // 71 bytes

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.