Skip to content

Conversation

@Mathewlenning
Copy link
Contributor

More cleaning.

@zero-24
Copy link
Contributor

zero-24 commented Mar 6, 2015

@Mathewlenning can you add more information for your change? e.g. how we can this if needed etc. Thanks!


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/6338.

@Mathewlenning
Copy link
Contributor Author

@zero-24 I just removed a switch that had 3 options with nested conditionals in each option from the stream_seek method of this utility class.

None of the inputs/outputs have changed, so using it will be the same as before.

Does that answer your question?

@sovainfo
Copy link
Contributor

sovainfo commented Mar 7, 2015

Don't think so. Assume the question is how to test this. Maybe someone intimate with the platform/framework can confirm this: Looks like this can be tested when changing configuration with the FTP Layer in effect. It should work. Installing an extension with FTP Layer should work.

@Mathewlenning
Copy link
Contributor Author

@sovainfo thanks =^D I'm not sure where this is being used. Just opened the file and started looking for clean code easy wins.

@ghost
Copy link

ghost commented Mar 16, 2015

I think this would be better:

switch ($whence)
{
    case SEEK_SET:
        if ($offset < strlen($this->buffers[$this->name]) && $offset >= 0)
        {
            $this->position = $offset;

            return true;
        }
        break;

    case SEEK_CUR:
        if ($offset >= 0)
        {
            $this->position += $offset;

            return true;
        }
        break;

    case SEEK_END:
        if (strlen($this->buffers[$this->name]) + $offset >= 0)
        {
            $this->position = strlen($this->buffers[$this->name]) + $offset;

            return true;
        }
        break;
}

return false;

@Mathewlenning
Copy link
Contributor Author

@nonumber

That is the same construct that I re-factored out.

Could you explain the reasoning behind your thinking? Is there something that I've overlooked?

@ghost
Copy link

ghost commented Mar 16, 2015

I guess it is down to taste.
A switch is very good to do different things/checks based on the same variable.

In this case, the 3 different values for $whence.

The cleanup that can be made is to remove the useless elses.

What is the advantage of not using the switch but 3 separate ifs in your view?

@Mathewlenning
Copy link
Contributor Author

In general I try to avoid the switch, simply because it is a sign that I'm doing too much in one function.

I'd actually like to break this method into three private methods. Something like

public function stream_seek($offset, $whence)
{
    if(!in_array($whence, array('SEEK_SET','SEEK_CUR', 'SEEK_END'))
    {
         return false;
    }

    $whence = strtolower($whence);
    return $this->$whence($offset);
}

private method seek_set($offset)

private method seek_cur($offset)

private method seek_end($offset)

But when you make that much change, chances of getting merged seems unlikely.

The only real benefits of the 3 separate if clauses is it shifting the method back to one indentation and uses less lines to accomplish the same logic.

@Bakual
Copy link
Contributor

Bakual commented Mar 16, 2015

I agree with @nonumber here. Switches aren't bad as long as you do simple compares like this one here. They are very easy to understand.
Your "cleaner" code has indeed less indentation, but this doesn't make the code always easier to read. In this case I actually think it's harder to see what it does.

@ghost
Copy link

ghost commented Mar 16, 2015

I agree with @Mathewlenning that splitting it up would be even better. I'd go with:

public function stream_seek($offset, $whence)
{
    if(!in_array($whence, array('SEEK_SET', 'SEEK_CUR', 'SEEK_END'))
    {
        return false;
    }

    switch ($whence)
    {
        case SEEK_SET:
            return $this->stream_seek_set($offset);

        case SEEK_CUR:
            return $this->stream_seek_cur($offset);

        case SEEK_END:
            return $this->stream_seek_end($offset);
    }
}

private method seek_set($offset)
{
...
}

private method seek_cur($offset)
{
...
}

private method seek_end($offset)
{
...
}

Reason I try to stay away from $this->$foobar is because IDEs have a hard time of figuring out what is going on (can't jump to functions from call).
And I believe that if it is to hard for IDEs, then it is probably too difficult for people to quickly see what is going on too.

@Mathewlenning
Copy link
Contributor Author

@nonumber good reasoning.

I hate it when my IDE cannot tell me what is going on. It means I have to stop and think about it.

What if we got rid of that opening conditional and just returned false at the end.

public function stream_seek($offset, $whence)
{
    switch ($whence)
    {
        case SEEK_SET:
            return $this->stream_seek_set($offset);

        case SEEK_CUR:
            return $this->stream_seek_cur($offset);

        case SEEK_END:
            return $this->stream_seek_end($offset);
    }

    return false;
}

private method seek_set($offset)
{
...
}

private method seek_cur($offset)
{
...
}

private method seek_end($offset)
{
...
}

This would also get rid of the need to strtolower, which is always a +1

@Mathewlenning
Copy link
Contributor Author

@Bakual it isn't really the switch that I have a problem with, its the switch + conditional that tells me something isn't right.

@ghost
Copy link

ghost commented Mar 16, 2015

Yes, seems good!

And something like:

private method seek_set($offset)
{
    if ($offset < 0 || $offset >= strlen($this->buffers[$this->name]))
    {
        return false;
    }

    $this->position = $offset;

    return true;
}

private method seek_cur($offset)
{
    if ($offset < 0)
    {
        return false;
    }

    $this->position += $offset;

    return true;
}

private method seek_end($offset)
{
    $offset += strlen($this->buffers[$this->name]);

    if ($offset < 0)
    {
        return false;
    }

     $this->position = $offset;

    return true;
}

@Mathewlenning
Copy link
Contributor Author

@nonumber B-E-A-utiful.

Now we only have like 100,000 more clean ups to do and Joomla will be purring lol.

I'll make the changes.

@ghost
Copy link

ghost commented Mar 16, 2015

Yep. So let's just tackle at least one of them each day. Then we only need 274 years to finish the job!

@Mathewlenning
Copy link
Contributor Author

In that case, I hope Google's "end-to-death" project works out.

@ghost
Copy link

ghost commented Mar 16, 2015

Oh, one more thing:

$offset = strlen($this->buffers[$this->name]) + $offset;

Can change to

$offset += strlen($this->buffers[$this->name]);

@Mathewlenning
Copy link
Contributor Author

Oops missed that one. Got it this time. =^D

@Bakual
Copy link
Contributor

Bakual commented Mar 16, 2015

Nice one 👍
I love constructive discussions 😄

@ghost
Copy link

ghost commented Mar 16, 2015

@test: All good as far as I could test


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/6338.

@roland-d
Copy link
Contributor

@Mathewlenning Where is this file called? In other words, how can I test this PR?

@Kubik-Rubik
Copy link
Member

Thank you @Mathewlenning!

@Kubik-Rubik Kubik-Rubik merged commit fa07f78 into joomla:staging May 8, 2016
roland-d added a commit to roland-d/joomla-cms that referenced this pull request May 8, 2016
…leanup-installer-plugins

* 'staging' of https://github.com/joomla/joomla-cms:
  Smart Search: utf8_strpos: Offset must be an integer (joomla#10303)
  Removed an redundant else clause from JFeedParser::processElement (joomla#7961)
  CLEANING-JDataSet (joomla#7947)
  CLEANING-JAdapter (joomla#6679)
  Same  treatment as JArchiveBzip2 and JArchiveGzip (joomla#6515)
  Cleaning up JArchiveBzip2 (joomla#6495)
  Removed the nested conditional switch construct (joomla#6338)
  add edit tooltip to modules (joomla#10295)
  New installer plugins: remove the dot in the plugin name and other language review (joomla#10299)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants