I'm getting into Swift iOS development, and decided to create a Cookie Clicker application as a first attempt. Since I am very new with Swift, I would like feedback and criticism on everything possible, to kick bad habits to the curb early. Any and all feedback is welcome, appreciated and considered!
Since all of the code that I wrote is contained in the ViewController
class, I'm only posting the class. If needed, I can post other classes. A screenshot of the layout of my app will also be available below.
ViewController.swift
//MARK: Import Statements
import UIKit
class ViewController: UIViewController {
//MARK: Properties
//Labels
@IBOutlet weak var cookiesPerClick: UILabel!
@IBOutlet weak var numberOfCookies: UILabel!
@IBOutlet weak var mainCookie: UILabel!
//Buttons
@IBOutlet weak var upgradeButton: UIButton!
@IBOutlet weak var cookieButton: UIButton!
//Cookie Stuff
var cookies: Int = 0
var cookiesAClick: Int = 1
var cookieUpgradeCost: Int = 10
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
formatItems()
}
//MARK: Actions
@IBAction func upgradeClicker(_ sender: UIButton) {
if (cookies >= cookieUpgradeCost) {
cookiesAClick += 1
cookies -= cookieUpgradeCost
cookieUpgradeCost *= 2
upgradeButton.setTitle(
"Upgrade Cookie | Cost: \(cookieUpgradeCost) Cookies",
for: .normal
)
numberOfCookies.text = "\(cookies)"
cookiesPerClick.text = "Cookie Per Click: \(cookiesAClick)"
}
}
@IBAction func getCookie(_ sender: UIButton) {
cookies += cookiesAClick
numberOfCookies.text = "\(cookies)"
}
//MARK: Local Functions
func formatItems() {
//Upgrade Button Formatting
upgradeButton.layer.borderWidth = 1
upgradeButton.layer.cornerRadius = 5
upgradeButton.layer.borderColor = UIColor.black.cgColor
//Get Cookie Button Formatting
cookieButton.layer.borderWidth = 1
cookieButton.layer.cornerRadius = 5
cookieButton.layer.borderColor = UIColor.black.cgColor
//Number Of Cookies Label Formatting
numberOfCookies.layer.borderWidth = 1
numberOfCookies.layer.cornerRadius = 5
numberOfCookies.layer.borderColor = UIColor.black.cgColor
//Set main cookie to bold
mainCookie.font = UIFont.boldSystemFont(ofSize: 35.0)
}
}
App Layout Screenshot
1 Answer 1
Since there isn't much code there are only few things that can be improved and all of them are styling:
- when working with outlets, make sure to specify the outlet type in the property name.
ex.
cookiesPerClick
->cookiesPerClickLabel
Same applies for the@IBAction
. Common approach is to addbuttonTapped
at the endgetCookieButtonTapped
. A bit more android way isonAction
but still acceptable
The main reason for adding those suffixes is to make it clear what are you dealing with (button action, property label or whatever)
I would rename
cookies
tocookiesCount
.cookies
sounds more like list of cookiesdelete generated comments like
// Do any additional setup after loading the view.
If you are not gonna expose some properties/functions (
cookies
,cookiesAClick
,cookieUpgradeCost
,func formatItems()
) mark them asprivate
In general an "early return" strategy is preferred where applicable as opposed to nesting code in if statements. Using guard statements for this use-case is often helpful and can improve the readability of the code
@@IBAction func upgradeClicker(_ sender: UIButton) {
guard cookies < cookieUpgradeCost else { return }
cookiesAClick += 1
...
}
- You have three different views in
formatItems
that are styled the same way so it will be easier to maintain (and less code) to just separate the styling. I would add it in extension ofUIView
extension UIView {
func addBorder() {
layer.borderWidth = 1
layer.cornerRadius = 5
layer.borderColor = UIColor.black.cgColor
}
and in private func formatItems()
you will only have:
private func formatItems() {
upgradeButton.addBorder()
cookieButton.addBorder()
numberOfCookies.addBorder()
//Set main cookie to bold
mainCookie.font = UIFont.boldSystemFont(ofSize: 35.0)
}
Naming conventions are a bit optional since most of the companies have their own (or don't have at all) but you can find some official style guides