CpSc 461, Software Engineering Techniques

Coupling and Cohesion Exercise

For each of the following code snippets, identify the types of coupling or cohesion shown:

Code Snippet 1

void validate_checkout_request(input_form i)
{
	if (!valid_string(i.name)) {
		error_message("Invalid name");
	}

	if (!valid_string(i.book)) {
		error_message("Invalid book name");
	}

	if (!valid_month(i.date)) {
		error_message("Invalid month");
	}
}

int valid_month(date d)
{
	return	d.month >= 1 && d.month <= 12;
}

Code Snippet 2

void validate_checkout_request(input_form i)
{
	if (!valid_string(i)) {
		error_message("Invalid name");
	}

	if (!valid_string(i)) {
		error_message("Invalid book name");
	}

	if (!valid_month(i)) {
		error_message("Invalid month");
	}
}

int valid_month(input_form i)
{
	return	i.date.month >= 1 && i.date.month <= 12;
}

Code Snippet 3

void validate_checkout_request(input_form i)
{
	if (!valid(i.name, STRING)) {
		error_message("Invalid name");
	}

	if (!valid(i.book, STRING)) {
		error_message("Invalid book name");
	}

	if (!valid(i.date, DATE)) {
		error_message("Invalid month");
	}
}

int valid(string s, int type)
{
	switch (type) {
		case STRING:
			return strlen(s) < MAX_STRING_SIZE;
		case DATE:
			date d = parse_date(s);
			return	d.month >= 1 && d.month <= 12;
	};
}

Code Snippet 4

string patron_name, book_name;
date checkout_date;

void validate_checkout_request(input_form i)
{
	patron_name = i.name;
	if (!valid_string()) {
		error_message("Invalid name");
	}

	book_name = i.book;
	if (!valid_string()) {
		error_message("Invalid book name");
	}

	checkout_date = i.date;
	if (!valid_month()) {
		error_message("Invalid month");
	}
}

int valid_month()
{
	return	checkout_date.month >= 1 && checkout_date.month <= 12;
}

Code Snippet 5

void validate_checkout_request(input_form i)
{
	if (!valid_string(i.name)) {
		i.string = "Invalid name";
		error_message();
	}

	if (!valid_string(i.book)) {
		i.book = "Invalid book name";
		error_message();
	}

	valid_month(i.date);
}

void valid_month(date d)
{
	if (d.month < 1) {
		d.month = 1;
	}

	if (d.month > 12) {
		d.month = 12;
	}

	return 1;
}

Code Snippet 6

int gcd(int p, int q)
{
	int r;

	while (p != 0) {
		int r = p;
		p = q % p;
		q = r;
	}
}

Code Snippet 7

lynx -source $url | \
	sed 's//>\
/g' | \
	grep -v '^$' | \
	egrep -v '(<|>)' | \
	fmt

Code Snippet 8

void init(void)
{
	read_configuration();
	open_network_connections();
	init_random_number_generator();
	init_graphics();
}

Code Snippet 9

class DataProcessor {
	public:
		void send_email(string address, string message);
		void update_passwd(string user, string old_passwd, string new_passwd);
		double percent_free_disk_space(string partition);
		void erase_temp_files(void);
		void print_file(string filename);
}

Code Snippet 10

Number getNumber(int type) {
	switch (type) {
		case INTEGER:
			return new Integer();
		case FLOAT:
			return new Float();
		case DOUBLE:
			return new Double();
	}
}