Removed the nested conditional switch construct#6338
Removed the nested conditional switch construct#6338Kubik-Rubik merged 6 commits intojoomla:stagingfrom
Conversation
|
@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. |
|
@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? |
|
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. |
|
@sovainfo thanks =^D I'm not sure where this is being used. Just opened the file and started looking for clean code easy wins. |
|
I think this would be better: |
|
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? |
|
I guess it is down to taste. In this case, the 3 different values for 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? |
|
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. |
|
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. |
|
I agree with @Mathewlenning that splitting it up would be even better. I'd go with: Reason I try to stay away from |
|
@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 |
|
@Bakual it isn't really the switch that I have a problem with, its the switch + conditional that tells me something isn't right. |
|
Yes, seems good! And something like: |
|
@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. |
|
Yep. So let's just tackle at least one of them each day. Then we only need 274 years to finish the job! |
|
In that case, I hope Google's "end-to-death" project works out. |
|
Oh, one more thing: Can change to |
|
Oops missed that one. Got it this time. =^D |
|
Nice one 👍 |
|
@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. |
…g once again saves the day =^P Common TCI lets be friends.
|
@Mathewlenning Where is this file called? In other words, how can I test this PR? |
|
Thank you @Mathewlenning! |
…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)
More cleaning.