1. Please DON'T do that

Forked from Re: Try/Catch

Ekhnat0n said...

By replacing if error_code != 0 then by if error_code then you will save lots of typing

While technically it may work in the short term, if it damages intent or readability in any way it is not a good idea.

if x then ought really be reserved for when x is a boolean that can only be true or false1.

Simply having !=0 somewhere in the code can act as a reminder that it has special and/or other meaning.

In many cases it is -1 rather than "not 0" that you need to look out for anyway.

I don't equate the three characters !=0 with "lots of typing".

Should your compiler emit lower quality code when it hits a !=0, get a better compiler.

If anything I'd say "add !=0", selectively, to clarify intent.

Pete

1 alright, if length(x) then is I suppose a notable exception that breaks that rule.

new topic     » topic index » view message » categorize

2. Re: Please DON'T do that

Just to chime in, most editors that I've used have a MACRO capability.
So you could just have a two key combination insert

if error_no != 0 then 
 
else 
 
end if 

That would save a whole bunch of typing...

new topic     » goto parent     » topic index » view message » categorize

3. Re: Please DON'T do that

Agreed. To the casual observer it may seem verbose, but to anyone concerned with the operation of the code, adding these specific checks aids in readability and understanding.

A better way to (probably) accomplish this to have an is_error() function for testing the result. The code can be more easily read aloud to convey its operation.

function is_error( atom result ) 
    return (result != 0) -- zero = success, not zero = failure 
end function 

And then...

atom result = do_something() 
if is_error( result ) then 
    -- failure 
end if 

or...

atom result = do_something() 
if not is_error( result ) then 
    -- success 
end if 

etc.

-Greg

new topic     » goto parent     » topic index » view message » categorize

4. Re: Please DON'T do that

petelomax said...

Forked from Re: Try/Catch While technically it may work in the short term, if it damages
intent or readability in any way it is not a good idea.
if x then ought really be reserved for when x is a boolean that can only be true or false1.
etc
If anything I'd say "add !=0", selectively, to clarify intent.
Pete



The way I code it in assembly language
it will give a correct result ALWAYS

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu