Chapter 8 - Code conventions and design Flashcards
this is 80 characters
what is the
maximum line length
these are used where more detail is needed to explain the code
describe how
Multi line comments are used
describe a
String Literal (“”)
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.
this should contain the name of its superclass
what should a
subclass contain within their name
What is
**responsibility-driven **
design in programming?
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 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.
describe a
String Literal (“”)
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.
What is the
purpose of a break statement in a switch statement?
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)
in 2 points
how should classes be named
What is
cohesion
in programming?
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.
describe
Comparing String Literals with “==”
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 why we should be
Comparing Strings with “equals()”
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.
What is the
purpose of a default case in a switch statement?
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
describe
Comparing Explicitly Created Strings with “==”
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.
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
when are
local variables initialised
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”)
What are the two ways
classes may be reused in object-oriented design?
Data hiding and encapsulation are techniques to mitigate this.
-
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.
What are some methods to
mitigate coupling?
What is
**change localisation **
in programming?
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.
When is a
**refactor **
necessary in programming?
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.
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
describe in 3 points how
Single line comments (//) are used
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.
describe why we should be
Comparing Strings with “equals()”
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.
in a java source file what is the
order of declaration within a class
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
if we have a long
expression exceeding 80 charcters
how should we deal with it
how should
Class (static) variables and methods be accessed
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
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”)
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
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.
what is the
maximum line length
this is 80 characters
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
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?
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)
What are some methods to
mitigate coupling?
Data hiding and encapsulation are techniques to mitigate this.
-
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.
how should methods be named
these should be named as:
1. verbs (an action, state, or occurrence)
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
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.
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.
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?
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?
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?
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
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?
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
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.
this is the process of storing string values by the Java runtime system so that they can be reused efficiently.
describe
Interning
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 “==”
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.
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
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
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?
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.
ignore
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?
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.
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 “==”
describe how
Multi line comments are used
these are used where more detail is needed to explain the code
these should be named as:
1. verbs (an action, state, or occurrence)
how should methods be named
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?
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
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
describe
Interning
this is the process of storing string values by the Java runtime system so that they can be reused efficiently.
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. 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.
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.
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
what should a
subclass contain within their name
this should contain the name of its superclass
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?
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
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?
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
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?
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?
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
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.