Who Is First-Class Citizen In Programming World?
The concept of first-class objects (or citizens) was introduced by Christopher Strachey in the 1960s, in which it was mainly adopted to contrast real numbers and procedures. Later, the modern definition of it, which needs to contain the following conditions, was given by Robin Popplestone.
- All items can be actual parameters of functions
- All items can be returned as results of functions
- All items can be the subject of the assignment statements
- All items can be tested for equality
Various types of data fulfill these conditions in programming languages such as first-class function, control, message. Among them, it is the first-class function that is the most widely utilized.
A programming language is said to have first-class functions when functions in that language are treated like any other variable. For example, a function can be passed as an argument to other functions, can be returned by another function, and can be assigned as a value to a variable.
() at the end.
A function also can accept another function as an argument, which is called a callback function, and can return a new function treated as a value. (Higher-Order Function)
A first-class collection is a class that only contains a collection with no other member variables. This, which was designed by Jeff Bay in the ThoughtWorks Anthology (2008), is not so much related to the first-class objects I mentioned above. Rather, it was regarding how to better software design In object-oriented perspective. The author has suggested using first-class collections:
… Each collection gets wrapped in its own class, so now behaviors related to the collection have a home. You may find that filters become part of this new class. Filters may also become function objects in their own right. ALso, your new class can handle activities such as joining two groups together or applying a rule to each element of the group. This is an obvious extension of the rule about instance variables but is important for its own sake as well. A collection is really a type of very useful primitive. It has many behaviors but little semantic intent or clues for either the next programmer or the maintainer.
- Jeff Bay, The ThoughtWorks Anthology: Essays on Software Technology
This could be implemented by wrapping a collection into a class in the example. Using first-class collections have several advantages:
- Make a data structure dependent on business service logic
- Guarantee the consistent state of a collection
- Manage its state and action at the same time
Make a data structure dependent on business service logic
In this example, the
LottoService class has a critical problem that it needs validation logic everywhere lotto numbers are needed, which makes duplicate invocation of the validation methods and an error might happen if some of your teammates have no idea about all the code and domains related to that. You can solve this issue by using the first-class collection.
Guarantee the consistent state of a collection
Making a collection as a variable of a class helps to keep its values consistent. Some say that you can do the same thing declaring it
final . Half-right! A final variable is only initialized once but it does not mean you can only put values into the variable once. Actually, you can do whenever you want once it is initialized.
On the other hand, a first-class collection can be customized for your needs. The example code shows that the
Order class prohibits adding extra values after initialized so that you definitely cannot add or modify the values of the collection variable in it, which helps you to minimize the side-effects related.
Manage its state and action at the same time
State and action can be managed at the same time by grouping the related code into one class. Why does it need? Imagine that initializing a collection variable and implementing a business logic with it are totally separate.
This can make some mistakes. If you or your successor needs to make a similar function later without knowing the history, the
totalSum() is more likely to be made additionally, which is duplicate and makes it hard to maintain source code. You can solve this issue with first-class collections.
Now you can manage the state and action of the
List<Pay> at the same time!
The name of
First-class has been used in real society to discriminate between the privileged and the others. It is amazing that the concept remains in many programming languages where its usage is very different and further needs to be encouraged to better applications and their maintenance.
Note that it is somewhat controversial to use the first-class collections in your code since I did not find any documentation or guide written in English other than those in Korean, albeit its clear advantages. Please let me know if some of you have any idea about this.
Thanks for reading this post.
First-class citizen - Wikipedia
In programming language design, a first-class citizen (also type, object, entity, or value) in a given programming…
First-class Function - MDN Web Docs Glossary: Definitions of Web-related terms | MDN
A programming language is said to have First-class functions when functions in that language are treated like any other…