Notes of “Effective Java” (Chapter 7-11)
Posted: February 28, 2012 Filed under: Technical | Tags: Java Leave a commentItem 37: Use marker interfaces to define types
Marker interface contains no method declarations, which indicates some property or features. The typical marker interface is Serialize interface.
Item 38: Check parameters for validity
Check and restrict the parameters of functions.
- Throw Exception
- Assert
Item 39: Make defensive copies when needed
In general, if a class has mutable components that it gets from or returns to its clients, the class must defensively copy these components.
Defensive copy usually implemented by:
- clone() method
- copy constructor
- static factory
This is not mandatory. If the class trusts the clients, then defensive copy could be replaced by documentation explanation to save the cost.
Interesting Blogs
Posted: February 16, 2012 Filed under: Uncategorized Leave a commenthttp://blog.codingnow.com/ 云风的博客
http://hi.baidu.com/caoz/home 百度曹政
http://coolshell.cn/ 酷壳 陈皓
Mathematics in Programming
Posted: February 1, 2012 Filed under: Technical Leave a comment1. 牛顿迭代法和TAYLOR 级数可以解决大部分方程求解问题,函数近似问题。
Example: sqrt()函数
public static double sqrt ( double c ) { if (c < 0) return Double.NaN; double err = 1e-15; double t = c; while (Math.abs(t - c/t) > err * t) t = (c/t + t) / 2.0; return t; }
//QUAKE3中用的计算参数x的平方根的倒数函数,该函数的相对误差约为0.177585%
float InvSqrt (float x) { float xhalf = 0.5f*x; int i = *(int*)&x; i = 0x5f3759df - (i >> 1); // 计算第一个近似根 x = *(float*)&i; x = x*(1.5f - xhalf*x*x); // 牛顿迭代法 return x; }
2. 随机数相关问题
//Random int value drawn from discrete distribution (i with probability a[i])
public static int discrete(double[] a) { // Entries in a[] must sum to 1. double r = StdRandom.random(); double sum = 0.0; for (int i = 0; i < a.length; i++) { sum = sum + a[i]; if (sum >= r) return i; } return -1; }
//randomly shuffle the elements in an array of double values
public static void shuffle(double[] a) { int N = a.length; for (int i = 0; i < N; i++) { // Exchange a[i] with random element in a[i..N-1] int r = i + StdRandom.uniform(N-i); double temp = a[i]; a[i] = a[r]; a[r] = temp; } }
Notes of “Effective Java” (Chapter 1-6)
Posted: January 30, 2012 Filed under: Technical | Tags: Design Pattern, Java Leave a commentItem 1: Consider static factory methods instead of constructors
advantage:
- Customized meaningful names better than constructors.
- Associated with class instead of creating new objects.
- Able to return subtypes.
For the 2nd advantage, static factory methods form the basis of Service provider Framework including service interface, provider interface, provider registration API and service access API.
Item 2: Consider a builder when faced with many constructor parameters
Compare with telescoping constructors and JavaBean Style.
Builder is set as a static inner class. Builder makes the object initialized immutable.
Websites with django
Posted: December 25, 2011 Filed under: Technical | Tags: Django, Python Leave a comment- http://www.djangoproject.com/
官方网站 - http://www.djangobook.com/en/2.0/
大名鼎鼎的djangobook - http://www.djangosnippets.org/
一个专门发布django代码片断的地方 - http://djangopluggables.com/
django 一个著称的优点就是 pluggable app - http://findjango.com/
一个专门搜索django相关东东的搜索引擎 - http://djangosearch.com/
另一个django相关的搜索引擎 - http://djangofriendly.com/
django网站主机查找 - http://pinaxproject.com/
基于Django的更高层的框架 - http://djangositeoftheweek.com/
django网站推荐
Some useful or interesting links
Posted: December 2, 2011 Filed under: Technical Leave a commentThe Archive of Interesting Code: http://www.keithschwarz.com/interesting/
Web application category: http://stackparts.com/
Books, Papers and Links – Computer Graphics: http://ploobs.com.br/?p=766
Design pattern: http://www.vincehuston.org/dp/
Nice Introduction to Unicode: http://www.joelonsoftware.com/articles/Unicode.html
How to Write Doc Comments for the Javadoc Tool: http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html
HOW BROWSERS WORK: BEHIND THE SCENES OF MODERN WEB BROWSERS: http://www.html5rocks.com/en/tutorials/internals/howbrowserswork/
W3C HTML5 Specification: http://dev.w3.org/html5/spec/single-page.html
Bit Twiddling tricks: http://www-graphics.stanford.edu/~seander/bithacks.html
Coding Interview Problems
Posted: November 3, 2011 Filed under: Technical | Tags: Coding, Coding Interview Leave a commentPost here for backup and looking up.
Data Structures
1. Integer
– find number of 1s
– next largest smaller
– smallest larger number
– determine if is palindrom
– itoa, atoi
– add 2 numbers w/o using + or arithmetic operators
– implement *, -, / using only +
– find max of two numbers w/o comparison
– swap two numbers with +/-
– swap two numbers with ^
– given an integer, find the closest number that is palindrome
– implement putlong() by putchar()
2. Bit array
3. Linked list
– find cycle,
– find position of cycle starts
– reverse LL
– delete a node in middle
– each node contains a value pointer pointing to a node,
duplicate LL.
– remove duplicates from sorted/un-sorted LL.
– find n-th to last node to end
– number is represented by LL, add 2 numbers Read the rest of this entry »