R
R
Ruslan Kasymov2014-01-16 23:17:42
PHP
Ruslan Kasymov, 2014-01-16 23:17:42

How to implement a concise record of conditions?

For example, if no error occurred, a message about the successful operation should be displayed. But there are several conditions. This is how I do it... But I feel in my gut that I can write more beautifully.

if ($this->isNewRecord) {
    if(!empty($this->parent_id)) {
        if (some condition) $success = true;
    }
    else {
        if (some condition) $success = true;
    }
}
else {
    if(!empty($this->parent_id)) {
        if (some condition) $success = true;
    }
    else {
        if (some condition) $success = true;
    }
}

if (@$success) {
    $this->message = "OK!";
}

You can of course also do this:
if ($this->isNewRecord && !empty($this->parent_id)) {
    if (some condition) $success = true;
}
elseif ($this->isNewRecord && empty($this->parent_id)) {
    if (some condition) $success = true;
}
elseif (!$this->isNewRecord && !empty($this->parent_id)) {
    if (some condition) $success = true;
}
else {
    if (some condition) $success = true;
}

if (@$success) {
    $this->message = "OK!";
}

Or, instead of a bunch of if-s, you can use a case-switch construction, but it seems to me that all such code is smelly.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Push Pull, 2014-01-16
@HDAPache

Why if $this->isNewRecord and else?
after all, there can still be success
with !empty($this->parent_id)
along the way of the appearance of some condition

if (some condition1 || some condition2 || some conditio3 || some condition4) { 
  $success = true; 
}

If these conditions occur only under certain conditions
$condition1 = ($this->isNewRecord && !empty($this->parent_id) && some condition1);
$condition1 = ($this->isNewRecord && empty($this->parent_id) && some condition2);
$condition1 = (!$this->isNewRecord && !empty($this->parent_id) && some condition3);
$condition1 = (!$this->isNewRecord && empty($this->parent_id) && some condition4);

and the same as above.
it would be clearer if at least more explained
. If this function only reports success, do return
Split into smaller pieces, if you can not avoid such a branch.

R
rhaport, 2014-01-17
@rhaport

such problems are solved each in its own way. Finding a common solution for everything will not always be optimal.
In this case, you can define a binary function functor: (0,1)x(0,1) -> (f1,f2,f3,f4)
functions and there will be your some conditions.
Like this (sorry for the syntax, because I don’t know php),

int to0123(bool a, bool b)
{
   return ((int)(a == true) << 1) + (int)(b == true);
}

If, conditions = {f1, f2, f3, f4};, where fx are functions that check your some conditions,
then you can write something like this
idx = to0123($this->isNewRecord, empty($this->parent_id));
if (conditions[idx])
     success = true

R
Ruslan Kasymov, 2014-01-17
@HDAPache

Here is the real code:

if ($category->isNewRecord && empty($category->pid)) {
     if ($category->saveNode()) $categorySaved = true;
}
elseif ($category->isNewRecord && !empty($category->pid)) {
     $rootCategory = Categories::model()->findByPk($category->pid);
     if ($category->appendTo($rootCategory) && $category->saveNode()) $categorySaved = true;
}
elseif (!$category->isNewRecord && empty($category->pid) && !$category->isRoot()) {
    if ($category->moveAsRoot() && $category->saveNode()) $categorySaved = true;
}
elseif (!$category->isNewRecord && !empty($category->pid)) {
    $rootCategory = Categories::model()->findByPk($category->pid);
    if ($category->moveAsFirst($rootCategory) && $category->saveNode()) $categorySaved = true;
}

As you can see, inside each if block there is a non-repeating code, it turns out through OR it will not be possible to combine the conditions.

S
Sergey, 2014-01-17
Protko @Fesor

exemplary...

$categorySaved = true;

if (!empty($category->pid)) {
    $rootCategory = Categories::model()->findByPk($category->pid);
    $categorySaved = $category->isNewRecord ?
        $category->appendTo($rootCategory) : $category->moveAsFirst($rootCategory);
} else if ($category->isNewRecord && !$category->isRoot()) {
    $categorySaved = $category->moveAsRoot();
}
$categorySaved = $categorySaved && $category->saveNode();

but in general it is not very beautiful ... But there is no universal way. For good, a lot of branches in one method is not good ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question