Alina
Alina

Reputation: 2261

Naming of the wrapper functions and code structure

The project is to perform the same analysis on a collection of different files (in ruby). The main.rb is called with several arguments (path to folder with files and some more). So, the main.rb contains the parser and collects all files in the given folder and calls the function find_spikes for the collection of files. One report is then generated for all analyzed files.

So, main.rb:

options = { }
optparse = OptionParser.new do |opts|
  opts.banner = "Usage: example.rb [options]"
  opts.on("-f", "--folder FOLDER", "Path to folder", String) { |v| options[:folder] = v }
end
optparse.parse!

files = Dir.entries(options[:folder]).select { |entry| !(entry.start_with?('.') || entry.start_with?('..')) }
files.map! { |entry| File.join(options[:folder], entry) }

results = find_spikes(files)
generate_report(results)

run_steps.rb contains wrappers for the real functions.

def find_spikes(file_names)
    files_alarm = {}
    file_names.each do |file|
      input = File.read(file)
      alarm = find_spike(input, files_alarm)
      files_alarm[file] = alarm unless alarm.nil?
    end
    return files_alarm
end

The real analysis is performed in the find_spike function, whereas find_spikes applies find_spike on each file iteratively.

Regarding the short project I have several questions:

  1. Is there any good practice for the code organization in the main.rb? What should in fact that main file include?

  2. What would be the best names for find_spikes and find_spike to be able to distinguish between the wrapper function and the real analyzing function? Because for testing I have to pass not the file path to the function but the context of the file.

Upvotes: 1

Views: 367

Answers (1)

Raj
Raj

Reputation: 22926

Is there any good practice for the code organization in the main.rb? What should in fact that main file include?

You could have a separate "class" instead of a script to separate the input logic and your actual domain logic. So the class can be tested separately.

What would be the best names for find_spikes and find_spike to be able to distinguish between the wrapper function and the real analyzing function?

find_spikes_from_file_paths and find_spikes_from_file_content

Upvotes: 1

Related Questions