Programming: Doing More with Less (The Principle of Efficiency and DRY)
What Are We Trying to Do When We Write Code?
When a program is written, the goal is for the computer to perform specific tasks. At a more detailed level, programs often involve processing data, executing actions based on that data, and then either displaying results or using them for further processing.
What Affects How We Write Code?
Several factors influence how code is written:
Programming Language:
The programming language provides a set of "units of operation" that can be performed by the computer processor. Different languages can achieve the same result, but the code will look different based on each language's syntax and fundamental "units of operation."
What is a unit of operation?
A unit of operation is an instruction or action that can be performed in one line of code in a programming language. Everything that can be done in a single line of a language represents its units of operation.
Here is a list of languages and their units of operation:
- MOV R1, 42 (move value 42 into register R1)
- ADD R2, R1, R3 (add values in R1 and R3, store in R2)
- JMP label (jump to a labeled instruction)
- x = 5; (Store value in labeled memory)
- Sum = a + b; (add two values and store result in labeled memory)
- Add(); (calling function or calling a stored procedure/predefined instructions)
- Select name From table_name Where condition; (get a collection of records based on a condition)
- Create Table table_name (column1 datatype, column2 datatype); (create a table for storing records)
- Select Count(*) From table_name; (perform aggregation operations like sum, count, average, etc.)
- (Number->string 1001 2) (converting number to string)(
- define number 10) (create a labeled variable and store 10 in it)
- (define (square x) (* x x)) (define a function that outputs the square of the input)
Each language has its own units of operation. Depending on the units of operation a language offers, it may be more suitable for certain tasks than other programming languages.
When code is written, the sequence of "units of operation" the computer should execute is being dictated. From these instructions, applications like Photoshop or games like GTA are built.
Input Data Structure and Desired Output:
Beyond the choice of language, the specific sequence of instructions needed is heavily influenced by the data available and the specific result that is desired.
Consider a mathematical example involving quadratic equations to illustrate how both the structure of input data and the desired output affect how code is written.
Recommended by LinkedIn
The code required to achieve the desired output depends critically on the form of the input data and what specific output is needed.
This example shows that regardless of the programming language, the specific instructions and logic implemented are directly shaped by the structure of the input data (which form of the equation is provided) and the exact nature of the desired output (roots or vertex).
Why Create Multiple Different Coding Languages?
Processors fundamentally understand machine code. The difference between assembly and machine code is minimal, so it can be said that the processor understands assembly language.
All other languages must be converted (compiled or interpreted) into assembly language to run on the processor. Given this, why were these higher-level languages created?
Online resources list many reasons for the creation of higher-level languages. Some of the common reasons include:
These are some of the primary reasons for the development and widespread use of different coding languages, especially higher-level ones.
However, underlying many of these reasons is a core principle: DRY (Don't Repeat Yourself).
Principle Of Efficiency
The DRY principle is typically introduced as a concept of reducing code repetition. It can be argued that DRY is a specific application of efficiency, a principle practiced across many fields, not just coding. Efficiency, in this context, is about achieving a goal using less "work" or "human input." Efficiency is a key goal in software development.
Let's revisit the reasons for higher-level languages from the perspective of efficiency:
Efficiency is a principle that spans multiple fields, including mechanical engineering, mechatronics, the medical field, and more. Consider the following examples, which demonstrate achieving a goal with less human effort or input:
All these examples illustrate achieving a goal with less input or effort. That's the essence of efficiency—it's a ubiquitous concept that goes by many names or phrases, such as:
In software, the goal is defined, and generally, the less code written to achieve that goal efficiently and correctly, the better. Many features and programming principles exist specifically to increase efficiency by reducing the amount of code that needs to be explicitly written or repeated. Some of these features include:
Additionally, many languages include unique syntactic features or libraries designed to minimize the amount of code needed for common tasks (sometimes called "syntactic sugar" or shortcuts). List comprehensions in Python are a classic example of a shortcut for writing common loop patterns efficiently. These features all contribute to writing less code, making the programmer more efficient, and adhering to the DRY principle.
CONCLUSION
Achieving project goals often benefits from writing less code and minimizing effort. By leveraging the principles and language features discussed here, developers can find ways to improve efficiency and adhere to the DRY principle in their work.