|
Hello, I'm looking for some constructive criticism please. Although I added a little on my own this time for fun, my code always seems more verbose than others. Would love some thoughts on how to make things more efficient or the general idea that I am ok with how things are being done.
|
|
Michael observes:
Your code is verbose because your logic is verbose! A cursory look at your solution leads me to think that you have a background in procedural languages (VisualBasic, C++, JAVA, etc) and haven't quite made the jump to SwiftUI's declarative style. The good news is that you're learning! The better news is as you gain experience, you'll start to see some inefficiencies and your code will get better and less verbose. It will become more Swifty. Well you asked for thoughts, and not a lecture. So here are a few to get you started. MARKTen points to Ravenclaw for using MARK tags in your code! ContentViewMinus ten points from Ravenclaw for naming your view Verbose DeclarationsYou declare your structs variables like this:
Moving forward, trust the
Calculated PropertiesYou'll start to learn the benefits of calculated properties as you progress. This is a great way to consolidate your logic while reducing code. Take this use case for example: When do you want to end a game? You have this code:
This is referenced in several places in your code, tied to some value (10). It has to be set and reset. This is verbose, and can lead to logic errors trying to keep these syncronized. The Swift way is to declare it!
Instead of scattering logic throughout your code, consider the benefit of asking Swift to calculate if your player is at the end of a game.
This allows you to simplify your logic in your
Declarative CodeNota Bene: It is no secret that SwiftUI's declarative style is copied from many other languages. Hush now. |
|
Phenomenal answer. This is exactly what I was looking forward and very much appreciated. I did move to Swift/SwiftUI from VB and C++ (a little bit of HTML and Java) and am really enjoying this learning experience. Thank you again for the review. I will look to use this for future lessons/projects. |
|
Hi I agree with Also going on general put
as this is the With that it a bit big so you could factor it out.
I started with the first bit of the
NOTE change your Fonts size to use general one as these will size up and down depanding on thier size perference.
Next look at the the next section. (you put them in
Next looked at
Also look at the the starting properties and your put
(I have used
Also added
and change your
Lastly moved to ViewModel, so created a Swift file called
So now the
PS Added |
|
Thanks @NigelGee. Excellent information and much appreciated. Admittedly, some of your answer is a beyond my current skill-set (Day 25) but I fully understand the concepts you are showing and feel good about that. This will really help me moving forward in the 100 Day course. Thanks again! |
|
Glad you appreciated the answer, when I started I was only going to suggest to refactor the |
SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.