View
631
Download
5
Category
Tags:
Preview:
DESCRIPTION
Why you should write quality code and how to effectively use tools to do it.
Citation preview
Code Quality
Paulin Solutions LLC
Paulin Solutions LLC
Have code will travel•Mobile Web Development•CMS/Portal (Liferay Adobe CQ 5.x)•Delivery Innovation
Topics
• Why bother writing quality code?• What is Code Quality• Components of Code Quality• Tools• Final Thoughts/Discussion
Why Bother?
What your client sees
What you see
The Psychology of Computer Programming
Gerald Weinberg
Programming is a social activity
CODE – It's not just for computers anymore!
Why would you do this to another Human Being!?!?
What is Code Quality?
?
Good or Bad?protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {if(req != null){
if(req.getParameter("type") != null){
String reqType = req.getParameter("type");
if(reqType == "FOO")//Do something
}}}
Good or Badprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String type = req.getParameter("type");if(FORWARD_TYPE.equalsIgnoreCase(type)){
countForward(req, resp);}else if(BACKWARD_TYPE.equalsIgnoreCase(type)){
countBackwards(req, resp);}else{
throw new RuntimeException("This type is not supported.");
}
Bad Code Smells!
I've learned to live with the smell why else should I care?
Reduce Bugs
Lower Maintenance Effort
It's Easier to Extend Good Code
Create Good Code Karma
Give your client the warm and fuzzy feeling.
Quality Code = Trust
Components of Code Quality
Good Code Should Work!
Functional/Non-Functional
Components should be testable
Hard to Test
private void addPlayerObjects()
/* the physical objects are the enclosing box and its spheres */
{
// create the spheres
players = new PlayerManager(sceneBG, world, collSpace, BLUE, RED);
// need to add DLL files ElementXML.dll, Java_sml_ClientInterface.dll,
// and SoarKernalSML.dll to java.library.path
IFormation offFormation = new FndtFormation(
PlayerManager.OFF_PLAYER_TYPE);
IFormation defFormation = new FndtFormation(
PlayerManager.DEF_PLAYER_TYPE);
InitFootballAgents initTask = new InitFootballAgents(positions,
offFormation, defFormation);
FormationSoarTask task = new FormationSoarTask(playText, offFormation,
defFormation);
try
{
soarManager.clearAgents();
soarManager.executeSoarTask(initTask);
soarManager.executeSoarTask(task);
} catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(this, e.getMessage(), "Soar Error",
JOptionPane.ERROR_MESSAGE);
}
Easy to test
public ModelAndView welcome(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView("welcome.page", "data", dataManager);
}
public ModelAndView openMenu(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView("topNav.comp", "data", dataManager);
}
public ModelAndView manageUsers(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView("manageusers.page", "userList", dietDaoService.getUsers());
}
Rule of thumb for writing
testable code...
Practice good design!
Good code should be easy
to maintain
Readable
Stay DRY
Avoid Excessive Complexity
Ummmmmm that sounds like a lot of
work!
Making sure it works
Benefits of Functional Test Frameworks
Test often Self Documenting Code Fearlessly
Making sure it's tested
• Cobertura• Emma• Clover• Jacoco• DevPartner
Benefits of Code Coverage Tools
Know where your risk is Help with Debugging Determine what your QA is doing
Making sure it's maintainable
• PMD• FindBugs• DevPartner
Benefits of Static Analysis
Find bugs before you check in code Enforce Coding Standards Low effort code reviews
Putting it all together
Benefits of Sonar
Single Dashboard to display test results, coverage, static analysis.
Drill down into the code with syntax highlighting
Issue Assignment and workflow Simple build integration
Parting thoughts
Don't write crappy code. I
know where you live.
Recommended