Monday, April 30, 2012

CSS Naming Guidelines

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

Examples
  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
References:
http://woork.blogspot.com/2008/11/css-coding-semantic-approach-in-naming.html
http://sixrevisions.com/css/css-tips/css-tip-2-structural-naming-convention-in-css
http://www.stuffandnonsense.co.uk/archives/naming_conventions_table.html

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
data-role="header"
data-role="footer"
data-role="page"
data-role="content"
data-role="navbar"
data-role="content"

Then I looked at HTML new semantic tags
<article>
<aside>
<header>
<footer>
<nav>
<section>

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 (http://commons.apache.org/dbcp/api-1.2.2/index.html) has a bug https://issues.apache.org/jira/browse/DBCP-281 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.

"Timer-0":
        at org.apache.commons.dbcp.AbandonedTrace.addTrace(AbandonedTrace.java:175)
        - waiting to lock <0x00002aab1c09e780> (a org.apache.commons.dbcp.PoolableConnection)
        at org.apache.commons.dbcp.AbandonedTrace.init(AbandonedTrace.java:92)
        at org.apache.commons.dbcp.AbandonedTrace.(AbandonedTrace.java:82)
        at org.apache.commons.dbcp.DelegatingStatement.(DelegatingStatement.java:61)
        at org.apache.commons.dbcp.DelegatingConnection.createStatement(DelegatingConnection.java:224)
        at org.apache.commons.dbcp.PoolableConnectionFactory.validateConnection(PoolableConnectionFactory.java:331)
        at org.apache.commons.dbcp.PoolableConnectionFactory.validateObject(PoolableConnectionFactory.java:312)
        at org.apache.commons.pool.impl.GenericObjectPool.evict(GenericObjectPool.java:1217)
        - locked <0x00002aaae61d4038> (a org.apache.commons.pool.impl.GenericObjectPool)
        at org.apache.commons.pool.impl.GenericObjectPool$Evictor.run(GenericObjectPool.java:1341)
        at java.util.TimerThread.mainLoop(Timer.java:512)
        at java.util.TimerThread.run(Timer.java:462)


"http-8083-11":
        at org.apache.commons.pool.impl.GenericObjectPool.addObjectToPool(GenericObjectPool.java:1137)
        - waiting to lock <0x00002aaae61d4038> (a org.apache.commons.pool.impl.GenericObjectPool)
        at org.apache.commons.pool.impl.GenericObjectPool.returnObject(GenericObjectPool.java:1076)
        at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:87)
        - locked <0x00002aab1c09e780> (a org.apache.commons.dbcp.PoolableConnection)
        at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:181)



"http-8083-150":
        at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:916)
        - waiting to lock <0x00002aaae61d4038> (a org.apache.commons.pool.impl.GenericObjectPool)
        at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:96)
        at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
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
http://javascript.crockford.com/code.html
http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml

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");
    document.getElementById('svg').appendChild(dom.documentElement);
}
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");
    delta.addEventListener("mousedown",function(){alert('HI')},false);
}); 
SVG JS Libraries
Raphael
jQuery SVG
svgweb
SVGKit

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">
</object>
    * <img>  
<img src="simplest.svg" width="250" height="150"/>

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

Reference:
http://warunapw.blogspot.com/2009/07/svg-string-inline-with-html.html
http://raphaeljs.com/
http://keith-wood.name/svg.html
http://code.google.com/p/svgweb/
http://svgkit.sourceforge.net/SVGKit.html
http://stackoverflow.com/questions/2753732/how-to-access-svg-elements-with-javascript/3379830#3379830
http://stackoverflow.com/questions/588718/jquery-svg-vs-raphael 
http://www.w3.org/Graphics/SVG/IG/resources/svgprimer.html#SVG_in_HTML

Monday, April 9, 2012

JVM memory model 101

Heap
    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)
-XX:MaxDirectMemorySize=<value>


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

More GC Settings:
-XX:+CMSIncrementalMode
-XX:+CMSIncrementalPacing
-XX:+CMSIncrementalDutyCycleMin=0
-XX:+CMSIncrementalDutyCycle=10
-XX:+CMSPermGenSweepingEnabled

Debug
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps
-XX:+TraceClassUnloading
-XX:+HeapDumpOnOutOfMemoryError

Update 4/25/2012
http://javaeesupportpatterns.blogspot.com/2011/02/java-hotspot-vm-permgen-space.html

https://blogs.oracle.com/jonthecollector/entry/presenting_the_permanent_generation
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)