Case Types in Programming

Harnessing Case Types for Effective Code Communication

Balraj Verma
3 min readJun 6, 2023
Photo by Bernice Tong on Unsplash

Case types in programming are the various nomenclature or naming practices applied to identifiers such as variables, functions, classes, and constants. The code’s ability to be read and maintained can be impacted by the case type selection. The choice of case style is usually determined by programming language conventions or personal or team preference. We will review some of the common case styles and also review how they are being used in Swift.

Camel Case: This is the most widely known and common case type. In the Camel case, the first letter of the identifier is lowercase, and the first letter of each subsequent concatenated word is capitalized. This case style is being used by many programming languages i.e. Java, JavaScript, C#, and Swift, etc. See the example below.

//Swift adoption of Camel case
var myVariable: String
func calculateArea(width: Double, height: Double) -> Double {
let area = width * height
return area
}
var numberOfStudents: Int

Pascal Case or Upper Camel Case: This case is mostly used for naming classes, interfaces, and enums. In this case, all concatenated words are capitalized. Swift adopts this style for naming classes, enums, protocols, structs, etc,

//Swift adoption of Pascal case or Upper Camel Case
struct ContentView: View{
....
}

class AppDelegate: UIResponder, UIApplicationDelegate {
....
}

public protocol UIApplicationDelegate : NSObjectProtocol {
...
}

public enum UIStatusBarStyle : Int {
...
}

Hungarian Camel Case: It is a variation of the camel case where an abbreviation is added to indicate the type of the variable. The prefix is typically a few lowercase letters representing the data type of that variable.

//Swift adoption of Hungarian Camel Case
var isValid: Bool { get }
var strName: String
var intCount: Int

The prefixes is, str, and int indicate the data types Bool, String, and Int respectively.

Snake Case: Snake case uses underscores (_) to separate words within an identifier. All letters are usually lowercase. This case style is used by languages like Python and Ruby but snake case is not the preferred naming convention in Swift, it can still be used if required.

//Swift adoption of Hungarian Camel Case
var total_count: Bool { get }
var str_name: String
var int_count: Int

Screaming/UpperCase Snake Case/ Macro Case: Screaming snake case, also known as Uppercase snake case and sometimes Macro case too, uses underscores to separate words, and all letters are in uppercase. This case style is often used for constants or global variables. It is being used in Swift to define Macros or to add user-defined variables in Plists. Xcode itself uses this case in the Build Settings macro. See the example below.

Macro case style swift

Kebab Case: Kebab case, also known as dash case, uses hyphens (-) to separate words within an identifier. All letters are typically lowercase. This style is commonly used in URLs, file names, and CSS class names. Examples: my-variable, calculate-area, number-of-students.

Train Case: Train case, similar to kebab case, uses hyphens to separate words, but each word’s first letter is capitalized. This style is less common in programming but can be seen in certain contexts. Examples: My-Variable, Calculate-Area, Number-Of-Students, etc.

The choice of case style depends on programming language conventions, project guidelines, or personal preferences. Consistency within a codebase is crucial for readability and maintainability, so it’s important to adhere to the chosen case style consistently throughout the code.

If you like reading this blog, give it a thumbs up. I will see you in the next one 😊!!!

--

--

Balraj Verma
Balraj Verma

Written by Balraj Verma

Share What I Lean 😊 | Mobile Developer

No responses yet