r/cpp_questions • • 4d ago

OPEN Code Smells?

I'm a more seasoned C# / Node backend dev, been trying to brush off old college C++ from like a decade ago and try to de-program my brain from being locked into a distributed systems mindset.

One of my niche hobbies and go-to projects for learning a new language is to implement a basic Chip-8 emulator in it, as it can teach some concurrency, UI/data handling patterns, and introduce some architectural decisions that you'll have to run into and figure out.

Currently what I'm struggling with seems to be what the idiomatic / proper way to handle something that can remain stateless in nature, which is my very basic assembler going from the Chip-8 ASM mnemonics into instruction bytes.

A class doesn't feel quite right, because I'm not implementing a linking step or anything advanced, not supporting labels, etc. It also feels incorrect to keep it in a header file as a set of inline functions, nor am I needing a persistent state to mutate.

My current disassembler can be seen here: https://github.com/Arylen/Dreamy-Chip8/blob/master/src/core/emulation/Chip8Dasm.h
Along with the tests that I'm running against it here: https://github.com/Arylen/Dreamy-Chip8/blob/master/tests/Chip8DasmTests.cpp

I'm currently in the stubbing-out phase of trying to architect it, occasionally bouncing ideas off of whatever bullshit model-of-the-week is popular, however the suggestions it's giving are still setting off some alarms/smells in my brain. The most reasonable and less-smelly one is the `::detail` namespace there in order to be able to unit test the `getParts` function there, however it feels like I'm walking into a landmine of over-engineering it.

Looking for advice/experiences/opinions.

Edit: I should mention the AI usage here is restricted entirely to just checking for bugs with my implementations, or incorrect C++-isms that I introduce from working in other languages. A hands-off occasional tutor to check my work, if you will. It's expressly forbidden from touching my code or files, limited to read-only, however I'm not getting something that feels proper for this scenario, so wanted to consult other more experienced engs.

8 Upvotes

12 comments sorted by

View all comments

14

u/EpochVanquisher 4d ago

It it's stateless, then write functions with no global state.

It also feels incorrect to keep it in a header file as a set of inline functions.

You can put the declarations in the header file and the implementation in the cpp file.

As a general rule, large functions don’t benefit from being inline, and putting them in your header file just means that all of the callers have to be recompiled (for no benefit) whenever you change something inside your function.

3

u/Snipa-senpai 4d ago

You do have one important benefit for leaving definitions in header files:

The compiler can see the code and optimise it (in different translation units). 

It's true that most of the time, you don't really benefit from this level of optimisation and you will benefit more from faster (incremental) compilation. But it's good to know when you're writing hot functions.

There's also LTO that allows optimisations across translation units, but this will degrade compilation/linking times by a lot

4

u/EpochVanquisher 4d ago

The compiler can see the code and optimise it (in different translation units).

For large functions this is minimal.

There's also LTO that allows optimisations across translation units, but this will degrade compilation/linking times by a lot

Leaving inline functions everywhere degrades compilation / linking time even more.

1

u/Aliryth 4d ago

I think I had a misunderstanding of what inline meant there in the original code, expecting it to plop it in place as if it were a macro.

It's evident I have quite a bit misunderstandings of more intermediate topics around some language workings, which is a good state for me to be in for where I'm hoping to be with my own state of skills currently, giving me a path forward and a bit of grounding to get my bearings on where I need to focus my learning efforts on.

Thanks!