Monday, April 30, 2012

CSS Naming Guidelines

  • Name CSS class in a semantic approach
  • Use lowercase characters to define your class and use "-" or camelCase (ex. "main-content", or "mainContent) to separate multiple words
  • Optimize CSS code creating only the main classes and reusing HTML standard tags for child elements (h1, h2, p, ul, li, ...)
  • When assigning a class or id, ask yourself “What is this element for?"
  • Avoid using names that rely on locational or visual aspects of the particular element
  • Use names that are intuitive to you
  • Use a proper/good CSS framework
  • Reference top web sites or big companies
Typical web page structure

  1. container/wrapper/page/main/box
  2. header
  3. navbar/nav
  4. menu/sidebar/subnav/secondary-content
  5. main/content/main-content
  6. sidebar/sidepanel/subnav
  7. footer

Updates: (5/9/2012)
Today I joined a webcast from O'Reilly about jquery Mobile, the demo showed some very straightforward html codes with data-role attribute.jQuery mobile also uses semantic names for these data roles, for instance

Then I looked at HTML new semantic tags

With these trends (standards), we should start to define page structure and name CSS class in a semantic way.

Apache DBCP deadlock

commons-dbcp-1.2.2 ( has a bug which can cause deadlock when Evictor thread validating connection while another thread is closing the same connection. When the deadlock happens, all threads using DB connections will hang unless a restart of application server.

        at org.apache.commons.dbcp.AbandonedTrace.addTrace(
        - waiting to lock <0x00002aab1c09e780> (a org.apache.commons.dbcp.PoolableConnection)
        at org.apache.commons.dbcp.AbandonedTrace.init(
        at org.apache.commons.dbcp.AbandonedTrace.(
        at org.apache.commons.dbcp.DelegatingStatement.(
        at org.apache.commons.dbcp.DelegatingConnection.createStatement(
        at org.apache.commons.dbcp.PoolableConnectionFactory.validateConnection(
        at org.apache.commons.dbcp.PoolableConnectionFactory.validateObject(
        at org.apache.commons.pool.impl.GenericObjectPool.evict(
        - locked <0x00002aaae61d4038> (a org.apache.commons.pool.impl.GenericObjectPool)
        at org.apache.commons.pool.impl.GenericObjectPool$
        at java.util.TimerThread.mainLoop(

        at org.apache.commons.pool.impl.GenericObjectPool.addObjectToPool(
        - waiting to lock <0x00002aaae61d4038> (a org.apache.commons.pool.impl.GenericObjectPool)
        at org.apache.commons.pool.impl.GenericObjectPool.returnObject(
        at org.apache.commons.dbcp.PoolableConnection.close(
        - locked <0x00002aab1c09e780> (a org.apache.commons.dbcp.PoolableConnection)
        at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(

        at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(
        - waiting to lock <0x00002aaae61d4038> (a org.apache.commons.pool.impl.GenericObjectPool)
        at org.apache.commons.dbcp.PoolingDataSource.getConnection(
        at org.apache.commons.dbcp.BasicDataSource.getConnection(
Two solutions to this problem:
1. Remove testWhileIdle="true", use default instead.
2. Upgrade commons-dbcp library to latest version, or use other libraries like c3p0, proxool, BoneCP etc.

Sunday, April 29, 2012

Javascript Naming Conventions

These two articles give very good guidelines to Javascript naming conventions

This blog is to highlight some notes from these two guidelines:
  • File name: should use lowercase, end in .js, and should contain no punctuation except for - (dash) or _ (underscore) or . (dot)
  • Namespace: should be used to define one single global object for one library or one project, and nested object literal pattern is preferred
  • Naming: In general, use functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, and SYMBOLIC_CONSTANTS_LIKE_THIS
  • Access control: This pattern of public, private, and privileged members is possible because JavaScript has closures. Closure means that an inner function always has access to the vars and parameters of its outer function, even after the outer function has returned.
  • Variable: camelCase. Always define it using var, at the top of the function, and use of global variables should be minimized
  • Indentation: 4 spaces
  • Line length: 80 characters or 120 characters
  • Comment: Generally use line comments. Save block comments for formal documentation and for commenting out. Use JSDoc
  • Function: All functions should be declared before they are used. Constructor function uses ClassNamesLikeThis (PascalCase) instead of camelCase
  • Use {} instead of new Object(). Use [] instead of new Array()
  • Avoid the use of the comma operator except for very disciplined use in the control part of for statements. (This does not apply to the comma separator, which is used in object literals, array literals, var statements, and parameter lists)
  • In JavaScript blocks do not have scope
  • Avoid doing assignments in the condition part of if and while statements.
  • It is almost always better to use the === and !== operators.
  • Eval is evil. eval has aliases. Do not use the Function constructor. Do not pass strings to setTimeout or setInterval
  • Avoid the use of with
  • For consistency single-quotes (') are preferred to double-quotes ("). This is helpful when creating strings that include HTML
  • Pay attention to semicolon injection, so always add semicolon at the end
  • Compile JS and optimize JS

Wednesday, April 25, 2012

SVG 101

Recently, start to look at SVG (Scalable Vector Graphics), which is a language for describing two-dimensional vector graphics in XML. SVG has many advantages including open standard, scalable images etc. I quickly went through www3school online tutorial, and wrote some notes about SVG language.

Keywords: Shapes, Filter, Gradient, Canvas, WebGL

SVG 1.1 Second Edition, which includes all the errata and clarifications, but no new features to the original SVG 1.1 was released on August 16, 2011.

SVG Basics
Creating SVG Files - Inkscape
Viewing SVG Files - Adobe SVG Viewer
SVG In HTML Pages - <embed>, <object>, <iframe> or inline, Link to an SVG File (standalone, *.svg)

SVG has some predefined shape elements
    * Rectangle <rect>
    * Circle <circle>
    * Ellipse <ellipse>
    * Line <line>
    * Polyline <polyline>
    * Polygon <polygon>
    * Path <path>

SVG Filters are used to add special effects to SVG graphics.

A gradient is a smooth transition from one color to another. In addition, several color transitions can be applied to the same element.
There are two main types of gradients in SVG:
    * Linear
    * Radial

For multimedia/animation, refer to SMIL (Synchronized Multimedia Integration Language).

There are many discussion among SVG, Canvas, WebGL One javascript 3D library called three.js is to create a lightweight 3D library with a very low level of complexity — in other words, for dummies. The library provides <canvas>, <svg> and WebGL renderers.

Inline SVG to HTML
drawSVG = function(svgStr) {
    var parser = new DOMParser();
    var dom = parser.parseFromString(svgStr, "text/xml");
Embed SVG to HTML
In case you use jQuery you need to wait for $(window).load, because the embedded SVG document might not be yet loaded at $(document).ready
$(window).load(function () {
    var a = document.getElementById("alphasvg");
    var svgDoc = a.contentDocument;
    var delta = svgDoc.getElementById("delta");
SVG JS Libraries
jQuery SVG

Embedding SVG in HTML documents SVG Primer (from W3C) suggests 5 ways to putting SVG to HTML documents:
    * <embed>  --> browser preferred way
<embed name="E" id="E" src="simplest.svg" width="50" height="50"> 
    * <frame> and <iframe>
<iframe id="B" src="simpleShell.svg" width="250" height="150"/>
    * <object> --> W3C preferred way
<object id="E" type="image/svg+xml" data="ovals.svg" width="320" height="240">
 <param name="src" value="ovals.svg">
    * <img>  
<img src="simplest.svg" width="250" height="150"/>

    * inline (XHTML or HTML5) --> new way from HTML5
  <b>scriptable SVG in XHTML</b><br/>
  <svg id="SVG" xmlns="" 
     onload="receive(evt)" width="600" height="200">
     <circle cx="100" cy="100" r="100" fill="green" />


Monday, April 9, 2012

JVM memory model 101

    Young Generation
        Eden space, two Survivor (From/To) spaces                   
    Old Generation
        Tenured space
Non-Heap (Perm + C-Heap)
    Permanent Space
    Class files and jar files
    Code Generation
    Socket Buffers
    Thread stacks
    Direct Memory Space
    JNI Code
    Garbage Collection
    JNI allocated memory

Space Allocation
-Xmx -Xms -Xmn -Xss
Young size(-XX:NewRatio)
Survivor Ratio(-XX:SurvivorRatio)
-XX:PermSize=<value> (initial)
-XX:MaxPermSize=<value> (max)

Concurrent/Parallel Garbage Collection Settings
Parallel GC in the New(Young) Generation
Concurrent in the Old generation
Use these two combined
Multiple CPU box can take advantage of this

More GC Settings:


Update 4/25/2012
Java classes are stored in the permanent generation. What all does that entail? Besides the basic fields of a Java class there are
  • Methods of a class (including the bytecodes)
  • Names of the classes (in the form of an object that points to a string also in the permanent generation)
  • Constant pool information (data read from the class file, see chapter 4 of the JVM specification for all the details, including static fields)
  • Object arrays and type arrays associated with a class (e.g., an object array containing references to methods)
  • Internal objects created by the JVM (java/lang/Object or java/lang/exception for instance)
  • Information used for optimization by the compilers (JITs) 

Thursday, April 5, 2012

My experience with deep breath air duct clean

Couple of months ago Groupon had a coupon from "Deep Breath Air Duct Clean", and I did purchase it because I thought it was from Groupon, an IPO company. After purchased, I called this "Deep Breath Air Duct Clean" customer service and mentioned groupon, the woman said the latest appointment slot was 2 months away . I had no choice but waited 2 months.

When the appointment day came, there was no any pre-service call from this merchant, so I called them again and asked if my appointment was still there at 3:30pm. The woman responded that the appointment was 3pm to 5pm, with not kind voice. I was kind of curious how they could complete the clean work if they came over so late. At 5:10pm, two guys did show up, driving a small van, only pulled out one short ladder. What they did was quickly checked the attic and told us they could not do the service and recommended to change the air duct to new plastic. Damn, the total cost was around 3k dollars. Then they left with the ridiculous estimate. I realized this was a scam, starting from purchasing groupon. I did some research that night, and found a lot of useful information from youtube. The conclusion was the usual work of an air duct clean needs about 2 hours using negative pressure vacuum, and my experience was totally a potential scam.

Recently Groupon revised their 1st quarter revenue, and its stock dropped to $14.50 or lower. This trend reminded me to write up above experience with below thoughts:
  1. Groupon is not serious about consumer, returning coupon can not return consumer time and spirit
  2. Groupon is not careful when selecting merchants
  3. Groupon has a lot of discount every day, but these coupons are not the basics of every day life
  4. Deep Breath Air Duct Clean is for upsell instead of clean service (from my experience)
  5. Air Duct clean is not necessary, the most important part is furnace pre and post filters to ensure air quality. Change or clean your filters instead of clean your air duct. 
  6. Under $50 Air Duct clean is impossible, look for some service around 100 dollars if you do need clean your air duct (like after remodel, moving to a new place etc)