Best Practices for Clean Code: Conditional Statements, Rules, Layout, Comments, Slides of Java Programming

Guidelines for writing clean code, focusing on the use of conditionals, miscellaneous rules, and layout and comments. It emphasizes the importance of avoiding complex conditional expressions, putting the nominal case in the if-part, and executable statements in conditionals. It also suggests the use of named constants, proper indentation, and javadoc comments.

Typology: Slides

2011/2012

Uploaded on 08/09/2012

dhanyaa
dhanyaa 🇮🇳

4.7

(3)

60 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
9/17/2010
43
Conditionals
`Complex conditional expressions must be avoided.
Introduce temporary boolean variables instead
`bool isFinished = (elementNo < 0) ||
(elementNo > maxElement);
bool isRepeatedEntry = elementNo == lastElement;
if (isFinished || isRepeatedEntry) { : }
`// NOT: if ((elementNo < 0) || (elementNo >
maxElement)|| elementNo ==
lastElement) { : }
Fall 2010cs42043
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Best Practices for Clean Code: Conditional Statements, Rules, Layout, Comments and more Slides Java Programming in PDF only on Docsity!

Conditionals

` Complex conditional expressions must be avoided.

Introduce temporary boolean variables instead

` bool isFinished = (elementNo < 0) ||

(elementNo > maxElement);

bool isRepeatedEntry = elementNo == lastElement;

if (isFinished || isRepeatedEntry) { : }

` // NOT: if ((elementNo < 0) || (elementNo >

maxElement)|| elementNo ==

lastElement) { : }

43 cs420 Fall 2010

docsity.com

Conditionals

` The nominal case should be put in the if -part and the

exception in the else -part of an if statement

` boolean isOk = readFile(fileName);

if (isOk) {

} else {

` The conditional should be put on a separate line

` if (isDone) // NOT: if (isDone) doCleanup();

doCleanup();

44 cs420 Fall 2010

docsity.com

Miscellaneous

` The use of magic numbers in the code should be

avoided. Numbers other than 0 and 1 can be

considered declared as named constants instead

` private static final int TEAM_SIZE = 11;

Player[] players = new Player[TEAM_SIZE];

// NOT: Player[] players = new Player[11];

46 cs420 Fall 2010

docsity.com

Miscellaneous

` Floating point constants should always be written

with decimal point and at least one decimal

` double total = 0.0;

// NOT: double total = 0;

double speed = 3.0e8;

// NOT: double speed = 3e8;// p ;

double sum;

sum = (a + b) * 10.0;

47 cs420 Fall 2010

docsity.com

Layout and Comments

` Basic indentation should be 2.

` for (i = 0; i < nElements; i++)

a[i] = 0;

` while (!done) {

doSomething();

done = moreToDo();

` while (!done)

doSomething();

done = moreToDo();

49 cs420 Fall 2010

docsity.com

Layout and Comments

` The class and interface declarations should have the

following form

` class Rectangle extends Shape

implements Cloneable, Serializable

` Method definitions should have the following form

` public void someMethod() throws SomeException

50 cs420 Fall 2010

docsity.com

Layout & Comments

` while

` do while

` switch

` try catch

` singleg if else

52 cs420 Fall 2010

docsity.com

White Spaces

` Operators should be surrounded by a space character.

`` JJava reserved words should be followed by a white d d h ld b f ll d b hi

space.

` Commas should be followed by a white space.

` Colons should be surrounded by white space.

`` Semicolons in for statements should be followed by aSemicolons in for statements should be followed by a

space character.

` Method names can be followed by a white space when it

is followed by another name

` // Create a new identity matrix Matrix4x4 matrix = new Matrix4x4();

53 cs420 Fall 2010

// Precompute angles for efficiency double cosAngle = Math.cos(angle); double sinAngle = Math.sin(angle);

docsity.com

Comments

` Tricky code should not be commented but rewritten

` All comments should be written in English

55 cs420 Fall 2010

docsity.com

Comments

` Javadoc comments should have the following form

`` //**

  • Return lateral location of the specified position.
  • If the position is unset, NaN is returned.
  • @param x X coordinate of position.
  • @param y Y coordinate of position.
  • @param zone Zone of position.
  • @return Lateral location.
  • @throws IllegalArgumentException If zone is <= 0. */ public double computeLocation(double x, double y, int zone) throws IllegalArgumentException { ... }

56 cs420 Fall 2010

docsity.com

Comments

` Use // for all non-JavaDoc comments, including

multi-line comments.

` // Comment spanning // more than one line

` Comments should be indented relative to their

position in the codep

` while (true) { // NOT: while (true) { // Do something // Do something something(); something();

} }

58 cs420 Fall 2010

docsity.com

Comments

` All public classes and public and protected functions

within public classes should be documented using

the Java documentation (javadoc) conventions.

59 cs420 Fall 2010

docsity.com

Assignment 01

` Write a file sharing application.

` User can send zip file (upto 50 MB) from one machine to another

` User can select any of two protocols (UDP or TCP). i.e. implement both of

these

` Proper desktop swing Interface for file selection

` Create two folders “incoming” & “outgoing”

` File to be sent must first be copied to outgoing folder of sender

` File received must be placed in incoming folderp g

` Bonus marks

User can select more then one file or folder, application first create a Zip file & then unzip the files on receiver side Progress bar to show the progress of upload and download

` Test Case

` Successfully unzip the file on user side.

61 cs420 Fall 2010

y

` Read Creational Patterns

` Factory Pattern, Abstract Factory Pattern

` Singlton Pattern

docsity.com

Questions?

That’s all for today!

docsity.com