这段时间记录的一些杂七杂八的笔记

2013年5月28日 17:06

ruby代码

1.upto 10 do |i|
    puts i if i==3..i==5
end

sql代码

--方法1 慢。。。。。
select postby,(select count(*) from articles where status=1 and postby=ar.postby) as art_num,(select count(*) from articles where status=1 and isgood=1 and postby=ar.postby) as goodart_num,title from articles as ar group by postby order by goodart_num desc,art_num desc limit 10

--方法2 快。。。。。
select postby,sum(case when status=1 then 1 else 0 end) as art_num,sum(case when status=1 and isgood=1 then 1 else 0 end) as goodart_num,title from articles group by postby order by goodart_num desc,art_num desc limit 10;

JS代码

//You need an anonymous function to wrap around your function to avoid conflict
(function($){

    //Attach this new method to jQuery
    $.fn.extend({

        //This is where you write your plugin's name
        pluginname: function() {

            //options
            var defaults = {
                option1: "default_value"
            }

            var options = $.extend(defaults, options);

            //a public method
            this.methodName: function () {
                //call this method via $.pluginname().methodName();
            }

            //Iterate over the current set of matched elements
            return this.each(function() {

                var o = options;

                //code to be inserted here

            });
        }
    });

//pass jQuery to the function,
//So that we will able to use any valid Javascript variable name
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )      
})(jQuery);
$(function(){
  // define the application
  var Notekeeper = {};
  (function(app){
    // variable definitions go here
    app.init = function(){
    // stuff in here runs first
    }
   app.init();
  })(Notekeeper);
});

重构

CodeSmell:
1.Long Method
replace method with method object
extract method
2.divergent change
extract Class
3.inappropriate intimacy
4.case statement
replace conditional with polymorphism
move creation knowledge to factory
5.data clumps
6.uncommunicative name
7.feature envy
extracted objects tend to attract behavior
8.same name,different meaning
9.primitive obsession
we are using sample data types to represent complex ideas
10.nil checks
nil communicates that an unknown command has been requested
introduce Null Object