switch statement - How do I DRY up this Ruby code? -


here code:

when 'jty'  if j.type != "0"   @color = allcolors.find { |c| c['type'] == j.type.to_s }    clr << @color.color if @color  else    clr << @@default_map_marker_color  end              when 'jcat'   if j.priority != "0"     @color = allcolors.find { |c| c['type'] == j.priority.to_s }     clr << @color.color if @color   else     clr << @@default_map_marker_color   end 

i have 6 more when statements in case, , repeat part:

clr << @color.color if @color 

over , over. hate it.

here things need know:

clr empty array initialize outside case statement, , case statement inside for loop that's going through lot of objects. it's looking color associated particular thing, , nosql-hack-ish way relational data.

if like:

def push_color  clr << @color.color if @color end 

clr , @color aren't being passed around without params method. there's dislike passing params methods - it's dependent. i'm stretching understand better , write code doesn't have these implicit dependencies. have pass info 1 method another, creates dependency breaks if param i'm passing changes or disappears, , that's i'm trying avoid.

i don't understand how can clr filled whatever returns case statement. ideally like:

clr = pref.map |p|  p(&:do_the_switch_statement_here) end 

and p entire case , return something.

that's head is, can't quite imagine how.

your current code has duplications, instance:

j.type versus j.priority both times check value not string '0'.

later on again use same method. seems superfluous.

on top of use class variable when there no need it. recommend not use class variables.

it not code start wanting optimize @ all.

your data structures needlessly complicated.

when simplify data structures, code automagically become much simpler.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -