The document discusses programming style and algorithms. It covers topics like clarity, naming conventions, control structures, information hiding, and stepwise refinement. Guidelines for programming style include using simple expressions, appropriate naming, single entry/exit constructs, limiting nesting and side effects.
The document discusses programming style and algorithms. It covers topics like clarity, naming conventions, control structures, information hiding, and stepwise refinement. Guidelines for programming style include using simple expressions, appropriate naming, single entry/exit constructs, limiting nesting and side effects.
The document discusses programming style and algorithms. It covers topics like clarity, naming conventions, control structures, information hiding, and stepwise refinement. Guidelines for programming style include using simple expressions, appropriate naming, single entry/exit constructs, limiting nesting and side effects.
The document discusses programming style and algorithms. It covers topics like clarity, naming conventions, control structures, information hiding, and stepwise refinement. Guidelines for programming style include using simple expressions, appropriate naming, single entry/exit constructs, limiting nesting and side effects.
Download as PPTX, PDF, TXT or read online from Scribd
Download as pptx, pdf, or txt
You are on page 1of 15
Programming Style
• Programming style refers to the technique used in
writing the source code for a computer program. • Most programming styles are designed to help programmers quickly read and understands the program as well as avoid making errors. (Older programming styles also focused on conserving screen space.) • A good coding style can overcome the many deficiencies of a first programming language, while poor style can defeat the intent of an excellent language. • The goal of good programming style is to provide understandable, straightforward, elegant code. • The programming style used in a various program may be derived from the coding standards or code conventions of a company or other computing organization, as well as the preferences of the actual programmer. • Example: • To find the average of 3 numbers, the algorithm is as shown below. • Step1: Read the numbers a, b, c, and d. • Step2: Compute the sum of a, b, and c. • Step3: Divide the sum by 3. • Step4: Store the result in variable of d. • Step5: End the program. • Development of an Algorithm • The steps involved in the development of an algorithm are as follows: – Specifying the problem statement. – Designing an algorithm. – Coding. – Debugging – Testing and Validating – Documentation and Maintenance. • Specifying the problem statement: The problem which has to be implemented in to a program must be thoroughly understood before the program is written. • Problem must be analyzed to determine the input and output requirements of the program. • Designing an Algorithm: Once the problem is cleared then a solution method for solving the problem has to be analyzed. • There may be several methods available for obtaining the required solution. • The best suitable method is designing an Algorithm. • To improve the clarity and understandability of the program flowcharts are drawn using algorithms. • Coding: The actual program is written in the required programming language with the help of information depicted in flowcharts and algorithms. • Debugging: There is a possibility of occurrence of errors in program. These errors must be removed for proper working of programs. The process of checking the errors in the program is known as Debugging‘. • There are three types of errors in the program. • Syntactic Errors: They occur due to wrong usage of syntax for the statements. • Ex: x=a*%b • Here two operators are used in between two operands. • Runtime Errors : They are determined at the execution time of the program • Ex: Divide by zero • Range out of bounds. • Logical Errors: They occur due to incorrect usage of instructions in the program. They are neither displayed during compilation or execution nor cause any obstruction to the program execution. They only cause incorrect outputs. • Testing and Validating: Once the program is written, it must be tested and then validated.i.e., to check whether the program is producing correct results or not for different values of input • Documentation and Maintenance: Documentation is the process of collecting, organizing and maintaining, in written the complete information of the program for future references. • Maintenance is the process of upgrading the program, according to the changing requirements. General rules or guidelines in respect of programming style 1. Clarity and simplicity of Expression: The programs should be designed in such a manner so that the objectives of the program is clear. 2. Naming: In a program, you are required to name the module, processes, and variable, and so on. Care should be taken that the naming style should not be cryptic and non-representative. For Example: a = 3.14 * r * r area of circle = 3.14 * radius * radius; 3. Control Constructs: It is desirable that as much as a possible single entry and single exit constructs used.
4. Information hiding: The information secure in the data
structures should be hidden from the rest of the system where possible. Information hiding can decrease the coupling between modules and make the system more maintainable.
5. Nesting: Deep nesting of loops and conditions greatly
harm the static and dynamic behavior of a program. It also becomes difficult to understand the program logic, so it is desirable to avoid deep nesting. 6. User-defined types: Make heavy use of user-defined data types like enum, class, structure, and union. These data types make your program code easy to write and easy to understand.
7. Module size: The module size should be uniform. The size of
the module should not be too big or too small. If the module size is too large, it is not generally functionally cohesive. If the module size is too small, it leads to unnecessary overheads.
8. Module Interface: A module with a complex interface should
be carefully examined. 9. Side-effects: When a module is invoked, it sometimes has a side effect of modifying the program state. Such side-effect should be avoided where as possible. Refinement of coding • Stepwise refinement is the idea that software is developed by moving through the levels of abstraction, beginning at higher levels and, incrementally refining the software through each level of abstraction, providing more detail at each increment. • At higher levels, the software is merely its design models; at lower levels there will be some code; at the lowest level the software has been completely developed.
• At the early steps of the refinement process the software
engineer does not necessarily know how the software will perform what it needs to do. This is determined at each successive refinement step, as the design and the software is elaborated upon. • Refinement can be seen as the compliment of abstraction. • Abstraction is concerned with hiding lower levels of detail; it moves from lower to higher levels. • Refinement is the movement from higher levels of detail to lower levels. Both concepts are necessary in developing software.