sousou03 / glmatrix

Automatically exported from code.google.com/p/glmatrix
0 stars 0 forks source link

Return as early as possible #2

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
for example

vec3.add = function(vec, vec2, dest) {
        if(!dest) { dest = vec; }

        if(vec == dest) {
                dest[0] += vec2[0];
                dest[1] += vec2[1];
                dest[2] += vec2[2];
        } else {
                dest[0] = vec[0] + vec2[0];
                dest[1] = vec[1] + vec2[1];
                dest[2] = vec[2] + vec2[2];
        }

        return dest;
}
could be

vec3.add = function(vec, vec2, dest) {
        if(!dest) {
                var dest = vec;
                dest[0] += vec2[0];
                dest[1] += vec2[1];
                dest[2] += vec2[2];
                return dest; 
        }

        if(vec == dest) {
                dest[0] += vec2[0];
                dest[1] += vec2[1];
                dest[2] += vec2[2];
                return dest;
        }

        dest[0] = vec[0] + vec2[0];
        dest[1] = vec[1] + vec2[1];
        dest[2] = vec[2] + vec2[2];
        return dest;
        }
}

Original issue reported on code.google.com by danielhe...@gmail.com on 4 Jun 2010 at 7:18

GoogleCodeExporter commented 8 years ago
so try always this pattern:

if()
   ...
   return;
if()
   ...
   return;
if()
   ...
   return;
...
...

Original comment by danielhe...@gmail.com on 4 Jun 2010 at 7:24

GoogleCodeExporter commented 8 years ago
This has been much improved in version 0.9.

Original comment by Tojiro@gmail.com on 5 Jun 2010 at 8:29