Like if I’m using print statements to test my code. Is it okay to leave stuff like that in there when “publishing” the app/program?
Edit: So I meant logging. Not “tests”. Using console.log to see if the code is flowing properly. I’ll study up on debugging. Also, based on what I managed to grasp from your very helpful comments, it is not okay to do, but the severity of how much of an issue it is depends on the context? Either that or it’s completely avoidable in the first place if I just use “automated testing” or “loggers”.
The question mixes up tests and logging. You are referring to logs.
Use a good logging package. It will allow you to distinguish at least (possibly with verbosity levels)
- trace
- debug
- information
- warning
- error
In production, configure the log level to be info-level or above, everything below will be hidden.
Lower levels can be useful for debugging automated tests. Possibly via a flag in production as well.
I honestly never realised the terminology was that important (so I never put much effort into remembering any of it). Yes I meant logging. I’m having trouble understanding the rest of what you mean though.
Are you familiar with automated testing? From the subject line I thought this was going to be about that, but the body of the post is something else.
Great question.
There’s no universal rule against it and descriptive logs and errors are considered good practice, but one-off test statements in output are a common code smell, which isn’t a proper bug but suggests there might be one elsewhere.
Why?
The reasoning for this varies but for example:
- in most settings, output is expected to be meaningful; for example a print statement in a C program is included in its result and expected to be parsable, even if it’s just a number like 0 (success)
- logging specific values can expose runtime data that sometimes shouldn’t be exposed, or can otherwise offer an attacker intel about the application structure
- debug statements in logging can indicate the dev has not yet learned to use a debugger, the preferred tool for that job
- logging non-descriptive or otherwise unhelpful messages can indicate the developer is aware of a bug they’re attempting to catch on prod, or maybe just has a habit of not cleaning up after themselves
- otherwise, obvious output-based testing might suggest the developer is not writing unit tests or maybe has no test harness at all, which for any larger application is a big red flag to most experienced developers
Again there’s no hard and fast rule against it, and whether it’s frowned upon depends on the context. For example, I actually expect slapdash logging in stuff like one-off scripts, app mockups, recently scaffolded projects, and so forth.
Also, not every codebase merits a full testing solution. I would consider it a form of premature optimization, which is more of a process inefficiency than a mistake but should still be avoided.
Most importantly, it’s OK to be a beginner. I wouldn’t think poorly of a developer due to a code smell like this. It’s more like an opportunity to learn, and IMO you’re ahead of the game for even thinking to ask.
Edit: use spoiler for info dump
In general no. They aren’t even ok during development. Instead you should learn to use a proper debugger. They let you break at a certain point, inspect all the variables, step through the program.