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.


//MARK: Import Statements
import UIKit

class ViewController: UIViewController {

    //MARK: Properties


    @IBOutlet weak var cookiesPerClick: UILabel!
    @IBOutlet weak var numberOfCookies: UILabel!
    @IBOutlet weak var mainCookie: UILabel!


    @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() {
        // Do any additional setup after loading the view.

    //MARK: Actions

    @IBAction func upgradeClicker(_ sender: UIButton) {
        if (cookies >= cookieUpgradeCost) {
            cookiesAClick += 1
            cookies -= cookieUpgradeCost
            cookieUpgradeCost *= 2
                "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

Screenshot of app layout


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 add buttonTapped at the end getCookieButtonTapped. A bit more android way is onAction 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 to cookiesCount. cookies sounds more like list of cookies

  • delete 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 as private

  • 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 of UIView
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() {

    //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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.