Docsity

Coding Standards in Java: Naming Classes and Variables - Prof. Fathima, Slides of Programming Languages

Partial preview of the text

Download Coding Standards in Java: Naming Classes and Variables - Prof. Fathima and more Slides Programming Languages in PDF only on Docsity!

Unit 1 -

Programmi

ng

HNC

Objectives

  • (^) To describe the purpose of coding standards.
  • (^) To implement some coding standards supported in Java.

Coding Standards

  • (^) Programming is a way to communicate with computers by giving instructions

to them to perform a particular task.

  • Thus, our act of writing codes is a form of communication not only with

machines but also with other developers.

  • (^) Coding standards, otherwise known as coding conventions, is a set of rules

that programmers adhere to improve the standards of writing code.

  • (^) Adhering to coding standards helps in producing code that is easier to

understand, debug, modify, and collaborate on by multiple developers.

Revisiting Variable Program

The following program does not adhere to

the coding conventions.

Can you identify what the problems are?

Coding Standards – Class Name

  • (^) When naming a class, it is advisable to follow these conventions:
    • (^) Class names should be nouns, in upper camel case with the first letter of each

internal word capitalized.

  • (^) Try to keep your class names simple and descriptive.
  • (^) Use whole words-avoid acronyms and abbreviations (unless the abbreviation is

much more widely used than the long form, such as URL or HTML).

  • (^) Example: class MyDetails, MyFirstJavaProgram

Coding Standards – Variable Name

  • (^) When naming a variable, it is advisable to follow these conventions:
    • (^) Variable names follow lower camel case where the first word is in lowercase and subsequent words begin with a capital letter.
    • Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.
    • (^) Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use.
    • One-character variable names should be avoided except for temporary "throwaway" variables.
    • Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.