CHAPTER:11 Programming Guidelines in JAVA Prepared By : VINAY ALEXANDER (विनय अलेक्सजेंड़र) PGT(CS) ,KV JHAGRAKHAND Objective In this presentation you will learn about the various guidelines regarding development of a good program. Understanding goodness of a program Steps to develop a Program. Errors classification and understanding Program Testing Documentation STYLISTIC GUIDELINES Meaningful names for identifiers Identifiers identify different parts of Java programs. Identifiers should have meaningful names. Guidelines followed while dealing with identifiers- Assign meaningful names for all identifiers. That is Variable, Class and Methods. Do not use similar looking names e. g .,manage ,manager. Always assign names to scalar constants (created with final keyword) as it helps in clarity & readability. Ensure Clarity of Expressions Expression in the program should be written clearly. Proper parenthesis may be used to enhance the clarity of the expression. Avoid programming tricks whose intent will not be obvious to a reader. Use standard methods in to enhance readability e.g. ,use Math . sqrt () to calculate square root rather than writing your own logic for it. Use Comments & Indentation Comments play very important role as they provide internal documentation of a program. Indentation makes the statements clear & readable. JAVA comments are given either by //or /*....*/brackets 1.Always insert prologues , the comment in the beginning of a program. 2.Insert explanatory comments where ever applicable. 3.Use comments to help identify {…}pairs greatly & enhance program understanding. 4.Always indent statements to highlight nesting of groups of control statements. The insertion of blank lines & blank spaces: Blank lines can inserted before the declaration of blocks, comment lines, method’s prototype etc. It increases readability. Space must be given before and after an operator to make it more readability.. Statement Formatting Program should be pretty typed i.e. All the statement must be written in separate lines. So that program may be readable and understandable. When program is formatting is done to make a program more readable, it is called prettyprinting. Characteristics of a Good program A good Program not only efficiently solves the given problem but also it should be presentable. The following features are expected for its goodness Effective and efficient: The program produces correct results and is faster, taking into account the memory constraints User Friendly: The program should be user friendly. The user should not be concerned about what is happening inside the program. The user should be only responsible for providing the input data values and rest everything be the program’s responsibility. The output provided by the program should also be easily understandable and presentable. Self-Documenting Code: The source code of the program should have meaningful names for Identifiers and Variables, and proper comment should be used to make it self-documenting. Reliable: The program should be reliable i.e. it must be able to handle unexpected situation like wrong data or no data. The program should display proper error messages. Portable: A Program should be portable i.e. It can run on different Hardware and software platform. STAGES OF PROGRAM DEVELOPMENT PROCESS: A program development problem is a step by step process where each stage contributes to building of an effective and efficient program. => There are four stages in program development process as given below: 1.Crack the problem: The problem is cracked and an algorithm is formulated which gives the solution for the problem. 2.Code the Algorithm: The algorithm should be translated into the program(source code)carefully to avoid errors that may crop up. 3.Compile the program: It is a process to convert the source code into the object code and reported errors should be rectified 4.Execute the program: At the run time ,errors if any ,should be removed & 3rd stage should be repeated to compile & finally execute the corrected version of the program. ROBSTNESS The ability of a program, to recover following error & to continue operating within its environment, is Robustness. GUARD CODE: The code that can handle exceptional data errors and operational errors is called Guard Code. Types of errors in the program An error or ‘Bug’ is anything in the program code that prevents a program to run in correct manner. compile-time errors: An Errors that occur during compiling-time ,are known as compile-time errors. Syntax Error: It is occurred when rules of the programming language are not followed. It is most common due to spelling errors in the Keywords and methods. Semantic Error: It is occurred when a statement written is not meaningful. Ex. X + Y = Z Logical Error: When Program does not give correct result due to mistake in the calculation or use of operators etc. These errors are most difficult to trace in a big sized program. Run Time Error: This error occurred at Run-Time and it is due to incompatible Data Types used, divide by zero in the expression etc. which may cause the termination of program execution abnormally. Run time errors are also called exceptions, and handling such errors in the application is called exception handling Exception : Unexpected situation or unexpected error, during program execution is known as Exception. Exception Handling: Way of handling anomalous situation in a program-run , is known as Exception Handling Advantages: 1. Exception Handling separates error-handling code from normal code. 2. It clarifies the code and enhances readability. 3. It makes for clear, robust, fault-tolerant programs Concept of Exception Handling The raising of imaginary error flag is called throwing an error. When as error is thrown , the overall system responds by catching the error. And surrounding a block of error-sensitive code-withexception-handling is called trying to execute a block Exceptional Handling in Java: In java exception handling is done using try{ } and catch{ } blocks. Statements that can raise an error are placed inside the try{ } block and its handling code is written inside the catch{ } block. if there are multiple exceptional classes that might arise in the try block, then several catch block are allowed to handle them.code must be executed no matter what happens can be placed in finally block. The thrown keyword: Keyword thrown is used to inform that an error has occurred. It is specified with method prototype. i.e., Public static void main(String[ ] args) throws IOException Syntax: try { // tested statement(s); } Catch(ExceptionName e1) { // Trap handler statement(s); } Catch(ExceptionName e2) { // Trap handler statement(s); } Finally { // always executed block } try { int age=Integer.parseInt(ageTF.getText()); } Catch(Exception e) { JOptionPane.showMessageDialog(null, “Enter a Numeric Value”); } PROBLEM SOLVING METHADOLOGY AND TECHNIQUES The following steps are taken while designing a program1. Understanding the Problem 2. Analyzing the Problem 3. Designing the Program 4. Coding the Program 5. Testing & Debugging The Program 6. Documentation 7. Maintenance of the Program 1.Understand the problem: You must recognize the need of program. Try to explore ‘WHAT’ component in the problem 2.Analyse the problem :After determining the need, carefully analyze the problem to get an effective solution. Try to explore the inputs to get required output. 3. Design the program: In this step ‘HOW’ component is explored. Try to find out the bet sequence of steps (Algorithm) to solve the problem. A big problem can be broken in to smaller portion to get effective solution. 4.Coding of Program: The translate your solution into Programming Language. While translating you need to identify the control structure, arithmetic and logical calculations to be used. An IDE (eg. NetBeans) can be used to code the Program. 5.Test & Debug the program: Once a Program has been coded, it can be executed and find out the errors (Syntactical & Logical), if any. The errors also termed as ‘bugs’. Testing is the process of finding errors in a program ,and debugging is the process of correcting errors found during the testing process. 6.complete your documentation: Documentation helps to others to understand the program. Details of the program including its source code, Input/Output screen and its operating instructions are recorded and compiled in the form of booklet. 7.Maintenance of the Program Maintenance is required To modify the program, To remove the undetected errors or To enhance the program functionality To keep the program up-to-date with its environment. General Debugging Methods: 1. Create the main class first. 2. Test each method separately. 3. Trace code Blocks. 4. Insert debug code. 5. Test the program under different conditions. Program verification Program verification is the process of determining the degree to which a software product fulfills its specification. Types of Program verification 1.Deskchecking: It is a manually verifying of a program. the essential data are written down and the programmer walks the design as specified by the statements , making changes in the data on the paper 2. Walk-through : a manual simulation of the program with simple test input is performed , by a team keeping track of the program’s data on the paper or on a black board. It is not intended to simulate all possible test cases. 3. Inspection: A team carries out the inspection where a reader reads the code line by line. inspection participants point out errors that are recorded on an inspection report. Program Testing A Program must be tested to remove the errors. The following types of testing methods may be used. 1.Unit Testing: In this method, a single unit (module or package) of the program is tested to meet its specification. It is much easier to test and debug a small module (units) one at a time. 2.Functional (Black Box) Testing: The functionality of the module or program is tested by giving possible input values, treating the code (program) as a “Black box” without concerning its internal working. 3.Structural (White Box) Testing: In this testing module or program is tested to trace the all path of execution of statements in the module. This technique is used to see the structural behavior of the program. 4.Path Testing: In this technique, all the possible paths of statement are tried to execute by giving possible input values or test cases. 5. Metric Based Testing: The goals are based on certain measurable factors. These measurements can also be used to indicate when certain level of testing has been completed 6.Integration Testing: This technique is performed to integrate all the program modules (units) that have been tested independently in unit testing. It can be TopDown, Bottom-up or in combined approach. DOCUMENTATION: A proper developed application must be equipped with its explanations and descriptions to facilitate users. Documentation refers to written descriptions, design, code and comment, internal and external requirement to the program, which make a program more understandable, readable and more easily modifiable. MODULES OF DOCUMENTATION: Modules make information easily accessible to the specific user for which they were prepared and they reduce the costs of production and maintenance. The aggregation of modules and their details would depend upon 1. Complexity of the system 2. Technical sophisticated of user 3. People involved in development and use 4. Expected life of documentation The documentation module (also called Manual) may be following types as per their contents. User Manual: Describes how to use the program and contains all the I/O screens and operating instruction for its user. Operation Manual: Describes the various steps to perform a specific task using the system. Program Manual: It contains the Source code of the program for reference purpose. It is required during program Maintenance. System Manual: It contains overall specification, results of all the program development stages etc. Program Maintenance: Program maintenance refers to the modification of a program, in order to meet changing requirements or to take care of undetected errors. The following types of Maintenance may occur: Corrective Maintenance: It is required to correct the errors which were undetected during the design of the program. Adaptive Maintenance: Change in user’s need or program environment may occur and program must be changed to adapt such changes. This change in the program is called Adaptive Maintenance. Preventive Maintenance: Sometimes you could anticipate the errors before they actually occur. Preventive Maintenance is carried out to avoid such errors. Perfective Maintenance: To cope with changing world i.e. changes in the technology, you should tune your system to keep it up-to-date. Due to arrival of new technology and facilities, the program is updated to make it perfect in respect of new technological need.