1 |
On Tue, 31 Mar 2009 11:05:30 -0700 |
2 |
Donnie Berkholz <dberkholz@g.o> wrote: |
3 |
> I noticed some eclass commits using java-pkg_func-exists() and it's a |
4 |
> lot more complicated than it needs to me. Perhaps not everybody knows |
5 |
> that bash generally gives a return status from functions of the last |
6 |
> command run in that function. So these two things are equivalent: |
7 |
[...] |
8 |
> java-pkg_func-exists() { |
9 |
> [[ -n "$(declare -f ${1})" ]] |
10 |
> } |
11 |
|
12 |
What's with that -n thing? If you're going for less complicated, you |
13 |
might as well go the whole hog: |
14 |
|
15 |
java-pkg_func-exists() { |
16 |
declare -f ${1} >/dev/null |
17 |
} |
18 |
|
19 |
But then you're wasting time writing all those extra bytes |
20 |
to /dev/null, so: |
21 |
|
22 |
java-pkg_func-exists() { |
23 |
declare -F ${1} >/dev/null |
24 |
} |
25 |
|
26 |
But wait! $1 and ${1} are the same when they're surrounded by |
27 |
whitespace. Those bytes are important! |
28 |
|
29 |
java-pkg_func-exists() { |
30 |
declare -F $1 >/dev/null |
31 |
} |
32 |
|
33 |
And we don't need that extra space before the >, either: |
34 |
|
35 |
java-pkg_func-exists() { |
36 |
declare -F $1>/dev/null |
37 |
} |
38 |
|
39 |
I hope people can apply these lessons to write much more compact code. |
40 |
I'm sure we all agree that shorter code is always better, since having |
41 |
less to read makes things easier to understand. |
42 |
|
43 |
-- |
44 |
Ciaran McCreesh |