SlideShare uma empresa Scribd logo
1 de 20
Coding Standards for JavaAn Introduction
Why Coding Standards are Important? Coding Standards lead to greater consistency within your code and the code of your teammates. Easier to understand Easier to develop Easier to maintain Reduces overall cost of application
Components of Java Source File /*  * Copyright notice  */ package java.awt; import java.awt.peer.CanvasPeer; /**  * class description  *  * @version 1.10 04 Oct 1996  *  * @author First name Last name */ public class ColorPickerPanel { /* A class implementation comment can go here. */ 	 /**  	  *class variables – doc comment 	  */ 	public static Integer colorVariant; 	/**  	  *instance variables – doc comment 	  */ 	private String colorCode; Beginning Comments Package and Import Statements Class/interface documentation comment (/**...*/) class or interface statement Class/interface implementation comment (/*...*/), if necessary Class (static) variables Instance variables
Components of Java Source File continued… 	/**  	  *default constructor 	  */  public ColorPickerPanel() { colorVariant = 11; 		colorCode = #FFFFFF; 	} /**  	  *two argument constructor 	  */ 	public ColorPickerPanel(Integer colorVariant, String colorCode) { 		this.colorVariant = colorVariant; this.colorCode = colorCode; 	} /**  	  *@retrun the color code 	  */ 	public String getColorCode() { 		return colorCode; 	} /**  	  *@param Integer the color variant 	  */ 	public void setColorVariant(Integer colorVariant) { 		……… 		……… 	} } Indentation Constructors Blank line Documentation comments Methods
Comments /**  * class description  * @version 1.10 04 Oct 1996  * @author First name Last name */ public class ColorPickerPanel { /* A class implementation comment can go here. */ 	/* 	private static final String DEFAULT_COLOR = “#FFFFFF”	 	private static final int DEFAULT_VARIANT = 0;	 	*/	 	 /**  	  *class variables – doc comment 	  */ 	public static Integer colorVariant; 	/**  	  *instance variables – doc comment 	  */ 	private String colorCode; 	/**  	  *@param Integer the color variant 	  */ 	public void setColorVariant(Integer colorVariant) { 		if (colorVariant == 0) { 			colorCode = “#000000”; /*  set the color to black*/ 		} else { 			colorCode = “#FFFFFF”; //  set the color to while 		} 	} } Documentation Comments Implementation Comments Single Line Comments Block Comments Trailing Comments End-of-Line Comments
Naming Conventions  What Makes Up a Good Name? Use full English descriptors  For example, use names like firstName, grandTotal, or CorporateCustomer Use terminology applicable to the domain Banking domain -  Customer, Software services domain - Client Use mixed case to make names readable Avoid long names (< 15 characters is a good idea) User abbreviations sparingly Capitalize the first letter of standard acronym
Naming Conventions Continued… Classes / Interfaces –  	Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Methods –  	Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.  Variables –  	Variables should be nouns, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Class Constants –  	Class Constants should be all uppercase with words separated by underscores (“_”).
Blank Spaces Before / After Parenthesis – 	A keyword followed by a parenthesis should be separated by a space. while_(true)_{ 			... 		} 	A blank space should appear after commas in argument lists. 	All binary operators except . should be separated from their operands by spaces.  a = (a + b) / (c * d); 	The expressions in a for statement should be separated by blank spaces. 		for (expr1; expr2; expr3) 	Casts should be followed by a blank. 		User x = (User) anObject; blank spaces
Returning Values Try to make the structure of your program match the intent. Example: 	if (booleanExpression) { 		return TRUE; 	} else { 		return FALSE; 	} should instead be written as 	return booleanExpression;
Ternary Operator (?:) Use Ternary Operator for conditional assignment Example: 	Int x; 	If (expression) { 		x = 9; 	} else { 		x = 0; 	} can be written as 	x = (expression) ? 9 : 0;
much more to learn … References Google…
Ambler’s Law of Standards Industry standards > organizational standards > project standards > personal standards > no standards
so, what is the lesson to learn? Whenever possible, reuse standards and guidelines, don’t reinvent them
Static Code Analysis Static code analysis is the analysis of computer software that is performed without actually executing programs built from that software. 	(analysis performed on executing programs is known as dynamic analysis) In most cases the analysis is performed on some version of the source code and in the other cases some form of the object code. The term is usually applied to the analysis performed by an automated tool, with human analysis being called program understanding, or code review. Some of automated SCA Tools – PMD, AppPerfect, FindBugs, IntelliJ IDEA etc.
PMDhttp://pmd.sourceforge.net/ PMD scans Java source code and looks for potential problems like: 	Possible bugs - empty try/catch/finally/switch statements 	Dead code - unused local variables, parameters and private methods 	Suboptimal code - wasteful String/StringBuffer usage 	Overcomplicated expressions - unnecessary if statements, for loops that could be while loops 	Duplicate code - copied/pasted code means copied/pasted bugs
PMD – Rule Set for SCAhttp://pmd.sourceforge.net/rules/index.html Basic JSP rules 	NoLongScripts: Scripts should be part of Tag Libraries, rather than part of JSP pages.  	NoScriptlets: Scriptlets should be factored into Tag Libraries or JSP declarations, rather than being part of JSP pages.  	NoInlineStyleInformation: Style information should be put in CSS files, not in JSPs. Therefore, don't use <B> or <FONT> tags, or attributes like "align='center'".  	NoClassAttribute: Do not use an attribute called 'class'. Use "styleclass" for CSS styles.  	NoJspForward: Do not do a forward from within a JSP file.
AppPerfect Java Code Test AppPerfect Java Code Test is a static Java code analysis software designed to perform the following two key tasks: Automate Java code review and Enforce Good Java Coding Practices.  AppPerfect Code Test analysis your Java and Java Server Pages (JSP) source code and applies over 750 Java coding rules to apply the collective knowledge of leading experts in the Java programming field to your code. Some Rules – 	Avoid method calls in loop 	Declare methods not using instance variables static 	User equals method instead of equality operator 	Etc.
AppPerfect Java Code Testa screen shot
Queries! Google…
Thank You Mahesh Babu M

Mais conteúdo relacionado

Mais procurados (20)

Coding standards and guidelines
Coding standards and guidelinesCoding standards and guidelines
Coding standards and guidelines
 
Junit
JunitJunit
Junit
 
TypeScript - An Introduction
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An Introduction
 
Coding standard
Coding standardCoding standard
Coding standard
 
Kotlin Code style guidelines
Kotlin Code style guidelinesKotlin Code style guidelines
Kotlin Code style guidelines
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
 
Coding standard and coding guideline
Coding standard and coding guidelineCoding standard and coding guideline
Coding standard and coding guideline
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
TypeScript
TypeScriptTypeScript
TypeScript
 
Java 17
Java 17Java 17
Java 17
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practices
 
Clean code
Clean codeClean code
Clean code
 
Coding conventions
Coding conventionsCoding conventions
Coding conventions
 
Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
 
Coding standard
Coding standardCoding standard
Coding standard
 
Clean code
Clean codeClean code
Clean code
 

Semelhante a Coding standards for java

Best Coding Practices in Java and C++
Best Coding Practices in Java and C++Best Coding Practices in Java and C++
Best Coding Practices in Java and C++Nitin Aggarwal
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldDavid McCarter
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldDavid McCarter
 
Android coding standard
Android coding standard Android coding standard
Android coding standard Rakesh Jha
 
Code Documentation. That ugly thing...
Code Documentation. That ugly thing...Code Documentation. That ugly thing...
Code Documentation. That ugly thing...Christos Manios
 
76829060 java-coding-conventions
76829060 java-coding-conventions76829060 java-coding-conventions
76829060 java-coding-conventionsslavicp
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)David McCarter
 
Language tour of dart
Language tour of dartLanguage tour of dart
Language tour of dartImran Qasim
 
GTU Guidelines for Project on JAVA
GTU Guidelines for Project on JAVAGTU Guidelines for Project on JAVA
GTU Guidelines for Project on JAVATOPS Technologies
 
Programming style guideline very good
Programming style guideline very goodProgramming style guideline very good
Programming style guideline very goodDang Hop
 
In-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLIn-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLeSparkBiz
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perlmegakott
 

Semelhante a Coding standards for java (20)

Best Coding Practices in Java and C++
Best Coding Practices in Java and C++Best Coding Practices in Java and C++
Best Coding Practices in Java and C++
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Android coding standard
Android coding standard Android coding standard
Android coding standard
 
Code Documentation. That ugly thing...
Code Documentation. That ugly thing...Code Documentation. That ugly thing...
Code Documentation. That ugly thing...
 
latest slide
latest slidelatest slide
latest slide
 
testing add
testing addtesting add
testing add
 
76829060 java-coding-conventions
76829060 java-coding-conventions76829060 java-coding-conventions
76829060 java-coding-conventions
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
 
Language tour of dart
Language tour of dartLanguage tour of dart
Language tour of dart
 
Presentation c++
Presentation c++Presentation c++
Presentation c++
 
GTU Guidelines for Project on JAVA
GTU Guidelines for Project on JAVAGTU Guidelines for Project on JAVA
GTU Guidelines for Project on JAVA
 
Coding conventions
Coding conventionsCoding conventions
Coding conventions
 
Programming style guideline very good
Programming style guideline very goodProgramming style guideline very good
Programming style guideline very good
 
c# at f#
c# at f#c# at f#
c# at f#
 
C# AND F#
C# AND F#C# AND F#
C# AND F#
 
Bb Tequila Coding Style (Draft)
Bb Tequila Coding Style (Draft)Bb Tequila Coding Style (Draft)
Bb Tequila Coding Style (Draft)
 
In-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLIn-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTML
 
Basics1
Basics1Basics1
Basics1
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perl
 

Último

Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfPaul Menig
 
Socio-economic-Impact-of-business-consumers-suppliers-and.pptx
Socio-economic-Impact-of-business-consumers-suppliers-and.pptxSocio-economic-Impact-of-business-consumers-suppliers-and.pptx
Socio-economic-Impact-of-business-consumers-suppliers-and.pptxtrishalcan8
 
Regression analysis: Simple Linear Regression Multiple Linear Regression
Regression analysis:  Simple Linear Regression Multiple Linear RegressionRegression analysis:  Simple Linear Regression Multiple Linear Regression
Regression analysis: Simple Linear Regression Multiple Linear RegressionRavindra Nath Shukla
 
Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Roland Driesen
 
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service DewasVip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewasmakika9823
 
Call Girls in Gomti Nagar - 7388211116 - With room Service
Call Girls in Gomti Nagar - 7388211116  - With room ServiceCall Girls in Gomti Nagar - 7388211116  - With room Service
Call Girls in Gomti Nagar - 7388211116 - With room Servicediscovermytutordmt
 
Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999
Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999
Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999Tina Ji
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...lizamodels9
 
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779Delhi Call girls
 
Call Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine ServiceCall Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine Serviceritikaroy0888
 
Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.Eni
 
Sales & Marketing Alignment: How to Synergize for Success
Sales & Marketing Alignment: How to Synergize for SuccessSales & Marketing Alignment: How to Synergize for Success
Sales & Marketing Alignment: How to Synergize for SuccessAggregage
 
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdfCatalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdfOrient Homes
 
A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMANIlamathiKannappan
 
BEST ✨ Call Girls In Indirapuram Ghaziabad ✔️ 9871031762 ✔️ Escorts Service...
BEST ✨ Call Girls In  Indirapuram Ghaziabad  ✔️ 9871031762 ✔️ Escorts Service...BEST ✨ Call Girls In  Indirapuram Ghaziabad  ✔️ 9871031762 ✔️ Escorts Service...
BEST ✨ Call Girls In Indirapuram Ghaziabad ✔️ 9871031762 ✔️ Escorts Service...noida100girls
 
Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Neil Kimberley
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
Monthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxMonthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxAndy Lambert
 

Último (20)

Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdf
 
Socio-economic-Impact-of-business-consumers-suppliers-and.pptx
Socio-economic-Impact-of-business-consumers-suppliers-and.pptxSocio-economic-Impact-of-business-consumers-suppliers-and.pptx
Socio-economic-Impact-of-business-consumers-suppliers-and.pptx
 
Regression analysis: Simple Linear Regression Multiple Linear Regression
Regression analysis:  Simple Linear Regression Multiple Linear RegressionRegression analysis:  Simple Linear Regression Multiple Linear Regression
Regression analysis: Simple Linear Regression Multiple Linear Regression
 
Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...
 
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service DewasVip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
 
Call Girls in Gomti Nagar - 7388211116 - With room Service
Call Girls in Gomti Nagar - 7388211116  - With room ServiceCall Girls in Gomti Nagar - 7388211116  - With room Service
Call Girls in Gomti Nagar - 7388211116 - With room Service
 
Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999
Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999
Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
 
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
 
Call Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine ServiceCall Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine Service
 
Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.
 
Sales & Marketing Alignment: How to Synergize for Success
Sales & Marketing Alignment: How to Synergize for SuccessSales & Marketing Alignment: How to Synergize for Success
Sales & Marketing Alignment: How to Synergize for Success
 
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdfCatalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
 
A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMAN
 
Forklift Operations: Safety through Cartoons
Forklift Operations: Safety through CartoonsForklift Operations: Safety through Cartoons
Forklift Operations: Safety through Cartoons
 
BEST ✨ Call Girls In Indirapuram Ghaziabad ✔️ 9871031762 ✔️ Escorts Service...
BEST ✨ Call Girls In  Indirapuram Ghaziabad  ✔️ 9871031762 ✔️ Escorts Service...BEST ✨ Call Girls In  Indirapuram Ghaziabad  ✔️ 9871031762 ✔️ Escorts Service...
BEST ✨ Call Girls In Indirapuram Ghaziabad ✔️ 9871031762 ✔️ Escorts Service...
 
Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
 
Monthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxMonthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptx
 
KestrelPro Flyer Japan IT Week 2024 (English)
KestrelPro Flyer Japan IT Week 2024 (English)KestrelPro Flyer Japan IT Week 2024 (English)
KestrelPro Flyer Japan IT Week 2024 (English)
 

Coding standards for java

  • 1. Coding Standards for JavaAn Introduction
  • 2. Why Coding Standards are Important? Coding Standards lead to greater consistency within your code and the code of your teammates. Easier to understand Easier to develop Easier to maintain Reduces overall cost of application
  • 3. Components of Java Source File /* * Copyright notice */ package java.awt; import java.awt.peer.CanvasPeer; /** * class description * * @version 1.10 04 Oct 1996 * * @author First name Last name */ public class ColorPickerPanel { /* A class implementation comment can go here. */ /** *class variables – doc comment */ public static Integer colorVariant; /** *instance variables – doc comment */ private String colorCode; Beginning Comments Package and Import Statements Class/interface documentation comment (/**...*/) class or interface statement Class/interface implementation comment (/*...*/), if necessary Class (static) variables Instance variables
  • 4. Components of Java Source File continued… /** *default constructor */ public ColorPickerPanel() { colorVariant = 11; colorCode = #FFFFFF; } /** *two argument constructor */ public ColorPickerPanel(Integer colorVariant, String colorCode) { this.colorVariant = colorVariant; this.colorCode = colorCode; } /** *@retrun the color code */ public String getColorCode() { return colorCode; } /** *@param Integer the color variant */ public void setColorVariant(Integer colorVariant) { ……… ……… } } Indentation Constructors Blank line Documentation comments Methods
  • 5. Comments /** * class description * @version 1.10 04 Oct 1996 * @author First name Last name */ public class ColorPickerPanel { /* A class implementation comment can go here. */ /* private static final String DEFAULT_COLOR = “#FFFFFF” private static final int DEFAULT_VARIANT = 0; */ /** *class variables – doc comment */ public static Integer colorVariant; /** *instance variables – doc comment */ private String colorCode; /** *@param Integer the color variant */ public void setColorVariant(Integer colorVariant) { if (colorVariant == 0) { colorCode = “#000000”; /* set the color to black*/ } else { colorCode = “#FFFFFF”; // set the color to while } } } Documentation Comments Implementation Comments Single Line Comments Block Comments Trailing Comments End-of-Line Comments
  • 6. Naming Conventions What Makes Up a Good Name? Use full English descriptors For example, use names like firstName, grandTotal, or CorporateCustomer Use terminology applicable to the domain Banking domain - Customer, Software services domain - Client Use mixed case to make names readable Avoid long names (< 15 characters is a good idea) User abbreviations sparingly Capitalize the first letter of standard acronym
  • 7. Naming Conventions Continued… Classes / Interfaces – Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Methods – Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Variables – Variables should be nouns, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Class Constants – Class Constants should be all uppercase with words separated by underscores (“_”).
  • 8. Blank Spaces Before / After Parenthesis – A keyword followed by a parenthesis should be separated by a space. while_(true)_{ ... } A blank space should appear after commas in argument lists. All binary operators except . should be separated from their operands by spaces. a = (a + b) / (c * d); The expressions in a for statement should be separated by blank spaces. for (expr1; expr2; expr3) Casts should be followed by a blank. User x = (User) anObject; blank spaces
  • 9. Returning Values Try to make the structure of your program match the intent. Example: if (booleanExpression) { return TRUE; } else { return FALSE; } should instead be written as return booleanExpression;
  • 10. Ternary Operator (?:) Use Ternary Operator for conditional assignment Example: Int x; If (expression) { x = 9; } else { x = 0; } can be written as x = (expression) ? 9 : 0;
  • 11. much more to learn … References Google…
  • 12. Ambler’s Law of Standards Industry standards > organizational standards > project standards > personal standards > no standards
  • 13. so, what is the lesson to learn? Whenever possible, reuse standards and guidelines, don’t reinvent them
  • 14. Static Code Analysis Static code analysis is the analysis of computer software that is performed without actually executing programs built from that software. (analysis performed on executing programs is known as dynamic analysis) In most cases the analysis is performed on some version of the source code and in the other cases some form of the object code. The term is usually applied to the analysis performed by an automated tool, with human analysis being called program understanding, or code review. Some of automated SCA Tools – PMD, AppPerfect, FindBugs, IntelliJ IDEA etc.
  • 15. PMDhttp://pmd.sourceforge.net/ PMD scans Java source code and looks for potential problems like: Possible bugs - empty try/catch/finally/switch statements Dead code - unused local variables, parameters and private methods Suboptimal code - wasteful String/StringBuffer usage Overcomplicated expressions - unnecessary if statements, for loops that could be while loops Duplicate code - copied/pasted code means copied/pasted bugs
  • 16. PMD – Rule Set for SCAhttp://pmd.sourceforge.net/rules/index.html Basic JSP rules NoLongScripts: Scripts should be part of Tag Libraries, rather than part of JSP pages. NoScriptlets: Scriptlets should be factored into Tag Libraries or JSP declarations, rather than being part of JSP pages. NoInlineStyleInformation: Style information should be put in CSS files, not in JSPs. Therefore, don't use <B> or <FONT> tags, or attributes like "align='center'". NoClassAttribute: Do not use an attribute called 'class'. Use "styleclass" for CSS styles. NoJspForward: Do not do a forward from within a JSP file.
  • 17. AppPerfect Java Code Test AppPerfect Java Code Test is a static Java code analysis software designed to perform the following two key tasks: Automate Java code review and Enforce Good Java Coding Practices. AppPerfect Code Test analysis your Java and Java Server Pages (JSP) source code and applies over 750 Java coding rules to apply the collective knowledge of leading experts in the Java programming field to your code. Some Rules – Avoid method calls in loop Declare methods not using instance variables static User equals method instead of equality operator Etc.
  • 18. AppPerfect Java Code Testa screen shot