Not Nothing - coding practise

D

David Cox

I always shudder when I see:

IF Not blah Is Nothing Then
.....

I would write it thus:

If blah Is Nothing
Then ' no action
Else 'things are going to change
.......

Comments?
 
S

Stefan Hoffmann

hi David,

David said:
I always shudder when I see:

IF Not blah Is Nothing Then
....

I would write it thus:

If blah Is Nothing
Then ' no action
Else 'things are going to change
......

Comments?
Using NOT as predicate is only a bad habit, when you have both branches
in the IF statement and you get a wrong logical order of your code, e.g.:

If Not Bool Then
'do the unimportant stuff
Else
'do the important stuff
End If

Having only one branch in the IF statement it gives you shorter code to
read, also empty branches are imho irritating.

mfG
--> stefan <--
 
B

Brendan Reynolds

No offence, David, but have you seen that television add for insurance where
people worry about inconsequential things? The punch-line goes something
like 'once you have Acme insurance, you'll need something else to worry
about'?
 
G

Guest

We all have our Idiosyncrasies.
The way you have it written, I would not do. You are adding two unnecessary
lines of code and making it harder to read. Try to keep in mind others may
have to read and modify your code. Plain, simplistic, well commented code
will make those who come behind you appreaciate your effort. I also do it
for myself. I may have to read the code six months from now and try to
remember what I was thinking.

It is interesting to note that a seasoned expert's code and a beginner's
code are not that different. It is the programmer who has enought experience
to have learned all the "cute" tricks and write obsfucating code because they
can.

As I said, we all have our pet peeves. Mine are macros and the DoMenuItem
 
D

David Cox

Klatuu said:
We all have our Idiosyncrasies.
The way you have it written, I would not do. You are adding two
unnecessary
lines of code and making it harder to read. Try to keep in mind others
may
have to read and modify your code. Plain, simplistic, well commented code
will make those who come behind you appreaciate your effort. I also do it
for myself. I may have to read the code six months from now and try to
remember what I was thinking.

It is interesting to note that a seasoned expert's code and a beginner's
code are not that different. It is the programmer who has enought
experience
to have learned all the "cute" tricks and write obsfucating code because
they
can.

As I said, we all have our pet peeves. Mine are macros and the DoMenuItem

DoMenuItem is not a peeve, it is a design sin. :->
 
G

Guest

if VBA is anything like c++ it compiles it all the same.

single line statments may be confusing to read, but are no different from
multi lines. matter of choice. (or demand by a boss LOL)
 
G

Guest

I really don't know c++, but my understanding is it compiles to an executable
..exe. VBA, on the other hand, is really an interprative language and only
compiles to a tokenized state. Whether coding style affects how it is
compiled, I don't know.

It certainly always boils down to perference and shop standards. My
personal preference is to make the code as easy to read as possible. I am
also a coding minimalist. If I have to do the same operation more than once,
it will probably become a function, for example.
 
H

HumanJHawkins

Yeah, but what if you get paid by the line! :)

FYI, many poets with short lines have admitted that that was a more
compelling reason for their choice than the actual content of their
poetry.
 
J

J. Goddard

But then you have MS Access idiosyncracies, such as

If not rst.nomatch then
'
' Go ahead and do stuff!
'
endif

I've always wondered who dreamed up that - instead of checking if a find
was successful, we check if it was not unsuccessful. Maybe written by a
TV lawyer, or a government bureaucrat. (No offence intended to anyone -
apologies in advance)

John
 
D

David Cox

Klatuu said:
I really don't know c++, but my understanding is it compiles to an
executable
.exe. VBA, on the other hand, is really an interprative language and only
compiles to a tokenized state. Whether coding style affects how it is
compiled, I don't know.

I was taught that the computer worked for me, not I for it. I was coding for
humans, not for the machine. I do not care which is 30 microseconds faster
than the other.

It just seems natural for me to avoid double negatives, as I would in
speech.
It certainly always boils down to perference and shop standards. My
personal preference is to make the code as easy to read as possible. I am
also a coding minimalist. If I have to do the same operation more than
once,
it will probably become a function, for example.

I was also taught to be minimalist, and use many subroutines and functions,
using descriptive names. "Do it in one page.". At first I cried for the
wasted machine cycles, but learned through experience the wisdom. Then
discovered what virtual memory and cache was, and doing it all in one page
had a different implication. Small was indeed beautiful.
 
D

David Cox

If blah Is Nothing
You can see I was brought up when programing was done in black and white,
and long lost languages invade my VB. :-<

If rst.nomatch then ' nothing to do
else ' process the data
 
J

J. Goddard

Are you grey enough to remember this horror?

IF (A) 10,20,30

Hint: VB it's not!

John
 
J

J. Goddard

The statement comes from an earlier version of FORTRAN, which didn't
have if-then-else strucure.

It evaluates the numeric (ONLY!) expression A; if A<0 go to statement
10, if A=0 go to 20, and if A>0 go to 30.

Ever heard the term spaghetti code? That's what lead to it.

John
 
D

David Cox

Fortran IV was the first programming language I learned.

"Youth of today, don't know they're born. " Else clause? We didn't have else
clauses.
IF ... GOTO was all we had. We built your world with IF .. GOTO ......
 
D

David Cox

I just had to do some searches ...

http://en.wikipedia.org/wiki/FORTRAN

The Fortran jokes section made me laugh.

The IF, Uh?, Else syntax you described came from the first version and
predated the IF ... GOTO ... syntax. that I learned. I could not remember
where I had seen it, and actually thought it more versatile when I did. (
:-<)


The IF syntax you mentioned
 
O

onedaywhen

David said:
I always shudder when I see:

IF Not blah Is Nothing Then
....

I would write it thus:

If blah Is Nothing
Then ' no action
Else 'things are going to change
......

Comments?

I agree that:

If Not blah Is Nothing

isn't very intuitive. However, I prefer it to your preference i.e. a
blank/missing If clause. I note you felt the need to add a comment to
make explicit that the omission of the If clause is by design.

Where there are both If and Else clauses, I do the test for Nothing in
the If clause. Consequently, the following would make me shudder:

If x <> 1 Then
<<code1>>
Else
<<code2>>
End If

IIF(end_date IS NOT NULL, <<value1>>, <<value2>>)

Jamie.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top