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.

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
    
"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

1 Answer 1

up vote 3 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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