|
Hello, So in the how to return values from functions in Day 7 I tried the exercise problem: "do two strings contain the same letters, regardless of their order?" this was optional and I tried it and figured it out. However my question regarding this is not how but why. My first attempt I tried the following:
Why didn't this work? I noticed I had to add "return false" at the end, why did it need that? After I fixed my solution and got the program to return true with this:
Was this way of doing it unnecessary maybe not the best practice? I know it worked but I don't want to pick up bad habits and I want to know why. It bothered me that it looked a little too different to Pauls solution below.
So in conclusion, Which one was the better solution? To me it seems that writing a bit more code for readability is a good idea for example when I used if statements and return to make it clear what I was doing, but paul keeps showing us ways of deleting "unnecessary code" how do you know when you should sacrifice readability for shorter maybe more abstract code? Hopefully this makes sense thank you for your time reading and I look forward to your answers. |
|
Because these lines:
sort And then in this line:
you compare the original unsorted But in your corrected code:
you are comparing the sorted values with one another and it works.
Because all functions must return a value and you have to explicitly tell the function what value to return.* * Unless the return value is
They're both perfectly fine. If it were me, I would go with Paul's way, for one reason: There is only a single place in the code where you return from the function, versus two exit points in your code. But that's really a stylistic preference more than anything else. |
|
Thank you for your reply and I understand your points. However before I mark this as solved you still haven't answered my last question, When do you know whether to write more code for the sake of readability or shorter code that's maybe more difficult to understand at first glance for the sake of efficiency or performance? For example for some people the ternary operator is more difficult to understand what's going on rather than just writing if statements. Is it longer? yes but It's obvious what I'm trying to do. |
|
Here's an example from another post. I defined a bool, named
Perfectly fine code, but verbose. What may trouble some, is the way the programmer needs to know the internal structure of my This is a long chunk of code. AlternativeInstead, I added a computed variable in my class named The other benefit, in my opinion, is this approach greatly simplifies updating the Here's a more terse version of that logic:
Which is easier to understand?I think the first example looks like a plate full of C++ spaghetti 🍝 logic. I think it's easier to 'read' the second as it follows a single line of logic. |
|
Addressing another point...
Your iPhone or iPad can execute millions of logic decisions each second. MILLIONS! When you drag your finger across its screen your iPhone is performing tens of thousands of calculations determining distances, velocity, pressures, directions, pauses, and more. iOS is highly tuned and the Apple silicon processors are insane. I think trying to re-write a Keep Coding! |
|
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.