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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule Request: Discouraged Object Literal (Opt-In) #1987

Closed
ornithocoder opened this issue Jan 2, 2018 · 1 comment
Closed

Rule Request: Discouraged Object Literal (Opt-In) #1987

ornithocoder opened this issue Jan 2, 2018 · 1 comment

Comments

@ornithocoder
Copy link
Contributor

ornithocoder commented Jan 2, 2018

SwiftLint provides a rule to enforce the use of literals over UIImage and UIColor initializer, but while object_literal makes sense for some projects and users, it doesn't for others.

Some of the reasons not to use literals:

  1. Icons and colors (but specially icons) can be really hard to identify
    1. because they're teeny-tiny rendered, and/or
    2. against dark background of some Xcode color schemes
  2. When reading the code, it's hard to know which color or image is being used. There's no context, no asset or color name, no immediate information about it.
    • To learn more about the color or image being used, one has to stop reading the code and browse through additional code to find more about these.
  3. etc...

Below, the same image literal example with four different Xcode templates.

Default:

screen shot 2018-01-02 at 9 19 31 pm

Civic:

screen shot 2018-01-02 at 11 09 43 pm

Nord:

screen shot 2018-01-02 at 9 19 07 pm

Ayu 2:

screen shot 2018-01-02 at 9 19 23 pm

As shown above, the icon is hard to visualize when using dark background color and there's no information about the asset used (which asset is being used?, what's its name?).

A custom rule can be used as workaround to disable image literals while the new opt-in rule isn't implemented and merged into master.

  discouraged_image_literal:
    name: "Discouraged Image Literal"
    regex: "#imageLiteral"
    message: "Please use UIImage(named:) instead"
    severity: warning

A similar custom rule can be easily implemented for color literals.

@ornithocoder
Copy link
Contributor Author

@marcelofabri @jpsim please let me know if the rule makes sense for SwiftLint. If it does, I'll work on it.

@ornithocoder ornithocoder changed the title Rule Request: Discouraged Object Literal Rule Request: Discouraged Object Literal (Opt-In) Jan 2, 2018
ornithocoder added a commit to ornithocoder/personal-fork-swiftlint that referenced this issue Jan 2, 2018
jpsim pushed a commit to ornithocoder/personal-fork-swiftlint that referenced this issue Jan 4, 2018
@jpsim jpsim closed this as completed Jan 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants