Skip to content

Instantly share code, notes, and snippets.

@idlewan
Last active August 29, 2015 14:01
Show Gist options
  • Save idlewan/ceeebc2c67c0f7299bce to your computer and use it in GitHub Desktop.
Save idlewan/ceeebc2c67c0f7299bce to your computer and use it in GitHub Desktop.
"return ..." vs "result = ..."
// ...
N_NIMCALL(NimStringDesc*, test1_82003)(NimStringDesc* a) {
NimStringDesc* result;
NimStringDesc* LOC1;
result = 0;
LOC1 = 0;
LOC1 = rawNewString(a->Sup.len + 5);
appendString(LOC1, a);
appendString(LOC1, ((NimStringDesc*) &TMP5));
result = LOC1;
goto BeforeRet;
BeforeRet: ;
return result;
}
N_NIMCALL(NimStringDesc*, test2_82009)(NimStringDesc* a) {
NimStringDesc* result;
NimStringDesc* LOC1;
result = 0;
LOC1 = 0;
LOC1 = rawNewString(a->Sup.len + 5);
appendString(LOC1, a);
appendString(LOC1, ((NimStringDesc*) &TMP5));
result = LOC1;
return result;
}
N_NIMCALL(NimStringDesc*, test3_82017)(NimStringDesc* a, NI b) {
NimStringDesc* result;
NimStringDesc* res;
NI i_82044;
NI HEX3Atmp_82045;
NI res_82047;
result = 0;
res = copyString(((NimStringDesc*) &TMP8));
i_82044 = 0;
HEX3Atmp_82045 = 0;
HEX3Atmp_82045 = b - 1;
res_82047 = 0;
while (1) {
NimStringDesc* LOC2;
NimStringDesc* LOC3;
if (!(res_82047 <= HEX3Atmp_82045)) goto LA1;
i_82044 = res_82047;
LOC2 = 0;
LOC3 = 0;
LOC3 = nimIntToStr(i_82044);
LOC2 = rawNewString(LOC3->Sup.len + a->Sup.len + 0);
appendString(LOC2, LOC3);
appendString(LOC2, a);
res = resizeString(res, LOC2->Sup.len + 0);
appendString(res, LOC2);
res_82047 += 1;
} LA1: ;
result = copyString(res);
goto BeforeRet;
BeforeRet: ;
return result;
}
N_NIMCALL(NimStringDesc*, test4_82052)(NimStringDesc* a, NI b) {
NimStringDesc* result;
NI i_82078;
NI HEX3Atmp_82079;
NI res_82081;
result = 0;
result = copyString(((NimStringDesc*) &TMP8));
i_82078 = 0;
HEX3Atmp_82079 = 0;
HEX3Atmp_82079 = b - 1;
res_82081 = 0;
while (1) {
NimStringDesc* LOC2;
NimStringDesc* LOC3;
if (!(res_82081 <= HEX3Atmp_82079)) goto LA1;
i_82078 = res_82081;
LOC2 = 0;
LOC3 = 0;
LOC3 = nimIntToStr(i_82078);
LOC2 = rawNewString(LOC3->Sup.len + a->Sup.len + 0);
appendString(LOC2, LOC3);
appendString(LOC2, a);
result = resizeString(result, LOC2->Sup.len + 0);
appendString(result, LOC2);
res_82081 += 1;
} LA1: ;
return result;
}
// ...
proc test_1(a: string): string =
return a & "asdfg"
proc test_2(a: string): string =
result = a & "asdfg"
echo test_1("abc")
echo test_2("def")
proc test_3(a: string, b: int): string =
var res = ""
for i in 0.. < b:
res.add($i & a)
return res
proc test_4(a: string, b: int): string =
result = ""
for i in 0.. < b:
result.add($i & a)
echo test_3("abc", 3)
echo test_4("def", 3)
@idlewan
Copy link
Author

idlewan commented May 14, 2014

Using the result implicit variable is better than using return when control flow is not affected by the use of return.

As you can see between the test_3 and test_4 procs, it can avoid copying the returned variable (result = copyString(res); in test3_82017 when using return res in test_3).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment