Skip to content

Instantly share code, notes, and snippets.

View yuanchuan's full-sized avatar

Yuan Chuan yuanchuan

View GitHub Profile
@yuanchuan
yuanchuan / gist:998296
Created May 30, 2011 00:47 — forked from anonymous/gist:997052
在搜狐空间里自动获取最新动态
// pull.every(10);
// pull.stop();
var pull = (function(container){
var target, event, timer;
if (container) {
target = container.getElementsByTagName('a')[0];
}
@yuanchuan
yuanchuan / gist:1037937
Created June 21, 2011 14:11
代码优化尝试

今天出于好奇,读了下一款 webgame 的源代码。从整体上看,代码很简单,但也很复杂。简单是因为程序的设计思路很容易懂,除了很多全局变量以外,全是一个接一个的函数;复杂是因为每当从一个函数当中读到一个的变量名时,或是看到一个函数调用的话,总要到其它地方去找,然后再回来,加上一个函数通常都很长,冗余很多,理解起来就变得复杂了。假如让我接手这些代码的话,维护起来一定相当费劲,耦合的太紧,不容易理清楚。要是程序继续像这样下去,一不小心,就会变得跟 “意大利面条“ 一样。

这款游戏还处在开发阶段,但是想要向更大规模发展的话,以现在的程序结构来看不太容易,因为控制不了复杂性。除此之外,某些地方的性能方面也有待改进,毕竟游戏不像其它的应用程序,它的要求比较高。下面是我对一些代码的优化尝试。

避免 if else 过多

太多的 if else 看上去不够简洁优美,总是提醒你还可以写得更好。 在constant.js当中有这么两个函数:
@yuanchuan
yuanchuan / gist:1091528
Created July 19, 2011 07:01
更新并显示日志
#不想每次用TortoiseSVN 时都用鼠标右击更新
import os
import sys
def run_command(commands):
op_os = lambda x: os.system(x)
op_ms = lambda x: sys.stdout.write(x+'\n')
for cmd in commands:
var message = (function() {
var _buf = ['hello', 'hi']
, _len = _buf.length
, M = Math;
return {
get: function(){
return _buf[M.floor(M.random() * _len)] || '';
}
, add: function() {
@yuanchuan
yuanchuan / gist:1231163
Created September 21, 2011 03:29
login
#!/usr/bin/env node
var md5 = require('./md5.js').md5
, exec = require('child_process').exec
, username = process.argv[2] || ''
, password = process.argv[3] || ''
, getUrl = function(u, p) {
var href = '"http://xxx.xxx/xxx.php';
return href + '?uid=1&username=' + u + '&passwd=' + p + '&time=100192&flag=1"';
}
@yuanchuan
yuanchuan / server.js
Created October 20, 2011 06:51
一个两小时的编程实践
#!/usr/bin/env node
var http = require('http')
, fs = require('fs')
, url = require('url')
, exec = require('child_process').exec
, qs = require('querystring')
, path = require('path');
@yuanchuan
yuanchuan / gist:1330150
Created November 1, 2011 08:27
simple draggable using jquery
/**
* usage:
* $('div').draggable();
* $('*').draggable();
*/
$.fn.draggable = function() {
var $document = $(document)
, mouse = { update: function(e) {this.x = e.pageX; this.y = e.pageY;} };
@yuanchuan
yuanchuan / build.py
Created December 10, 2011 06:09
document generator
#!/usr/bin/env python
# Get markdown module at: http://pypi.python.org/pypi/Markdown
from markdown import markdownFromFile
from string import Template
from datetime import datetime
import os
doc_path = 'document'
@yuanchuan
yuanchuan / LICENSE.txt
Created December 22, 2011 01:36 — forked from 140bytes/LICENSE.txt
140byt.es -- Click ↑↑ fork ↑↑ to play!
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@yuanchuan
yuanchuan / index.html
Last active May 17, 2017 05:11
随机数
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<style>
body {
background: #333;
text-align: center;
}
#num {