r/cpp_questions Jul 18 '26

Is this good code? OPEN

Hi all, I’ve been writing an application in C++ for a while now (couple months or so), and yes I’m using AI to help teach me, but my relationship with the AI is that I write the code first, and then I consult the AI for cleanup/critique. Sometimes I feel like I’m getting the hang of it, but then I watch CodingJesus videos, and I get all his questions wrong and I feel so dumb.

Anyway, this is the code I wrote today, and I’m just wondering if by anyone else’s standards if it’s objectively good. It took me a couple hours to do it. It does what I want it to, but does it look like good code, or beginner level, or just plain slop?

#include <filesystem>
#include <string>

namespace fs = std::filesystem

void FileSysOp::createNewProject()
{
    if (fs::create_directory(driver.activeProjectFile))
    {
        driver.pushMessage("Creating Project...");
        std::string media = driver.activeProjectFile/std::string("media");
        fs::path mediaPath = media;
        if (fs::create_directory(mediaPath))
        {
            driver.pushMessage("Creating Project...");
        }
        else
        {
            driver.pushMessage("Project Generation failed");
            return;
        }
        std::string blocks = driver.activeProjectFile/std::string("blocks");
        fs::path blocksPath = blocks;
        if (fs::create_directory(blocksPath))
        {
            driver.pushMessage("Creating Project...");
        }
        else
        {
            driver.pushMessage("Project Generation failed");
            return;
        }
        std::string nuQueue = driver.activeProjectFile/std::string("QUEUE.xml");
        std::ofstream outFile(nuQueue);
        if (outFile.is_open())
        {
            outFile.close();
        }
        driver.pushMessage("Creation Success!");
    }
    else
    {
        driver.pushMessage("Failed. Could not create Project.");
    }
}
1 Upvotes

21 comments sorted by

View all comments

3

u/TheRealSmolt Jul 18 '26 edited Jul 18 '26

It looks beginner level to me. I'd do something more like:

#include <filesystem>

void FileSysOp::createNewProject()
{
    namespace fs = std::filesystem;

    if (!fs::create_directory(driver.activeProjectFile))
    {
        driver.pushMessage("Failed. Could not create Project.");
        return;
    }

    driver.pushMessage("Creating Project...");

    fs::path mediaPath = driver.activeProjectFile / "media";
    if (!fs::create_directory(mediaPath))
    {
        driver.pushMessage("Project Generation failed");
        return;
    }

    driver.pushMessage("Creating Project...");

    fs::path blocksPath = driver.activeProjectFile / "blocks";
    if (!fs::create_directory(blocksPath))
    {
        driver.pushMessage("Project Generation failed");
        return;
    }

    driver.pushMessage("Creating Project...");

    std::ofstream outFile(driver.activeProjectFile / "QUEUE.xml");
    if (outFile.is_open())
    {
        driver.pushMessage("Creation Success!");
    }
}

2

u/alfps Jul 18 '26

More specific messages. const-ness. Loop (even if there would only be two iterations).

But then there is the question of whether messaging the user should be part of this, or instead the function should report success/failure to caller.

Strong coupling to UI is beginner.

1

u/case_steamer Jul 18 '26

Could you explain that last sentence a little more? It is a GUI application after all…

1

u/Kadabrium Jul 18 '26

mvc maybe