[programming] Where do u keep ur code templates, snippets, ecc.?

whired123

Member
Joined
Nov 15, 2021
Messages
145
Reaction score
9
Credits
1,062
Where do u keep ur portions of incomplete reusable code (which includes templates, snippets, ecc.)?

In ~/dev/resources/templates/ ?
In ~/resources/templates/ ?
 
Last edited:


Snippets should be part of your project because IDE normally uses some default location where you put them.

I don't use templates, templates are only useful for replicating unit tests, but you don't replicate your main project on which you're working, unless you're learning and need to start over every now and then to follow a book or something.

Reusable code, on github and then downloading zip to external storage.

My tree is all under ~/dev which contains several subdirectories, consisting of directory for github projects, directory for Android SDK, directory for libraries, directory for my own projects etc. I add all development related stuff in subfolders of this directory.

Another benefit of having it all in home is you don't need root privileges to upgrade build tools, IDE etc.

What are you working on? which language are you learning?
 
Almost all of our development computers are shared with multiple users..

so either in your home directory (often with IDE specific sub-directories) or if it's really important.. GitHub.
(which isn't always free).
 
Me, I keep all those kinds of odds'n'ends in a directory named "SCRIPTS". This is on a secondary data drive which is sym-linked in to every 'Puppy' in the "kennels".....and thus accessible from anywhere.

I just copy'n'paste as & when necessary, given that all I ever use is Bash. I don't bother with any kind of 'structured' development filesystem 'tree', or owt like that, though I do have half-a-dozen 'work areas' set up on that same large secondary data drive.....from where I frequently have two or three projects "on the go" at any given time.

My set-up is like its owner; carefree, and somewhat disorganized.....but I can lay my hands on anything I need within seconds..! :D


Mike. ;)
 
I put them on "postit notes" - but my postit notes are searchable text files ( a la zettelkasten ) - I left paper postit notes behind a long time ago.
 
Where do u keep ur portions of incomplete reusable code (which includes templates, snippets, ecc.)?

In ~/dev/resources/templates/ ?
In ~/resources/templates/ ?
If it were needed


Template example: Generic model to model/adapt/sculpt into your project

Code:
<?php 
class {NAME} {

    public ${PROPERTY1};
    public ${PROPERTY2};
    public ${PROPERTY3};

    function __construct(...) {
        $this->{PROPERTY1} = ...;
        $this->{PROPERTY2} = ...;
        $this->{PROPERTY3} = ...;
    }

    function {METHOD}() {
        ...
    }
}

Snippet example: To be pasted somewhere (where needed) in your project

Code:
define('YOUR_APP_ID', 'YOUR APP ID');

//uses the PHP SDK.  Download from https://github.com/facebook/php-sdk
require 'facebook.php';

$facebook = new Facebook(array(
  'appId'  => YOUR_APP_ID,
  'secret' => 'YOUR APP SECRET',
));

$userId = $facebook->getUser();
 
Snippets should be part of your project because IDE normally uses some default location where you put them.
What if I want to use them for multiple projects?
I don't use templates, templates are only useful for replicating unit tests, but you don't replicate your main project on which you're working,
Explain yourself better
unless you're learning and need to start over every now and then to follow a book or something.
Explain yourself better
Reusable code, on github and then downloading zip to external storage.
Git generally contains standalone code, meaning libraries that, once "loaded" (and possibly configured) into your project, can run smoothly without any intervention.

Reusable code is another matter (a more personal thing): https://www.linux.org/threads/progr...code-templates-snippets-ecc.57611/post-275597

My tree is all under ~/dev which contains several subdirectories, consisting of directory for github projects, directory for Android SDK, directory for libraries,
What kind of libraries?
An example?
directory for my own projects etc. I add all development related stuff in subfolders of this directory.

Another benefit of having it all in home
$HOME or $HOME/dev ?
is you don't need root privileges to upgrade build tools, IDE etc.
What do you mean?
Explain yourself better.
A concrete example?
What are you working on? which language are you learning?
php
 
What if I want to use them for multiple projects?
Doesn't matter if you use same IDE for multiple projects. default snippets directory is set in IDE, but it may be possible to direct it to your custom directory so it doesn't matter where you put them, you have freedom to configure your project/IDE however you want.

Explain yourself better
Have you never heard of unit tests? you have 1 template that is bare minimum for unit test and then copy it when needed to write new unit test.

Explain yourself better
Explain what? you should explain instead how you plan to use templates, why do you even need them?
Then I can explain whether that's normal or whether there is better method.

So by "template" your referring to VS templates?
IDE specific templates are for convenience and for beginners, most people will want to start writing their new project from zero.

Those templates are again only useful for unit tests just like non VS specific templates, I see no purpose of them for anything else because 99% of coders deal with some specific project instead of starting from scratch and dropping their work every time they want to code something so no need for templates.

If you don't understand that then explain why do you need templates?

Reusable code generally means libraries, it doesn't matter if it's your own or not, you'll want those in your github account, you can make private repository if you don't want to share them.

What kind of libraries?
An example?
Boost for instance, and any other that may affect multiple projects, otherwise libraries should be part of your project, e.g. your project directory should contain a directory called "External" or "3rd Party" where you put libraries that your project depends on.
This may also be git submodules.

$HOME or $HOME/dev ?
~/dev means $HOME/dev

What do you mean?
Explain yourself better.
A concrete example?
Android studio and SDK for instance if installed system wide will fail to upgrade using UI because you don't have permissions since the IDE is started as standard user.
You'll have no way to upgrade other than deleting everything and installing from zero the new version.

If you install it somewhere into $HOME that's not an issue.

Is language not a project.

I was hoping you share what are you're dealing with to give you advice, anyway the best advice is to use github or gitlab or what ever to manage your code, then all you issues so far are not relevant.
 
Template example: Generic model to model/adapt/sculpt into your project
I see you're referring to file template in MS's parlance that is in VS, well this should rather be a snippet because it's much easier to get starting code with a key press than navigating 5 steps in GUI with a mouse.

IMO, the fewer snippets/templates you have the better, no one will fit all your needs anyway and you'll happen to delete half of their contents to adapt it to needs.

Shorter snippets are better than long ones because can fit more cases.
Such as comment block or class scope, but there shorts are already default in most IDE's and all it takes is a sign or word + TAB to get them.
 
There would also be the templates directory, which for the English localization of Ubuntu should be ~/Templates

Code:
$ xdg-user-dir TEMPLATES
/home/MY-NAME/Templates

But it's usually used by those who work in editorial offices, secretarial offices, etc.
In practice, it usually contains basic templates for creatingoffice stuff: addministrative documents, business letters, greeting cards, spreadsheets, little hearts, and other niceties..
 
I put them on "postit notes" - but my postit notes are searchable text files ( a la zettelkasten ) - I left paper postit notes behind a long time ago.
The templates or the snippets?
Explain what you're talking about.

I put them on "postit notes"

What does it mean?
 
Doesn't matter if you use same IDE for multiple projects. default snippets directory is set in IDE, but it may be possible to direct it to your custom directory so it doesn't matter where you put them, you have freedom to configure your project/IDE however you want.
I do not use ide (not always).

Have you never heard of unit tests? you have 1 template that is bare minimum for unit test and then copy it when needed to write new unit test.


Explain what? you should explain instead how you plan to use templates, why do you even need them?
I need a fragment of code to log in

$ cat ~/dev/resources/templates/login.php

I copy the text

$ gedit ~/dev/projects/my-project/src/loginclass.php

I paste and scaling a corrections/suitable.

But the question is always the one in the initial post: like location it is better

~/dev/resources/templates/login.php
or
~/resources/templates/login.php
?


Then I can explain whether that's normal or whether there is better method.
That's exactly what I'm asking
So by "template" your referring to VS templates?
I don't use VS
IDE specific templates are for convenience and for beginners, most people will want to start writing their new project from zero.
I do not use IDE (not always), as I have already said: especially if I have to make interventions on the fly like correcting some not particularly insidious bugs.
Those templates are again only useful for unit tests just like non VS specific templates, I see no purpose of them for anything else because 99% of coders deal with some specific project instead of starting from scratch and dropping their work every time they want to code
????
bohh

I was hoping you share what are you're dealing with to give you advice
I am writing a WordPress pluin Er Collecting data on visitors
 
But the question is always the one in the initial post: like location it is better

~/dev/resources/templates/login.php
or
~/resources/templates/login.php
?
There is no functional difference, how you lay out your project structure is entirely up to you.
If you type it on command line shorter paths will probably help you with less typing, in which case I'd just put it all into ~/resources or even better ~/res to type less.

My directory tree is set up according to my wants not to some specific rules.

????
bohh
I am writing a WordPress pluin Er Collecting data on visitors
In that case I can't help, web development workflow is different from desktop development.

In any case you have freedom to set up your workflow according to your needs and what makes sense to you, there is no standard on how development should be done.

Go for choice that's easier to manage and easier to work with, shorter paths and less directory nesting is always better than otherwise.

What works for others may not work for you, first because there are differences in workflows that are language/project dependent and secondly because people have differing preferences.
 
You keep your executables in /usr/local/bin, and i personally use hard links to link the other copies with them. Its much better than adding directories to your PATH.

But idk as far as the rest of what you ask, you should make your own system for that.
 
I need a fragment of code to log in

$ cat ~/dev/resources/templates/login.php

I copy the text

$ gedit ~/dev/projects/my-project/src/loginclass.php

I paste and scaling a corrections/suitable.

But the question is always the one in the initial post: like location it is better

~/dev/resources/templates/login.php
or
~/resources/templates/login.php
?
Err, I didn't read all the back 'n forth when this thread got long, so I kinda fast-forwarded to the last post. Anyway here's a simple solution:
~/projects/my-project/
^ Main, where you put everything for this project. K.I.S.S.
~/projects/my-project/00-resources/
^ Where you put stuff you need for refs, templates, etc. for this project. Add to your .gitignore file. K.I.S.S.

Don't worry about redundancy in this instance as space is not much of a concern for smaller files.
 
The templates or the snippets?
Explain what you're talking about.

I put them on "postit notes"

What does it mean?
Pretty much everything - code snippets, templates (which are really just big shell script snippets), instructions for setting the clock on my car radio, how to change the belt on my lawn mower, my home-brew chili recipes. Even my master password list, although that one is encrypted. Every "postit note" (except the encrypted ones) is a simple text file.

The system is generic enough to handle anything that's plain text. If I search for any word, it will list the first line of every note that contains that word, whether it be a snippet, a recipe or a journal entry.
windowshot-2025Aug05-151151.png
 


Follow Linux.org

Staff online


Latest posts

Top