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
What are the two ways **classes may be reused in object-oriented design?**
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")
26
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
when are **Fields (class and instance variables) initialised**
27
What is **Coupling?**
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.
28
what is the **maximum line length**
this is 80 characters
29
What are 4 examples of **coupling?**
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
these are methods that provide functionality used by two or more methods. By using these, we can reduce duplicate code and improve code reuse.
What are **helper methods ** in programming?
31
in 2 points **how should classes be named**
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
What are some methods to **mitigate coupling?**
Data hiding and encapsulation are techniques to mitigate this. 1. **Data hiding** - The use of access modifiers to restrict access to fields from outside of a class. 2.** Encapsulation **- The packaging together in an object of data and the operations that can be applied to that data, together with the use of data hiding to restrict access to implementation 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
how should methods be named
these should be named as: 1. verbs (an action, state, or occurrence)
34
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.
describe **Explicitly Creating a New String**
35
decsribe the principle of **code reuse** in object-oriented design?
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
What is the general advice for **using break and continue statements in loops?**
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
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.
What is the general advice for **using break and continue statements in loops?**
38
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.
What is **Coupling?**
39
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.
When is a **refactor ** necessary in programming?
40
if we have a long **expression exceeding 80 charcters** how should we deal with it
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
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.
What is **responsibility-driven ** design in programming?
42
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 should **Class (static) variables and methods be accessed**
43
What is the importance of ** Data hiding and Encapsulation ** in reducing coupling?
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
this is the process of storing string values by the Java runtime system so that they can be reused efficiently.
describe **Interning**
45
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.
describe **Comparing Explicitly Created Strings with "=="**
46
What are **helper methods ** in programming?
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
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
why should method calls within the constructor be used with caution
48
when are **local variables initialised**
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
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.
What is the importance of ** Data hiding and Encapsulation ** in reducing coupling?
50
What are **utility methods** in programming?
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
[Chapter 8 glossary quiz](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014924)
ignore
52
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.
What are **utility methods** in programming?
53
describe **Explicitly Creating a New String**
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
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.
describe **Comparing String Literals with "=="**
55
describe how Multi line comments are used
these are used where more detail is needed to explain the code
56
these should be named as: 1. verbs (an action, state, or occurrence)
how should methods be named
57
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.
What is **change localisation ** in programming?
58
why should method calls within the constructor be used with caution
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
when is it acceptable to use short name variables and what are 3 examples of this in use
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
describe **Interning**
this is the process of storing string values by the Java runtime system so that they can be reused efficiently.
61
in a java source file what is the **order of declaration within a class**
within a class this goes: 1. package statement (not used in M250) 2. import statements 3. comments about the class 4. fields class (static or static 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
What is the **purpose of a break statement in a switch statement?**
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
when are **Fields (class and instance variables) initialised**
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
what should a **subclass contain within their name**
this should contain the name of its superclass
65
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
What is the **purpose of a default case in a switch statement?**
66
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
when is it acceptable to use short name variables and what are 3 examples of this in use
67
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
What are 4 examples of **coupling?**
68
describe in 3 points how **Single line comments (//) are used**
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
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.
decsribe the principle of **code reuse** in object-oriented design?
70
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.
What is **cohesion** in programming?
71
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.
describe **Explicitly Creating a New String**
72
describe **Explicitly Creating a New String**
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.