Do you prefer “if (var)” or “if (var != 0)”?

I’ve been programming in C-derived languages for a couple of decades now. Somewhere along the line, I decided that I no longer wanted to write:

if (var)    // in C
if ($var)   # in Perl

when what I meant was:

if (var != 0)
if (defined $var and $var ne '')

I think part of it is that I have a strongly-typed brain and in my mind, “if” requires a boolean expression.

Or maybe it’s because I use Perl so much and truth and falsehood in Perl is such a mine-field.

Or maybe it’s just because these days, I’m mainly a Java programmer.

What are your preferences and why?

How do I reconcile ‘top’ and ‘free -m”s memory usage reporting in linux?

From free -m i can see that there is 377MB of memory used and 1649MB free on the machine(Of which 1567 is cached by ubuntu). See below for the actual output:

[email protected]:~$ free -m
             total       used       free     shared    buffers     cached
Mem:          2026       1975         51          0         30       1567
-/+ buffers/cache:        377       1649
Swap:         3153         87       3065

However when I look at top‘s output I can see processes using 1GB and 273MB in the RES column. RES means resident in non-swapped physical memory from the top man page.

top - 11:45:26 up 1 day, 38 min,  3 users,  load average: 0.27, 0.21, 0.23
Tasks: 125 total,   1 running, 123 sleeping,   0 stopped,   1 zombie
Cpu(s):  0.2%us,  6.6%sy,  0.0%ni, 92.8%id,  0.5%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   2075560k total,  2023796k used,    51764k free,    31264k buffers
Swap:  3229024k total,    89764k used,  3139260k free,  1605400k cached

 6689 root      20   0 1242m 1.0g 1.0g S   13 51.1 292:59.21 vmware-vmx
 6658 root      20   0  492m 273m 262m S    2 13.5  41:16.75 vmware-vmx
    1 root      20   0  2844  536  484 S    0  0.0   0:01.50 init
    2 root      15  -5     0    0    0 S    0  0.0   0:00.00 kthreadd
    3 root      RT  -5     0    0    0 S    0  0.0   0:00.02 migration/0

I understand that some of that is shared by other processes (Shared objects and the like). But if there is 1GB of memory shared by other processes, surely at least that much must be used?

How does free report 377MB of memory used and when I look at top I see processes using 1GB and more RES memory?