c#完成把異常寫入日記示例(異常日記)。本站提示廣大學習愛好者:(c#完成把異常寫入日記示例(異常日記))文章只能為提供參考,不一定能成為您想要的結果。以下是c#完成把異常寫入日記示例(異常日記)正文
優先應用 字符串插值 來取代 字符串串連。
# bad
email_with_name = user.name + ' <' + user.email + '>'
# good
email_with_name = "#{user.name} <#{user.email}>"
# good
email_with_name = format('%s <%s>', user.name, user.email)
Consider padding string interpolation code with space. It more clearly sets the
code apart from the string.斟酌應用空格填充字符串插值。它更明白了除字符串的插值起源。
"#{ user.last_name }, #{ user.first_name }"
Consider padding string interpolation code with space. It more clearly sets the
code apart from the string.
斟酌替字符串插值留白。這使插值在字符串裡看起來更清晰。
"#{ user.last_name }, #{ user.first_name }"
采取分歧的字符串字面量援用作風。這裡有在社區外面受迎接的兩種作風,它們都被以為異常好 -
默許應用單引號(選項 A)和雙引號作風(選項 B)。
(Option A) 當你不須要字符串插值或許例如 \t, \n, ' 如許的特別符號的
時刻優先應用單引號援用。
# bad
name = "Bozhidar"
# good
name = 'Bozhidar'
(Option B) Prefer double-quotes unless your string literal
contains " or escape characters you want to suppress.
除非你的字符串字面量包括 " 或許你須要克制本義字符(escape characters)
優先應用雙引號援用。
# bad
name = 'Bozhidar'
# good
name = "Bozhidar"
第二種作風可以說在 Ruby 社區更受迎接些。該指南的字符串字面量,不管若何,
與第一種作風對齊。
不要應用 ?x 符號字面量語法。從 Ruby 1.9 開端根本上它是過剩的,?x 將會被說明為 x (只包含一個字符的字符串)。
# bad char = ?c # good char = 'c'
別忘了應用 {} 來環繞被拔出字符串的實例與全局變量。
class Person
attr_reader :first_name, :last_name
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
end
# bad - valid, but awkward
def to_s
"#@first_name #@last_name"
end
# good
def to_s
"#{@first_name} #{@last_name}"
end
end
$global = 0
# bad
puts "$global = #$global"
# good
puts "$global = #{$global}"
在對象插值的時刻不要應用 Object#to_s,它將會被主動挪用。
# bad
message = "This is the #{result.to_s}."
# good
message = "This is the #{result}."
操作較年夜的字符串時, 防止應用 String#+ 做為替換應用 String#<<。當場級聯字符串塊老是比 String#+ 更快,它創立了多個字符串對象。
# good and also fast
html = ''
html << '<h1>Page title</h1>'
paragraphs.each do |paragraph|
html << "<p>#{paragraph}</p>"
end
When using heredocs for multi-line strings keep in mind the fact
that they preserve leading whitespace. It's a good practice to
employ some margin based on which to trim the excessive whitespace.
heredocs 中的多行文字會保存前綴空白。是以做好若何縮進的計劃。這是一個很好的
做法,采取必定的容貌在此基本上增添過量的空白。
code = <<-END.gsub(/^\s+\|/, '') |def test | some_method | other_method |end END #=> "def test\n some_method\n other_method\nend\n"