The only case I use snippets for is for debug code that I use often. Sometimes there are things I find myself doing a lot for debugging that don’t have any reason to be in code (e.g. nicely formatting certain objects for debug purposes)
Comment on How do you manage code snippets?
Zeth0s@lemmy.world 11 months ago
I create proper libraries. I don’t do snippets because they make code dirty, redundant and difficult to read on the long run.
I actively discourage people in my team to use snippets copy and pasted everywhere themselves. If it’s reusable code, it should be usable by everyone and well tested
savedbythezsh@sh.itjust.works 11 months ago
xmunk@sh.itjust.works 11 months ago
Write a function or macro so you can reuse them. The project I work on has dozens of debug assisting code paths. Here are two examples: normally when talking to the db you’ll call
run($sql, $boundVariables)
on a handle. Alternatively you can calldebug($sql, $boundVariables)
to have the handle run the query normally then rerun the query prefixed withEXPLAIN (blah,blah)
to get the execution plan. We also haveassembleEmulatedQuery($sql, $boundVariables)
which will manually replace all the binding tokens in the SQL with their values, do some string escaping and return a big honking string that you can dump into the database… that last one is useful for performance tuning since it can be used to easily capture expensive query forms. Also - assembleEmulatedQuery will throw an exception on our production environment because it’s unsafe due to the potential of SQL Injection.Build debugging functions and add tests over them - future you will thank you!
abhibeckert@lemmy.world 11 months ago
This. Replace commonly used code snippets with a well written library that reduces them to a single function call and take advantage of auto-complete in your IDE.