I'm Well and Truly Stuck!
I'm trying to embrace the "learning in public" ideal here on Dev, but I find it flies against my instincts. I tend to want to understand something really well first, then write about my learning process with some confidence. I want to present the story that I figured something out, and here's how I thought through it, and here's the correct conclusion I came to. You'll see that narrative in some of my previous articles.
Today I'm pushing myself to post what I'm doing right now. Which means I have to tell an incomplete story. I feel a bit vulnerable talking about how stuck I am, but hopefully this will be helpful to you as well as to myself.
Stick with me...
In a moment I will type up a list of things to do when you're stuck. I'm reminding myself right now that I am capable of making a useful such list based on my life's experience as a songwriter, performer, teacher, and programming enthusiast - a list that will be helpful to all.
But first I want to actually do something I know I'll put on the list, which is to write about the problem. I'm doing this partly just to vent, partly to give you some context for why I'm writing this article, and mostly to try and help get myself unstuck.
An Intractable Problem
I'm working on a challenge app in the 100DaysOfSwiftUI program by Paul Hudson, highly recommended for anyone interested in learning Swift.
In the app, the user can select a photo from the PhotosPicker
, and a pointer to the data is stored as a PhotosPickerItem
. Then an .onChange(of: selectedItem)
triggers a sheet to open that displays the selected photo and asks the user to give it a title and description.
Mostly this works great, but when I run the app in the simulator, there's one photo that refuses to load in when you select it. The sheet opens with the message "no photo to display," which I have programmed it to do when the value of the selected photo is nil
.
However - and here's the baffling bit - the selected photo should never be nil
if the sheet is open because the property that triggers the sheet to open, isDisplayingNewPhoto
, is a Bool
, set to (newPhoto != nil)
. To state this again in plain language: The sheet can only open if the photo is not empty, yet the sheet is displaying the empty case for the photo.
I'm leaving out a bit of detail, which involves loading the data for the photo. The PhotosPickerItem
is just a pointer to some data, which has to be loaded into an Image
property before it can be displayed as a photo.
I suspect the problem is with timing; the particular image I'm struggling with is larger than the ones that work fine. Maybe by the time the sheet opens, the image hasn't loaded in yet. But then why is the sheet triggered to open at all? And also I'm using the async/await pattern that supposedly handles the time-consuming process of loading data so it will be present when needed.
I've tried every variation I can think of, switching the order of things around, and while different approaches all work generally, they also all fail in the same way with this one photo.
Now is a good time to share this meme, thanks to @avanichols, who posted it for this week's Meme Monday. I may be sitting near the top of this bell curve. I'm not ready to say I've uncovered a bug, because I'm probably doing something wrong, but maybe??
What To Do When You're Well and Truly Stuck!!
Okay, here's the list I promised you...
1. Write about the problem.
Explaining it carefully to yourself on paper can be clarifying.
2. Phone a friend.
Maybe you're too used to looking at your own code to see the problem. Get a fresh pair of eyes on it. Your friend may see some obvious thing you're missing or some assumption you've made that you aren't aware of.
3. Ask AI.
Sometimes Chat GPT can suggest a solution you haven't tried yet. Try asking your question in multiple different ways, from general to specific, with and without context. Try asking it to do different associated tasks: you can ask it to write code, review your code, or brainstorm different ways to solve a given problem. (Always keep in mind the fact that Chat GPT's personality doesn't allow it to just say "I don't know," which sometimes leads to unhelpful responses.)
4. Read the documentation.
Look up every relevant bit of code to make sure you know how it's supposed to work and what your options are. As shocking as this may sound, sometimes the official documentation is helpful.
5. Post to an online discussion.
Maybe others have dealt with the same problem. It's helpful to isolate the issue first, and post some code just illustrating the problem, rather than posting your entire app. The process of isolating the problem may even shake loose a solution on its own! I'll make that a separate bullet point...
6. Try to isolate the problem with fresh code.
Starting from scratch, write the simplest app you can to re-create the problem, and see if you can solve it in that simplified context.
7. Take a different approach.
Maybe the code isn't designed to do what you're trying to do. There may be forty ways to code a solution, but there are also forty solutions to the bigger problem. What if you go back to the ultimate goal of your app and take a completely different approach to accomplishing it? A little brainstorming can get you going in a whole new direction.
8. Work on different code.
This is one I need to listen to myself. Maybe the problem is unsolvable. Maybe the solution is obvious and you're just not seeing it. Either way, after you've spent a reasonable amount of time on it, move on to something else for a while. Start the next lesson. Taking in new, seemingly unrelated information can sometimes lead to a solution.
9. Do something else entirely.
Like take a walk, take a shower, do the dishes. Any task that involves your body but not your mind can loosen up those thoughts and help a solution pop into your head. Try not to think about it directly. Just let your brain crank away in the background.
10. Don't be too picky.
I always like to think I should be able to make the code do exactly what I want. But maybe I just don't have the required skills yet, or maybe I'm asking the impossible. In my particular case (as described above), if I try to load the same image a second time, it works. To me, this isn't a very satisfying solution, asking the user to do a thing twice before it works. But I need to keep in mind the bigger picture - I'm coding this app to learn, and while I'm well and truly stuck, I'm not learning. So sometimes maybe good enough is good enough for now.
Bonus. Have faith and patience.
Millions before you have learned to code. Millions before you have written apps that do something similar to what you're trying to do. You're as smart as they are. You'll figure it out, too.
Out of ideas
I managed to come up with a few items I haven't tried yet, so hopefully my list will help. I hope it helps you as well!
What have I left off? What do you do when you're stuck? Please add your thoughts in the comments!!