16
votes
Fortnightly Programming Q&A Thread
General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.
Don't forget to format your code using the triple backticks or tildes:
Here is my schema:
```sql
CREATE TABLE article_to_warehouse (
article_id INTEGER
, warehouse_id INTEGER
)
;
```
How do I add a `UNIQUE` constraint?
Does anyone have a good resource for people who want to learn to build web applications but already know general software principles? I'm an infrastructure guy so I how to design systems and write code, but I keep finding resources where it's like "This is JS! Here's how a
for
loop works!" etc. I have this same kind of issue with JS itself, where if I'm looking for JS-learning resources they vastly assume that I have no idea what programming principles are.Probably the best thing to do would be pick a framework (vue, angular, htmx, or even plain old jquery) and dig into the tutorials and documentation as you go. Since you already know how to code I'd skip general Javascript tutorials and primers and do a web search whenever you need to know the specific syntax or available features for something you know/suspect should exist (like "javascript switch statement" or "javascript asynchronous programming" or whatever). Even the crappy resources like w3schools will usually get you what you need at a quick glance.
One thing that throws off a lot of programmers getting into web development is the expectation that there's a single recommended default vanilla way to make a good website, in the same way that there's a main recommended way (language, tooling, and framework) by Apple to make iOS apps, there's a main recommended way by Google to make Android apps, etc. There's not. There's no single official website/webapp-making SDK. Most web developers use piles of modular open source tools that fit well together. The stack I use for most stuff personally and for work mainly consists of VSCode, Typescript, React, Next.js, Node & Express on the server, npm, ESLint, Prettier, and Jest. I would point a newcomer at a framework like Next.js because it comes with a working React + (optionally) Typescript + Node server-side-rendering setup, it's the most popular modern (frontend+backend, supports interactive components that are server or client rendered) web framework, has decent intro tutorials, works well with npm libraries, it supports fully server-rendered non-hydrated pages unlike most modern non-React frameworks, and is really easy to host yourself or on free services like Vercel.
There is plenty to learn about web development that's separate from these tools, but sometimes I see new web developers set out to do everything themselves the "vanilla" way without using any external tools in order to focus on that. I think that's a big mistake and self-handicap to put on oneself, at least for any more than a practice or small-scope project. At best it leads to a project full of not-invented-here syndrome and at worst it involves someone setting very low expectations for themselves because of how hard many basic tasks are. It's painful and involves a lot of redundant work to support things like interactive components that can be both server and client rendered if you aren't using a modern view library like React.
MDN and web.dev are my go-to resources for JS and web APIs.
I'm looking for advice on how to organize a program. I've been scripting for some months now, but I'd like to build bigger projects, but I have no clue how to organize it. Can anyone give me some pointers?
Knowing when and where to break your code up into abstractions is the most subjective and contextual aspect of programming that there is, and the details really depend on what language you're using. Expect that you'll naturally get better with time and practice.
But to lay out some general steps:
And most importantly, read code that has been written by other people. Don't expect to understand most of it at first, and don't spend more than thirty minutes on this per session. As long as you can get the gist of some basic parts and sniff out the general intent, you'll learn by pure osmosis.
This is absolutely helpful, thank you!
There are existing design patterns that can help organize a project, the most popular probably being MVC (model-view-controller), but there are a ton of them (multitier architecture, microservices, pubsub--here's a good Wikipedia article that gives a broad overview). Even if none of these exactly fit your project, reading up a bit and understanding architectural patterns can still help inform your decisions.
Probably the key takeaway I'd emphasize is to focus on separation of concerns--keep related code grouped together and separated from unrelated code. That being said it's all highly subjective and there's no "best" way to do things that fits every project.
There are many good books on the subject. Some of the better ones I read were the pragmatic programmer and code complete.
Since FluffyKittens already gave you some great tips, I'm going to add some more general stuff and resources here.
Most people start by using the MVC pattern, which is rather easy to use and introduces you to the world of software architecture.
When you have understood and used MVC successfully (and maybe have discovered some other patterns), you can move on to more sophisticated things such as DDD (Domain Driven Design). This is probably far in the future, but it's good to know that it exists (and it rocks).
It's also important to know that software patterns are often based on object-oriented programming (OOP). So maybe have a look into that first.
And here's another tip. I haven't used Angular in ages, but I think that the way it enforces certain practices and project layouts is perfect for learning some architectural basics. It's no wonder so many big companies love it; most Angular projects look really similar to each other.
I've been trying off and on for a couple weeks to host a simple static website from my Raspberry Pi. I'm almost there, but the best I can do is a 502 error. For my professional development, and to keep things portable when I eventually upgrade hardware, I'm going all-in on Docker Compose. I have a handful of containers up and running and accessible locally. The part I'm having trouble with is pointing my domain to the right place.
Here are all the containers I have at the moment:
Because everything works locally, I'm pretty sure my problem is somewhere between the tunnel setup, the CNAME records in Cloudflare, and the Cloudflare SSL cert in NPM. Like I said before, the closest I can get is a 502 error. I've looked at several guides, but I'm having trouble finding a guide specific to this Docker-ized configuration, and I'm struggling to cross-reference things to put the pieces together.
Has anyone out there hosted something in docker through a tunnel? What am I missing here?
I'm running a very similar setup on an intel nuc with Cloudflare tunnels and Dockers for multiple websites. Just set it up a few months ago.
Why did you need to free up port 80? Cloudflare tunnels wouldn't need it. See here for how it works: https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/#:~:text=Cloudflare%20Tunnel%20provides%20you%20with,connections%20to%20Cloudflare's%20global%20network
I don't run PiHole so maybe that's where some of the issues are taking place. I'm curious what your cloudflare tunnel config looks like.
Edit: Just now saw you are running Cloudflared in a container as well. I run mine locally, perhaps it is not able to reach the other containers? Are you able to get into that container and test connectivity? Might be something with your docker networking.
Nginx-proxy-manager listens on ports 80 for http and 443 for https in addition to hosting it's http web interface on 81. PiHole was hogging port 80 all for itself in host network mode, so I couldn't even create the nginx-proxy-manager container with the conflict.
PiHole listens on port 53 to handle DNS requests on my LAN and hosts an http admin interface on port 80 by default, which I moved it to 8081. DNS requests come in on port 67 from the host network through the dhcphelper relay.
All of my containers are connected to the same bridge network, so I think they should all be visible to each other just fine. But I should try running cloudflared directly in Raspbian to see if the docker network setup is my issue.
Anyone use chatGPT or GitHub CoPilot as a helpful source with their programming?
I asked it some JavaScript questions or to explain what my function was doing and I thought it was remarkably helpful