Skip to content

Instantly share code, notes, and snippets.

@stolarczykt
stolarczykt / ContentCoupling.java
Created January 25, 2020 06:28
Content coupling example
Class<?> clazz = Image.class;
Object imageInstance = clazz.newInstance();
Field descriptionField = imageInstance
.getClass().getDeclaredField("description");
descriptionField.setAccessible(true);
descriptionField
.set(imageInstance, "Pen Pineapple Apple Pen");
@stolarczykt
stolarczykt / ControlCoupling.java
Created January 25, 2020 06:33
Example of control coupling
public void save(boolean validationRequired) {
if (validationRequired) {
validate();
store();
} else {
store();
}
}
@stolarczykt
stolarczykt / StampAndDataCoupling.java
Created January 25, 2020 06:36
Examples of stamp and data coupling
//Stamp coupling
public EmployeeAddress findAddressFor(EmployeeData employeeData) {
EmployeeAddress address = repository
.findByEmployeeId(employeeData.getId())
//method body
return address;
}
//Data coupling
@stolarczykt
stolarczykt / CoincidentalCohesion.java
Created January 27, 2020 08:26
Coincidental cohesion
public class CoincidentalCohesion {
public void log(Exception e) {
//method body
}
public void sendMessage(Recepient recepient) {
//method body
}
@stolarczykt
stolarczykt / MathUtils.java
Created January 27, 2020 08:31
Logical cohesion example
public class MathUtils {
public static double sin(double a) {
//method's body
return sin;
}
public static double cos(double a) {
//method's body
return sin;
@stolarczykt
stolarczykt / DocsInitializer.java
Created January 27, 2020 10:27
Temporal cohesion example
public class DocsInitializer {
public void createBaseDirectoryStructure() {
//creating required directory structure
}
public void cacheDefaults() {
//caching required defaults
}
@stolarczykt
stolarczykt / DocsRepository.java
Created January 27, 2020 10:32
Communicational cohesion example
public class DocsRepository {
public Doc findById(DocId id) {
//method's body
return doc;
}
public Doc save(Doc document) {
//method's body
return doc;
@stolarczykt
stolarczykt / PolishTaxPolicy.java
Last active January 27, 2020 11:23
Functional cohesion example
public class PolishTaxPolicy implements TaxPolicy {
public Money calculateTax(Money amount) {
//method body
return tax;
}
}
test 'change advertisement content' do
advertisement_id = SecureRandom.random_number
author_id = SecureRandom.random_number
new_content = "Random content: #{SecureRandom.hex}"
command_bus.(PublishAdvertisement.new(advertisement_id, author_id))
assert_events(
"Advertisement$#{advertisement_id}",
ContentHasChanged.new(data: {content: new_content})
) do
def assert_events(stream_name, *expected_events)
scope = Rails.configuration.event_store.read.stream(stream_name)
before = scope.last
yield
actual_events = before.nil? ? scope.to_a : scope.from(before.event_id).to_a
to_compare = ->(ev) { { type: ev.event_type, data: ev.data } }
assert_equal expected_events.map(&to_compare),
actual_events.map(&to_compare)
end