最近我发现自己编写的方法连续调用其他方法,并根据哪种方法首先返回适当的值来设置一些值.我一直在做的是用一种方法设置值,然后检查值,如果它不好,那么我检查下一个.这是最近的一个例子:
private void InitContent()
{
if (!String.IsNullOrEmpty(Request.QueryString["id"]))
{
Content = GetContent(Convert.ToInt64(Request.QueryString["id"]));
ContentMode = ContentFrom.Query;
}
if (Content == null && DefaultId != null)
{
Content = GetContent(DefaultId);
ContentMode = ContentFrom.Default;
}
if (Content == null) ContentMode = ContentFrom.None;
}
Run Code Online (Sandbox Code Playgroud)
如果不在数据库中,GetContent则应返回此方法.这是一个简短的例子,但你可以想象如果有更多的选择,这可能会变得笨重.有一个更好的方法吗?nullid
我正在使用rake帮助编译我正在编写的Chrome扩展程序的Coffeescript.
我的Rakefile看起来像这样:
COFFEE = FileList['src/*.coffee']
JS = COFFEE.ext 'js'
directory 'extension'
rule '.js' => ['.coffee', 'extension'] do |t|
`coffee -c -o extension #{t.source}`
end
desc "Build the extension in the 'extension' directory"
task :build => ['extension', JS] do
cp File.join('src', 'manifest.json'), 'extension'
end
Run Code Online (Sandbox Code Playgroud)
当.coffee我的src目录中只有一个文件时,没有问题.但是,只要我有多个.coffee文件,它就会出错:
$ rake build
> rake aborted!
> Don't know how to build task 'src/app.js src/background.js'
>
> Tasks: TOP => build
> (See full trace by running task with --trace) …Run Code Online (Sandbox Code Playgroud)