0% found this document useful (0 votes)
2 views5 pages

Core JAVA: Why Bother? It's Arbitrary!

java

Uploaded by

shamagondal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Core JAVA: Why Bother? It's Arbitrary!

java

Uploaded by

shamagondal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Core JAVA

nFundamental Concepts
nBootstrapping
nBasic Language Syntax
nCommon Caveats
nCoding Conventions

Adapted with permission from JAVA CODE CONVENTIONS.


Copyright 1995-1999 Sun Microsystems, Inc. All rights
reserved

Why bother? Its arbitrary!


n 80% of the lifetime cost of a piece of software
goes to maintenance.
n Hardly any software is maintained for its whole
life by the original author.
n Code conventions improve the readability of the
software, allowing engineers to understand new
code easily.
n If you publish your source code, you need to
make sure it is as well packaged and clean as
any other product you create.

Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited

49
Comments
n We have already talked about this
n JAVADOC comments at the beginning of
every class, method and field
n Inline comments every other line to
describe what the following line of code
does

Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited

Line Length
n No line of code should be > 80 characters
in length
n Line breaks should make sense

longName1 = longName2 * (longName3 + longName4 - longName5)


+ 4 * longname6; // PREFER

longName1 = longName2 * (longName3 + longName4


- longName5) + 4 * longname6; // AVOID

Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited

50
Variables
n Initialize all variables all of the time
n Only declare one variable per line
n For local variables, use an inline
comment immediately after the variable
declaration to describe what the variable
is for
n For fields, use a JAVADOC comment

Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited

Indentation
n All open curly braces imply that the next
line should be indented
n Indentation should be uniform across all
files
n Large indentations are a bad idea
because you run out of room to nest
blocks of code

Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited

51
Parentheses
n Be explicit everywhere
n Order of operations applies, but you
should be explicit to make sure that
anyone reading your code can easily
understand what is going on

Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited

Identifiers
n Class names should start with a capital
letter and have an additional capital letter
for each word in the noun phrase
(MyClassName)
n Methods and Variables names do not
have a leading capital letter (myVar)
n Constants all all caps with _ breaking the
words (MY_CONSTANT)

Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited

52
Clean code is good code
n The vast majority of software defects can
be avoided through a combination of:
n Thorough paper designs
n Writing clean, standardized code
n Proper unit testing while coding

n Meticulous documentation

Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited

53

Common questions

Powered by AI

Meticulous documentation can reduce software maintenance costs by ensuring that future developers can quickly understand the purpose, functionality, and dependencies of each part of the code, thus speeding up the process of making changes or fixing issues. According to Java conventions, documentation should include JAVADOC comments at the beginning of every class, method, and field, accompanied by inline comments describing individual lines or blocks of code to provide clarity .

Indentation rules contribute to writing clean and maintainable code by visually representing the hierarchical structure of the code, making it easier to follow the flow of program logic. The document specifies that all open curly braces imply that the next line should be indented and that indentation should be uniform across all files. Large indentations are discouraged to prevent running out of room to nest code blocks .

Adhering to code conventions in software maintenance improves the readability of the software, enabling engineers to easily understand new code. Since 80% of the lifetime cost of a piece of software is associated with maintenance, clean and well-structured code ensures that it can be more efficiently maintained by programmers who were not the original authors of the code .

The recommended practice for naming Java identifiers is to use capital letters for class names with additional capital letters for each word (e.g., MyClassName), not use a leading capital letter for methods and variable names (e.g., myVar), and use all caps with underscores separating words for constants (e.g., MY_CONSTANT). This convention enhances code quality by providing a clear and consistent way to differentiate types of identifiers, making the code more readable and maintainable .

Initializing all variables at the time of declaration ensures that the variables are in a known, stable state before they are used. This practice reduces the likelihood of runtime errors caused by uninitialized variables and enhances code robustness. Additionally, it makes the code easier to read and maintain by clearly showing the intended starting value of each variable immediately .

Not using parentheses explicitly in Java code can lead to misinterpretation of the intended order of operations, since default precedence rules might not be immediately clear to someone reading the code. This affects code readability by making it harder for others to quickly understand what the code is supposed to do, especially for complex expressions. Explicit parentheses make the intended logic clear and unambiguous, reducing the potential for errors during maintenance .

Thorough paper designs help minimize software defects by allowing developers to outline and plan their code structure, data flow, and algorithms before actual coding begins. This practice is recommended because it provides a clear blueprint for the intended software functionality, identifies potential issues early in the development process, and aligns the development team on objectives, reducing the likelihood of defects arising from miscommunication or poor planning .

Declaring only one variable per line is recommended because it enhances code clarity by allowing each variable declaration to be accompanied by an inline comment explaining its purpose. This practice helps developers understand the roles of individual variables at a glance, reduces confusion that may arise from multiple declarations on a single line, and simplifies the process of adding or removing variables without affecting others .

Writing clean, standardized code prevents software defects by ensuring that the code is consistent, understandable, and easy to debug. Standardized code adheres to established conventions, such as proper naming, indentation, and formatting rules, reducing the likelihood of introducing errors due to misunderstanding or overlooked logic. According to the document, following coding standards such as consistent naming conventions, clear indentation, and thorough documentation are critical to minimizing defects .

Standardizing line length is important because it contributes to code readability and maintainability by ensuring consistency across various environments and editors. The recommended maximum line length in Java code conventions is 80 characters, which helps avoid horizontal scrolling and makes the code easier to read and understand .

You might also like