sheng00 的所有文章

成为优秀程序员的10点建议

这篇文章要介绍的,是我作为专业程序员这些年来学到的能真正提高我的代码质量和整体工作效率的10件事情。
1. 永远不要复制代码
不惜任何代价避免重复的代码。如果一个常用的代码片段出现在了程序中的几个不同地方,重构它,把它放到一个自己的函数里。重复的代码会导致你的同事在读你的代码时产生困惑。而重复的代码如果在一个地方修改,在另外一个地方忘记修改,就会产生到处是bug,它还会使你的代码体积变得臃肿。现代的编程语言提供了很好的方法来解决这些问题,例如,下面这个问题在以前很难解决,而如今使用lambdas却很好实现:
1
现在我们重构含有部分相同代码的函数,用delegate模式重写它们:
2
2. 留意你开始分心的时候
当你发现自己在浏览facebook或微博、而不是在解决问题,这通常是一种你需要短暂休息的信号。离开办公桌,去喝一杯咖啡,或去跟同事聊5分钟。尽管这样做看起来有点反直觉,但长久去看,它会提高你的工作效率。
3. 不要匆忙赶任务而放弃原则
当带着压力去解决一个问题或修改一个bug,你很容易失去自制,发现自己匆匆忙忙,甚至完全忘了一直坚持的重要的测试过程。这通常会导致更多的问题,会让你在老板或同事眼里显得很不专业。
4. 测试你完成的代码
你知道你的代码能做什么,而且试了一下,它确实好用,但你实际上需要充分的验证它。分析所有可能的边界情况,测试在所有可能的条件下它都能如期的工作。如果有参数,传递一些预期范围外的值。传递一个null值。如果可能,让同事看看你的代码,问他们能否弄坏它。单元测试是到达这种目的的常规方法。
5. 代码审查
提交你的代码之前,找个同事一起坐下来,向他解释你做了哪些修改。通常,这样做的过程中你就能发现代码中的错误,而不需要同事说一句话。这比自己审查自己的代码要有效的多得多。
6. 精简代码
如果你发现写了大量的代码来解决一个简单的问题,你很可能做错了。下面的boolean用法是一个很好的例子:
3
这时你应该写成这样:
4
代码越少越好。这会使bug更少,重构可能性更小,出错的几率更小。要适度。可读性同等重要,你可不能这样做而使代码丧失可读性。
7. 为优雅的代码而努力
优雅的代码非常的易读,只用手边很少的代码、让机器做很少的运算就能解决问题。在各种环境中都做到代码优雅是很难的,但经过一段时间的编程,你会对优雅的代码是个什么样子有个初步的感觉。优雅的代码不会通过重构来获得。当你看到优雅的代码是会很高兴。你会为它自豪。例如,下面就是一个我认为是优雅的方式来计算多边形面积的方法:
5
8. 编写不言自明的代码
勿庸置疑,注释是编程中很重要的一部分,但能够不言自明的代码跟胜一筹,因为它能让你在看代码时就能理解它。函数名变量名要慎重选择,好的变量/方法名字放到语言语义环境中时,不懂编程的人都能看懂。例如:
6
9. 不要使用纯数字
直接把数字嵌入代码中是一种恶习,因为无法说明它们是代表什么的。当有重复时更糟糕——相同的数字在代码的多个地方出现。如果只修改了一个,而忘记了其它的。这就导致bug。一定要用一个命名常量来代表你要表达的数字,即使它在代码里只出现一次。
10. 不要做手工劳动
当做一系列动作时,人类总是喜欢犯错误。如果你在做部署工作,并且不是一步能完成的,那你就是在做错事。尽量的让工作能自动化的完成,减少人为错误。当做工作量很大的任务时,这尤其重要。
11. 避免过早优化
当你要去优化一个已经好用的功能代码时,你很有可能会改坏它。优化只能发生在有性能分析报告指示需要优化的时候,通常是在一个项目开发的最后阶段。性能分析之前的优化活动纯属浪费时间,并且会导致bug出现。
好吧,我说是10个,但你却得到了额外赠送的一个!
这些就是我要说的,我希望它们能帮助你改进编程开发过程。
下次再见!祝快乐!
Cheers, Paul.
英文链接:www.wildbunny.co.uk/blog/2012/11/01/10-steps-to-becoming-a-better-programmer/
来源:外刊IT评论网

网络购票秘技

12306网购火车票的同学,如果出现页面加载缓慢,可以在
C:WINDOWSsystem32driversetchosts
中添加一条记录:
“122.228.243.22 dynamic.12306.cn”,
或者”61.183.42.94 dynamic.12306.cn”,
这是提供给海外用户访问的CDN节点,几乎没什么人。

编译源代码修改jwplayer Build jwplayer source code

首先checkout jwplayer的源代码
地址:http://developer.longtailvideo.com/svn/trunk/fl5
编译jwplayer需要以下程序
* Flex SDK 4.1: http://sourceforge.net/adobe/flexsdk/wiki/Downloads/
* Ant 1.7.0: http://ant.apache.org/bindownload.cgi
* FlexUnit 4: http://opensource.adobe.com/wiki/display/flexunit/FlexUnit (for testing the player)
环境配置好之后需要修改配置文件
flexsdk = C:/Program Files/Adobe/Adobe Flash Builder 4.5/sdks/4.5.1
windows环境下需要更改execextension
execextension = .exe
根据flash player版本更改flexsdk.target
C:Program FilesAdobeAdobe Flash Builder 4.5sdks4.5.1frameworkslibsplayer10.2
这个是本机的player版本,所以
flexsdk.target = 10.2.0
配置文件修改完成之后就可以用ant来build了
控制台切换到build文件夹运行:ant -buildfile buildbuild.xml
输出:
Buildfile: E:Developmentjwplayersourcebuildbuild.xml
check-properties:
clean-release:
build-release-player:
release-swf:
build-swf:
[exec] ????????????????C:Program FilesAdobeAdobe Flash Builder 4.5sdks4.5.1frameworksflex-config.xml??
[exec] E:Developmentjwplayersourceplayer.swf??112357 ????
BUILD SUCCESSFUL
Total time: 4 seconds
如果你的电脑上装的是64位的java sdk的话,可能会报错
现在开始修改源代码
1、去掉左下角的jwplayer水印
找到com.longtailvideo.jwplayer.view里的View.as
找到protected function setupComponents()里的setupComponent(_logo, n++);注释掉即可
2、修改右键about的菜单项
找到com.longtailvideo.jwplayer.view里的RightclickMenu.as
修改function setAboutText()函数
about = new ContextMenuItem(‘About Sheng00 …’);
修改function aboutHandler(evt:ContextMenuEvent)函数
navigateToURL(new URLRequest(‘http://www.sheng00.com’), ‘_blank’);
 
然后ant -buildfile buildbuild.xml
生成的就是修改过的player了
以下是效果:
 

JW Player goes here




 

How to read VOD files from Amazon S3 on Standalone Wowza Server

From http://www.wmconsulting.info/read-vod-files-amazon-s3-standalone-wowza-server/
 
I’ll explain how to is possible read the files from Amazon S3 Storage but without using a EC2 instance.
For example your “localhost” or your Standalone Wowza server running on your own datacenter.
I did this for working on my local development environment, I’ll describe the steps for a Linux environment.
1- Download from this URL the 2 Jars, and copy this in /lib directories.
[download id=”5″]
[download id=”6″]
2- Create the MediaCache folder:

mkdir /mnt/mediacache

3- Insert into the conf/Server.xml this lines:

<ServerListeners>
<ServerListener>
<BaseClass>com.wowza.wms.plugin.amazonaws.ec2.mediacache.MediaCacheServerListenerAmazonEC2</BaseClass>
</ServerListener>
</ServerListeners>

4- Insert into the conf/vods3/Application.xml (create this first) this lines:

<MediaReader>
<Properties>
<Property>
<Name>randomAccessReaderClass</Name>
<Value>com.wowza.wms.plugin.amazonaws.ec2.mediacache.MediaCacheRandomAccessReaderAmazonEC2</Value>
</Property>
<Property>
<Name>bufferSeekIO</Name>
<Value>true</Value>
<Type>Boolean</Type>
</Property>
</Properties>
</MediaReader>

And in the end of the file, add this properties (I don’t know is needed this for read Public content):

<Properties>
<!-- Set these two properties to do S3 authentication -->
<Property>
<Name>awsAccessKeyId</Name>
<Value>KEY</Value>
</Property>
<Property>
<Name>awsSecretAccessKey</Name>
<Value>KEY</Value>
</Property>
</Properties>

5- Restart the Wowza Server and try reading from S3 Bucket.
The URL can be like this:
rtmp://localhost/vods3/_definst_/mp4:amazons3/wmconsulting.content/Extremists.m4v

Umbraco:get datatype values

Code:

@{
    //var prevalues = umbraco.library.GetPreValues(1235);
    var prevalues = umbraco.cms.businesslogic.datatype.PreValues.GetPreValues(1235);
}

@prevalues.ToString()

@if (prevalues.Count > 0) { for (int i=0;i@prevalue.Id:@prevalue.Value

} } }

output:

System.Collections.SortedList

81:国内

82:国外

83:资金

84:技术

85:人才

WordPress: modify the upload list of allowed file types

When you attempt to upload a file in WordPress that is not in the default list of acceptable file types, you will receive the following error: File type does not meet security guidelines. Try another. While there’s no admin-based tool for editing list of allowed file types, it’s not at all difficult to add your own or remove any existing.
Upload filetypes are checked by the function wp_check_filetype in wp-includes/functions.php. But we will add new file types to the file functions.php in our template due to upgrade WordPress.
Open your theme’s functions.php file and add this line somewhere between the <?php and ?>:
 

add_filter('upload_mimes', 'custom_upload_mimes');
function custom_upload_mimes ( $existing_mimes=array() ) {
// add your extension to the array
$existing_mimes['deb'] = 'application/x-deb';
// add as many as you like
// removing existing file types
unset( $existing_mimes['exe'] );
// add as many as you like
// and return the new full result
return $existing_mimes;
}

In this case I allowed upload .deb files and banned upload .exe files. List of MIME Types can be found here, or use google, if your file type is not in the list (e.g. deb or rpm file type).