Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

Sorry, I'd be happy to have someone improve the title of this question.

What are the benefits and downsides of "positively formulated" logic vs. having it "english readable". I'm looking for a generic position that can be placed into code guidelines.

Examples of what I see as "positively formulated" logic

if ($obj->isValid()){


if ($found){



if ($first){
    //do nothing
}else{
    doX();
}

$this->useFields(array('a','b'));

Examples of what I see as "negatively formulated" logic

if (!$first)
    doX();
}

$this->unset(array('d','e','f','g'));

Whereas if focusing on "English readable" I would see this as good:

if (isValid()){




if ($found){

and this is not "English readable"

if ($l){



if (isValid() || !isActive()){
    doX();
}
share|improve this question

marked as duplicate by gnat, Dan Pichelman, GlenH7, amon, Bart van Ingen Schenau 15 hours ago

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
"What are the benefits ... "positively formulated" logic vs. having it "english readable". for your organization? We can provide random benefits. You say both are good. Are you looking for help in splitting a hair here between "positive" and "English"? All good books with advice on writing better English say "positive". What more do you want? –  S.Lott Sep 6 '11 at 23:43
    
There are really two questions here: "Should code read like English" and "Should I avoid negative as in '!condition' tests" –  James Anderson Sep 7 '11 at 4:05
1  
Why not "German" or "Latin" readable - they are much more precise languages. –  James Anderson Sep 7 '11 at 4:06
    
thanks for the comments - I think when setting up coding guidelines choosing one style is better than "everyone does what they want" –  jdog Sep 8 '11 at 7:47

2 Answers 2

up vote 4 down vote accepted

Firstly I would question the benefits of "English readable". Natural languages and English in particular can be vague, ambiguous and misleading to an astounding degree.

How many meanings can a statement like "If x is not red and green" have?

The one profession which is stuck with using English yet requires a high degree of precision - the law - has ended up using a version of English that is unrecognizable to and unreadable by most native English speakers.

You need to assume that whoever is reading your code has a minimal understanding of the program language's syntax and it's common idioms.

Secondly I see no reason while "positively formulated" should be superior to "negatively formulated" in general. The most concise formulation which conveys the intention is the one to use.

So

if (!$first) {
    doX();
}

Is in my opinion better than:-

if ($first){
    //do nothing
}else{
    doX();
}

If you must go down that road then its better to change the condition name and invert the logic:

if ($subsequent) {
    doX();
}

The only thing I would really avoid is the double negative as in

if (!notValid)

Or visual basics unavoidbale:

if not Obj IsNothing
share|improve this answer
    
Strong support on avoiding inherent negatives (and as result, double negatives). It produces code that requires undue amount of effort in reading. Always go for isValid vs notValid, isOK vs isFaulty etc. –  SF. Sep 7 '11 at 8:38

I would go with negative logic.

Well, you have something here... The issue is not with the ability to read it in a given language but in the process beneath.

Going "negative logic" simplifies the process.
If you have a single layered condition then the benefits are to be drawn from the names of the procedures, like isValid, isFound, as you pointed out.
However the major benefit becomes visible below, if you choose to go with negative logic and not to use nested ifs:

if c1:
        work1t
        if c2:
                work2t
                if c3:
                        work3t
                else:
                        work3f
        else:
                work2f
else:
        work1f

vs.        

if ! c1:
        work1f
work1t

if ! c2:
        work2f
work2t

if ! c3:
        work3f
work3t

Instead of having a bushy structure that can only grow and that quickly turns into a nightmare to maintain, we now have a constant depth flow that can be manipulated fairly easy.

It is the same idea as with sanitization, we don't work unless all the parameters are valid, just that we sanitize the process.

This reduced complexity transcends any natural language as it is pure logic. I think that English readable is an appalling title. Stay away of natural languages. They are horrific because you start to talk to the computer and you end up with tons of inefficient code. You could go with something quantifiable like keeping the cyclomatic and NPath complexity of the class/method under a certain level.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.