Kashiftufail
Kashiftufail

Reputation: 10885

How i can dry this code rather than put many conditions

i'm using ROR and in my controller function i recived params and base of these params i need to perform action according condition. But i see these are about 18 conditions.

How can i dry this code.

if params[:topic] == "Topic (title)" and params[:sort] == "Date (ASC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Date (DESC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Topic (ASC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Topic (DESC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Author (ASC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Author (DESC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Date (ASC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Date (DESC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Topic (ASC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Topic (DESC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Author (ASC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Author (DESC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Date (ASC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Date (DESC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Topic (ASC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Topic (DESC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Author (ASC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Author (DESC)"
  # custom code
end

Many many thanks

Upvotes: 0

Views: 81

Answers (3)

Joshua Cheek
Joshua Cheek

Reputation: 31786

If the code is legitimately different, consider a case statement:

case [params[:topic], params[:sort]]
when ["Topic (title)", "Date (ASC)"]
  # custom code
when ["Topic (title)", "Date (DESC)"]
  # custom code
when ["Topic (title)", "Topic (ASC)"]
  # custom code
when ["Topic (title)", "Topic (DESC)"]
  # custom code
when ["Topic (title)", "Author (ASC)"]
  # custom code
when ["Topic (title)", "Author (DESC)"]
  # custom code
when ["Post (body)", "Date (ASC)"]
  # custom code
when ["Post (body)", "Date (DESC)"]
  # custom code
when ["Post (body)", "Topic (ASC)"]
  # custom code
when ["Post (body)", "Topic (DESC)"]
  # custom code
when ["Post (body)", "Author (ASC)"]
  # custom code
when ["Post (body)", "Author (DESC)"]
  # custom code
when ["Author", "Date (ASC)"]
  # custom code
when ["Author", "Date (DESC)"]
  # custom code
when ["Author", "Topic (ASC)"]
  # custom code
when ["Author", "Topic (DESC)"]
  # custom code
when ["Author", "Author (ASC)"]
  # custom code
when ["Author", "Author (DESC)"]
  # custom code
end

If there is repetition in the code, or you wind up using this case statement in multiple locations, then there are probably better ways to do this.

Upvotes: 1

ilan berci
ilan berci

Reputation: 3881



  topics = ['Topic (title)', 'Post (body)', 'Author' ]
  sorts = ['Date (ASC)', 'Date (DESC)', 'Topic (ASC)', 'Topic (DESC)']
  code_map = {
    [topics[0], sorts[0]] => ->() {
      # custom code
    },
    [topics[0], sorts[1]] => ->() {
      # custom code
    }
  }

  # usage
  code_map[[params[:topic], params[:sort]]()

Although that removes the duplication, I don't believe you want to go down this path as I believe the 'custom code' will have a lot of duplication as well.

Show us your 'custom code' and how it varies from one case to the next, and we can dry that for you instead of at the dispatch level

Upvotes: 0

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 71009

Use the case... when syntax. Will make things a bit clearer. Also move the first part of all ifs out of the cases - they seem to be common for quite a few cases.

Upvotes: 1

Related Questions