LemonHX

LemonHX

CEO of Limit-LAB 喜欢鼓捣底层的代码,意图改变世界
twitter
tg_channel

On Header Files

In computer programming, a header file is a file that contains declarations of functions, classes, variables, and other entities. Header files are commonly used to reference external libraries or modules in a program.

The purpose of a header file is to allow programmers to use functions, variables, and other entities provided by external libraries or modules when writing code. These external libraries or modules may have complex functionality or require many lines of code to implement, and header files provide a convenient way to reference and use these functionalities, making it easier for programmers to use them.

Header files typically contain declarations of functions, classes, variables, and preprocessor directives. When using header files, programmers need to include these header files in their code so that the program can find and use the functions, classes, variables, etc. defined in them. The way header files are included may vary depending on the programming language.

Why do many languages no longer have header files?#

In modern programming languages, many languages no longer use explicit header files. This is mainly because traditional C language header files have some disadvantages, such as:

  1. Header files are prone to errors: When using header files, developers need to ensure that all definitions in the header files match the definitions in the implementation files. This is prone to errors, especially when the header files are modified. These errors can lead to compilation errors or runtime errors, wasting time and resources.

  2. Header files slow down compilation time: When using header files, the compiler needs to process the header files during compilation, which slows down the compilation time. If the header files are very large or contain complex dependencies, this can result in longer compilation times.

  3. Header files are not conducive to code refactoring: Header files can become obstacles when code needs to be refactored. If the definitions in the header files do not match the implementation or are no longer used, this can make code refactoring more difficult.

Therefore, modern programming languages tend to use other methods to address these issues. For example, many languages use modules or namespaces to organize code and use automation tools to handle dependencies. These approaches can reduce the problems introduced by header files and improve code maintainability and readability. At the same time, modern compilers have become more intelligent and can better handle code structure and dependencies, reducing the problems introduced by header files.

Why do I think header files are still needed now?#

Header files provide a top-down development approach that allows developers to define interfaces before implementing their functionality. This approach makes the code clearer, easier to maintain, and easier to extend.

Traditional C language header files also have some disadvantages, such as potentially slowing down compilation speed, complex code maintenance, and not conducive to code modularization. Therefore, many modern programming languages and frameworks have adopted other ways to organize and manage code, such as namespaces, modules, and packages.

However, I believe that we can refute this argument from multiple perspectives:

Is it possible to modularize header files?#

In fact, I have always believed that modules are a new way of using header files. However, languages without header files usually place the content that used to be written in header files directly in the business code, which increases coupling and makes maintenance more difficult.

My point is that we can use both header files and modules, but the role of header files should be limited to declarations, not implementations. This way, we can avoid the disadvantages of header files and the disadvantages of modules.

For example, let's imagine a hypothetical language called Rust that supports both header files and modules, but header files can only be used for declarations, not implementations.

// header xxx.mod.rs
struct Something

impl Something {
    fn method1(&self) -> sometype
    fn method2(&self) -> sometype
}


// source xxx.impl.rs
struct Something {
    // ...
}

fn Something::method1(&self) -> sometype {
    // do something
}

fn Something::method2(&self) -> sometype {
    // do something
}

The advantage of this approach is that we can implement the business code first and then implement the methods in the header file after writing the entire business code. This way, we can avoid the disadvantages of header files and the disadvantages of modules.

// source of some business code

fn use_something(input: sometype) -> sometype {
    let something = Something::new();
    let i1 = something.method1();
    let i2 = something.method2();
    somefunc(i1, i2)
}

We can assume that the methods in the header file initially generate an empty implementation for us, so we can write unit tests first and then implement the methods in the header file.

// source of some business code test
#[test]
fn test_use_something() {
    let res1 = use_something(input1);
    assert_eq!(res1, output1);
    let res2 = use_something(input2);
    assert_eq!(res2, output2);
    // ...
}

Then we can implement the methods in the header file to make them pass the unit tests.

Is it possible that we don't need to write code ourselves anymore?#

If we have this mechanism, we can use AI on a larger scale to generate code, or even automatically generate code. This way, we can focus more on abstract problems rather than specific implementation issues.

Another issue is that languages without header files do not support dynamic linking well#

In languages without header files, the linker needs to handle function calls without knowing the function parameters and return types.

The implementation of the compiler determines that all the code needs to be loaded in advance, which actually causes a lot of waste because we don't need all the code every time, sometimes we only need a part of it.

Languages with header files can support dynamic linking by allowing the linker to dynamically load shared libraries at runtime. This method allows for more precise loading and linking of code when needed.

Some people may argue at this point, saying that we don't need dynamic linking, static linking is better, we can compile all the code into the executable, so we don't need dynamic linking.

I just think these people are idiots.

Dynamic linking has the following advantages:

Saves memory: When multiple programs share the same library, dynamic linking allows them to share the code and data in the library, saving memory space.

Easy updates: If the code in the library needs to be updated, only the library file needs to be replaced, without the need to recompile the programs that use the library.

Flexibility: Dynamic linking allows programs to load library files at runtime, enhancing the flexibility of the program. This means that the program can dynamically adapt to user needs without the need for recompilation or redeployment.

Shared libraries can be used by multiple programs: The code in shared libraries can be used by multiple programs, avoiding the problem of duplicating the same code.

Reduces executable file size: Using dynamic linking can reduce the size of the executable file because the code and data in the library do not need to be copied in each program.

I think the current trend is wrong, everyone wants everything to be static. If you want to use unikernels, go ahead, if you don't want to, let users install your software and run it in a VM or Docker. It's ridiculous.

Interface Description Language#

Do we still need this with header files?

Just write a transformer to convert header files into IDL.

It's a nightmare to support IDL in languages without header files.

The problem of closed-source code#

I think the current trend of relying on modules and package management is wrong and completely ignores the issue of closed-source code.

If there are no header files, we must distribute all the source code, and even if we publish closed-source binaries, they may be reverse-engineered, let alone the source code.

Conclusion#

When discussing the advantages and disadvantages of header files, we should not only focus on their disadvantages and ignore their significance.

Header files are an important invention in the development of computer programming. They greatly simplify code writing and maintenance, and accelerate the development process of programs.

In modern programming, we can choose to use modular systems, static analysis tools, etc. to address the problems caused by header files. We can also choose to use new programming languages that do not require header files to achieve a better development experience. However, this does not mean that we should completely abandon header files, as they still have an irreplaceable role, especially in large projects.

Therefore, we should consider the advantages and disadvantages of header files, use them correctly, and constantly explore new technological means to improve programming efficiency and code quality.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.