Files
2012-02-21 01:15:00 -05:00

1 line
7.4 KiB
JSON

[{"user_id": 11223, "stars": [], "topic_id": 16129, "date_created": 1301447567.690608, "message": "do_something if some_condition?", "group_id": 109, "id": 474734}, {"user_id": 2190, "stars": [], "topic_id": 16129, "date_created": 1301447496.6705339, "message": "Divide and conquer", "group_id": 109, "id": 474731}, {"user_id": 4221, "stars": [], "topic_id": 16129, "date_created": 1301447092.8777101, "message": "I have a command-line app that I'm parsing a bunch of options out, so I have what looks like alot of if statements in a row\n\nif something\n # do something\nend\nif an_additional_thing\n # do this other thing, too\nend\nif some_other_feature", "group_id": 109, "id": 474669}, {"user_id": 4221, "stars": [], "topic_id": 16129, "date_created": 1301447121.7971101, "message": "What can I do that will make all of these \n\nif X\n # do X_thing\nend", "group_id": 109, "id": 474672}, {"user_id": 4221, "stars": [], "topic_id": 16129, "date_created": 1301447125.10132, "message": "simpler?", "group_id": 109, "id": 474673}, {"user_id": 11223, "stars": [], "topic_id": 16129, "date_created": 1301448117.415612, "message": "lots of options :)", "group_id": 109, "id": 474783}, {"user_id": 11223, "stars": [], "topic_id": 16129, "date_created": 1301447729.330266, "message": "(some_condition) && do_something", "group_id": 109, "id": 474750}, {"user_id": 11223, "stars": [], "topic_id": 16129, "date_created": 1301448051.3834791, "message": "def parseopt(condition, &b); yield if condition; end", "group_id": 109, "id": 474773}, {"user_id": 11223, "stars": [], "topic_id": 16129, "date_created": 1301448625.1658089, "message": "(some_condition) ? do_something : nil", "group_id": 109, "id": 474825}, {"user_id": 11223, "stars": [], "topic_id": 16129, "date_created": 1301448633.928807, "message": "had to throw that in for completeness", "group_id": 109, "id": 474830}, {"user_id": 11223, "stars": [], "topic_id": 16129, "date_created": 1301447892.0057321, "message": "if (some_condition) : do_something end", "group_id": 109, "id": 474764}, {"user_id": 11223, "stars": [], "topic_id": 16129, "date_created": 1301448059.2617919, "message": "parseopt(1<2) {do_something}", "group_id": 109, "id": 474775}, {"user_id": 24357, "stars": [], "topic_id": 16129, "date_created": 1301448960.185216, "message": "Also, if you're looking a a lot of nested logic and processing and it looks like a general mess, you probably should extract some of that logic/processing into another class, file, or gem.", "group_id": 109, "id": 474873}, {"user_id": 22997, "stars": [{"date_created": 1302001494.3008831, "user_id": 24069}], "topic_id": 16129, "date_created": 1301473544.795023, "message": "You might want to take a look at the thor gem to handle parameter parsing for you. It is a dependency of Rails so you might already have it installed.", "group_id": 109, "id": 479644}, {"user_id": 1767, "stars": [{"date_created": 1301500744.229033, "user_id": 9262}], "topic_id": 16129, "date_created": 1301477753.721427, "message": "Another way of doing something like this:\n\nactions = {\n :something => Proc.new { puts 'Something' },\n :other => Proc.new { puts 'Something else' }\n}\nactions[:other].call\n\nYou could easily extend this to use command-line arguments.", "group_id": 109, "id": 480333}, {"user_id": 4221, "stars": [], "topic_id": 16129, "date_created": 1301483622.9976959, "message": "@koraktor I forgot about that pattern. That might work. :)", "group_id": 109, "id": 480876}, {"user_id": 4221, "stars": [], "topic_id": 16129, "date_created": 1301483584.4514279, "message": "@peterheilberg I haven't looked at Thor. I will give that a go.", "group_id": 109, "id": 480873}, {"user_id": 25530, "stars": [], "topic_id": 16129, "date_created": 1301485832.5295019, "message": "For your specific case, go with thor", "group_id": 109, "id": 480992}, {"user_id": 25530, "stars": [], "topic_id": 16129, "date_created": 1301485873.2021511, "message": "As a general rule, I would either use a case condition or an hash that maps to procs as @koraktor suggested", "group_id": 109, "id": 480995}, {"user_id": 15236, "stars": [], "topic_id": 16129, "date_created": 1301668675.0821581, "message": "When I face so many \"if\" usually is because I have something wrong with my classes design. Objects should be \"lazy\", do small amount of work, and delegate other responsibilities to collaborators. Besides, maintaining a code full of \"if\" and hundred of lines per method could be a pain... What about to refactor your code having in mind the Command or the Factory patterns?", "group_id": 109, "id": 501932}, {"user_id": 22043, "stars": [], "topic_id": 16129, "date_created": 1301702348.6664901, "message": "then after that line, you can assume @account is not assigned. It can prevent future need for if statements.", "group_id": 109, "id": 506846}, {"user_id": 22043, "stars": [], "topic_id": 16129, "date_created": 1301702322.075753, "message": "return unless @account.nil? or something like that", "group_id": 109, "id": 506845}, {"user_id": 22043, "stars": [{"date_created": 1301710223.419035, "user_id": 5778}, {"date_created": 1301741199.475554, "user_id": 19808}], "topic_id": 16129, "date_created": 1301702309.19295, "message": "I usually prefer a short circuited approach.", "group_id": 109, "id": 506844}, {"user_id": 5778, "stars": [], "topic_id": 16129, "date_created": 1301710264.838819, "message": "I also prefer short circuiting when possible", "group_id": 109, "id": 507261}, {"user_id": 4221, "stars": [], "topic_id": 16129, "date_created": 1301788772.730021, "message": "@cyaconi Yeah, in my case, there haven't been classes to design with. It's really option parsing more like a command-line app that I want to build actions upon. Going with @peterhellberg 's suggestion to try thor is probably a better option than trying to write it myself. Great suggestions all around, though.", "group_id": 109, "id": 510988}, {"user_id": 4221, "stars": [], "topic_id": 16129, "date_created": 1301788980.0892961, "message": "What brought about the question was looking at how things like OptionParser in Ruby core worked and forced you down the route of having to handle a bunch of different options. If I had to nest options, things started looking ugly and I was getting frustrated with it's burgeoning lack of \"Ruby-ness\" to it.", "group_id": 109, "id": 511014}, {"user_id": 14343, "stars": [], "topic_id": 16129, "date_created": 1301819348.464529, "message": "code smell?", "group_id": 109, "id": 513511}, {"user_id": 4221, "stars": [], "topic_id": 16129, "date_created": 1301832576.8859401, "message": "It's a code smell if I used alot of if statements, hence the point of this topic? :)", "group_id": 109, "id": 514255}, {"user_id": 2576, "stars": [{"date_created": 1301934550.9581599, "user_id": 5778}], "topic_id": 16129, "date_created": 1301933658.7810709, "message": "Check out: http://www.amazon.com/Refactoring-Improving-Design-Existing-Code/dp/0201485672 If you have a large number of if statements you like need to refactor to class composition. In ruby this can also be achieved with mix-ins to provide/override various functions. Dispatching from a hash like @koraktor suggests is also another alternative.", "group_id": 109, "id": 524374}, {"user_id": 4221, "stars": [], "topic_id": 16129, "date_created": 1301950295.0658071, "message": "Yeah, I recently bought Eloquent Ruby by Russ Olson and I have been really loving the details he gives on module composition and inheritance. Modules seem the way to go", "group_id": 109, "id": 528960}]