Development Standards for Program Logic
This page is a subset of our complete Software Development Standards document. The original document includes many standards that you do not need for Program Logic. I provide this web page as convenience, however, in subsequent courses, you will be responsible for knowing and applying all our standards.
Definition of Terms
Identifier
An identifier is the name you give variables, methods, classes, packages, interfaces and named constants.
Pascal Case
Use uppercase letters for the first letter in the identifier and the first letter of each subsequent.
- Examples: BackColor, DataSet
We use Pascal case when creating class names. Our Controller class is just one word but it still starts with an uppercase letter.
Camel Case
Use lowercase letters for the first letter of the identifier. Use uppercase letters for the first letter of each subsequent word.
- Examples: numberOfDays, isValid
Camel case is our most used format for identifiers. Use camel case when creating variable names and method names. Our main method is just one word but it still starts with an lowercase letter.
Uppercase
Capitalize all letters in the identifier. Separate multi-word identifiers with an underscore between each word. Note: This convention is ONLY USED for named constants.
- Examples: HEADING, TAX_RATE
Remember, once assigned, the value of a named constant cannot be re-assigned.
Hungarian notation
The name of the variable indicates its type or intended use. Note: Hungarian notation is NOT USED in Java.
- Examples: strFirstName
In the above example, "str" stands for "string". Using Hungarian notation is somewhat redundant when you have properly named identifiers.
Commenting Code
Contrary to popular belief, code is not self-documenting; using descriptive variable, class, and method names is desirable; however, it is no substitute for commenting your code. In general, you want to describe the "why" and/or "how" and/or "where" not just the "what". Describing why, allows you and subsequent programmers to determine the thought process that went into creating the code.
In Program Logic, you can comment your applications using the "words" of you write in a Solution Algorithm. I also highly recommend you use inline comments at the end of curly braces (e.g. //end of sayGoodbye method)
Below are examples of each type of comment in Java. Remember, depending on the programming language you are using, the symbols for commenting code can differ.
- An inline comment starts with two forward slashes:
//end of method - A block comment is:
/* comment goes here and can be multiple lines but you must end with */ - A JavaDoc comment is used to dynamically create documentation:
/** You must start with a forward slash and two asterisks but you end with */
Naming Conventions
General Naming Conventions
- Do not use Hungarian notation.
- Do not use dollar signs and digits in an identifier name.
- Spell words out instead of using abbreviations (e.g. use temperature instead of temp).
- Accept for named constants, do not use underscores in your identifier name
- Single letter variable names are not allowed EXCEPT as an iterator (loop control) variable in counter-controlled loops: (for int i = 0; …).
- The variable
sbis common and acceptable for identifying a StringBuilder class. - Avoid acronyms.
- Identifiers should be less than 30 characters.
- Do not qualify variables with a number suffix (name1, name2, name3).
- If you want to change an identifier name, use the
refactorfunctionality built into the development environment.
Variable Names
- Use camel case.
- Be descriptive.
- Use a noun or noun phrase.
- Examples: firstName, numberOfDays
Remember, single letter variable names (i, j, k, l) may only be used for counter loops. It is common and acceptable to use sb as the variable name for the StringBuilder class.
Named Constants
- Use all uppercase letters.
- Be descriptive.
- Use a noun or noun phrase.
- Separate the noun phrase with an underscore
- Example: SALES_TAX
Using an underscore makes the named constant easier to read.
Boolean Variable Names
- Start with a verb that reflects the Boolean-ness of the variable
- "is"
- "has"
- "can"
- Reflect a positive or optimistic result.
- Positive Example: isSuccessful = false;
- Negative Example: isNotSuccessful = true;
Package Names
- Follow the Java package name standards
- Use all lowercase letters
- Use the reverse domain naming convention
- edu.northwoodtech.packagename
In the above example, "packagename" would be replaced with the actual name of the package. In Program Logic, we only use edu.northwoodtech.controller.
Method Names
- Use camel case
- Start with a verb and reflect the functionality of the method
- getNumberInRange()
- findBedrooms()
Remember, whether calling or writing a method, you always include the parentheses, however, in some cases, the parentheses will not be empty.
Formatting Code
Well formatted code is easier to read. Use whitespaces such as tabs and blank lines to make your code more readable. Avoid too much whitespace as that can lead to writing code in the wrong place.
In general, indent code that "belongs to" a parent entity. For example, indent the variable declarations inside of the body of a method. Consistency in indentations can be achieved easily by using the TAB key instead of spaces.
There is no excuse for poorly formatted code. Additionally, many development environments give you a set of keystrokes that will format your written code.
Long Code Expressions
Sometimes you have a long line of code that will go past the vertical line in our NetBeans environment. It is okay to extend a few characters beyond this line, however, for readability, if your code is too long, please hit the enter key to break the code into multiple line. If needed, NetBeans should automatically add the concatenation character.
Curly Braces
In Java, and other programming languages, blocks of code are grouped together using curly braces { }. Believe it or not, there is an ongoing heated debate on the proper positioning of curly braces. Your main takeaway should be usage and consistency. Make sure you use curly braces and be consistent in your placement of them. Following are two common placement options.
Option One:
private void validateForm( )
{
//notice where the beginning
//curly brace is at
}//end curly brace
Option Two:
private void validateForm( ){
//notice where the beginning
//curly brace is at
}//end curly brace
Variable Declaration
- Declare variables at the top of the method.
- Use one line per variable.
- Initialize local variables at declaration.
- For safer programming, avoid global variables
Remember to carefully read my assignment requirements. Sometimes I will state where to put variables and/or when to use a global variable.
Developing Classes and Methods
Remember, classes use Pascal case and methods use camel case. In Program Logic, we only have two classes, Controller and Main. If you follow my instructions, these classes are automatically created for you. Our classes will always use public as their access modifier. When creating your own methods do the following:
- Use
privateas the access modifier for a method. - Design your method to perform one primary task.
- Methods that return a value should have only ONE
returnstatement and that statement must be at the bottom of the method. - Try to name and code your methods so they can be reused within the application.
- print() is a more reusable name than printMyName().
- A Scanner variable named input is more reusable than one named birthdayInput.