Hospitality in Computer Programming

Lori Carter

2021/07/21

Student Materials

Worksheet

Acknowledgement

This lab was motivated, in part, by a paper written by Victor Norman of Calvin University. https://cs.calvin.edu/static/documents/christian/TeachingHospitableCode.pdf

Overview

Ethics background desired: Virtue Ethics, Analogies, Utilitarianism, & Deontology. If students or professor are not familiar with these frameworks, a brief summary is provided in the reflection.

Subject matter referred to in this lab: Hospitable computer programming (Exercise written in C++ (uses pointers and vectors), and also Java). Could be adapted to another language if preferable.

Placement in overall ethics curriculum:

Time required:

Learning objectives:

Ethical dilemma or issue to be considered: Hospitality

Hospitality is defined by Dictionary.com as

the friendly reception and treatment of guests or strangers.

As students, often the only motivation for hospitable programming practices is the grade. This exercise should help point out that it is frustrating and time-consuming to both code users and code maintainers (our guests and strangers) to try to deal with code that is not well-written and well-documented.

Flow

Preparation

Guide for Instructors

Lesson plan

Introduction (5 min)

Start by running (or having students run) the code. If running it as a demo, choose a student to play the coded game and tell the class that they are tasked with figuring out what the game does as their peer plays it. The prompts for the user are one of the inhospitable aspects of the code so let the student chosen to interact with the game struggle in responding. (The game is similar to Blackjack, but the goal is 20 instead of 21. This may also be an inhospitable aspect since there are no instructions). For the C++ version, a “seed” is required for the random number generator and this is also awkward.

Activity (15 min)

Reflection (10 min)

Ask students to consider (silently) common student behaviors with respect to hospitable programming practices. They should be able to relate to many! This list should be displayed as previously prepared.

Discuss

Consider this. You are the manager of a software team. Some of your team members are writing code without good variable names or comments. One way to address the situation would be with a deontological approach. Recall that the deontological ethical framework relies on a set of rules. One such rule may be that “variable names must be meaningful and you just use comments at least every 5 lines in your code.” How do you think this would go over with the team member? (Probably be resistant at being commanded to do something without a good reason)

Would there be a better way to approach the team member? Suggest a method for approaching the team member using one of the other ethical frameworks:

Assessment (to be included on a later quiz or exam)

Example Code

Select one of the languages blow.

#include <iostream>
#include <vector>
#include <exception>
using namespace std;

bool w(vector<int> y)
{
	int z = 0;
	for (int i = 0; i < y.size(); i++)
		z += y.at(i);
		if(z>20)
			return false;
	return true;
}
void f(int * r, bool a)
{
	if (*r == 14 && a)
		*r = 11;
	else if (*r == 14 && !a)
		*r = 1;
	else if (*r > 10)
		*r = 10;
}
int main()
{
	int seed;
	cin >> seed;
	srand(seed);
	char n,c;
	do{
		vector <int> x;
		int p = 0;
		int * h = &p;
		p = rand() % 14 + 1;
		f(h,true);
		x.push_back(p);
		int t=0;
		do{
			p = rand() % 14 + 1;
			if (t <= 10)
				f(h, true);
			else
				f(h, false);
			x.push_back(p);
			//cout << "you have " << endl;
			t = 0;
			for (int i = 0; i < x.size(); i++) {
				t += x.at(i);
				cout << x.at(i) << " ";
			}
			cout << endl;
			if (w(x)) {
				cout << "Another?" << endl;
				cin >> c;
			}
		} while (w(x) && c=='y');
		int o = rand() % 20;
		if (w(x) && o < t)
			cout << "You win! :)" << endl;
		else if (w(x) && o > t) {
			cout << "You got beat :(" << endl;
			cout << "Other person had " << o << endl;
		}
		else if (w(x) && o == t)
			cout << "You tied :/" << endl;
		else
			cout << "You lose. :(" << endl;
		cout << "again? y or n" << endl;
		cin >> n;
	} while (n == 'y');
	cin.ignore(1);
	return 0;
}
import java.util.*;

public class hMod
{
   public static boolean w(ArrayList<Integer> y)
   {
      int z = 0;
      for (int i = 0; i < y.size(); i++)
         z += y.get(i);
         if(z>20)
            return false;
      return true;
   }
   public static void f(int r, boolean a)
   {
      if (r == 14 && a)
         r = 11;
      else if (r == 14 && !a)
         r = 1;
      else if (r > 10)
         r = 10;
   }
   public static void main(String[]args)
   {
      Scanner sc=new Scanner(System.in);
      Random srand = new Random();
      char n,c='y';
      do{
         ArrayList <Integer> x = new ArrayList<Integer>();
         int p = 0;
        
         p = srand.nextInt(14) + 1;
         System.out.println("first p "+p);
         f(p,true);
         x.add(p);
         int t=0;
         do{
            p = srand.nextInt(14) +1;
            System.out.println("next p "+p);
            if (t <= 10)
               f(p, true);
            else
               f(p, false);
            x.add(p);
            //System.out.println("you have " );;
            t = 0;
            for (int i = 0; i < x.size(); i++) {
               t += x.get(i);
               System.out.print( x.get(i) + " ");
            }
            System.out.println();
            if (w(x)) {
               System.out.println("Another?" );
               c =sc.next().charAt(0);
            }
         } while (w(x) && c=='y');
         int o = srand.nextInt(20);
         if (w(x) && o < t)
         {
            System.out.println( "You win! :)");
            System.out.println("Other person had " + o);}
         else if (w(x) && o > t) {
            System.out.println("You got beat :(" );
            System.out.println("Other person had " + o);
         }
         else if (w(x) && o == t)
            System.out.println( "You tied :/");
         else
         {
            System.out.println("You lose. :(");
            System.out.println("Other person had " + o);
            }
         System.out.println("again? y or n" );
         n=sc.next().charAt(0);
      } while (n == 'y');
     
   }
}
Python code coming soon.
R code coming soon.