laitimes

Back to basics: Understanding the source code

Back to basics: Understanding the source code

Anyone interested in creating and maintaining software should understand the importance of code and the logic and design patterns of coding principles.

译自 Back to the Basics: Understanding Source Code,作者 Robert Curlee。

Whether you're browsing a website, watching a TV show, using a program on your phone, or even turning on the air conditioner in your car, the source code is the driving force behind all of these capabilities.

Developers use source code to describe the behavior of electronic devices in a computer. But coding is no longer just a programmer's job. Since computers are at the heart of almost all modern devices, programming has become more common. Anyone interested in the fundamentals of software creation and maintenance needs to understand not only why code is important, but also the logical concepts and design patterns in coding principles.

What is Source Code?

Source code is a set of logical instructions written by a programmer to create software. The instructions that make up these algorithms are written in specific programming languages such as JavaScript, HTML, CSS, Python, Java, or C#. These instructions act as detailed recipes that the computer follows, formulating every action required to perform a series of tasks. These tasks are collected in a file called a program, which is written in a language that a person can understand.

Just as DNA carries the instructions that determine how cells grow and function, think of source code as the DNA of every piece of software you use. Code helps in software creation, maintenance, and enhancement. It's at the heart of all our digital tools, apps, and systems.

Why is source code important?

Whether it's computing basic math or a complex system that runs billions of trades, such as a stock exchange, source code is the foundation of software in the devices and technologies we use every day.

Source code is critical to software maintenance, including patching issues, optimizing issues, and enhancing them in new ways. Through open source projects, software developers collectively develop applications and a shared library of reusable features that foster innovation and accelerate technological progress.

One of the most critical aspects of coding is security. Identifying and addressing vulnerabilities in your code can prevent attackers from exploiting your application. Knowing what constitutes a threat in your code is often difficult and a challenge when building secure and stable applications.

What are some common types of source code?

While there are several ways to categorize source code, the most common ones are:

  • Open Source vs. Proprietary: As the name suggests, open-source code can be used or modified by anyone. It is collectively owned by the community and is free for all. Often, the author of open source code waives its rights so that the code can be used without restriction. The open source movement is very powerful because it fosters innovation, and technological advancements immediately benefit everyone. Proprietary or closed source code is private and can only be used by the owner. Companies or individuals with proprietary code are only allowed to modify or use it with explicit permission. Keeping code secret is done to protect the owner's intellectual property, often for profit.
  • Compilation and interpretation: Code can be categorized not only by language, but also by language compilation into an executable application or executed by an interpreter. For compiled languages, the compiler converts high-level source code into CPU-understandable 1 and 0 machine code instructions and packages them into a standalone application. The computer can then read and execute the application directly. Interpreted languages, such as JavaScript, are read by interpreters at runtime and translated into CPU instructions. Interpreted languages allow for more flexibility and are easier to test, but often have poor performance compared to compiled applications.

What is a source code sample?

Let's take a quick look at some JavaScript and C code samples to see how they differ. Both examples define a function that adds two numbers into a variable and then prints and returns the sum. If you're not familiar with the concept, a function is a set of repeatable instructions that a program uses.

JavaScript

Developers typically use JavaScript to build web and server applications. The function shown here takes two parameters, calculates the sum of the parameters, and displays the sum using the built-in method console.log. You'll see that console.log's arguments take advantage of JavaScript's convenient string concatenation by adding a static string to a variable named sum.

function displaySum(a, b) {
    let sum = a + b;
    console.log("The sum is: " + sum);
    return sum;
}           

C

Developers often use C for system software (the platform on which other software runs) and embedded systems. This C function does the same thing as the JavaScript function above. However, it uses the C standard library function printf to output the summation result. The %d in the printf statement is a placeholder for the result of the sum of integers, demonstrating C's ability to format the output in a specific type.

#include <stdio.h>

int displaySum(int a, int b) {
    int sum = a + b;
    printf("The sum is: %d\n", sum);
    return sum;
}           

At first glance, the syntax of these two functions looks very similar, but the more subtleties you pay attention to, the more you will see how different the two languages are.

What is a source code tool?

Coding tools help developers create, manage, analyze, and improve code quality while helping them work more efficiently. Many automated tools can detect issues in your code that can lead to bugs, security flaws, and code odors. With the help of these tools, developers can get the most value from their code.

Here are the most common types of coding tools:

  • Integrated development environments (IDEs), such as VS Code, Visual Studio, and IntelliJ, are essential to help developers with software development. The IDE includes a specialized text editor that comments the code as you type, identifying syntax or other issues in the code. They also integrate with codebases and build pipelines to manage version control as you develop.
  • DevOps CI/CD tools, such as GitHub, GitLab, BitBucket, and Azure DevOps, include code repositories that store your code in a single source of truth so that development team members can easily access and share it. In addition, the repository tracks changes in the source code, so you can manage different versions and undo changes. Branching and merging capabilities exist so developers can work on code at the same time as they develop without worrying about overwriting each other's work or breaking stable code. DevOps tools include automation of the build process so that changes can be released quickly and easily.
  • Static code analyzers run seamlessly in developer workflows to detect issues in code that lead to bugs, vulnerabilities, and technical debt without having to build and execute applications. These analyzers allow developers to catch coding errors at the earliest stages of the development process before the application is tested and released to production. Other benefits include helping to enforce coding standards and best practices, reducing manual code review, and educating developers on how to code properly, which can help improve their skills.

Source code tools don't just detect problems. They help make developers better and improve the quality of their work, leading to more reliable and secure software that businesses value. Tools and solutions like SonarQube, SonarCloud, and SonarLint can improve code quality and help developers get the most value from their most important asset: source code.

Final Thoughts

Source code quality can determine the success or failure of applications and systems that are critical to our daily lives and work. As a developer, you not only need to understand coding concepts, but also make sure you're using the right development and testing tools. By integrating these essential components, you'll efficiently produce high-quality work that will ensure your success.

Read on