Nagisa-AC / Flixter

Movie based app that allows users to get information on current playing movies
0 stars 0 forks source link

Declare strings (and other values) that won't change as constants #6

Open tisganitis-fb opened 2 years ago

tisganitis-fb commented 2 years ago

https://github.com/Nagisa-AC/Flixter_4/blob/55b210bc6a267e674b58ec5d2b526b8ea0013a8e/Flixer_4/MovieViewController.m#L96

If you don't expect a value to change you should declare it as a constant. This helps catch mistakes at compile time (e.g. later trying to change this value, which could introduce a bug).

You can declare a string constant with: NSString *const Foo = @"Foo"; if it's going to be shared across files or static NSString *const Foo = @"Foo"; if it's only going to be used in a single file, as in this case.

(The static keyword means it is only allocated once and shared across all instances of the class it is declared in.)

It is also common to put all the static string constants at the top of the file, so they are easy to find later. (Since often they are essentially config, so often you are just making a change to the constant value without touching any other code.)

This isn't a bad longer explanation: https://bytes.vokal.io/objc-string-constants/