Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code quality by refactoring variable names #57

Open
wejrox opened this issue Jul 17, 2024 · 0 comments
Open

Improve code quality by refactoring variable names #57

wejrox opened this issue Jul 17, 2024 · 0 comments

Comments

@wejrox
Copy link

wejrox commented Jul 17, 2024

The variable names used in the code describe what the type of data their holding is, instead of the contextual information required to read the code.

For example, DWORD dw; could be named DWORD bytesWritten; so that when you're reading the code without context it's obvious whether it's important to you.

Here's a diff as an example that improves code readability

Before

if (!std::filesystem::exists(filePath)) {
	HANDLE hOrg = CreateFileA("config.ini", (GENERIC_READ | GENERIC_WRITE), NULL, NULL, CREATE_ALWAYS, NULL, NULL); DWORD dw;
	if (hOrg) {
		HMODULE hModule = GetModuleHandle(L"dinput8.dll"); // Get handle to current DLL
		HRSRC hResource = FindResource(hModule, MAKEINTRESOURCE(IDR_RCDATA1), RT_RCDATA);
		HGLOBAL hResourceData = LoadResource(hModule, hResource);
		DWORD resourceSize = SizeofResource(hModule, hResource);
		LPVOID resourceData = LockResource(hResourceData); Sleep(2); SuspendThread(MainMain::mainTHread);
		if (WriteFile(hOrg, resourceData, resourceSize, &dw, NULL) && dw == resourceSize) { 
			CloseHandle(hOrg);
			ResumeThread(MainMain::mainTHread);
		}
		else
		{
			CloseHandle(configFileHandle);
			MessageBox(nullptr, L"your config.ini file doesn't exist, please re-download config.ini from Ezorsia v2 releases at https://github.com/444Ro666/MapleEzorsia-v2", L"bad config file", 0);
			ExitProcess(0);
		}
	}

After

HANDLE configFileHandle = CreateFileA("config.ini", (GENERIC_READ | GENERIC_WRITE), NULL, nullptr, CREATE_ALWAYS, NULL, nullptr);
	DWORD bytesWritten;
	if (configFileHandle)
	{
		HMODULE dllHandle = GetModuleHandle(L"dinput8.dll"); // Get handle to current DLL
		HRSRC dllResource = FindResource(dllHandle, MAKEINTRESOURCE(IDR_RCDATA1), RT_RCDATA);
		HGLOBAL dllResourceData = LoadResource(dllHandle, dllResource);
		DWORD resourceSize = SizeofResource(dllHandle, dllResource);
		LPVOID lockedResourceData = LockResource(dllResourceData);
		Sleep(2);
		SuspendThread(mainThread);
		if (WriteFile(configFileHandle, lockedResourceData, resourceSize, &bytesWritten, nullptr) && bytesWritten == resourceSize)
		{
			CloseHandle(configFileHandle);
			ResumeThread(mainThread);
		}
		else
		{
			CloseHandle(configFileHandle);
			MessageBox(nullptr, L"your config.ini file doesn't exist, please re-download config.ini from Ezorsia v2 releases at https://github.com/444Ro666/MapleEzorsia-v2", L"bad config file", 0);
			ExitProcess(0);
		}
	}

The benefit of improved code readability is that the barrier to entry reduces, and the project is more likely to receive contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant