Learn Programming

!learn_programming

@programming.dev
Create post
Please read: community rules

Please read: community rules

This community is aimed at two specific topics:

  • Support general programming questions, of any language, mainly for people beginning their journey in programming.
  • Give some advice on programming education or career.

What this community doesn't intend to do:

  • Give specific answers to very specific, non-beginners, problems of a particular language. You probably can go to a community of that language to get help with that.
  • Solve your programming assignments. You can ask for a specific issue, but it's essential that you learn to think and solve them, or you'll never progress.

As suggested by Captain Janeway, here are some rules specific to the posts:

  • Paste your code. Unless there's not any other way, please don't provide screenshots of the code, it's harder to review.
  • If possible, try to provide a runnable example of the code in question
  • Explain as much as you can: what you’ve tried, what the error is, what you think the problem is
  • As usual, be kind

The probability of getting an answer will increase dramatically if you follow these points.

This post will be updated periodically, with any new inputs considered necessary.-----

Is it better to check if a file exists before trying to load it, or to try to load it, and catch the error and move on?

Is it better to check if a file exists before trying to load it, or to try to load it, and catch the error and move on?

For context, I am trying to do a save system for a game.

Why Go Interfaces Are the Remarkable Secret to Clean Code Mastery

Why Go Interfaces Are the Remarkable Secret to Clean Code Mastery

Open link in next tab

Just a moment...

https://blog.devgenius.io/why-go-interfaces-are-the-remarkable-secret-to-clean-code-mastery-8eeac43d2e1b?sk=217a06be690e9834e04b9d7153c3d13d

Just a moment...
Unlock the Secrets of Web Storage APIs in JavaScript

Unlock the Secrets of Web Storage APIs in JavaScript

Open link in next tab

Just a moment...

https://medium.com/@harendra21/unlock-the-secrets-of-web-storage-apis-in-javascript-262f94cf50d3?sk=a44c867664f8e87f1aa885601ffc1b51

Just a moment...
What are some easy-to-solve errors you spent awhile fixing?

What are some easy-to-solve errors you spent awhile fixing?

I just spent an hour searching for how I could have gotten an

Uncaught TypeError: Cannot set properties of null

javascript. I checked the spelling of the element whose property I was trying to set and knew that element wasn't null because the spelling was the same in the code as in the HTML. I also knew my element was loading, so it wasn't that either.

Turns out no, the element was null. I was trying to set " NameHere" when the element's actual name was "NameHere".

Off by a single space. No wonder I thought the spelling was the same—because all the non-whitespace was identical. (No, the quotation marks slanting in the second NameHere and being totally vertical in the first NameHere wasn't a part of the error, I am typing them all vertical and either Lemmy or my instance is "correcting" them to slanted for the second NameHere. But that is also another tricky-to-spot text difference to watch out for!)

And what did not help is that everywhere I specifically typed things out, I had it correct with no extra spaces. Trying to set " NameHere" was the result of modifying a bunch of correct strings, remembering to account for a comma I put between them, but not remembering to account for the space I added after the comma. In short, I only ever got to see " NameHere" written out in the debugger (which is how I caught it after like 30 repeats of running with the debugger), because everywhere I had any strings written out in the code or the HTML it was always written "NameHere".

I figured I'd post about it here in case I can help anyone else going crazy over an error they did not expect and cannot figure out. Next time I get a similar error I will not just check spelling, I'll check everything in the name carefully, especially whitespace at the beginning and end, or things one space apart being written with two spaces instead. Anyone else have a similar story to save the rest of us some time?

GitHub - free-news-api/news-api: Top Free News API Comparison

GitHub - free-news-api/news-api: Top Free News API Comparison

Open link in next tab

GitHub - free-news-api/news-api: Top Free News API Comparison

https://github.com/free-news-api/news-api

Top Free News API Comparison. Contribute to free-news-api/news-api development by creating an account on GitHub.

GitHub - free-news-api/news-api: Top Free News API Comparison
I don't grok repositories and services and where's the cut-off point for them

I don't grok repositories and services and where's the cut-off point for them

I understand the basic principle but I have trouble determining what is the hard line separating responsibilities of a Repository or a Service. I'm mostly thinking in terms of c# .NET in the following example but I think the design pattern is kinda universal.

Let's say I have tables "Movie" and "Genre". A movie might have multiple genres associated with it. I have a MovieController with the usual CRUD operations. The controller talks to a MovieService and calls the CreateMovie method for example.

The MovieService should do the basic business checks like verifying that the movie doesn't already exist in the database before creating, if all the mandatory fields are properly filled in and create it with the given Genres associated to it. The Repository should provide access to the database to the service.

It all sounds simple so far, but I am not sure about the following:

  • which layer should be responsible for column filtering? if my Dto return object only returns 3 out of 10 Movie fields, should the mapping into the return Dto be done on the repository or service layer?

  • if I need to create a new Genre entity while creating a new movie, and I want it to all happen in a single transaction, how do I do that if I have to go through MovieRepository and GenreRepository instead of doing it in the MovieService in which i don't have direct access to the dbcontext (and therefore can't make a transaction)?

  • let's say I want to filter entries specifically to the currently logged in user (every user makes his own movie and genre lists) - should I filter by user ID in the MovieService or should I implement this condition in the repository itself?

  • is the EF DbContext a repository already and maybe i shouldn't make wrappers around it in the first place?

Any help is appreciated. I know I can get it working one way or another but I'd like to improve my understanding of modern coding practices and use these patterns properly and efficiently rather than feeling like I'm just creating arbitrary abstraction layers for no purpose.

Alternatively if you can point me to a good open source projects that's easy to read and has examples of a complex app with these layers that are well organized, I can take a look at it too.

How to go from writing code that works to writing efficient, clean code and following good practices?

How to go from writing code that works to writing efficient, clean code and following good practices?

Besides some of the very, very obvious (don't copy/paste 100 lines of code, make it a function! Write comments for your future self who has forgotten this codebase 3 years from now!), I'm not sure how to write clean, efficient code that follows good practices.

In other words, I'm always privating my repos because I'm not sure if I'm doing some horrible beginner inefficiency/bad practice where I should be embarrassed for having written it, let alone for letting other people see it. Aside from https://refactoring.guru, where should I be learning and what should I be learning?

Classes and objects. Just started learning, working in python.

Classes and objects. Just started learning, working in python.

So I have struggled with classes and objects but think I'm starting to get it...? As part of a short online class I made a program that asked a few multiple choice questions and returns a score. To do this there are a few parts.

  1. Define some inputs as lists of strings ((q, a), (q2, a2),...). The lists contain the questions and answers. This will be used as input and allows an easy way to change questions, add them, whatever.

  2. Create a class that takes in the list and creates objects - the objects are a question and it's answer.

  3. Create a new list that uses that class to store the objects.

  4. Define a function that iterates over the list full of question/answer objects, and then asks the user the questions and tallies the score.

Number 2 is really what I am wondering about, is that generally what a class and object are? I would use an analogy of a factory being a class. It takes in raw materials (or pre-made parts) and builds them into standard objects. Is this a reasonable analogy of what a class is?

Top Javascript Coding Round Questions For Beginners - 1

Top Javascript Coding Round Questions For Beginners - 1

Open link in next tab

Top Javascript Coding Round Questions For Beginners - 1

https://withcodeexample.com/blog/top-javascript-coding-round-questions-for-beginners-1/

Top JavaScript coding round questions for beginners: Reverse a string, check palindrome, find largest number in array, and calculate factorial.

Top Javascript Coding Round Questions For Beginners - 1