Chapter 8 - Code conventions and design Flashcards

1
Q

this is 80 characters

A

what is the
maximum line length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

these are used where more detail is needed to explain the code

A

describe how
Multi line comments are used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

describe a
String Literal (“”)

A

A string created using this method is created in a pool of previously created strings in the Java runtime system. If the string already exists in the pool, the reference is used, otherwise, a new string is created and added to the pool. This process is known as interning and saves memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

this should contain the name of its superclass

A

what should a
subclass contain within their name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is
**responsibility-driven **
design in programming?

A

this is the principle that a class should be responsible for manipulating its own data.

This helps to maintain high cohesion by keeping the responsibilities of a class focused and related to one another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

A string created using this method is created in a pool of previously created strings in the Java runtime system. If the string already exists in the pool, the reference is used, otherwise, a new string is created and added to the pool. This process is known as interning and saves memory.

A

describe a
String Literal (“”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

this is used in a switch statement to prevent the flow of execution from “falling through” to the next case. This is usually desired to prevent unintended consequences.

A

What is the
purpose of a break statement in a switch statement?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

these should be named as:
1. a noun (a word that refers to a person, place, thing, event, substance, or quality)
2. be singular (Animal not Animals)

A

in 2 points
how should classes be named

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
A

ignore

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is
cohesion
in programming?

A

this is a measure of how strongly related and focused the responsibilities of a class or method are.

it is desirable that this is kept high, as it means that a class or method completes a specific task and is not carrying out too many subtasks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

describe
Comparing String Literals with “==”

A

When comparing these using the “==” operator, they will compare as equal because the Java runtime system will return the reference to the string in the pool of previously created strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

describe why we should be
Comparing Strings with “equals()”

A

To determine if two string objects contain the same characters, this method should be used instead of “==”.

This method compares the state of the objects, not just their identity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the
purpose of a default case in a switch statement?

A

this in a switch statement is used as a catch-all case and is usually included to handle unexpected input or signal an error.

note:
this should be included with all switch statements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

describe
Comparing Explicitly Created Strings with “==”

A

When comparing these using the “==” operator, they may compare as not equal even though they contain the same characters. This is because “==” compares only object identity, not state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

these are usually:
1. declared and initialised at the same time
2. at the start of a method or at the start of a block of code

A

when are
local variables initialised

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

these may be reused through inheritance and composition.
1. Inheritance allows a subclass to reuse the elements of its superclass
2. composition allows a class to have a field of a different class type to form a relationship. (may also be known as a “has a”)

A

What are the two ways
classes may be reused in object-oriented design?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Data hiding and encapsulation are techniques to mitigate this.

  1. Data hiding - The use ofaccess modifiersto restrict access tofieldsfrom outside of a class.
    2.** Encapsulation **- The packaging together in an object of data and theoperationsthat can be applied to that data, together with the use ofdata hidingto restrict access toimplementation details.

They ensure that another component of the class cannot be coupled to the implementation of a class, allowing it to be freely modified as long as the public interface remains unchanged.

A

What are some methods to
mitigate coupling?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is
**change localisation **
in programming?

A

this is the principle of restricting the amount of code that needs to be changed when maintenance or extension of the source code is required.

The goal is to make changes in a single place for better maintainability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

When is a
**refactor **
necessary in programming?

A

this is necessary when a class or method begins carrying out too many subtasks and is no longer completing a specific task.

This is known as low cohesion, and refactoring can improve cohesion by dividing the responsibilities into smaller, more focused components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

some points on these are:
1. used to describe fields
2. used to describe local variables
3. used to describe how single line of code works

A

describe in 3 points how
Single line comments (//) are used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

To determine if two string objects contain the same characters, this method should be used instead of “==”.

This method compares the state of the objects, not just their identity.

A

describe why we should be
Comparing Strings with “equals()”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

within a class this goes:
1. package statement (not used in M250)
2. importstatements
3. comments about the class
4. fields class (staticorstatic final) variables > instance variables
5. constructors
6. getter and setter methods, in the same order as the corresponding fields are declared
7. other methods.

A

in a java source file what is the
order of declaration within a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

if we have an expression such as this then we should
1. break it on to a new line
2. with the operator as the first character

A

if we have a long
expression exceeding 80 charcters
how should we deal with it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

how should
Class (static) variables and methods be accessed

A

these should always be accessed via the class name they belong to

note:
although it is possible to achieve the same using an instance of the class or using the this keyword. this should be avoided as it is misleading and does not explicitly state that we are accessing a class element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are the two ways
classes may be reused in object-oriented design?

A

these may be reused through inheritance and composition.
1. Inheritance allows a subclass to reuse the elements of its superclass
2. composition allows a class to have a field of a different class type to form a relationship. (may also be known as a “has a”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

these should be:
1. Initialised excusively in the constructor
2. The exception is class (static) and class constants (final static) which will be initialised with declaration

A

when are
Fields (class and instance variables) initialised

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is
Coupling?

A

this refers to the interdependence of components in a system, meaning that they have a dependency on each other to function.

It’s generally desirable to minimize this, but it is often unavoidable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

what is the
maximum line length

A

this is 80 characters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What are 4 examples of
coupling?

A

this can occur when
1. composition - objects of class P have an instance variable that references an object of class Q
2.** inheritance** - when P is a subclass of Q
3. method invocation coupling - when an object of class P calls a method in an object of class Q
4. object reference coupling - when P has a method that receives or returns an object of class Q

30
Q

these are methods that provide functionality used by two or more methods. By using these, we can reduce duplicate code and improve code reuse.

A

What are
**helper methods **
in programming?

31
Q

in 2 points
how should classes be named

A

these should be named as:
1. a noun (a word that refers to a person, place, thing, event, substance, or quality)
2. be singular (Animal not Animals)

32
Q

What are some methods to
mitigate coupling?

A

Data hiding and encapsulation are techniques to mitigate this.

  1. Data hiding - The use ofaccess modifiersto restrict access tofieldsfrom outside of a class.
    2.** Encapsulation **- The packaging together in an object of data and theoperationsthat can be applied to that data, together with the use ofdata hidingto restrict access toimplementation details.

They ensure that another component of the class cannot be coupled to the implementation of a class, allowing it to be freely modified as long as the public interface remains unchanged.

33
Q

how should methods be named

A

these should be named as:
1. verbs (an action, state, or occurrence)

34
Q

A string can be explicitly created using the “new String()” method. This creates a new string object and is not stored in the pool of previously created strings.

A

describe
Explicitly Creating a New String

35
Q

decsribe
the principle of
code reuse
in object-oriented design?

A

this principle of object-oriented design is about designing and writing code in such a way that it can be used and reused in multiple places to improve efficiency, maintainability, and decrease the risk of logic errors.

36
Q

What is the general advice for
using break and continue statements in loops?

A

It is generally advisable to avoid using these two statements to control the flow of a loop as it can make the loop behavior unclear. Instead, alternative control structures such as if-else statements should be used.

37
Q

It is generally advisable to avoid using these two statements to control the flow of a loop as it can make the loop behavior unclear. Instead, alternative control structures such as if-else statements should be used.

A

What is the general advice for
using break and continue statements in loops?

38
Q

this refers to the interdependence of components in a system, meaning that they have a dependency on each other to function.

It’s generally desirable to minimize this, but it is often unavoidable.

A

What is
Coupling?

39
Q

this is necessary when a class or method begins carrying out too many subtasks and is no longer completing a specific task.

This is known as low cohesion, and refactoring can improve cohesion by dividing the responsibilities into smaller, more focused components.

A

When is a
**refactor **
necessary in programming?

40
Q

if we have a long
expression exceeding 80 charcters
how should we deal with it

A

if we have an expression such as this then we should
1. break it on to a new line
2. with the operator as the first character

41
Q

this is the principle that a class should be responsible for manipulating its own data.

This helps to maintain high cohesion by keeping the responsibilities of a class focused and related to one another.

A

What is
**responsibility-driven **
design in programming?

42
Q

these should always be accessed via the class name they belong to

note:
although it is possible to achieve the same using an instance of the class or using the this keyword. this should be avoided as it is misleading and does not explicitly state that we are accessing a class element

A

how should
Class (static) variables and methods be accessed

43
Q

What is the importance of
** Data hiding and Encapsulation **
in reducing coupling?

A

these allow the user to know only what a class does (public interface), not how it does it (implementation), ensuring that the implementation of a class cannot be coupled to another component.

This means that the implementation can be freely modified as long as the public interface remains unchanged.

44
Q

this is the process of storing string values by the Java runtime system so that they can be reused efficiently.

A

describe
Interning

45
Q

When comparing these using the “==” operator, they may compare as not equal even though they contain the same characters. This is because “==” compares only object identity, not state.

A

describe
Comparing Explicitly Created Strings with “==”

46
Q

What are
**helper methods **
in programming?

A

these are methods that provide functionality used by two or more methods. By using these, we can reduce duplicate code and improve code reuse.

47
Q

the reason for this is:
methods could be overridden by subclasses if this happens then unexpected behaviour could occur

in general a best practice is to only initialise fields within a constructor and avoid method calls

A

why should method calls within the constructor be used with caution

48
Q

when are
local variables initialised

A

these are usually:
1. declared and initialised at the same time
2. at the start of a method or at the start of a block of code

49
Q

these allow the user to know only what a class does (public interface), not how it does it (implementation), ensuring that the implementation of a class cannot be coupled to another component.

This means that the implementation can be freely modified as long as the public interface remains unchanged.

A

What is the importance of
** Data hiding and Encapsulation **
in reducing coupling?

50
Q

What are
utility methods
in programming?

A

these are static methods that implement frequently used operations that are of general use to other classes.

They provide a way to reuse code across multiple classes.

51
A

ignore

52
Q

these are static methods that implement frequently used operations that are of general use to other classes.

They provide a way to reuse code across multiple classes.

A

What are
utility methods
in programming?

53
Q

describe
Explicitly Creating a New String

A

A string can be explicitly created using the “new String()” method. This creates a new string object and is not stored in the pool of previously created strings.

54
Q

When comparing these using the “==” operator, they will compare as equal because the Java runtime system will return the reference to the string in the pool of previously created strings.

A

describe
Comparing String Literals with “==”

55
Q

describe how
Multi line comments are used

A

these are used where more detail is needed to explain the code

56
Q

these should be named as:
1. verbs (an action, state, or occurrence)

A

how should methods be named

57
Q

this is the principle of restricting the amount of code that needs to be changed when maintenance or extension of the source code is required.

The goal is to make changes in a single place for better maintainability.

A

What is
**change localisation **
in programming?

58
Q

why should method calls within the constructor be used with caution

A

the reason for this is:
methods could be overridden by subclasses if this happens then unexpected behaviour could occur

in general a best practice is to only initialise fields within a constructor and avoid method calls

59
Q

when is it acceptable to use short name variables and what are 3 examples of this in use

A

this naming style is acceptable only with local variables that will shortly go out of scope. some examples:
1. i, j - usually used for integers
2. c, ch - usually used for characters
3. f - usually used for floating points

60
Q

describe
Interning

A

this is the process of storing string values by the Java runtime system so that they can be reused efficiently.

61
Q

in a java source file what is the
order of declaration within a class

A

within a class this goes:
1. package statement (not used in M250)
2. importstatements
3. comments about the class
4. fields class (staticorstatic final) variables > instance variables
5. constructors
6. getter and setter methods, in the same order as the corresponding fields are declared
7. other methods.

62
Q

What is the
purpose of a break statement in a switch statement?

A

this is used in a switch statement to prevent the flow of execution from “falling through” to the next case. This is usually desired to prevent unintended consequences.

63
Q

when are
Fields (class and instance variables) initialised

A

these should be:
1. Initialised excusively in the constructor
2. The exception is class (static) and class constants (final static) which will be initialised with declaration

64
Q

what should a
subclass contain within their name

A

this should contain the name of its superclass

65
Q

this in a switch statement is used as a catch-all case and is usually included to handle unexpected input or signal an error.

note:
this should be included with all switch statements

A

What is the
purpose of a default case in a switch statement?

66
Q

this naming style is acceptable only with local variables that will shortly go out of scope. some examples:
1. i, j - usually used for integers
2. c, ch - usually used for characters
3. f - usually used for floating points

A

when is it acceptable to use short name variables and what are 3 examples of this in use

67
Q

this can occur when
1. composition - objects of class P have an instance variable that references an object of class Q
2.** inheritance** - when P is a subclass of Q
3. method invocation coupling - when an object of class P calls a method in an object of class Q
4. object reference coupling - when P has a method that receives or returns an object of class Q

A

What are 4 examples of
coupling?

68
Q

describe in 3 points how
Single line comments (//) are used

A

some points on these are:
1. used to describe fields
2. used to describe local variables
3. used to describe how single line of code works

69
Q

this principle of object-oriented design is about designing and writing code in such a way that it can be used and reused in multiple places to improve efficiency, maintainability, and decrease the risk of logic errors.

A

decsribe
the principle of
code reuse
in object-oriented design?

70
Q

this is a measure of how strongly related and focused the responsibilities of a class or method are.

it is desirable that this is kept high, as it means that a class or method completes a specific task and is not carrying out too many subtasks.

A

What is
cohesion
in programming?

71
Q

A string can be explicitly created using the “new String()” method. This creates a new string object and is not stored in the pool of previously created strings.

A

describe
Explicitly Creating a New String

72
Q

describe
Explicitly Creating a New String

A

A string can be explicitly created using the “new String()” method. This creates a new string object and is not stored in the pool of previously created strings.