Naming convention requires classes and structs to be capitalized when written. Although this practice is ubiquitously followed by all developers, few stop and ask for the reason behind it. The object closest to our nose often evades us.
I am fan of conscious programming, that is- coding with a clear mind and intent. In order to achieve such clarity, one must understand the subtleties of the programming language they use. That is what this blog is about, becoming a clear headed master builder. Let us start by taking a deeper look behind the well known naming convention of classes and structs.
Many developers believe that classes and structs are capitalized to be differentiated from variable. Although correct, it misses another important truth. The reason why classes and structs are capitalized is due to the fact that they are data types. They are not primitive data types such as an Int, String, Float, or Double, but they are data types nonetheless.
Let’s just say we create a new class but forget the convention of capitalizing its first letter. Your new class begins with a lower case but everything else functions as usual.
Now let’s create a variable and set its type to be the recent class created.
You should notice that something is strange in this syntax. It appears as if your new variable is typed of another variable, which makes no sense. Another developer might even confuse it for a ternary operator. Let’s see the difference when we follow naming conventions.
When we follow naming convention by capitalizing the first letter of our class, it is no longer confusing when we apply it as a type for a variable
It is not just classes and structs that are data types, but protocols and enumerations as well. Therefore, be sure to follow naming convention for any class, struct, protocol, or enum you create. Click here for the video counterpart of this article