Peter Spth
Leipzig, Germany
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484244661 . For more detailed information, please visit http://www.apress.com/source-code .
ISBN 978-1-4842-4466-1 e-ISBN 978-1-4842-4467-8
https://doi.org/10.1007/978-1-4842-4467-8
Peter Spth 2019
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
Introduction
Computer programs are for executing operations using input data to produce output data, sometimes by manipulating data taken from a database during that operation. The word database here is used in the most general sense: It could be a file, some memory storage, or a full-fledged database product.
Many different programming languages exist nowadays, each with its own merits and drawbacks. Some of them aim at execution stability, some at high performance, some are tailored to solve specific tasks, and some exist only because a company wants to establish a strong market position. Looking at the way programming languages have developed over time is an interesting subject in and of itself, and it has implications for various aspects of information technology. One could write a separate book about that, but for this book I simply want to stress one important fact about computer language development, which I think has a direct effect on the way modern computer programs are written. If you are looking at the historical development of computer languages, you will notice a substantial change in the abstraction level the languages exhibit. Whereas in the infancy of the industry a programmer needed to have a fairly good knowledge of computer hardware, now different levels of abstraction have been introduced into the languages, meaning an increased conceptional and linguistic distance from hardware features. This has increasingly alleviated the requirement that software developers know what is occurring in a computers central processing unit (CPU).
Along with an increasing level of abstraction, modern computer languagessometimes implicitly, sometimes explicitlyexhibit a prominent new feature: the
expressiveness of language constructs. Let me try to illustrate this using an example written in pseudo-code. Lets say you have a list of items and want to perform an operation on each of the items. With some knowledge of the internal functioning of computers, a programmer might write a code snippet like this:
Although this looks a little bit complex, it closely relates to what computers are doing under the hood, and early languages looked more or less like this. As a first abstraction and a way to improve readability, we can try to get rid of the pointer element and instead write:
variable theList = [somehow create the list of items]
loop over "theList", assigning each item to an iteration variable "item":
do something with "item", for example print it
end loop
This already looks more expressive compared to the first version, and a lot of current programming languages allow for this kind of programming style. We can do even better, though: You can see the definition of the list being written in one line, separated from the list processing in the loop. Theres nothing preventing us from writing a lot of overly complex code between the list definition and the loop, and this is what you see quite often, making the program hard to read and understand. Wouldnt it be better to have it all in one statement? Using a more expressive snippet allows us to write such a combined statement. In pseudo-code, it could look like this:
[somehow create the list of items].
[maybe add some filter].
forEach { item ->
do something with "item", for example print it
}
This is about the maximum of expressiveness you can get, if you see the dot . as some kind of do something with it command and { } as a block of code doing something, with the identifier in front of the -> in this case designating a loop variable.
Note
Making your code expressive from the very beginning will not only help you to write good code, it will also help you to develop your programming skills beyond average. Expressive code is easier to maintain and extend, easier to reuse, easier to understand for others, and easier to debug if the program shows some deficiencies.
The programming language Kotlin is capable of getting us to such an extent of expressiveness, and in this book I want to introduce Kotlin as a programming language for Android that allows you to accomplish things in an expressive and concise way. As a matter of fact, in Kotlin the little looping example, with a filter added, reads:
arrayOf("Blue", "Green", "Yellow", "Gray").
filter { it.startsWith("G") }.