Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify Rule: Name unused function parameters as underscores #210

Merged
merged 1 commit into from
Jan 7, 2023

Conversation

Jager-yoo
Copy link
Contributor

Summary

Reasoning

  • In Add rule to name unused function paremeters as underscores聽#136, calda said it was a 'real world' example, and I totally understand that, but the Context type is a bit ambiguous compared to other easy types used in this style guide. (like Universe, Galaxy, Planet, Star, etc.)
  • so, I clarified this example with a more easy type, Weather.
  • Open to suggestions on this, thanks! 馃槃

Reviewers

cc. @calda

Please react with 馃憤/馃憥 if you agree or disagree with this proposal.

Comment on lines +1288 to 1292
func updateWeather(_ newCondition: WeatherCondition) -> Weather {
var updatedWeather = self
updatedWeather.condition = condition // this mistake inadvertently makes this method unable to change the weather condition
return updatedWeather
}
Copy link
Contributor Author

@Jager-yoo Jager-yoo Jan 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I added the missing return type here! -> Weather

the full code of this Weather example is:

import SwiftUI

struct WeatherView: View {

  @State private var currentWeather = Weather(condition: .sunny)

  var body: some View {
    VStack {
      weatherIcon(type: currentWeather.condition)
        .font(.largeTitle)

      Button("change weather") {
        currentWeather = currentWeather.updateWeather(.rainy)
      }
    }
  }

  private func weatherIcon(type: Weather.WeatherCondition) -> some View {
    switch type {
    case .sunny:
      return Image(systemName: "sun.max.fill")
    case .rainy:
      return Image(systemName: "cloud.rain.fill")
    }
  }
}

struct Weather {
  var condition: WeatherCondition

  enum WeatherCondition {
    case sunny
    case rainy
  }

  func updateWeather(_ newCondition: WeatherCondition) -> Weather {
    var updatedWeather = self
    updatedWeather.condition = condition // logical error that is hard to notice!
    return updatedWeather
  }
}

struct WeatherView_Previews: PreviewProvider {
  static var previews: some View {
    WeatherView()
  }
}

Copy link
Member

@calda calda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice improvement, thank you!

@calda calda merged commit dbddec8 into airbnb:master Jan 7, 2023
@Jager-yoo Jager-yoo deleted the clarify-unusedArguments branch January 7, 2023 17:47
waroo added a commit to slumberGroup/swift that referenced this pull request Jan 12, 2023
Clarify Rule: Name unused function parameters as underscores (airbnb#210)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants