SlideShare a Scribd company logo
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

More Related Content

What's hot

Generics in java
Generics in javaGenerics in java
Generics in java
suraj pandey
 
TypeScript - An Introduction
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An Introduction
NexThoughts Technologies
 
Java constructors
Java constructorsJava constructors
Java constructors
QUONTRASOLUTIONS
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
Nilimesh Halder
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programming
Riccardo Cardin
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
Lovely Professional University
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
Mudit Gupta
 
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
Jérôme Rocheteau
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Testing In Django
Testing In DjangoTesting In Django
Testing In Django
Daniel Greenfeld
 
C# conventions & good practices
C# conventions & good practicesC# conventions & good practices
C# conventions & good practices
Tan Tran
 
angular fundamentals.pdf
angular fundamentals.pdfangular fundamentals.pdf
angular fundamentals.pdf
NuttavutThongjor1
 
Java arrays
Java arraysJava arrays
Java arrays
Kuppusamy P
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 

What's hot (20)

Generics in java
Generics in javaGenerics in java
Generics in java
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Coding standard
Coding standardCoding standard
Coding standard
 
TypeScript - An Introduction
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An Introduction
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
 
Java Strings
Java StringsJava Strings
Java Strings
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programming
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
 
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Testing In Django
Testing In DjangoTesting In Django
Testing In Django
 
C# conventions & good practices
C# conventions & good practicesC# conventions & good practices
C# conventions & good practices
 
angular fundamentals.pdf
angular fundamentals.pdfangular fundamentals.pdf
angular fundamentals.pdf
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Java arrays
Java arraysJava arrays
Java arrays
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 

Similar to 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 World
David 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 World
David 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 dart
Imran Qasim
 
Presentation c++
Presentation c++Presentation c++
Presentation c++
JosephAlex21
 
GTU Guidelines for Project on JAVA
GTU Guidelines for Project on JAVAGTU Guidelines for Project on JAVA
GTU Guidelines for Project on JAVA
TOPS Technologies
 
Programming style guideline very good
Programming style guideline very goodProgramming style guideline very good
Programming style guideline very good
Dang 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 & HTML
eSparkBiz
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perl
megakott
 

Similar to 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...
 
testing add
testing addtesting add
testing add
 
latest slide
latest slidelatest slide
latest slide
 
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
 

Recently uploaded

Cracking the Workplace Discipline Code Main.pptx
Cracking the Workplace Discipline Code Main.pptxCracking the Workplace Discipline Code Main.pptx
Cracking the Workplace Discipline Code Main.pptx
Workforce Group
 
CADAVER AS OUR FIRST TEACHER anatomt in your.pptx
CADAVER AS OUR FIRST TEACHER anatomt in your.pptxCADAVER AS OUR FIRST TEACHER anatomt in your.pptx
CADAVER AS OUR FIRST TEACHER anatomt in your.pptx
fakeloginn69
 
Company Valuation webinar series - Tuesday, 4 June 2024
Company Valuation webinar series - Tuesday, 4 June 2024Company Valuation webinar series - Tuesday, 4 June 2024
Company Valuation webinar series - Tuesday, 4 June 2024
FelixPerez547899
 
-- June 2024 is National Volunteer Month --
-- June 2024 is National Volunteer Month ---- June 2024 is National Volunteer Month --
-- June 2024 is National Volunteer Month --
NZSG
 
VAT Registration Outlined In UAE: Benefits and Requirements
VAT Registration Outlined In UAE: Benefits and RequirementsVAT Registration Outlined In UAE: Benefits and Requirements
VAT Registration Outlined In UAE: Benefits and Requirements
uae taxgpt
 
5 Things You Need To Know Before Hiring a Videographer
5 Things You Need To Know Before Hiring a Videographer5 Things You Need To Know Before Hiring a Videographer
5 Things You Need To Know Before Hiring a Videographer
ofm712785
 
Search Disrupted Google’s Leaked Documents Rock the SEO World.pdf
Search Disrupted Google’s Leaked Documents Rock the SEO World.pdfSearch Disrupted Google’s Leaked Documents Rock the SEO World.pdf
Search Disrupted Google’s Leaked Documents Rock the SEO World.pdf
Arihant Webtech Pvt. Ltd
 
The-McKinsey-7S-Framework. strategic management
The-McKinsey-7S-Framework. strategic managementThe-McKinsey-7S-Framework. strategic management
The-McKinsey-7S-Framework. strategic management
Bojamma2
 
RMD24 | Debunking the non-endemic revenue myth Marvin Vacquier Droop | First ...
RMD24 | Debunking the non-endemic revenue myth Marvin Vacquier Droop | First ...RMD24 | Debunking the non-endemic revenue myth Marvin Vacquier Droop | First ...
RMD24 | Debunking the non-endemic revenue myth Marvin Vacquier Droop | First ...
BBPMedia1
 
Set off and carry forward of losses and assessment of individuals.pptx
Set off and carry forward of losses and assessment of individuals.pptxSet off and carry forward of losses and assessment of individuals.pptx
Set off and carry forward of losses and assessment of individuals.pptx
HARSHITHV26
 
Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...
Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...
Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...
Lviv Startup Club
 
The Parable of the Pipeline a book every new businessman or business student ...
The Parable of the Pipeline a book every new businessman or business student ...The Parable of the Pipeline a book every new businessman or business student ...
The Parable of the Pipeline a book every new businessman or business student ...
awaisafdar
 
20240425_ TJ Communications Credentials_compressed.pdf
20240425_ TJ Communications Credentials_compressed.pdf20240425_ TJ Communications Credentials_compressed.pdf
20240425_ TJ Communications Credentials_compressed.pdf
tjcomstrang
 
What is the TDS Return Filing Due Date for FY 2024-25.pdf
What is the TDS Return Filing Due Date for FY 2024-25.pdfWhat is the TDS Return Filing Due Date for FY 2024-25.pdf
What is the TDS Return Filing Due Date for FY 2024-25.pdf
seoforlegalpillers
 
falcon-invoice-discounting-a-premier-platform-for-investors-in-india
falcon-invoice-discounting-a-premier-platform-for-investors-in-indiafalcon-invoice-discounting-a-premier-platform-for-investors-in-india
falcon-invoice-discounting-a-premier-platform-for-investors-in-india
Falcon Invoice Discounting
 
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.docBài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
daothibichhang1
 
Premium MEAN Stack Development Solutions for Modern Businesses
Premium MEAN Stack Development Solutions for Modern BusinessesPremium MEAN Stack Development Solutions for Modern Businesses
Premium MEAN Stack Development Solutions for Modern Businesses
SynapseIndia
 
Project File Report BBA 6th semester.pdf
Project File Report BBA 6th semester.pdfProject File Report BBA 6th semester.pdf
Project File Report BBA 6th semester.pdf
RajPriye
 
Maksym Vyshnivetskyi: PMO Quality Management (UA)
Maksym Vyshnivetskyi: PMO Quality Management (UA)Maksym Vyshnivetskyi: PMO Quality Management (UA)
Maksym Vyshnivetskyi: PMO Quality Management (UA)
Lviv Startup Club
 
ikea_woodgreen_petscharity_cat-alogue_digital.pdf
ikea_woodgreen_petscharity_cat-alogue_digital.pdfikea_woodgreen_petscharity_cat-alogue_digital.pdf
ikea_woodgreen_petscharity_cat-alogue_digital.pdf
agatadrynko
 

Recently uploaded (20)

Cracking the Workplace Discipline Code Main.pptx
Cracking the Workplace Discipline Code Main.pptxCracking the Workplace Discipline Code Main.pptx
Cracking the Workplace Discipline Code Main.pptx
 
CADAVER AS OUR FIRST TEACHER anatomt in your.pptx
CADAVER AS OUR FIRST TEACHER anatomt in your.pptxCADAVER AS OUR FIRST TEACHER anatomt in your.pptx
CADAVER AS OUR FIRST TEACHER anatomt in your.pptx
 
Company Valuation webinar series - Tuesday, 4 June 2024
Company Valuation webinar series - Tuesday, 4 June 2024Company Valuation webinar series - Tuesday, 4 June 2024
Company Valuation webinar series - Tuesday, 4 June 2024
 
-- June 2024 is National Volunteer Month --
-- June 2024 is National Volunteer Month ---- June 2024 is National Volunteer Month --
-- June 2024 is National Volunteer Month --
 
VAT Registration Outlined In UAE: Benefits and Requirements
VAT Registration Outlined In UAE: Benefits and RequirementsVAT Registration Outlined In UAE: Benefits and Requirements
VAT Registration Outlined In UAE: Benefits and Requirements
 
5 Things You Need To Know Before Hiring a Videographer
5 Things You Need To Know Before Hiring a Videographer5 Things You Need To Know Before Hiring a Videographer
5 Things You Need To Know Before Hiring a Videographer
 
Search Disrupted Google’s Leaked Documents Rock the SEO World.pdf
Search Disrupted Google’s Leaked Documents Rock the SEO World.pdfSearch Disrupted Google’s Leaked Documents Rock the SEO World.pdf
Search Disrupted Google’s Leaked Documents Rock the SEO World.pdf
 
The-McKinsey-7S-Framework. strategic management
The-McKinsey-7S-Framework. strategic managementThe-McKinsey-7S-Framework. strategic management
The-McKinsey-7S-Framework. strategic management
 
RMD24 | Debunking the non-endemic revenue myth Marvin Vacquier Droop | First ...
RMD24 | Debunking the non-endemic revenue myth Marvin Vacquier Droop | First ...RMD24 | Debunking the non-endemic revenue myth Marvin Vacquier Droop | First ...
RMD24 | Debunking the non-endemic revenue myth Marvin Vacquier Droop | First ...
 
Set off and carry forward of losses and assessment of individuals.pptx
Set off and carry forward of losses and assessment of individuals.pptxSet off and carry forward of losses and assessment of individuals.pptx
Set off and carry forward of losses and assessment of individuals.pptx
 
Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...
Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...
Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...
 
The Parable of the Pipeline a book every new businessman or business student ...
The Parable of the Pipeline a book every new businessman or business student ...The Parable of the Pipeline a book every new businessman or business student ...
The Parable of the Pipeline a book every new businessman or business student ...
 
20240425_ TJ Communications Credentials_compressed.pdf
20240425_ TJ Communications Credentials_compressed.pdf20240425_ TJ Communications Credentials_compressed.pdf
20240425_ TJ Communications Credentials_compressed.pdf
 
What is the TDS Return Filing Due Date for FY 2024-25.pdf
What is the TDS Return Filing Due Date for FY 2024-25.pdfWhat is the TDS Return Filing Due Date for FY 2024-25.pdf
What is the TDS Return Filing Due Date for FY 2024-25.pdf
 
falcon-invoice-discounting-a-premier-platform-for-investors-in-india
falcon-invoice-discounting-a-premier-platform-for-investors-in-indiafalcon-invoice-discounting-a-premier-platform-for-investors-in-india
falcon-invoice-discounting-a-premier-platform-for-investors-in-india
 
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.docBài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
 
Premium MEAN Stack Development Solutions for Modern Businesses
Premium MEAN Stack Development Solutions for Modern BusinessesPremium MEAN Stack Development Solutions for Modern Businesses
Premium MEAN Stack Development Solutions for Modern Businesses
 
Project File Report BBA 6th semester.pdf
Project File Report BBA 6th semester.pdfProject File Report BBA 6th semester.pdf
Project File Report BBA 6th semester.pdf
 
Maksym Vyshnivetskyi: PMO Quality Management (UA)
Maksym Vyshnivetskyi: PMO Quality Management (UA)Maksym Vyshnivetskyi: PMO Quality Management (UA)
Maksym Vyshnivetskyi: PMO Quality Management (UA)
 
ikea_woodgreen_petscharity_cat-alogue_digital.pdf
ikea_woodgreen_petscharity_cat-alogue_digital.pdfikea_woodgreen_petscharity_cat-alogue_digital.pdf
ikea_woodgreen_petscharity_cat-alogue_digital.pdf
 

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