Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

2014-11-03

Four "C" of the Clean Code

Recently I am reading the book "Clean code" again. I have found there are 4 characteristics of the clean code. 

The 4 characters of the clean code should have:  
  • Controllable
  • Comprehensible
  • Convenient
  • Consistent 



Controllable


If there only just one concept to be remembered, it should be this one. 
If the program can be controlled by you, the author of the code or other who involved, this program will be a good program. 

For a controllable program, which is the program can be easily understood, changed, adding new features, and easy to debug. 
The more code involved, the more uncontrollable. It will become the "Gordian Knot.


Comprehensible:

Every program we write have two group of audiences; 
First is the machine, compiler, syntax checker, …., they will transfer our programs to executable;
Second is us, human being; The program should be readable so that we could understand and work on it. 
There are many chances that our programs will be read by someone else or yourself later. 
Therefore it is important to make the code comprehensible. 


Convenient:

While making a large program, we may involve a lots of code. Therefore, we need to arrange the code so that it helps to do thing fast. 

Here some examples:
- Find the libraries and functions available in the project and how to use it
- Quickly navigate to the class or method we want to go. 
- Easy to remember the classes involved

To do so, we need to adopt a good naming contention and code style.


Consistent

As we know, many words in English can refer to a similar meaning; One frequent met example in coding: number of X, count of X, size of X;  In order to prevent our code audience getting confused or mislead, we need to make sure the same word have the same meaning;
If you use "count" as the suffix telling how many, keep to use it over your class. 




2014-09-02

A day in my coding life.

Here is a short sharing of my daily coding life.

While arrived to office, prepare the basic first, breakfast, water and tea;

Then first thing first, I starts with a simple prayer and listening to biblical messages.

And then I read some industrial news from Twitter, Google Plus, Techcrunch, ... to get updated;

Next I prepare:
- Spotify (provide music for my concentration)
- Tomato.es (For Pomodoro Technique, one of time management skill)
- Trello (For task management)
- Evernote (for note taking and daily planning)

And then Tomato Clock starts, several 25 minutes session begin:

Every 25 minutes, I will focus on my different coding works:

  • Planning : Fail to plan, plan to fail. Always keep works being planned.
  • Program Design : Design is the origin, without a design, it will be very hard to code. 
  • Write and Run Code 
  • Refactor and Documentation : It help to re-organise what I have done
  • Sharing to other coders
  • Learn something new 
  • Testing : More test, more bugs unveiled and quality UP. 


While my team members are all there, we start the daily SCRUM to update the progress of each others.

A day of coding if full of fun and challenges !!!

Wish you enjoy your work and coding as well.









2014-05-22

TDDLib for cocos2d-x-3.0 go public

I am going to put my Unit Test Framework for cocos2d-x 3.0 to open source (github). If you are a lover of TDD or Unit Test, you may love it. 

Github: https://github.com/tklee1975/tddlib_cocos2dx3

Currently, I am writing some documents about how to setup the library in your project and how to add a new test. If you are interested and like this project, please let me know.  


Testing "testSprite"


Testing "testLabel"

2014-05-11

Cocos2d-x 3.0 Setup (Include Git & Github support)

Recently, I am going to try the latest cocos2d-x 3.0 final version. Here is the note to use cocos2d-x 3.0 with the source control of Git & Github;





Steps to setup



1. Download and zip cocos2d-x 


Simply following the instruction at cocos2d-x official site ( http://www.cocos2d-x.org/download) to download.


2. Setup the directory structure 


The following is my structure related to the cocos2dx
  • /w/cocos2dx/engine - Place the cocos2d core at here. (The pack of the cocos2d-x project will be placed here)
  • /w/cocos2dx/project - The project directory, where is my code placed at.
  • /w/cocos2dx/script/ -  The script files simplify some task. (will discuss later)


3. Run the setup 


Just run the following commands

cd /w/cocos2dx/engine/./setup.py

It will change your .bash_profile to add some environment variable

Prepare the following information:  

  • ANDROID_SDK_ROOT
  • ANT_ROOT

4. Create a new project using script


Use the following command to make a new project :
cd /w/cocos2dx/engine/tools/bin/cocos_console/./cocos.py new -p ken.game -l cpp Example1 -d /w/cocos2dx/project/


5. (optional) Trim down the project size

Since every project made by the cocos.py will make a copy of the cocos2d engine inside the project. If you want to share the engine code among different projects, we can do steps below; However, if you are going to hack the engine code and don't affect other project, you shouldn't do that. One more point, this action will place the cocos2d engine source code outside the source control scope. (Pro: save space;  Con: change won't be logged)

Just do the following to keep the project smaller
mv ./cocos2d ../ln -s ../cocos2d ./cocos2d


6. Setup GIT 



Do the following step to setup GIT. 
  • Install Git. (suppose Xcode will help to install the git, but it doesn't happened. just go to http://git-scm.com/ to download it) 
  • Add the .gitignore to the project directorycd /w/cocos2dx/cp ./script/.gitignore project/Example1
  • Git initialisation
    run "git init" in your project directory
  • Add to SourceTree (SourceTree is a GUI version of Git. available at http://www.sourcetreeapp.com/)Select "Add Working copy" and the select the project directory (w/cocos2dx/project/Example1)
  • Commit the work at SourceTree


7. Sync with Github 


In the previous step, we just put the source code to the local repository; It will be more safe if we put the source on the remote location. Here, I select github. Besides github, there are many alternatives such as you setup your own git remote server or using bitbucket. 

There are the steps to setup repository in github.


     
  • Create a repository in www.github.com. (Assume that you are already registered) and copy-and-paste the git url.
  • Open SourceTree and select your project to add a remote location- Select the project.
    - Choose "Repository -> Repository Setting" in the menu.
    - Select the "Remotes" Tab.
    - Click the "Add" button; It will show the "Add Remote" Popup.
    -  Enter "origin" in "Remote Name" and the given github url in "Remote Path"
  • Pull the master branch to github (origin location)

8. Start coding and Enjoy it


Open the project in Xcode, build and Run it. 


2014-04-24

Making python module with C++ using boost.python (mac)


1. Install Python 

For most of the case, the python is already installed.
The paths of my mac are as followings:
  • bin: /usr/bin/python2.7
  • include:  /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
  • library: /usr/lib/python2.7
  • archive library:  /usr/lib/python2.7/lib/libpython.a

2. Install boost

Download boost at http://www.boost.org/users/download/
Build the library: 
./bootstrap.sh
and 
./b2 install

When success, can find the include and library in the following path:
include path: /usr/local/boost/boost/
library path:  /usr/local/boost/stage/lib

3. Create the hello world program
Find out the detail in boost.python doc; it is clear.
http://www.boost.org/doc/libs/1_55_0/libs/python/doc/tutorial/doc/html/index.html

4. Build the program to share library


Create a make script as follows:
--------------------------------------------------------------------------------------------------------------
#!/bin/sh

BOOST_ROOT=/usr/local/boost
PYTHON_ROOT=/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7

LD_LIBRARY_PATH=/usr/local/boost/stage/lib
PYTHON_LIBRARY_PATH=/usr/lib/python2.7/lib

echo $PYTHON_ROOT

g++ -c hello.cpp -I $PYTHON_ROOT -I $BOOST_ROOT
g++ -shared -o hello.so hello.o -lboost_python -lpython \
     -L $LD_LIBRARY_PATH \
     -L $PYTHON_LIBRARY_PATH

--------------------------------------------------------------------------------------------------------------
and run it.

Note:
  • to compile successfully, need to define the correct python and boost include path
  • to link successfully, need to define the boost.python and python library location



5. Play the module in python

First, need configure the environment so that python know boost.python
DYLD_LIBRARY_PATH=$BOOST_ROOT/stage/lib

Then run ‘python’ and type 
import hello
print hello_ext.greet()



If the DYLD_LIBRARY_PATH not define, may receive the following error message:
Library not loaded: libboost_python.dylib




2014-03-16

Abnormal java.net.UnknownHostException on resin

Recently, I encountered a problem while I am calling to Google API in my java code running on Resin. The problem is that: The http call raise an UnknownHostException;



It doesn't caused by Java itself because the UnitTest show it is correct.
It doesn't caused by Resin because it work in my development platform.

So what go wrong? Finally, I has discovered it is wrong in the staging server environment;
The /etc/hosts isn't configured as the resin want to. (... maybe it is the problem of resin too ..)

To fix it:
- type "hostname" to find your hostname . e.g "my.pc"
- edit /etc/hosts and add the line "127.0.0.1 my.pc"
- restart the server to make it effective

Notes:
- This problem may not be happened in all environment. My macbook doesn't have the problem.

Related Links:






2014-03-13

Make java connection pool auto reconnect

Recently we encountered a problem when using java db connection pool. The problem is that the connection is being closed by the DB server since it is already timeout. To due with this, need to configure two more parameters.

They are:
testOnBorrow  ;  define it as "true"
validationQuery ; define it as "SELECT 1 FROM dual" if you are also using MySQL

Yes, It is easy to config it. But how to test it? (Many sites may not give the answer). 

To verify the configuration is work, we can make a simple Unit Test;

Step 1: Make a failure case

Make an unit test base on the following code;


Properties p = new Properties(); 
prop.put("driver", "org.gjt.mm.mysql.Driver");
prop.put("url", "jdbc://localhost:3306/db");
prop.put("username", "user");
prop.put("password", "123456");
p.setProperties("testOnBorrow", "false");
p.setProperties("validationQuery", "");

BasicDataSource ds = BasicDataSourceFactory.createDataSource(prop);
ds.setMaxActive(1);
ds.setMaxIdle(1);
ds.setMinIdle(0);

Connection conn = ds.getConnection();
System.out.println("First connection");


System.out.println("Sleep start");
Thread.sleep(10000);
System.out.println("Sleep end");

Connection conn = ds.getConnection();
System.out.println("Second connection");


To make the connection fail at the second time, try to kill the connection in DB side while "Thread.sleep";


Step 2: Fix the failure 

Simply change "testOnBorrow=false" and "validationQuery=" to 
 "testOnBorrow=true" and "validationQuery=SELECT 1 FROM dual"

Then, run the test again. we will see "Second connection" show up instead of Exception message.



2013-08-29

Key Tasks of cocos2d-x development


Setup Development Environment

  • Make the iOS binary (.ipa), run on Simulator and Device
  • Make the Android binary (.apk), run on Device

Seek and Read References

  • Reference of Cocos2d-x
  • Reference of C++
  • Reference of POCO library

Create Testing Code

  • Create UnitTest for Logic (text mode stuff), suggest to use UnitTest++
  • Create UnitTest for GUI or Graphics stuff

Create Architecture / Utility / General Code

  • Clarify how MVC architecture work
  • Create Data Class
  • Create a singleton class
  • Make and understand Useful MACROs

Manage GUI, Graphics and Scene 

  • About GUI 
    • Custom View
    • Popup View
    • Fullscreen View
    • TableView
    • Scrollable View
    • Using Cocos builder to create the views
    • Gallery View
    • Loading View
  • About Graphics
    • Render Sprites
    • Render Maps
    • Render something you want to
    • Controlling the zoom and location of the Camera
  • About Scene
    • Add a new scene
    • Switch between scenes

Network Stuff

  • Connect using Socket or HTTP
  • Adding Network Protocol
    • Create Requests
    • Handle Response
    • Handle Network errors
  • Testing the network protocol

Debugging and Profiling

  • Check the potential problem
  • Check memory leak
  • Check the performance using profiler
  • Locate where and when will crash the app

Deployment

  • Create different kinds of build using Makefile/build.xml/.… 
  •  Publish the app (ipa or apk) using TestFlight


2013-05-31

Cocos2dx Study Checklist

Start learning Cocos2dx this week (End of May 2013)

Here is the checklist to be studies


  • [ ok ] Hello World
  • [ ok] Setting up TDD framework
  • [ ] Using CCMenu
  • [ ok ] Building iOS and Android using the same source code
  • [ ok ] Understanding the Coordinate System of Cocos2d
  • [ ] Memory Management
  • [ ] Make a CCNode/CCLayer Scrollable
  • [ ] Make a custom CCNode
  • [ ] Understand the SELECTOR (Method Pointer) 

Will keep update this checklist!!

2013-04-28

2013年技术钻研计划

主要研究对象

  • 引擎类:
    • Cocos2Dx
    • Unity3D
    • OpenGL
  •  语言
    • C++
    • Lua
  •  工具
    • Spine - 2D动画工具
    • Tiled - 地图工具
  • 开发技巧
    • Test Driven Development
    • MVC Design