E
E
Evgeny Fedorov2015-11-07 20:18:03
Programming
Evgeny Fedorov, 2015-11-07 20:18:03

How to properly think through the logic of conditional "if" branch statements?

Guys, I just can’t learn from the first and even from 3 times to draw a scheme of complex conditional operators in my head. I know how the code should behave, but for some reason I always have a bug in the logic with several operators, in groups and with the reverse value of the conditional expression "!". I notice that at these stages I slow down especially much =) Can anyone suggest a method that will make everything easier, a method that will make it easy to visualize the behavior and get rid of the brakes at this stage of development? =)
Banal simple example from php

if(!$this->data['status']) {
    //code
}

throws an error if there is no "status" index "Undefined index:". remodeling in
if (isset($this->data['status']) && !$this->data['status']) {
    //code
}

and I get wrong logic. I had to climb for a notebook ... I already decided but I spent too much time on it.
And so it always is with me =)
Maybe someone has already created his own simple template and is ready to share it =)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Night, 2015-11-07
@maxtm

I'm a bit freaked out by local answers :)
One advises to fucking change the language, the second one talks about executions that don't exist in puff, the third one suggests switching to TDD... no comments.
And only one, sane fshp , said what the person's problem is - array_key_exits (applaus)
But in the case, IMHO, only experience in the language, experience in industrial development, dozens of broken mines and crutches can allow you to write code without worrying over logical operators.

M
Maxim Moseychuk, 2015-11-07
@fshp

And what about if? You are initially using the wrong functions. Accordingly, the result is not correct.
1) Use array_key_exists instead of isset. isset works differently with null
2) Is there a guarantee in PHP that the left operand of the && operator will always be executed first? If not, then again you can catch the "Undefined index" error.

C
Curly Brace, 2015-11-07
@stasuss

it's not about if, it's about thinking ahead.
usually programmers think through all possible and impossible options for input data, and you are just too frivolous about logic and if has nothing to do with it, so you can get a null reference exception (whether there is such a thing in php or not)).
however, there is a downside, when especially meticulous, the code is cluttered with a bunch of checks in case of scenarios that can only happen if soton hellish enslave this world)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question