`
yang_kunlun
  • 浏览: 74917 次
  • 性别: Icon_minigender_1
  • 来自: 地球
最近访客 更多访客>>
社区版块
存档分类
最新评论

Code style

阅读更多
Developer Review¶
Part to copy for push to Dev QA

Migrations (No duplicate numbers, class names) -
Code Indentation and Style (2 spaces + formatted) -
Naming (meaningfull method names, ! ?) -
Structure and Length (no long methods, light controllers, repeated code)-
Javascript Checks (JSLint, Global, Namespacing) -
Unit Tests -
HTML and CSS (IE6, IE7, FF)-
SQL (ansi, capitals, indentation) -

Checklist¶

   1. Style
      Have you followed the coding style guide for all of your javascript, html, css, js and ruby?
   2. Naming
      Do your method names and attribute names convey meaning?
      If a method alters or modifies something you should consider ending the method with ! remove_all_units!
      if it's a method that returns true or false it should end with a question mark is_allowed?
   3. Structure and length
      Have you got long methods, do they need to be separated out into smaller easy to manage chunks?
      Have you moved methods up to models where possible?
      Are there repeated chunks that should be refactored into a single function?
   4. Comments for hard or odd bits
      Have you done something weird that's going to confuse one of the other coders, put in quick comments to explain why.
      Are your comments in english? Does the english make sense or do you need to get it checked?
   5. Javascript
      Have you run your code and passed through JSLint?
      Have you ensured that your not polluting the global namespace.
      Are your functions in namespaced appropriately (i.e. the order me a coffee functions are all in SomeNamespace.CoffeeOrdering = {}
      Are all of your function names in camel case?
   6. Unit tests
      Does your code have sufficient unit test coverage where appropriate?
   7. HTML / CSS
      Does it work and look good in IE6 / IE7 /FF / Safari (Where possible and necessary to test)
      Ensure that there are no inline styles (unless absolutely necessary)
      Is your html formatted
   8. SQL
      Are all of your sql statements in capital letters?
   9. Ansi Standard Joins
      Have you used Ansi-Standard joins and ensured that your table joins are separated from your where clauses.
      e.g.

The Code Style Guide¶

The developer review is to ensure high quality, maintainable readable code. Each developer should "review" the code themselves before submitting it, but it needs to cover the following aspects:

   1. Code indentation and style.
      Is the entire file you submitted neat, indented (2 spaces) and formatted correctly?

Ruby¶

Spacing
2 Spaces for every piece of indented code

Comments
Comments should the follow the styles in:
http://rdoc.sourceforge.net/doc/index.html
In short

  # Calculates some_code by using bla and some other business rule
  some_code = bla + 1

and

# Test to see if a new word contains the same
# letters as the original
def is_anagram?(text)
  @initial_letters == letters_of(text)
end

If the functionality is not obvious, there needs to be a comment describing what it does in English. If you think your English is wrong, then you can just ask someone else to review it for you.

Spacing between functions
use

a = 1


not

a=1

Lining functions up
use

dog            = 1
cattle         = 2
something_else = 3


not

dog=1
cattle=2
something_else=3

Comments should use the rubydoc styles shown at
Javascript¶
Function calls should be:

this.foo(bar);

Not

this.foo (bar);

The space differentiates them from method invocations.

Control statements should be:

// space between if and parentheses
if (something == x) {
  doSomethingElse();
} else { // else on same line as close and open braces
  doSomethingNice();
}

//space between switch and parentheses
switch (x) {
  case 'y' :
    return 'something';
  break;

  case 'n' :
    return 'somethingElse';
  break;
}

//space between for and parentheses
for (var i=0; i<n; i++) {
  ...
}


Conditions and assignments should have spaces ( except maybe in for loop declarations )
Use:

if (x == y) {
  ...
}

y = x;

Not

if (x==y) {
  ...
}

y=x;

HTML Formatting (2 spaces)¶

<div>
  <ul>
    <li><a href="bla.html">something</a></li>
  </ul>
  <p>Some Paragarph</p>
  <p>Some other paragraph</p>
</div>

CSS Formatting (2 spaces)¶

#something {
  border: 1px solid red;
  color: green}
  #something a {
    font-weight: bold;}
  #something div {
    background-color: red;}

.some_other_class {
  border: none;}
  .some_other_class {
  border: 1px solid blue;}

Javascript¶
The majority of javascript should be stored in separate files, which group functionality together. E.g. the functionality for a page.
The javascript should be namespaced and not pollute the global namaspace.
Only very small inline javascript should exist. E.g.

  <% js do %>
  Prohost.admin_page.init(<%= @init.to_json %>);
  <% end %>


h2. SQL Formatting Note:

    * Capital Letters for SQL keywords
    * Line everything up

Instead of:


  SELECT *
  FROM table_a, table_b, table_c
  WHERE table_a.id = table_b.foreign_key_id and
  table_a.desc LIKE '%hello%' and 
  table_c.foreign_key_id = table_a.id (+)
 


Format it like this:

 
  SELECT *
  FROM table_a
  JOIN table_b      ON table_b.foreign_key_id = table_a.id 
  LEFT JOIN table_c ON table_c.foreign_key_id = table_a.id
  WHERE table_a.desc LIKE '%hello%'
 

分享到:
评论
1 楼 minma_yuyang 2009-05-15  
还不错,借鉴了。

相关推荐

Global site tag (gtag.js) - Google Analytics