Code Assignment

Overview

The purpose of this assignment is to practice applying the source code readability criteria in the syllabus. This will help you get into the habit of following patterns that improve the readability of your code.

Assignment folder

Create a directory named code in your repository to hold the work for this assignment. When you complete this assignment, the contents of this folder will be the following.

Instructions

The following code is a Node.js program that prints the first 100 prime numbers. Place this code in a file named main.js and verify that it runs as expected.

// This program prints the first 100 priime numbers.

// Return true if n is prime.
function isPrime(n) {
  for (var i = 2; i < n; ++i) {
  if (n % i==0) return false;
  }
  console.log(n);
  return true;

}

   var numberOfPrimesDeleted = 0;
var candidatePrime = 2;
var k = 0;

while (numberOfPrimesDeleted<100) {
	if (isPrime(candidatePrime)) {
		++numberOfPrimesDeleted;
	}
	// Increment candidatePrime.
	++candidatePrime;
}

The code given above does not satisfy the readability criteria stated in the course syllabus. For this assignment, you should modify main.js to resolve these issues. When you are done, the resulting code should satisfy all of the given readability criteria.

There are more efficient algorithms to accomplish prime number detection than the algorithm used in the code. However, this is not a readability issue, so improving the prime detection algorithm is not required for this assignment. However, you can do this if you want to, just make sure the resulting code satisfies the readability criteria.

For your convenience, the readability criteria in the course syllabus are repeated in the following table.

Readability Criteria for Source Code
Criterion Description
Cleanliness Have unnecessary variables and logic been removed from the code?
Logical indentation Does indentation show logical structure?
Consistent indentation Does indentation follow a consistent policy?
Portable indentation* Are tabs omitted?
Logical spacing Does spacing show logical structure?
Consistent spacing Does spacing follow a consistent policy?
Expressive and clear naming Do variables, functions and classes have names that clearly express their purpose in the program?
Clear responsibilities Are responsibilities of functions and classes clear and consistent with their names? Is the code structured to avoid reliance on side effects produced by functions?
Necessary comments Are comments included when needed?
Unnecessary comments Are superfluous comments omitted?
Spelling Are user-defined identifiers free of spelling errors? Are comments and other documentation free of spelling and grammatical errors?
Nonredundant When 2 or more places inside a program need to perform the same activity, is that activity defined as a function and called as such from where it is needed?
Robust to changes Does the code avoid breakage problems when modified. For example, the following code is not robust to changes.
            var s = 'hello';
            print 'The length of the string is ' + 5;
It should be written as follows.
            var s = 'hello';
            print "The length of the string is " + s.length;
Organization Is source code well organized into files and folders?

*Don't use tabs for indentation in your source code in this course. Tabs display differently in different viewing and editing tools. In general, tabs will degrade readability for some people who read your code, especially if you mix tabs with spaces. If you want full score on your assignments in this course, you should not use tabs. If you are in the habit of pressing tab in your editor and want to continue working this way, look for an option in your editor to replace tabs with spaces.

How Many Spaces Should I Use for Indentation?

The most common indentation for Javascript is 2 spaces. People working on other languages tend to use more than 2 spaces.

The reason for 2 spaces in Javascript is that Javascript programs include more indents than programs written in other languages. This is due to the use of anonymous functions and other reasons. When there are so many indents, too much of the code gets shifted too far to the right.

Excessive indents is also a problem with HTML.

In this course, you can use any number of spaces for indents as long as you are consistent with each assignment. For example, if you use 4 spaces in an assignment, then use 4 spaces for every indent in that assignment. You can vary number of spaces between assignments.

Grading Rubric for this Assignment

CriterionExpected OutcomePercent of score
Cleanliness Did student remove unnecessary code? 10%
Logical indentation Did the student fix the indent that doesn't show dependency on a conditional? 10%
Consistent indentation Did the student remove an unnecessary indent? 10%
Portable indentation Did the student remove all tab characters? 10%
Logical spacing Did the student adjust spacing between operators and operands to better convey the order of operations? 10%
Consistent spacing Did the student achieve consistent spacing between operators and operands? 10%
Expressive and clear naming Did the student fix the misnamed identifier? 10%
Clear responsibilities Did the student restructure the code to increase consistency between the name of a function and the function's responsibilities. 10%
Unnecessary comments Did the student remove unnecessary comments? 10%
Spelling Did the student fix the spelling error? 10%