我刚刚完成我的 PHP 应用程序编码,现在编码变得有点庞大,而且我使用的注释看起来丑陋且无效,因为我用//注释的每一行代码,这是我的第一次编码,我我完全不知道采用什么方法可以让我的评论看起来更好更清晰,以便将来引用我或任何人。如果有人用示例向我建议模式,我将不胜感激。
这是我用丑陋的注释编写的函数。您会使用哪种模式来注释代码?
//function to check if the uploaded Image is valid
function valid_image($image, $target, $url, $width, $height = 0) {
//Check if the uploaded image is of type jpeg
//if not then pop up a warning message and return false and redirect back
if( $image["type"] !== "image/jpeg") {
alert('File must be of type image/jpeg');
redirect_url($url);
return false;
}
//Check the file Dimension of the Uploaded Image if it matches with the defined Value
//Get the Dimensions of the image
list($image_width, $image_height) = getimagesize($image['tmp_name']);
//If the Parameter Height is empty then just check the image width and ignore the height rule
//Print the appropriate message and return false and redirect back
if( $height == '0') {
if( $image_width !== $width) {
alert("Incorrect File Dimension for " . $image['name'] . " Please make sure it is $width in width");
redirect_url($url);
return false;
}
}
//If the function has got all the parameter then check both the defined height and width
//Print the appropriate message and return false and redirect back
else if($image_width !== $width || $image_height !== $height) {
alert("Incorrect File Dimension for " . $image['name'] . " Please make sure it is $width X $height ");
redirect_url($url);
return false;
}
return true;
}
提前致谢。
最佳答案
在注释代码时考虑文档:可以从源代码注释中自动生成程序文档,这非常有用,迟早会成为一个问题。
我认为可以这么说 phpDocumentor's表示法已达到 PHP 行业标准的地位。他们的tutorial很好地概述了它的工作原理。 Here是带有 phpDoc 样式注释的完整示例 PHP 文件。简而言之,文档标准是在每个文件、类和方法之前加上“docBlocks”:
/**
* This is a description of my function
* @param string this is a description of the first parameter
* @param int this is a description of the second parameter
* @return int this describes what the function returns
*/
function myfunc ($param1, $param2)
{ ... }
phpDocumentor 有许多预定义的@keyword 关键字:@license、@version、@deprecated 和很多很多。
许多 PHP IDE 都能识别这种表示法,并能够从中提取您键入时出现的查找信息。
许多 IDE 用来编译任务列表的关键字是 @todo。
phpDoc 和 consorts 没有设置规则的一个区域是“内联注释”,就像您在特定步骤之间的注释一样。
据我所知,这里没有约束性规则;然而,多年来,尤其是自从我加入 SO 以来,我越来越不热衷于对我的代码所做的每一步进行评论,采用了“好的代码应该评论自己”.
这意味着限制对代码中尚不明显的事物以及所用变量名称的注释。 (变量名的正确选择极其重要,比冗长的注释更重要!)
关于php - 我应该采用哪种模式来评论我的 php 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3639071/
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的