DEV Community

Andrew Chou
Andrew Chou

Posted on

Code snippets or nah?

Code snippet tools seem wildly popular in many text editors and IDEs. While I haven't worked on any heavy projects, I don't find much appeal in using code snippets. I can understand the productivity benefits that they can provide, but I feel as though it can take away from the enjoyment of understanding what you're writing to some extent.

Am I missing something or being naïve about this? I would love to read thoughts from people who use them in a more professional environment or work on things daily!

Top comments (12)

Collapse
 
alephnaught2tog profile image
Max Cerrina

I love snippets--they help me write more consistently and quickly, eliminate some errors, and leaves more mental CPU and enjoyment for me to spend on writing the code I care about and want to focus on. I try to avoid them in a language until I'm comfortable enough to tell where I would want them, and I personally won't use them unless I know what I am... snippeting?..., and usually I prefer writing my own regardless.

Some are trivial and I don't give a second thought too--stuff like a barebones HTML skeleton, or for public static void main(String[] args){...} in Java, etc etc. Some are to prevent mistakes are quick for me to make--if I'm doing Javascript and adding event listeners, I'd just as soon type addevi and expand it out so I don't forget the last parenthesis after the anonymous function (which at least now is how I usually write them, until I know if I'm going to be reusing it), etc etc.

Some stuff is for consistency--I have one set up for forms, where I type labin and it expands into the label, the input (defaulted to text), and all I have to do is type in what I want in the name spot, and it prepopulates the ID and label content for me as well. Stuff like that I super recommend, because it helps so much to keep things consistent--I have fallen into a rhythm for my naming conventions for forms (this past semester was PHP and MySQL, so all the forms ever), and like things super explicit, and know that my id attribute will almost certainly be whatever the name is with _id appended, and using a snippet means I'm typing that once and it populates it the way I expect it.

At the end of the day, I feel like snippets are just autocomplete for variables and functions with a lot more layers and customizability; same way I embrace autocomplete to make sure I'm not suddenly calling the method calculateTotal instead of calculateTotals, I'm all for snippets and anything else that outsources details that human brains largely suck at to the computer, so that the human brains can do what they're good at, the actual coding parts.

I also appreciate them for coursework where I know the teacher wants a certain comment style to align with a certain method format, for example, and I can get that set up and make sure it matches right off the bat.

Collapse
 
recursivefaults profile image
Ryan Latta

I use them regularly in only one kind of case. I use code snippets to do something that is esoteric but required in a technology. The example that comes to mind for me is from iOS. If you were building a table view you'd have to implement one method that had in it a lot of very specific things that were easier to do as a snippit than remember each time.

Past that though I refrain from using them, mostly because I don't like being reliant on any given tool. Just a personality quirk of mine.

Collapse
 
andrewchou profile image
Andrew Chou

That's a good point! Yeah I guess some technologies can have some quirks that code snippets help with. Definitely agree with you on trying to stay away from tool dependence if possible 👍🏼

Collapse
 
alainvanhout profile image
Alain Van Hout

It’s rather important to note that different people here understand ‘snippets’ as being (two) different things:

  1. Non-minimal chuncks of code that you insert
  2. Minimal language constructs (e.g. a for-loop) that is autocompleted by your IDE

The first is akin to grabbing stuff from stackoverflow: chances are it might generally address part of your issue, but it won’t be a perfect fit for it. For most business code, the matter at hand tends to be far too specific for copy-pasting to be well-suited.

The second is simply a time-saver and typo-preventer: it doesn’t solve your issues, it just allows you to write code with less effort and less chance for stupid errors. (That is, a definite plus)

Collapse
 
andrewchou profile image
Andrew Chou

Yeah I think I was referring more to the latter to be honest, but you make a good point!

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

I never use snippets. I prefer to invest time in avoiding repetition rather than making it easier. Not to say I never do anything repetitive. @alephnaught2tog mentioned snippets for common HTML combinations, which could save time. But I also try to avoid getting too invested in my tools.

Collapse
 
alephnaught2tog profile image
Max Cerrina

Yeah, as I think about it more too, I feel like the vaaaaaaaaaaaaaaast majority of snippets I actually use are either hyperspecific and temporally bound (ie a formatting thing for a specific class requirement) or HTML hunks where there is pretty little there in terms of flexibility anyways.

Collapse
 
andrewchou profile image
Andrew Chou

That's how I generally feel about it. Though I can see how avoiding repetition could be difficult to avoid when working on something all day every day

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

I think this is somewhat of a personality preference, too. I am a builder rather than a maintainer, so I would never be happy in the long term working on something where snippets were the right tool for the job.

Although I guess you could say helper functions are like snippets. They streamline common combinations of code. A lot of the infrastructure code I've made reads like a DSL and uses helpers for common things. However, they are just reusable code rather than something in an outside tool.

Collapse
 
aurelkurtula profile image
aurel kurtula

I use emit for html html:5 and I use a snippet for css, a small resetter.

Whilst using those two, I found that at some point I forgot how to write them without using the snippet.

Those two aren't very important, and I've since learned the logic behind them, but I imagined that if I relied on snippets I'll start forgetting things.

So I don't use them.

Besides, I haven't found any appealing tool to universally manage snippets! And having to configure snippets in vs code, sublime and so on, is just annoying

Collapse
 
andrewchou profile image
Andrew Chou

Haha forgetting how to actually write something would be a concern that I'd have if I started using snippets

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

I dislike code snippets for a few reasons:

  • Most, if not all, snippets I've seen online, for code I'm looking for, have mistakes in them. I've not once been able to find a numerical algorithm that didn't have floating point errors in it. I can't imagine there being any resource of perfectly clean code snippets.
  • Problems in snippets are magnified when used out of context. If you don't hand analyze every line of the snippet, you'll have no idea what lurks within.
  • It's basically boilerplate code, refer to the unfortunate world of boilerplate code

By all means, refer to snippets and examples for reference, but avoid the desired to Ctrl+C/+V them. And if for some reason you do, then you must analyse every line of that code and make sure you understand it. A hint, if you haven't modified it, possibly significantly, then you likely haven't understood it -- or you've hit the unfathomably rare occurence of a supreme quality, universally relevant, snippet.