java打印以後辦法名示例分享。本站提示廣大學習愛好者:(java打印以後辦法名示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是java打印以後辦法名示例分享正文
在C與C++中可以如許打印以後函數名:
printf("%s",__func__);
但在Java沒有此說法,一切即對象,得從某個對象中去獲得,可分為兩種方法:
第一種:經由過程Thread類來獲得。
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.println(Thread.currentThread().getStackTrace()[1].getClassName());
第二種:經由過程Throwable類來獲得,第二種辦法有可以擴大,只需是throwable的子類都可以完成功效。
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
System.out.println(new Throwable().getStackTrace()[0].getClassName());
經由過程此兩種辦法能打印出以後辦法名和地點的類名,但有一個弊病就是以下index:
getStackTrace()[index]
取甚麼值,0或許1照樣其他數值,在這裡兩種方法的index分離寫成0和1,就是用來提示本身,它不是固定不變的,須要本身經由過程測試肯定上去。聽說JDK的版本紛歧樣取值能夠紛歧樣。但沒親身測試過。
我測試用的JDK版本:java version "1.7.0_17"
# note that elem is accessible outside of the for loop elem #=> 3 # good arr.each { |elem| puts elem } # elem is not accessible outside each's block elem #=> NameError: undefined local variable or method `elem'在多行的 if/unless 中果斷不要應用 then。
# bad if some_condition then # body omitted end # good if some_condition # body omitted end
在多行的 if/unless 老是把前提放在與 if/unless 的統一行。
# bad if some_condition do_something do_something_else end # good if some_condition do_something do_something_else end
愛好三元操作運算(?:)跨越if/then/else/end構造。
它加倍廣泛並且顯著的加倍簡練。
# bad result = if some_condition then something else something_else end # good result = some_condition ? something : something_else
應用一個表達式在三元操作運算的每個分支上面只應用一個表達式。也就是說三元操作符不要被嵌套。在如許的情況中寧可以使用 if/else。
# bad some_condition ? (nested_condition ? nested_something : nested_something_else) : something_else # good if some_condition nested_condition ? nested_something : nested_something_else else something_else end
不要應用 if x: ... - 它在Ruby 1.9中曾經移除。應用三元操作運算取代。
# bad result = if some_condition then something else something_else end # good result = some_condition ? something : something_else
不要應用 if x; ...。應用三元操作運算取代。
應用 if and case 是表達式如許的現實它們前往一個成果。
# bad
if condition
result = x
else
result = y
end
# good
result =
if condition
x
else
y
end
在 one-line cases 的時刻應用 when x then ...。替換的語法when x: xxx曾經在Ruby 1.9中移除。
不要應用when x; ...。檢查下面的規矩。
應用 ! 替換 not.
# 差 - 由於操作符有優先級,須要用括號。 x = (not something) # good x = !something 防止應用 !!. # bad x = 'test' # obscure nil check if !!x # body omitted end x = false # double negation is useless on booleans !!x # => false # good x = 'test' unless x.nil? # body omitted end
The and and or keywords are banned. It's just not worth
it. Always use && and || instead.
and 和 or 這兩個症結字被制止應用了。它名不符實。老是應用 && 和 || 來代替。
# bad # boolean expression if some_condition and some_other_condition do_something end # control flow document.saved? or document.save! # good # boolean expression if some_condition && some_other_condition do_something end # control flow
document.saved? || document.save!
防止多行的 ? :(三元操作符);應用 if/unless 來代替。
單行主體愛好應用 if/unless 潤飾符。另外一個好辦法是應用 &&/|| 掌握流程。
# bad if some_condition do_something end # good do_something if some_condition # another good option some_condition && do_something
布爾表達式應用&&/||, and/or用於掌握流程。(經歷Rule:假如你必需應用額定的括號(表達邏輯),那末你正在應用毛病的的操作符。)
# boolean expression if some_condition && some_other_condition do_something end # control flow document.save? or document.save!
防止多行?:(三元操作運算),應用 if/unless 替換。
在單行語句的時刻愛好應用 if/unless 潤飾符。另外一個好的選擇就是使 and/or 來做流程掌握。
# bad if some_condition do_something end # good do_something if some_condition # another good option some_condition and do_something
永久不要應用 unless 和 else 組合。將它們改寫成確定前提。
# bad unless success? puts 'failure' else puts 'success' end # good if success? puts 'success' else puts 'failure' end
不消應用括號包括 if/unless/while 的前提。
# bad if (x > 10) # body omitted end # good if x > 10 # body omitted end
在多行 while/until 中不要應用 while/until condition do 。
# bad while x > 5 do # body omitted end until x > 5 do # body omitted end # good while x > 5 # body omitted end until x > 5 # body omitted end
當你有單行主體時,盡可能應用 while/until 潤飾符。
# bad while some_condition do_something end # good do_something while some_condition
否認前提斷定盡可能應用 until 而不是 while 。
# bad do_something while !some_condition # good do_something until some_condition
輪回後前提斷定應用 Kernel#loop 和 break,而不是 begin/end/until 或許 begin/end/while。
# bad begin puts val val += 1 end while val < 0 # good loop do puts val val += 1 break unless val < 0 end
疏忽環繞外部 DSL 辦法參數的括號 (如:Rake, Rails, RSpec),Ruby 中帶有 "症結字" 狀況的辦法(如:attr_reader,puts)和屬性存取辦法。一切其他的辦法挪用應用括號環繞參數。
class Person
attr_reader :name, :age
# omitted
end
temperance = Person.new('Temperance', 30)
temperance.name
puts temperance.age
x = Math.sin(y)
array.delete(e)
bowling.score.should == 0
疏忽隱式選項 hash 內部的花括號。
# bad
user.set({ name: 'John', age: 45, permissions: { read: true } })
# good
user.set(name: 'John', age: 45, permissions: { read: true })
外部 DSL 辦法的內部括號和年夜括號。
class Person < ActiveRecord::Base
# bad
validates(:name, { presence: true, length: { within: 1..10 } })
# good
validates :name, presence: true, length: { within: 1..10 }
end
辦法挪用不須要參數,那末疏忽圓括號。
# bad Kernel.exit!() 2.even?() fork() 'test'.upcase() # good Kernel.exit! 2.even? fork 'test'.upcase
在單行代碼塊的時刻情願應用 {...} 而不是 do...end。防止在多行代碼塊應用 {...} (多行鏈式平日變得異常丑惡)。平日應用 do...end 來做 流程掌握 和 辦法界說 (例如 在 Rakefiles 和某些 DSLs 中)。防止在鏈式挪用中應用 do...end。
names = ['Bozhidar', 'Steve', 'Sarah']
# bad
names.each do |name|
puts name
end
# good
names.each { |name| puts name }
# bad
names.select do |name|
name.start_with?('S')
end.map { |name| name.upcase }
# good
names.select { |name| name.start_with?('S') }.map { |name| name.upcase }
有人會爭辯多行鏈式看起來和應用 {...} 一樣任務,然則他們問問本身 - 如許的代碼真的有可讀性碼而且為何代碼塊中的內容不克不及被提取到俏麗的辦法中。
Consider using explicit block argument to avoid writing block
literal that just passes its arguments to another block. Beware of
the performance impact, though, as the block gets converted to a
Proc.
斟酌應用明白的塊參數來防止寫入的塊字面量僅僅傳遞參數的給另外一個塊。當心機能的影響,即便,
塊被轉換成了 Proc。
require 'tempfile'
# bad
def with_tmp_dir
Dir.mktmpdir do |tmp_dir|
Dir.chdir(tmp_dir) { |dir| yield dir } # block just passes arguments
end
end
# good
def with_tmp_dir(&block)
Dir.mktmpdir do |tmp_dir|
Dir.chdir(tmp_dir, &block)
end
end
with_tmp_dir do |dir|
puts "dir is accessible as parameter and pwd is set: #{dir}"
end
防止在不須要流的掌握應用 return。
# bad def some_method(some_arr) return some_arr.size end # good def some_method(some_arr) some_arr.size end
防止在不須要的處所應用 self(它僅僅在挪用一些 self 做寫拜訪的時刻須要)(It is only required when calling a self write accessor.)
# bad
def ready?
if self.last_reviewed_at > self.last_updated_at
self.worker.update(self.content, self.options)
self.status = :in_progress
end
self.status == :verified
end
# good
def ready?
if last_reviewed_at > last_updated_at
worker.update(content, options)
self.status = :in_progress
end
status == :verified
end
作為一個必定的成果,防止將辦法(參數)放於部分變量暗影之下除非它們是相等的。
class Foo
attr_accessor :options
# ok
def initialize(options)
self.options = options
# both options and self.options are equivalent here
end
# bad
def do_something(options = {})
unless options[:when] == :later
output(self.options[:message])
end
end
# good
def do_something(params = {})
unless params[:when] == :later
output(options[:message])
end
end
end
不要在前提表達式裡應用 = (賦值)的前往值,除非前提表達式在圓括號內被賦值。
這是一個相當風行的 ruby 方言,有時被稱為 safe assignment in condition。
# bad (+ a warning) if v = array.grep(/foo/) do_something(v) ... end # good (MRI would still complain, but RuboCop won't) if (v = array.grep(/foo/)) do_something(v) ... end # good v = array.grep(/foo/) if v do_something(v) ... end
在任何可以的處所應用快捷的 self assignment 操作符。
# bad x = x + y x = x * y x = x**y x = x / y x = x || y x = x && y # good x += y x *= y x **= y x /= y x ||= y x &&= y
只要在變量沒有被初始化的時刻應用 ||= 來初始化變量。
# set name to Vozhidar, only if it's nil or false name ||= 'Bozhidar'
不要應用||=來初始化布爾變量。(想一想假如以後值為false的時刻會產生甚麼。)
# bad - would set enabled to true even if it was false enable ||= true # good enabled = true if enabled.nil?
應用 &&= 來預處置變量不肯定能否存在的變量。應用 &&= 僅僅在(變量)存在的時刻
才會轉變值,除去了應用 if 來檢討它的存在性。
# bad if something something = something.downcase end # bad something = something ? nil : something.downcase # ok something = something.downcase if something # good something = something && something.downcase # better something &&= something.downcase
防止全等(case equality)=== 操作符的應用。從稱號可知,這是 case 表達式的隱式應用而且在 case 語句外的場所應用會發生難以懂得的代碼。
# bad Array === something (1..100) === 7 /something/ === some_string # good something.is_a?(Array) (1..100).include?(7) some_string =~ /something/
防止應用 Perl 的指定變量作風(好比,$:,$; 等等。)。它們相當奧秘,不勉勵在單行代碼以外應用它們。
應用 English 庫供給的友愛別號。
# bad $:.unshift File.dirname(__FILE__) # good require 'English' $LOAD_PATH.unshift File.dirname(__FILE__) 歷來不要在辦法名和(參數)開括號之間應用空格。 # bad f (3+2) + 1 # good f(3 + 2) +1
假如辦法的第一個參數以開括號開端,平日應用括號把它們全體括起來。例如f((3 + 2) + 1)。
平日應用 -w 選項運轉 Ruby 說明器,在你忘卻下面所訴規矩,ruby 將會提醒你。
界說單行塊應用新的 lambda 語法。界說多行塊中應用 lambda 辦法。
# bad
l = lambda { |a, b| a + b }
l.call(1, 2)
# correct, but looks extremely awkward
l = ->(a, b) do
tmp = a * 7
tmp * b / 50
end
# good
l = ->(a, b) { a + b }
l.call(1, 2)
l = lambda do |a, b|
tmp = a * 7
tmp * b / 50
end
用 proc 而不是 Proc.new。
# bad
p = Proc.new { |n| puts n }
# good
p = proc { |n| puts n }
匿名辦法 和 塊 用 proc.call() 而不是 proc[] 或 proc.()。
# bad - looks similar to Enumeration access
l = ->(v) { puts v }
l[1]
# also bad - uncommon syntax
l = ->(v) { puts v }
l.(1)
# good
l = ->(v) { puts v }
l.call(1)
未應用的塊參數和部分變量應用 _。它也能夠接收經由過程 _ 來應用(即便它有少了些描寫性)。
這個通例由 Ruby 說明器和 RuboCop 如許的對象組織其將會克制它們的未應用參數正告。
# bad
result = hash.map { |k, v| v + 1 }
def something(x)
unused_var, used_var = something_else(x)
# ...
end
# good
result = hash.map { |_k, v| v + 1 }
def something(x)
_unused_var, used_var = something_else(x)
# ...
end
# good
result = hash.map { |_, v| v + 1 }
def something(x)
_, used_var = something_else(x)
# ...
end
應用 $stdout/$stderr/$stdin 而不是 STDOUT/STDERR/STDIN。STDOUT/STDERR/STDIN 是常量,固然在 Ruby 中是可以給常量從新賦值的(能夠是重定向到某個流),但說明器會正告假如你執意如許。
應用 warn 而不是 $stderr.puts。除加倍清楚簡練,假如你須要的話,
warn 還許可你克制(suppress)正告(經由過程 -W0 將正告級別設為 0)。
偏向應用 sprintf 和它的別號 format 而不是相當隱晦的 String#% 辦法.
# bad
'%d %d' % [20, 10]
# => '20 10'
# good
sprintf('%d %d', 20, 10)
# => '20 10'
# good
sprintf('%{first} %{second}', first: 20, second: 10)
# => '20 10'
format('%d %d', 20, 10)
# => '20 10'
# good
format('%{first} %{second}', first: 20, second: 10)
# => '20 10'
偏向應用 Array#join 而不是相當隱晦的應用字符串作參數的 Array#*。
# bad
%w(one two three) * ', '
# => 'one, two, three'
# good
%w(one two three).join(', ')
# => 'one, two, three'
當處置你願望像 Array 那樣看待的變量,然則你不肯定它是一個數組時,
應用 [*var] or Array() 而不是顯式的 Array 檢討。
# bad
paths = [paths] unless paths.is_a? Array
paths.each { |path| do_something(path) }
# good
[*paths].each { |path| do_something(path) }
# good (and a bit more readable)
Array(paths).each { |path| do_something(path) }
盡可能應用規模或 Comparable#between? 來調換龐雜的邏輯比擬。
# bad do_something if x >= 1000 && x <= 2000 # good do_something if (1000..2000).include?(x) # good do_something if x.between?(1000, 2000)
盡可能用謂詞辦法而不是應用 ==。比擬數字除外。
# bad if x % 2 == 0 end if x % 2 == 1 end if x == nil end # good if x.even? end if x.odd? end if x.nil? end if x.zero? end if x == 0 end
防止應用 BEGIN 區塊。
應用 Kernel#at_exit 。永久不要用 END 區塊。
# bad
END { puts 'Goodbye!' }
# good
at_exit { puts 'Goodbye!' }
防止應用 flip-flops 。
防止應用嵌套的前提來掌握流程。
當你能夠斷言不正當的數據,應用一個進攻語句。一個進攻語句是一個在函數頂部的前提聲明,如許假如數據不正當就可以盡快的跳出函數。
# bad
def compute_thing(thing)
if thing[:foo]
update_with_bar(thing)
if thing[:foo][:bar]
partial_compute(thing)
else
re_compute(thing)
end
end
end
# good
def compute_thing(thing)
return unless thing[:foo]
update_with_bar(thing[:foo])
return re_compute(thing) unless thing[:foo][:bar]
partial_compute(thing)
end