Qcloud1223 / ics-ld-public

Course project for Introduction to Computer Systems at XJTU: A basic x86-64 static linker.
4 stars 3 forks source link

Proposal to Encapsulate Functions and Global Variables in a Static-Linker Class #3

Open ccczzc opened 4 months ago

ccczzc commented 4 months ago

Considering that our codebase is written in C++, I would like to suggest encapsulating the functions and global variables in the main.cc, resolve.cc, and relocation.cc files within a StaticLinker class. This approach aims to eliminate the need for passing parameters between functions and minimize the usage of global variables.

By encapsulating the relevant functionality within a class, we can achieve better organization and modularity in our codebase. The class can act as a container for related functions and variables, allowing for better encapsulation and reducing the reliance on global state.

abdtyx commented 4 months ago

I don't think this is necessary. Personally speaking, I prefer the current design. You can provide an implementation of your design, though.

Qcloud1223 commented 4 months ago

I agree encapsulating all functionalities in a class is more modern C++. I'm not a production-level C++ programmer so I did not realize I could do that though. However, we are just writing a small toy program with no more than 1K LoC. Writing in C or C++ style hardly makes a difference.

This approach aims to eliminate the need for passing parameters between functions and minimize the usage of global variables.

I don't think there is anything wrong with global variables. Sharing states among class members across compilation units is eventually implemented as global variables with implicit compile-time checks, which can help preventing bugs in production. But again, we are a toy project.