以更简洁的方式重写此代码,无需太多其他代码
我想重写这段代码,而不需要那么多“其他”,但在不需要时不检查事物或运行查询方面仍然保持高效。
有人可以建议更好的方法来编写这个函数吗?
public static function fetch($content) {
products_library::init();
self::$cache = $cache = url::assetsPath() . '../cache/soldout_cache';
//check the cache
if (file_exists($cache)) {
$cache_date = filectime($cache);
db::select('date_modified');
db::orderBy('date_modified DESC');
db::limit(1);
$mod_date = db::get('sc_module_products')->fetch(PDO::FETCH_ASSOC);
if ($mod_date) {
$mod_date = strtotime('date_modified');
if ($cache_date >= $mod_date) { //serve the cache
try {
$soldout = filewriter::read($cache);
$soldout = unserialize($soldout);
} catch (Exception $e) {
$soldout = self::query();
}
}
else
$soldout = self::query();
}
else
$soldout = self::query();
}
else
$soldout = self::query();
$data['items'] = $soldout; // print_r($items); exit;
$html = view::load('Product_Display', $data, true);
return $html;
}
谢谢
I want to rewrite this code without so many "else's", but still keep it efficient in terms of not checking things or running queries if not needed.
Can someone suggest a better way to write this function?
public static function fetch($content) {
products_library::init();
self::$cache = $cache = url::assetsPath() . '../cache/soldout_cache';
//check the cache
if (file_exists($cache)) {
$cache_date = filectime($cache);
db::select('date_modified');
db::orderBy('date_modified DESC');
db::limit(1);
$mod_date = db::get('sc_module_products')->fetch(PDO::FETCH_ASSOC);
if ($mod_date) {
$mod_date = strtotime('date_modified');
if ($cache_date >= $mod_date) { //serve the cache
try {
$soldout = filewriter::read($cache);
$soldout = unserialize($soldout);
} catch (Exception $e) {
$soldout = self::query();
}
}
else
$soldout = self::query();
}
else
$soldout = self::query();
}
else
$soldout = self::query();
$data['items'] = $soldout; // print_r($items); exit;
$html = view::load('Product_Display', $data, true);
return $html;
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将其重构为返回而不是 else 语句的方法
我不明白这一行,那里有错误吗?
Refactored it into a method that returns instead of else statements
I don't understand this line, is there a bug there?
将 $soldout 设置为 NULL。然后删除
else $soldout = self::query()
语句。在
if
语句测试 $soldout 是否为 NULL 且结果正确后运行查询。Set $soldout to NULL. Then remove the
else $soldout = self::query()
statement.After the
if
statement test $soldout for NULL and it true run the query.开关盒块在这里会产生奇迹。您只需一个指向默认情况的
break
语句。然而,如果我处于你的立场,我会尝试重构整个事情,这需要的不仅仅是快速修复。A switch-case block would work wonders here. You'd just have a
break
statement that would point to a default case. However, if I were in your shoes, I'd attempt to refactor the whole thing, which would take more than a quick fix.像这样的东西可能会起作用。我不确定所有的 if 内部发生了什么以及为什么你需要这么多,它可能更紧凑。
Something like this might work. I'm not sure what's happening inside all the ifs and why you need so many, it might be more compact.
在我看来,好像你可以通过在第一个 if 检查之前将其设置为 self::query() 来默认 $soldout 为 self::query() ,然后删除所有其他内容,因此如果条件不匹配,它仍然是 self::query ()。可能不起作用,具体取决于 self::query() 的作用。
Seems to me as if you could default $soldout to be self::query() by setting it to that before the first if check then remove all the elses, so if the conditions doesn't match it will still be self::query(). Might not work depending on what self::query() does.