Sascha Manns
Sascha Manns

Reputation: 2510

Shrink down code complexity of a method

Actually i'm using this method, to replace a given content of a file:

def self.fix_authorgroup_work(title, name, email_business, company_name, company_division)
    agroup = "#{title}/de-DE/Author_Group.xml"
    namechomp = name.chomp
    # @note Split the variable to the array title[*]
    name = namechomp.split(' ')
    firstname = name[0]
    surname = name[1]
    # @note Author Group: Change the default stuff to the present user
    puts 'Replace the default content with the new content from the user (Authors_Group)'.color(:yellow)
    text = File.read(agroup)
    vorname = text.gsub('Enter your first name here.', "#{firstname}")
    puts vorname
    File.open(agroup, 'w') { |file|
      file.puts vorname
    }
    text = File.read(agroup)
    nachname = text.gsub('Enter your surname here.', "#{surname}")
    puts nachname
    File.open(agroup, 'w') { |file|
      file.puts nachname
    }
    text = File.read(agroup)
    email = text.gsub('Enter your email address here.', "#{email_business}")
    puts email
    File.open(agroup, 'w') { |file|
      file.puts email
    }
    text = File.read(agroup)
    member = text.gsub('Initial creation by publican', 'Initial creation')
    puts member
    File.open(agroup, 'w') { |file|
      file.puts member
    }
    text = File.read(agroup)

    org = text.gsub('Enter your organisation\'s name here.', "#{company_name}")
    puts org
    File.open(agroup, 'w') { |file|
      file.puts org
    }
    text = File.read(agroup)
    div = text.gsub('Enter your organisational division here.', "#{company_division}")
    puts div
    File.open(agroup, 'w') { |file|
      file.puts div
    }

So it looks a little bit complex. Maybe anyone can tell me, how to shrink down the code complexity? Sadly i havent done such thing earlier.

Upvotes: 0

Views: 54

Answers (3)

Andy Waite
Andy Waite

Reputation: 11076

A good starting point would be to restructure this into a Composed Method.

Upvotes: 0

user12341234
user12341234

Reputation: 7193

Daniel Slater's answer is a great one.

In addition you can also clean up your namechomp business from:

namechomp = name.chomp
name = namechomp.split(' ')
firstname = name[0]
surname = name[1]

to:

firstname, surname = name.chomp.split(' ')

Upvotes: 2

Daniel Slater
Daniel Slater

Reputation: 4143

Could be solved with a good refactor like so:

def self.add_result(nice_description, value_name, agroup)
    text = File.read(agroup)
    new_value = text.gsub(nice_description, value_name)
    puts new_value
    File.open(agroup, 'w') { |file|
      file.puts new_value
    }
def self.fix_authorgroup_work(title, name, email_business, company_name, company_division)
    agroup = "#{title}/de-DE/Author_Group.xml"
    namechomp = name.chomp
    # @note Split the variable to the array title[*]
    name = namechomp.split(' ')
    firstname = name[0]
    surname = name[1]
    # @note Author Group: Change the default stuff to the present user
    puts 'Replace the default content with the new content from the user (Authors_Group)'.color(:yellow)
    add_result('Enter your first name here.', "#{firstname}", agroup)
    add_result('Enter your surname here.', "#{surname}", agroup)
    add_result('Enter your email address here.', "#{email_business}", agroup)
    add_result('Initial creation by publican', "Initial creation", agroup)
    add_result('Enter your organisation\'s name here.', "#{company_name}", agroup)
    add_result('Enter your organisational division here.', "#{company_division}", agroup)

Upvotes: 2

Related Questions