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