Лабораторная работа №1 (одномерные массивы) 27


НазваниеЛабораторная работа №1 (одномерные массивы) 27
страница4/13
ТипЛабораторная работа
1   2   3   4   5   6   7   8   9   ...   13

4 - Indentation


Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).

4.1 Line Length


Avoid lines longer than 80 characters, since they're not handled well by many terminals and tools.

Note: Examples for use in documentation should have a shorter line length-generally no more than 70 characters.

4.2 Wrapping Lines


When an expression will not fit on a single line, break it according to these general principles:

  • Break after a comma.

  • Break before an operator.

  • Prefer higher-level breaks to lower-level breaks.

  • Align the new line with the beginning of the expression at the same level on the previous line.

  • If the above rules lead to confusing code or to code that's squished up against the right margin, just indent 8 spaces instead.

Here are some examples of breaking method calls:

someMethod(longExpression1, longExpression2, longExpression3,

longExpression4, longExpression5);

var = someMethod1(longExpression1,

someMethod2(longExpression2,

longExpression3));

Following are two examples of breaking an arithmetic expression. The first is preferred, since the break occurs outside the parenthesized expression, which is at a higher level.

longName1 = longName2 * (longName3 + longName4 - longName5)

+ 4 * longname6; // PREFER
longName1 = longName2 * (longName3 + longName4

- longName5) + 4 * longname6; // AVOID

Following are two examples of indenting method declarations. The first is the conventional case. The second would shift the second and third lines to the far right if it used conventional indentation, so instead it indents only 8 spaces.
//CONVENTIONAL INDENTATION

someMethod(int anArg, Object anotherArg, String yetAnotherArg,

Object andStillAnother) {

...

}
//INDENT 8 SPACES TO AVOID VERY DEEP INDENTS

private static synchronized horkingLongMethodName(int anArg,

Object anotherArg, String yetAnotherArg,

Object andStillAnother) {

...

}

Line wrapping for if statements should generally use the 8-space rule, since conventional (4 space) indentation makes seeing the body difficult. For example:

//DON'T USE THIS INDENTATION

if ((condition1 && condition2)

|| (condition3 && condition4)

||!(condition5 && condition6)) { //BAD WRAPS

doSomethingAboutIt(); //MAKE THIS LINE EASY TO MISS

}
//USE THIS INDENTATION INSTEAD

if ((condition1 && condition2)

|| (condition3 && condition4)

||!(condition5 && condition6)) {

doSomethingAboutIt();

}
//OR USE THIS

if ((condition1 && condition2) || (condition3 && condition4)

||!(condition5 && condition6)) {

doSomethingAboutIt();

}

Here are three acceptable ways to format ternary expressions:

alpha = (aLongBooleanExpression) ? beta : gamma;
alpha = (aLongBooleanExpression) ? beta

: gamma;
alpha = (aLongBooleanExpression)

? beta

: gamma;

5 - Comments


Java programs can have two kinds of comments: implementation comments and documentation comments. Implementation comments are those found in C++, which are delimited by /*...*/, and //. Documentation comments (known as "doc comments") are Java-only, and are delimited by /**...*/. Doc comments can be extracted to HTML files using the javadoc tool.

Implementation comments are mean for commenting out code or for comments about the particular implementation. Doc comments are meant to describe the specification of the code, from an implementation-free perspective. to be read by developers who might not necessarily have the source code at hand.

Comments should be used to give overviews of code and provide additional information that is not readily available in the code itself. Comments should contain only information that is relevant to reading and understanding the program. For example, information about how the corresponding package is built or in what directory it resides should not be included as a comment.

Discussion of nontrivial or nonobvious design decisions is appropriate, but avoid duplicating information that is present in (and clear from) the code. It is too easy for redundant comments to get out of date. In general, avoid any comments that are likely to get out of date as the code evolves.

Note:The frequency of comments sometimes reflects poor quality of code. When you feel compelled to add a comment, consider rewriting the code to make it clearer.

Comments should not be enclosed in large boxes drawn with asterisks or other characters.
Comments should never include special characters such as form-feed and backspace.

5.1 Implementation Comment Formats


Programs can have four styles of implementation comments: block, single-line, trailing, and end-of-line.

5.1.1 Block Comments


Block comments are used to provide descriptions of files, methods, data structures and algorithms. Block comments may be used at the beginning of each file and before each method. They can also be used in other places, such as within methods. Block comments inside a function or method should be indented to the same level as the code they describe.

A block comment should be preceded by a blank line to set it apart from the rest of the code.

/*

* Here is a block comment.

*/

Block comments can start with /*-, which is recognized by indent(1) as the beginning of a block comment that should not be reformatted. Example:

/*-

* Here is a block comment with some very special

* formatting that I want indent(1) to ignore.

*

* one

* two

* three

*/



Note: If you don't use indent(1), you don't have to use /*- in your code or make any other concessions to the possibility that someone else might run indent(1) on your code.

See also "Documentation Comments" on page 9.

5.1.2 Single-Line Comments


Short comments can appear on a single line indented to the level of the code that follows. If a comment can't be written in a single line, it should follow the block comment format (see section 5.1.1). A single-line comment should be preceded by a blank line. Here's an example of a single-line comment in Java code (also see "Documentation Comments" on page 9):

if (condition) {
/* Handle the condition. */

...

}

5.1.3 Trailing Comments


Very short comments can appear on the same line as the code they describe, but should be shifted far enough to separate them from the statements. If more than one short comment appears in a chunk of code, they should all be indented to the same tab setting.

Here's an example of a trailing comment in Java code:

if (a == 2) {

return TRUE; /* special case */

} else {

return isPrime(a); /* works only for odd a */

}

5.1.4 End-Of-Line Comments


The // comment delimiter can comment out a complete line or only a partial line. It shouldn't be used on consecutive multiple lines for text comments; however, it can be used in consecutive multiple lines for commenting out sections of code. Examples of all three styles follow:

if (foo > 1) {
// Do a double-flip.

...

}

else {

return false; // Explain why here.

}

//if (bar > 1) {

//

// // Do a triple-flip.

// ...

//}

//else {

// return false;

//}

5.2 Documentation Comments


Note: See "Java Source File Example" on page 19 for examples of the comment formats described here.

For further details, see "How to Write Doc Comments for Javadoc" which includes information on the doc comment tags (@return, @param, @see):

http://java.sun.com/products/jdk/javadoc/writingdoccomments.html

For further details about doc comments and javadoc, see the javadoc home page at:

http://java.sun.com/products/jdk/javadoc/

Doc comments describe Java classes, interfaces, constructors, methods, and fields. Each doc comment is set inside the comment delimiters /**...*/, with one comment per class, interface, or member. This comment should appear just before the declaration:

/**

* The Example class provides ...

*/

public class Example { ...

Notice that top-level classes and interfaces are not indented, while their members are. The first line of doc comment (/**) for classes and interfaces is not indented; subsequent doc comment lines each have 1 space of indentation (to vertically align the asterisks). Members, including constructors, have 4 spaces for the first doc comment line and 5 spaces thereafter.

If you need to give information about a class, interface, variable, or method that isn't appropriate for documentation, use an implementation block comment (see section 5.1.1) or single-line (see section 5.1.2) comment immediately after the declaration. For example, details about the implementation of a class should go in in such an implementation block comment following the class statement, not in the class doc comment.

Doc comments should not be positioned inside a method or constructor definition block, because Java associates documentation comments with the first declaration after the comment.

1   2   3   4   5   6   7   8   9   ...   13

Похожие:

Лабораторная работа №1 (одномерные массивы) 27 iconУрок 22. Тема урока: Массивы в языке Паскаль. Одномерные массивы ( 1 час)
Цель урока: систематизация и развитие знаний по использованию массивов в программах на Паскале

Лабораторная работа №1 (одномерные массивы) 27 iconЛабораторная работа №8 распределенный udp сервер/ udp клиент 38 Лабораторная...
Федеральное государственное бюджетное образовательное учреждение высшего профессионального образования

Лабораторная работа №1 (одномерные массивы) 27 iconЛабораторная работа №1
Лабораторная работа Выполнение расчетов с использованием программирования в среде Visual Basic for Applications (vba). 8

Лабораторная работа №1 (одномерные массивы) 27 iconЛабораторная работа №1
Лабораторная работа №8. Структурирование таблицы с автоматическим подведением итогов

Лабораторная работа №1 (одномерные массивы) 27 iconЛабораторная работа №1. Изучение основ микроструктурного анализа...
...

Лабораторная работа №1 (одномерные массивы) 27 iconОдномерные и двумерные массивы (таблицы) Массив
Массив — это пронумерованная последовательность величин одинакового типа, обозначаемая одним именем. Элементы массива располагаются...

Лабораторная работа №1 (одномерные массивы) 27 iconОдномерные и двумерные массивы Раздел описания типов
В разделе описания типов пользователь может определять свои типы данных, присваивая каждому из них определенный идентификатор. Синтаксис...

Лабораторная работа №1 (одномерные массивы) 27 iconЛабораторная работа Создание и использование запросов (продолжение)....
Задания на экзамен выполняются студентом в компьютерном классе при наличии конспектов под руководством преподавателя

Лабораторная работа №1 (одномерные массивы) 27 iconЛекция 12 Двумерные массивы
Многомерные массивы задаются указанием каждого измерения в квадратных скобках, например, оператор

Лабораторная работа №1 (одномерные массивы) 27 iconДвумерные массивы
Массивы, положение элементов в которых описывается двумя индексами, называются двумерными. Их можно представить в виде прямоугольной...

Вы можете разместить ссылку на наш сайт:


Все бланки и формы на filling-form.ru




При копировании материала укажите ссылку © 2019
контакты
filling-form.ru

Поиск