1
\$\begingroup\$

I've been asked to improve the modular decomposition of the main by moving as many lines of code out of main as possible, but i have no idea how i should do this

# writes the number of lines then each line as a string.
def write_data_to_file(aFile)
   aFile.puts('5')
end

# reads in each line.
def read_data_from_file(aFile)
  puts aFile.gets
end

# writes data to a file then reads it in and prints each line as it reads
def main
  aFile = File.new("mydata.txt", "w") # open for writing
  if aFile  # if nil this test will be false
    write_data_to_file(aFile)
    aFile.close
  else
    puts "Unable to open file to write!"
  end

  aFile = File.new("mydata.txt", "r") # open for reading
  if aFile  # if nil this test will be false
    read_data_from_file(aFile)
    aFile.close
  else
    puts "Unable to open file to read!"
  end
end
```
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Review. Try putting a newline after the closing ```. The title should reflect what the code is to achieve, see How do I ask a Good Question? \$\endgroup\$ – greybeard May 15 '19 at 6:22
  • \$\begingroup\$ hmm, I would move both read and write blocks in main to their own methods. \$\endgroup\$ – Christian Jun 9 '19 at 0:29

Browse other questions tagged or ask your own question.