top of page

Coding : Principles, Types and Concepts



Coding is a list of step-by-step instructions that get computers to do what you want them to do. Coding makes it possible for us to create computer software, games, apps and websites. Coders, or programmers, are people who write the programmes behind everything we see and do on a computer.


Coding Principles


1. Keep It Simple, Stupid (KISS)

It means you should be writing code as simple as possible. One of the rules of basic programming is to never get caught up in trying to be overly clever or showing off with a thick block of advanced code. If you can write a script in one line, write it in one line.


Here's a simple function:

function addNumbers(num1, num2) {
    return num1 + num2;
}

One programming principle in this spirit is to use clear variable names. Take advantage of coding libraries and use existing tools. Make it easy to come back after six months and get right back to work. Keeping things simple will save you so much needless suffering down the line.


2. Write DRY Code

The Don't Repeat Yourself (DRY) computer programming principle means, plainly, not repeating code. It's a common coding mistake. When writing code, avoid duplication of data or logic. If you've ever copied and pasted code within your program, it's not DRY code.


Take a look at this script:

function addNumberSequence(number) {
    number = number + 1;
    number = number + 2;
    number = number + 3;
    number = number + 4;
    number = number + 5;
    return number;
}

DRY code is easy to maintain. It's easier to debug one loop that handles 50 repetitions than 50 blocks of code that handle one repetition each.



3. Open/Closed

This principle of programming means that you should aim to make your code open to extension but closed to modification. This is an important principle when releasing a library or framework that others will use.


Their code will break. This will likely make your cohorts very unhappy. They won't want to use your library for much longer, no matter how helpful it may have been in its heyday.


Instead, release code that prevents direct modification and encourages extension. Basic programming principles like this separate core behavior from modified behavior. The code is more stable and easier to maintain.



4. Composition Over Inheritance

If you write code using object-oriented programming, you're going to find this principle of programming to be very useful. The composition over inheritance principle states: objects with complex behaviors should contain instances of objects with individual behaviors. They should not inherit a class and add new behaviors.


Relying on inheritance causes two major issues. First, the inheritance hierarchy can get messy in a hurry. You also have less flexibility for defining special-case behaviors. Let's say you want to implement behaviors to share:


Composition programming is cleaner to write, easier to maintain, and allows for flexibility-defining behaviors. Each individual behavior is its own class. You can create complex behaviors by combining individual behaviors.


5. Single Responsibility

The single responsibility principle states that every class or module in a program should only provide one specific functionality. As Robert C. Martin puts it, "A class should have only one reason to change."


Classes and modules often start off this way. Be careful not to add too many responsibilities as classes get more complicated. Refactor and break them up into smaller classes and modules.


The consequence of overloading classes is twofold. First, it complicates debugging when you're trying to isolate a certain module for troubleshooting. Second, it becomes more difficult to create additional functionality for a specific module. Good programming principles prevent these problems before they become problems to deal with.


6. Separation of Concerns

The separation of concerns concept is an abstract version of the single responsibility principle. This idea states that a program should be designed with different containers, and these containers should not have access to each other.


A well-known example of this is the model-view-controller (MVC) design. MVC separates a program into three distinct areas: the data (model), the logic (controller), and what the page displays (view). Variations of MVC are common in today's most popular web frameworks.


For example, the code that handles the database doesn't need to know how to render the data in the browser. The rendering code takes input from the user, but the logic code handles the processing. Each piece of code is completely independent.


The result is code that is easy to debug. If you ever need to rewrite the rendering code, you can do so without worrying about how the data gets saved or the logic gets processed.


7. You Aren't Going to Need It (YAGNI)

This principle means you should never code for functionality on the off chance that you may need something in the future. One of the most important principles of computer programming to learn is that you shouldn't try to solve a problem that doesn't exist.


In an effort to write DRY code, programmers may violate this principle. Often, inexperienced programmers try to write the most abstract and generic code that they can. Too much abstraction, however, causes bloated code that's impossible to maintain.


Only apply DRY programming principles when you need to; if you notice chunks of code written over and over, implement a layer of abstraction. Don't think too far ahead at the expense of your current code batch.


8. Document Your Code

With all of this talk of the principles of coding, it can be easy to forget about the human on the other side who may eventually be getting into your code themselves.


Any senior developer will stress the importance of documenting your code with proper comments. All languages offer them; you should make it a habit to write them. Leave comments to explain objects, enhance variable definitions, and make functions easier to understand.


Here's a JavaScript function with comments guiding you through the code:


// This function will add 5 to the input if odd, or return the number if even
function evenOrOdd(number) {
    // Determine if the number is even
    if (number % 2 == 0) {
        return number;
    }
    // If the number is odd, this will add 5 and return 
    else {
        return number + 5;
    }
}

Leaving comments is a little more work while you're coding. It takes time and steals your attention away from the real work at hand. You understand your code pretty well anyway, right? Who cares? It's worth remembering that nothing is disposable, even in the world of tech.


9. Refactor

Refactoring code means reviewing your code and looking for ways to optimize it, making it more efficient while keeping the results exactly the same. Codebases are constantly evolving. One of the principles of programming is remembering that it's completely normal to revisit, rewrite, or even redesign entire chunks of code. It doesn't mean you didn't succeed the first time you wrote your program; you're inevitably going to get more familiar with a project over time. Use that knowledge to adjust yourself as you make progress.


10. Clean Code At All Costs

Don't try to pack a ton of logic into one line. Leave clear instructions in your comments and documentation. If your code is easy to read, it will also usually be easy to maintain. Good programmers and readable code go hand-in-hand. Leave comments when necessary, adhere to style guides, and put yourself in the next guy's shoes whenever possible.


Types of Coding


There are four types of coding:

  • Data compression (or source coding)

  • Error control (or channel coding)

  • Cryptographic coding.

  • Line coding.

Data compression

It is the process of modifying, encoding or converting the bits structure of data in such a way that it consumes less space on disk. It enables reducing the storage size of one or more data instances or elements. It is also known as source coding or bit-rate reduction. Data compression enables sending a data object or file quickly over a network or the Internet and in optimizing physical storage resources. Data compression has wide implementation in computing services and solutions, specifically data communications. Data compression works through several compressing techniques and software solutions that utilize data compression algorithms to reduce the data size.


Error control coding

It is the coding procedure done to control the occurrences of errors. These techniques help in Error Detection and Error Correction. There are many different error correcting codes depending upon the mathematical principles applied to them. But, historically, these codes have been classified into Linear block codes and Convolution codes.


Cryptographic Coding

In computer science, cryptography refers to secure information and communication techniques derived from mathematical concepts and a set of rule-based calculations called algorithms, to transform messages in ways that are hard to decipher. These deterministic algorithms are used for cryptographic key generation, digital signing, verification to protect data privacy, web browsing on the internet and confidential communications such as credit card transactions and email.


Line Coding

A line code is the code used for data transmission of a digital signal over a transmission line. This process of coding is chosen so as to avoid overlap and distortion of signal such as inter-symbol interference.



The 5 Basic Coding Concepts

Here are five of the most important coding concepts that every young learner should know:


1. Variables

In programming, a variable is a value that can change, depending on conditions or on information passed to the program. Typically, a program consists of instruction s that tell the computer what to do and data that the program uses when it is running. The data consists of constants or fixed values that never change and variable values (which are usually initialized to "0" or some default value because the actual values will be supplied by a program's user). Usually, both constants and variables are defined as certain data type s. Each data type prescribes and limits the form of the data. Examples of data types include: an integer expressed as a decimal number, or a string of text characters, usually limited in length.


2. Data Structures

A data structure is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be accessed and updated efficiently. A data structure is not only used for organizing the data. It is also used for processing, retrieving, and storing data. There are different basic and advanced types of data structures that are used in almost every program or software system that has been developed. So we must have good knowledge about data structures.


Types of Data Structure:

1. Linear data structure: Data structure in which data elements are arranged sequentially or linearly, where each element is attached to its previous and next adjacent elements, is called a linear data structure. Examples of linear data structures are array, stack, queue, linked list, etc.

  • Static data structure: Static data structure has a fixed memory size. It is easier to access the elements in a static data structure. An example of this data structure is an array.

  • Dynamic data structure: In dynamic data structure, the size is not fixed. It can be randomly updated during the runtime which may be considered efficient concerning the memory (space) complexity of the code. Examples of this data structure are queue, stack, etc.

2. Non-linear data structure: Data structures where data elements are not placed sequentially or linearly are called non-linear data structures. In a non-linear data structure, we can’t traverse all the elements in a single run only.



3. Control Structures

A control structure is a codebase portion that supports decisions based on analysis of variables. These functional components are eminently useful in computer science and computer programming. Control Structures are just a way to specify flow of control in programs. Any algorithm or program can be more clear and understood if they use self-contained modules called as logic or control structures. It basically analyzes and chooses in which direction a program flows based on certain parameters or conditions. There are three basic types of logic, or flow of control, known as:

  1. Sequence logic, or sequential flow

  2. Selection logic, or conditional flow

  3. Iteration logic, or repetitive flow


4. Syntax

Syntax is the set of rules that define what the various combinations of symbols mean. This tells the computer how to read the code. Syntax refers to a concept in writing code dealing with a very specific set of words and a very specific order to those words when we give the computer instructions. This order and this strict structure is what enables us to communicate effectively with a computer. Syntax is to code, like grammar is to English or any other language. A big difference though is that computers are really exacting in how we structure that grammar or our syntax.


5. Tools

A programming tool may be any software program or utility that aids software developers or programmers in creating, editing, debugging, maintaining and/or performing any programming or development-specific task. A programming tool is also known as a software development tool. Tools are standalone utilities that provide or support a particular task within any phase of the development/programming cycle. For example, a debugger is a programming tool that helps programmers identify and resolve bugs within a program’s source code. Compilers, linkers, assemblers, disassemblers, load testers, performance analysts, GUI development tools and code editors are also all forms of programming tools.




The Tech Platform

0 comments
bottom of page