4 ms·
This. In fact, a friend of mine swears by writing C++ in a style called "snippets" --- meaning, whenever it makes sense to do so, he writes new functionality a
by palish 16y ago
This. In fact, a friend of mine swears by writing C++ in a style called "snippets" --- meaning, whenever it makes sense to do so, he writes new functionality as a single header and cpp file that has no dependencies whatsoever on other headers, etc, then open-sources it so that anyone can add the same functionality to their own C++ projects by just dropping in the code. A math lib, for example.
- deleted 16y ago[deleted]
- kenjackson 16y agoAren't those also just called "functions"? In this case one function per file?
- palish 16y agoNo. As soon as your function has a dependency on anything else whatsoever, it can no longer be copy-and-pasted into another project A snippet consists of functions which accomplish a specific task. The important part is that the entire snippet is a single cpp and a single header. No dependencies.
- masklinn 16y agoThat's a function with no dependencies. Still a function.
- palish 16y ago.... Something is obviously being lost in translation here. There are certain programming problems which can be solved once and used forever. The problem wouldn't necessarily be considered a single function. However, it isn't a massive problem either. My friend solves these problems by creating a single .cpp and a single .h file. The .cpp is the implementation; the .h file defines the interface. He calls this a "snippet". Now, the power of the snippet is that you can simply copy the .cpp and .h into any C++ project anywhere and it's good to go. There is no tweaking, include'ing, anything. It just works. This code can then serve you for the rest of your life, regardless of what project you are currently working on. You can say "I've solved that problem before", and be able to spend literally 30 seconds implementing it into your current project, because all you have to do is drag'n'drop two files.
- kenjackson 16y agoI get the distinction, and its a fair one. Although, I don't get how snippets could be useful. Especially in C++. It seems like for almost anything interesting I'm going to want a dependency on the standard library or Boost or the CRT. I could imagine some snippets that don't take any dependencies, but they are pretty few and far between. Do you have pointers to some of the more interesting snippets he's posted?
- palish 16y agoYeah, check 'em out: http://www.amillionpixels.us/sourcecode.htm http://www.amillionpixels.us/sourcecode.htm There are surprisingly many useful snippets. For example, a single header and source file for an entire, complete A* pathfinding implementation! That's a massive timesaver.
- kenjackson 16y agoIn a lot of the ones titled snippets, I do see dependencies. Some to Windows DLLs and some to the CRT. But they're relatively self-contained. Cool stuff. Thanks for the link. Although I'd still call them functions. :-)
- masklinn 16y agoAnd they're all functions (or function groups), apart from the one or two that are command-line utilities. At the end of the day, they're libraries. Small libraries? Sure, but still libraries.