Do you keep them in your IDE, or elsewhere? Do you have an app for that? Are they easily shared?

I realized I have no system at all but could use one to make it easier to find code I’ve written and might need again some day.

By snippets, I am referring to any chunk of code / text in any format or language, of any length.

Thanks!

EDIT A DAY LATER: Thanks you all! Reading all these ideas, I got inspired to create my own little web app. Wish me luck… :)

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

This. Replace commonly used code snippets well written code that reduces them to one or two lines of code and take advantage of auto-complete in your IDE.

For the rare case where that doesn’t make sense… I’m I’ll ususally find (or create) an extension/plugin for my IDE. Something that can be smarter than any snippet.

Tekhne
link
fedilink
41Y

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)

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 call debug($sql, $boundVariables) to have the handle run the query normally then rerun the query prefixed with EXPLAIN (blah,blah) to get the execution plan. We also have assembleEmulatedQuery($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!

Create a post

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



  • 1 user online
  • 1 user / day
  • 1 user / week
  • 1 user / month
  • 1 user / 6 months
  • 1 subscriber
  • 1.21K Posts
  • 17.8K Comments
  • Modlog