Programming Technique

 

Introduction to Programming Technique

Ø programming technique is about generalizing software components so that they can be easily reused in a wide variety of situations.

Ø  Programming techniques include :

Ø  Complexity factor

Ø  Using bitwise operators

Ø  Iteration vs Recursions

Ø  Implementing design patterns

Top down Approach

Ø  Top-Down Approach is an approach to design algorithms in which a bigger problem is broken down into smaller parts.

Ø  Thus, it uses the decomposition approach.

Ø  This approach is generally used by structured programming languages such as C, COBOL, FORTRAN.

Ø  The drawback of using the top-down approach is that it may have redundancy since every part of the code is developed separately.

Ø  Also, there is less interaction and communication between the modules in this approach.

Ø  The implementation of algorithm using top-down approach depends on the programming language and platform.

Ø  Top-down approach is generally used with documentation of module and debugging code.

Bottom up Approach

Ø  Bottom-Up Approach is one in which the smaller problems are solved, and then these solved problems are integrated to find the solution to a bigger problem.

Ø  Therefore, it uses a composition approach.

Ø  It requires a significant amount of communication among different modules.

Ø  It is generally used with object oriented programming paradigm such as C++, Java, and Python.

Ø  Data encapsulation and data hiding is also implemented in this approach.

Ø  The bottom-up approach is generally used in testing modules.

Top-Down Approach vs Bottom-Up Approach

Top-Down Approach

Bottom-Up Approach

In this approach, the problem is broken down into smaller parts.

In this approach, the smaller problems are solved.

It is generally used with documentation of module and debugging code.

It is generally used in testing modules.

It does not require communication between modules.

It requires relatively more communication between modules.

It contains redundant information.

It does not contain redundant information.

Decomposition approach is used here.

Composition approach is used here.

The implementation depends on the programming language and platform.

Data encapsulation and data hiding is implemented in this approach.

Cohesion

Ø  cohesion is an indication that shows the relationship within modules.

Ø  Cohesion provides the information about the functional strength of the modules.

Ø  The greater the cohesion, the better will be the program design.

Ø  Cohesion is basically the dependency between internal elements of modules like methods and internal modules. High cohesion will allow us to reuse the classes and methods.

Ø  There are three most popular types of cohesion,

Ø  functional cohesion

Ø  sequential cohesion

Ø  logical cohesion.

Types of Cohesion

Ø  Functional Cohesion: Every essential element for a single computation is contained in the component. A functional cohesion performs the task and functions.

Ø  Sequential Cohesion: An element outputs some data that becomes the input for other element, i.e., data flow between the parts. It occurs naturally in functional programming languages.

Ø  Logical Cohesion: The elements are logically related and not functionally. Ex- A component reads inputs from tape, disk, and network. All the code for these functions is in the same component. Operations are related, but the functions are significantly different.

Ø  Communicational Cohesion: Two elements operate on the same input data or contribute towards the same output data. Example- update record in the database and send it to the printer.

Ø  Procedural Cohesion: Elements of procedural cohesion ensure the order of execution. Actions are still weakly connected and unlikely to be reusable. Ex- calculate student GPA, print student record, calculate cumulative GPA, print cumulative GPA.

Ø  Informational Cohesion: Informational cohesion occurs when elements or tasks are grouped together in a module based on their relationship to a specific data structure or object, such as a module that operates on a specific data type or object.

Coupling

Ø  Coupling is the indication that shows the relationship between modules or we can say the interdependence between modules.

Ø  Basicly there are two types of coupling, namely, 

Ø  tight coupling and 

Ø  loose coupling

Ø  Loose coupling is frequently used because changing one class through loose coupling will not affect another class. Hence, it reduces dependencies on a class. Consequently, we can easily reuse it.

Ø  But, in case of tight coupling, the classes and objects are dependent on each other and hence it reduces the re−usability of the code.

Types of Coupling

Ø  Data Coupling: If the dependency between the modules is based on the fact that they communicate by passing only data, then the modules are said to be data coupled. 

Ø  Control Coupling: If the modules communicate by passing control information, then they are said to be control coupled. It can be bad if parameters indicate completely different behavior and good if parameters allow factoring and reuse of functionality.

Ø  External Coupling: In external coupling, the modules depend on other modules, external to the software being developed or to a particular type of hardware.

Ø  Common Coupling: The modules have shared data such as global data structures. The changes in global data mean tracing back to all modules which access that data to evaluate the effect of the change. 

Ø  Content Coupling: In a content coupling, one module can modify the data of another module, or control flow is passed from one module to the other module.

Ø  Sequential Coupling: Sequential coupling occurs when the output of one module is used as the input of another module, creating a chain or sequence of dependencies. 

Ø  Functional Coupling: Functional coupling occurs when two modules depend on each other’s functionality, such as one module calling a function from another module. This type of coupling can result in tightly-coupled code that is difficult to modify and maintain.

Cohesion vs Coupling

Cohesion

Coupling

Cohesion is the measure of degree of relationship between elements of a module.

Coupling is the measure of degree of relationship between different modules.

It is an intra module concept.

It is an inter module concept.

It represents relationships within the module.

It helps represent the relationships between the modules.

Increased cohesion is considered to be good for the software.

Increased coupling has to be avoided in software.

It represents the functional strength of the modules.

It represents the independence among the modules.

When modules are highly cohesive, a high quality software is built.

When the modules are loosely coupled, it results in a high quality software.

In cohesion, module focuses on the single thing.

The modules would be connected to each other.

 Structure Programming

Ø  we sub-divide the whole program into small modules so that the program becomes easy to understand.

Ø  The purpose of structured programming is to linearize control flow through a computer program so that the execution sequence follows the sequence in which the code is written.

Ø  It means that the code will execute the instruction by instruction one after the other.

Ø  The structured program mainly consists of three types of elements:

Ø  Selection Statements

Ø  Sequence Statements

Ø  Iteration Statements

Advantages of Structure Programming

Ø  Easier to read and understand

Ø  User Friendly

Ø  Easier to Maintain

Ø  Mainly problem based instead of being machine based

Ø  Development is easier as it requires less effort and time

Ø  Easier to Debug

Ø  Machine-Independent, mostly.

Disadvantages of Structure Programming

Ø  Since it is Machine-Independent, So it takes time to convert into machine code.

Ø  The converted machine code is not the same as for assembly language.

Ø  The program depends upon changeable factors like data-types. Therefore it needs to be updated with the need on the go.

Ø  Usually the development in this approach takes longer time as it is language-dependent. Whereas in the case of assembly language, the development takes lesser time as it is fixed for the machine.

Unstructured Programming

Ø  Unstructured Programming is a type of programming that generally executes in sequential order i.e., these programs just not jumped from any line of code and each line gets executed sequentially.

Ø  the code is written as a single whole block.

Ø  The whole program is taken as a single unit.

Ø  It is harder to do changes in the program.

Ø  This paradigm was used in earlier versions of BASIC, COBOL, and FORTRAN.

Structured

Unstructured

Structured Programming is a programming paradigm which divides the code into modules or function.

Unstructured Programming is the paradigm in which the code is considered as one single block.

Structured Programming based programs are easy to read.

Unstructured Programming based programs are hard to read.

Structured Programming is to make the code more efficient and easier to understand.

Unstructured programming is just to program to solve the problem. It does not create a logical structure.

Structured Programming is easier because of modules.

Unstructured programming is harder when comparing with the structured programming.

Structured programming can be used for small and medium scale projects.

Unstructured programming is not applicable for medium and complex projects.

It is easy to do changes in Structured Programming.

It is hard to do modifications in Unstructured Programming.

Structured programming avoids code duplication.

Unstructured programming can have code duplication.

 Deterministic Technique

Ø  aims to produce consistent and predictable results based on a given input.

Ø  It focuses on writing programs that always produce the same output for a given set of inputs, regardless of the runtime environment or execution conditions.

Ø  some common techniques used in deterministic programming:

Ø  Avoiding Randomness: By avoiding the use of random number generators or ensuring that they are initialized with the same seed value, you can make your program deterministic.

Ø  Pure Functions: Pure functions are functions that always produce the same output for a given set of inputs and have no side effects. They don't rely on external state and only depend on their input parameters.

Ø  Avoiding Global State: By minimizing the use of global state or ensuring proper synchronization when it is necessary, you can make your program more deterministic.

Non-deterministic Technique

Ø  introduce randomness or non-determinism into a program, allowing for variations in behavior or outcomes.

Ø  These techniques are often used in scenarios where randomness or exploration is desired, such as simulations, games, or optimization algorithms.

Ø  Here are a few examples of non-deterministic programming techniques:

Ø  Random Number Generation: Non-deterministic programs often use random number generators to introduce randomness. By using random numbers, you can simulate uncertain or unpredictable events in your program.

Ø  Monte Carlo Methods: Monte Carlo methods are probabilistic techniques that use random sampling to approximate solutions to complex problems. Examples include Monte Carlo simulations for risk analysis, numerical integration, or optimization problems.

Ø  Genetic Algorithms: Genetic algorithms are search and optimization techniques inspired by biological evolution.

Iteration and recursive Logic

Ø  Recursion and iteration are both different ways to execute a set of instructions repeatedly.

Ø  The main difference between these two is that in recursion, we use function calls to execute the statements repeatedly inside the function body, while in iteration, we use loops like “for” and “while” to do the same.

Ø  Iteration is faster and more space-efficient than recursion.

Ø  So why do we even need recursion?

Ø  The reason is simple — it's easier to code a recursive approach for a given problem. 

Ø  Recursion is when a function calls itself within its code, thus repeatedly executing the instructions present inside it.

Ø  Iteration is when a loop repeatedly executes the set of instructions like "for" loops and "while" loops.

Recursive Logic

When to Use Recursion?

Ø  Recursion is when a function calls itself directly or indirectly, and the function calling itself is called a recursive function.

Ø  It is mainly used when the solution to a bigger problem can be expressed in terms of smaller problems.

Ø  To terminate the recursion, we use some conditions so that the function knows when not to make any further recursive call to itself and return; otherwise, it will lead to infinite recursion once the function is called. Such conditions are called base conditions in recursion.

When to Use Iteration?

Ø  Iteration is when we execute a set of instructions repeatedly until the condition controlling the loop becomes false.

Ø  It includes initialization, comparison, executing statements inside the iteration, and updating the control variable. 

Ø  In iteration, it is necessary to have the right controlling condition; else, the program may go in an infinite loop.


Modular designing

Division of software into separate modules which are differently named and addressed and are integrated later on in to obtain the completely functional software.

Single large programs are difficult to understand and read due to a large number of reference variables, control paths, global variables, etc.

Reduces the design complexity and results in easier and faster implementation by allowing parallel development of various parts of a system.

Modular Programming

A software design technique that focuses on separating the program functionality into independent, interchangeable methods/modules. 

Each of them contains everything needed to execute only one aspect of functionality.

Modularity can be on different levels -

Libraries in projects

Function in the files

Files in the libraries or repositories

Modularity is all about making blocks, and each block is made with the help of other blocks. 

Every block in itself is solid and testable and can be stacked together to create an entire application.

All the object-oriented programming languages like C++, Java, etc., are modular programming languages.

Advantages of modular programming

Code is easier to read: As each functions perform different tasks 

Code is easier to test: some functions perform fewer tasks and also functions that perform numerous tasks. If the software is easily split using modules, it becomes easier to test. 

Reusability: There are times where a piece of code is implemented everywhere in our program. Instead of copying and pasting it, again and again, modularity gives us the advantage of reusability so that we can pull our code from anywhere using interfaces or libraries. 

Faster fixes: finding and fixing bugs becomes much more smooth and maintainable. 

Low-risk update: Unless there is a change in the API, there is a low risk for someone's code-breaking.  

Easy collaboration: Different developers work on a single piece of code in the team.

Disadvantages of modular programming

There is a need for extra time and budget for a product in modular programming.

It is a challenging task to combine all the modules.

Careful documentation is required so that other program modules are not affected.

Some modules may partly repeat the task performed by other modules. Hence, Modular programs need more memory space and extra time for execution.

Integrating various modules into a single program may not be a task because different people working on the design of different modules may not have the same style.

It reduces the program's efficiency because testing and debugging are time-consuming, where each function contains a thousand lines of code.


 


No comments:

Post a Comment

Research in Social Science

   C  Concept of Research in Social Science -         Understanding the concept of Research Methods, Techniques and Tools: Interview, Fo...