jlyons210 / discord-bot-ol-bootsie

Ol' Bootsie is a highly configurable Discord bot that interfaces with the OpenAI API, written for Node.js in Typescript.
The Unlicense
2 stars 2 forks source link

Clean up `Logger` implementation #107

Closed jlyons210 closed 1 year ago

jlyons210 commented 1 year ago

Issues I've identified after reading 3 chapters of "Clean Code" (I know...)

Implementation static async log() accepts a LoggerConfiguration interface as its parameter. The interface has three members: message, logLevel, and debugEnabled?. It contains a switch block that forks logic based upon the provided logLevel.

Problems P1. Functions should do one thing - this one does three. P2. debugEnabled has to be passed on every call. This should be set at creation of a Logger instance. P3. I should strive to make log* monadic functions. Function calls are currently convoluted. P4. LogLevel should not need to be exported to interface with the functions. It's revealing internal implementation.

Solution S1. Break out the current log function into logInfo, logError, and logDebug public functions [P1, P3]. S2. Re-implement Logger as a proper class that can be instantiated as a class member where it is consumed. It currently lacks a constructor and only contains static functions [P2]. S3. Establish a constructor that accepts configuration argument debugEnabled, removing this from every call to log* [P2]. S4. With [S1, S2, S3] implemented, the LogLevel enum can be abolished.

I'll have to go through the whole codebase to update function calls, and this will be much cleaner in the end.