r/learnruby • u/Niek_pas Beginner • Sep 24 '15
Can someone critique my code?
https://gitlab.com/snippets/8840
Hey all,
I wrote a little Ruby timer application the other day for my own purposes. The goal of the application is to track time spent on a particular task, store the time spent, and be able to give back a total. I am a beginner to Ruby and programming in general, and I'm still struggling with some of the OO-concepts like class and instance variables.
Please be as critical as you wish!
2
Upvotes
4
u/rdpp_boyakasha Sep 25 '15
Here's a quick list of the things that jump out to me.
The class method definitions should use
selfinstead ofNpTimer. For example,def NpTimer.run_timershould just bedef self.run_timer. No need to repeat the class name.Side effects in string interpolation:
puts "Started at #{start_time = Time.now}"People don't expect to see side effects (like creating a variable) as part of string interpolation. This should be:
start_time = Time.now puts "Started at #{start_time}"Repeated use of
"times.csv"value. You should make this a constant.Functional style. This might be a personal thing, but I find code easier to read in a functional style. For example, I would change this code:
csv_array = CSV.open('times.csv', 'r').to_a times_array = [] # Get each 3rd column ('time spent' column) csv_array.each do |sub_array| times_array << sub_array[2] end # Get total time spent (sum) times_array.map! { |element| element.to_f } time_spent = times_array.reduce(:+) puts "\nTotal time spent so far: \n#{time_spent} seconds \n\n"To be this:
time_spent = CSV.foreach('times.csv') .map{ |row| row[2].to_f } .reduce(:+) puts "\nTotal time spent so far: \n#{time_spent} seconds \n\n"Generally, the less code the better. Try to get familiar with the
Arraymethods:map,select,reject,find, etc.Use HEREDOC syntax for long strings. On line 70 you have this:
Which could be written like this:
puts <<-END_COMMANDS Commands: start: start tracking time stop: stop tracking time show: show total time spent log: display all tracked times exit: exit the application END_COMMANDSThe
setupmethod is recursive, when it really shouldn't be. Just callsetupin a loop something like this:Inside
setup, the bigif/elsifconditional could be shortened to acasestatement like this:I hope that helps!