Table of Contents:

Free course: “Quick start in Python»
Learn more
Jeremy Grifsky
The author of this text is a professional in his field with extensive experience and deep knowledge. He is engaged in research and development, striving to share his knowledge with a wide audience. His work covers a variety of topics, allowing him to bring a unique perspective to the issues discussed. The author actively participates in scientific conferences and publishes articles in specialized journals, which confirms his status as an expert. His goal is to provide readers with high-quality and relevant content that is useful and informative.
An enthusiastic programmer who is currently pursuing a PhD in engineering aspires to become an educator. He is passionate about writing articles about programming and shares his knowledge on his website, The Renegade Coder.
Links play a key role in the structure of the internet and SEO. They provide connections between different web pages and help users find the information they need. There are two main types of links: internal and external. Internal links direct users to other pages within the same site, which improves navigation and visitor retention. External links point to other resources, which can increase your site's authority in the eyes of search engines. Link optimization, including the use of keywords in anchor text and proper link equity distribution, is an important part of an SEO strategy. Furthermore, having high-quality external links to your website can significantly improve its visibility and ranking in search engines. Therefore, it is important to pay attention to link building and management to achieve the best results in online marketing.
I have been writing a series of articles called "Program Examples in Every Language" for a long time. During this time, I have studied about fifty programming languages and want to share their most interesting features. In each article, I examine the unique features of the languages, their syntax, and code examples, which will help both beginners and experienced developers better understand their potential. I hope my experience will be useful and inspire you to explore new technologies and tools in the world of programming.
The translator's note contains important clarifications and comments regarding the translation of the text. In this section, the author shares his thoughts on the choice of words and phrases that could best convey the original meaning. Cultural and contextual aspects that can affect the perception of the translation are also considered. These notes help the reader better understand the nuances of the text and justify the translator's decisions. A translator's note is an important element that contributes to a deeper understanding and interpretation of the material.
We have prepared two articles to explore these interesting features in more detail. In this first part, you'll find many fascinating facts and details that will allow you to gain a deeper understanding of the topic. Stay tuned for the next article, where we'll continue covering interesting topics and provide even more information.
- Extensions
- Macros
- Automatic Properties
- Optional Chaining
- Lambda Expressions
- Gradient Typing
The second part of the article is available at the following link.
Extension Methods
One of the most interesting features I've discovered is extensions. I first encountered them while working on a simple Hello World project in Kotlin. While extensions weren't necessary in such a basic program, that experience introduced me to their existence and potential. Extensions in Kotlin allow you to add new functionality to existing classes without changing their code, which significantly simplifies development and improves code readability.
Extensions provide the ability to add new methods to existing classes without changing their structure. This allows you to expand the functionality of classes, improving your code and providing greater flexibility in software development. Using extensions promotes code reuse and simplifies project maintenance, since changes can be made locally without affecting the core classes.
Imagine you like the String class in Java, but you want to improve its functionality by adding a new method. The most suitable way to do this is to create your own class that inherits String. This approach allows you to extend the capabilities of the standard class by adding unique methods and properties, while preserving all the benefits of working with strings in Java. Subclassing String gives you the ability to use existing methods alongside new ones, making your code more flexible and easier to develop.
A translator's note is an important element that helps the reader better understand the context and nuances of the translation. In this section, the translator can explain the specifics of the original text, highlight any challenges they encountered, and explain the choice of words and phrases. Notes can include cultural references, lexical variations, and explanations of grammatical constructions that may not be obvious to the reader. This approach ensures a deeper understanding of the translated material and helps avoid misunderstandings. Professional translators always strive to provide readers with as much information as possible in order to preserve the meaning and spirit of the original.
Creating a class that extends String in Java is impossible, as String is a final class. This means that it cannot be inherited, which limits its modification and extension. Instead, developers can use other approaches, such as creating their own classes that encapsulate string data and provide additional methods for working with it. This allows them to achieve the desired functionality without violating the principles of object-oriented programming and the restrictions imposed on final classes in Java.
In Kotlin, it is possible to create a method that directly extends the String class. This allows you to add new functionality to existing classes without changing their source code. Method extensions in Kotlin provide a more flexible and convenient way to work with strings, allowing developers to create custom functions that can be used in the same way as standard String class methods. This approach improves code readability and promotes code reuse, which is especially important in software development.
Now, when creating an instance of the String class, we have the ability to use the mutate method as if it were a regular public method of the class. This opens up new horizons for working with string data, allowing us to modify their contents with convenience and efficiency.
Extensions have their drawbacks. For example, if a method named mutate is added to the standard methods of the String class, this could lead to errors in your program. Although such name conflicts do not occur often, they still represent a potential problem. Therefore, it is important to be careful when using extensions to avoid unexpected failures in your code.
I have not found a more effective way than using extensions for rapid prototyping. If you have more interesting suggestions, please share them.
Macros
Macros are an interesting feature of programming languages that significantly simplifies development. I first encountered them when I was creating a Hello World program in Rust. In this language, console output is performed using macros, which highlights Rust's flexibility and power. Macros allow you to optimize your code and reduce development time, making them an essential tool for programmers.
Macros are an important aspect of metaprogramming, allowing you to directly extend the programming language. They allow you to add new rules and operations to the abstract syntax tree, significantly increasing the flexibility and expressiveness of your code. These rules are created using a pattern matching mechanism, making the process more intuitive and powerful. Using macros opens up new opportunities to optimize and simplify software development, allowing developers to create more complex and adaptive solutions.
To explain this topic more clearly, let's look at an example in the Rust programming language. Rust is a modern language focused on safety and performance. It offers unique memory management capabilities without the need for a garbage collector, making it ideal for developing high-performance applications. Let's dive into a concrete example to illustrate the key concepts and features of Rust.
This code is taken from the Rust programming language source code. The example demonstrates the use of the print macro, which applies a single pattern match. This macro can take any number of arguments and convert them to the appropriate format before printing.
If you are struggling with pattern matching, it is recommended to deepen your knowledge of regular expressions, as they have a similar syntax. Regular expressions are a powerful tool for searching and processing text, and understanding them can greatly facilitate working with patterns. Learning regular expressions will help you perform tasks related to data analysis and process automation more effectively.
Working with macros can be challenging. Writing them requires time and skill, and debugging them can often be difficult. The Rust documentation considers macros a last resort, to be used only in exceptional cases and with certain guidelines. This highlights the importance of using them consciously to avoid potential problems and improve development efficiency.
Automatic Properties
I became familiar with them when I was learning C# (details can be found in the article "Hello World in C#").
Automatic properties are a simplification for implementing getters and setters in object-oriented programming languages. They allow developers to reduce the amount of code, improving its readability and maintainability. Using automatic properties eliminates the need to explicitly define fields and accessor methods, making the process of creating classes more efficient. This is especially useful when working with simple data, where additional logic in getters and setters is not required. Automatic properties are becoming increasingly popular in modern programming languages such as C# and Java due to their convenience and simplicity.
In this example, we will create a Person class that will contain a name field, since every person has a name. Here is how this can be implemented in Java:
«`java
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
«`
In the above code, we declare a Person class with a private String field named name. The class constructor allows us to set the name when creating an object. The getName and setName methods provide access to the name field, allowing both retrieval and modification. This is a basic example demonstrating how to work with class fields in Java.
If we need to set the value of name, we must create a public method with the public modifier. This will allow us to update the private field. These methods are often called setters because they set the properties of an object. Officially, they are called mutator methods. Setters play an important role in ensuring encapsulation, allowing you to control access to the internal data of a class and ensure its integrity.
In the Java programming language, the code to create a mutator is represented as follows:
We have already written six lines of code, but we cannot get the value of the name variable outside the class. To solve this problem, we need to create a getter that will allow access to the variable's value. A getter is a method designed to retrieve data from an object, making it available for use outside the class.
In languages that support automatic properties, you can significantly reduce your code by eliminating six lines of boilerplate. Here's the C# equivalent of our Java class:
By using automatic properties, we can simplify our code by writing just one line for each field accessible to other classes. This greatly simplifies the development process and makes the code more readable. Without this feature, we would have to use six lines of code to achieve the same result. Automatic properties make it easier to create your own classes and improve programming efficiency.
Optional Chaining
Our new hero is optional chaining, also known as optional chaining. I first learned about them when writing my first Swift project. Although for a simple "Hello World" program, This feature wasn't necessary, but learning about it was quite insightful. Optional chaining allows you to simplify your code by preventing errors when accessing properties and methods of objects that may be nil. This functionality makes your code safer and more readable, which is especially important when developing complex applications. Optional chaining becomes clearer when you examine optional variables. Let's start with their definition and features. Optional variables allow you to avoid errors associated with attempting to access properties or methods of objects that may not exist. This simplifies your code and makes it safer. Understanding this concept is the basis for using optional chaining in programming, as it allows you to call methods and access properties of objects without having to first check their existence.
In Swift, variables cannot be null, meaning they cannot store the NIL value directly. This is an important aspect of the language because it ensures that each variable always contains a valid value. This feature helps improve the reliability and safety of code by minimizing the possibility of errors related to missing data. Swift offers mechanisms such as optionals that allow you to work with potentially missing values while maintaining strong typing and preventing runtime errors.
In some cases, you may need to assign the NIL value to a variable. Fortunately, Swift provides this ability through the use of optional variables. These variables wrap the actual value, including NIL, in a special container. The value can later be retrieved from this container, simplifying work with potentially missing data and improving code safety.
In this example, we create an optional string variable and assign it the value "Hello, World!" Since we are sure that the variable contains a string, we can safely extract its value and print it to the screen without the need for additional NIL checks. This approach simplifies working with variables and avoids unnecessary checks, resulting in cleaner and more understandable code.
Translator's Note
This section is intended to provide readers with additional information about the translation. Here you can find explanations of selected words and phrases, as well as clarifications on cultural aspects that may affect comprehension of the text. The translator has strived to convey the meaning of the original as accurately as possible, taking into account the nuances of language and context. This process includes not only lexical but also stylistic features in order to preserve the author's style. If you have any questions or require additional information, please do not hesitate to contact us.
The ? symbol after the String type tells the compiler that the printString variable is optional. This means that this variable can store either a string value or an empty value (NIL). Optional variables allow for more flexible data management and avoid errors associated with missing values. Using optional types in programming improves code readability and increases its reliability.
The «!» symbol is used to extract a value from a variable and display it. This method allows for efficient data output, providing convenient access to the information contained in variables. Correct use of this symbol in code helps improve readability and simplifies the debugging process.
Extracting a value without prior checks is considered bad programming practice. I deliberately neglected this recommendation in this example to make it easier to understand. However, it is important to remember that in real projects, you should always consider the possibility of errors and provide appropriate checks. This will help avoid potential problems and improve the reliability of your code.
The concept of optional values also applies to method calls and fields in programming. In this context, we are talking about chains of optional calls. Consider a situation where we have a long chain of method calls. This allows us to safely access properties and methods of objects without fear of errors related to missing values. This approach leads to cleaner and more readable code, as it minimizes the need to check for the presence of values at each level of the chain. Using optional calls helps improve code reliability and improve its performance, which is especially important in modern applications that require a high degree of robustness and efficiency.
In this example, we retrieve a string value from the command line and split it into segments separated by hyphens (-). We then access the fifth segment and get the seventh character from this segment. If at least one of the three methods fails, it will lead to a crash of the entire program. This approach requires care, as error handling is critical for the stable operation of the application.
Using optional chaining, we can intercept the NIL value at any stage of the chain and handle it gracefully. As a result, instead of raising an error, we get the important_char value equal to NIL. This solution is significantly preferable to encountering the "pyramid of death" problem. Optional coupling makes code more readable and error-resistant, which improves the overall reliability of the application.
A translator's note is an important element that allows readers to better understand the context of the translation. In this section, the translator can explain word choice, cultural context, and point out difficulties encountered during the translation process. This helps create a more complete perception of the text and avoids misunderstandings. Considering that translation is not simply a replacement of words, but a conveyor of the meaning and style of the original, notes can be useful for in-depth analysis of the text. They also contribute to a better perception of the translator's work and emphasize the importance of high-quality translation in literature and other fields.
When functions serve as arguments to other functions, which can also contain functions, it becomes possible to create complex and multi-level constructs. This significantly expands the functionality of the program code and increases its flexibility. For example, in pseudocode it might look like this:
Writing function code with right indentation makes it easier to perceive the program structure, allowing you to better understand which lines of code relate to which functions. This approach makes the code more readable and organized, resembling the shape of a pyramid. Proper code formatting not only improves its appearance but also makes it easier to debug and maintain, especially in large projects.
If the number of nesting levels in the code is too high, the top of the pyramid shifts far to the right. This makes reading and maintaining such code extremely difficult. Incorrect code structure can lead to difficulties in maintenance and understanding, which negatively affects developer productivity. Optimizing nesting levels and simplifying the code structure are important aspects for increasing its readability and ease of maintenance.
Lambda Expressions
This list would be incomplete without lambda expressions. Lambda expressions are not a new concept; their roots lie in the theory of computation and they have been around even longer than computers themselves. Yet, they continue to be integrated into modern programming languages, including established systems like Java. Java introduced lambda expressions in version 8, released in 2014. This confirms the relevance and importance of lambda expressions in modern programming, improving readability and reducing code size.
Lambda expressions have become popular in programming, and I first heard about them three or four years ago when I was learning Java. At that time, I didn't fully understand their meaning and benefits, so I didn't pay much attention to the topic. However, lambda expressions open up new possibilities for simplifying code and increasing its readability, making them an essential tool for developers.
A few years later, I began learning Python, a programming language with many open-source libraries that make extensive use of lambda expressions. As a result, I had to master them to work effectively with the language. Lambda expressions in Python allow you to create anonymous functions, which significantly simplifies the coding process and makes it more flexible.
Lambdas, or lambda expressions, are anonymous functions that can be used as data. The main difference between lambdas and regular functions is that they can be packaged in variables and used like regular data. This allows for more compact and expressive code. For example, you can assign a lambda expression to a variable and then call it just like any other function, simplifying function manipulation and making code more flexible. The use of lambdas is widespread in functional programming and provides developers with powerful tools for working with functions as first-class objects.
We've created a function, stored it in a variable, and called it like a regular function. In fact, it's possible to create a function that returns other functions, allowing for dynamic function generation. This approach opens up new possibilities for programming, allowing for more flexible and adaptive solutions.
Feels great!
Gradual Typing
If you have programming experience, you are likely familiar with the two main types of typing: static and dynamic. It is important not to confuse them with explicit and implicit typing, as well as strong and weak typing, as these are completely different concepts. Static typing involves determining data types at compile time, which allows for earlier detection of errors. Dynamic typing, in contrast, allows for determination of data types at runtime, which provides greater flexibility but can lead to errors that only arise at runtime. Understanding these differences is key when choosing a programming language and designing software architecture.
A translator's note is an important element that helps the reader better understand the context and nuances of the translated text. In this section, the translator can point out features of the original that were not captured in the translation, explain the choice of certain words and phrases, and share their thoughts on cultural differences. Such notes can significantly enrich the perception of the text, adding depth and understanding. It is important to remember that a high-quality translation not only conveys the content but also preserves the author's style and atmosphere of the original. Therefore, in addition to the translation, the translator's note serves as an additional bridge between the two cultures and languages, allowing the reader to understand the author's original intentions.
With static typing, the variable type is known at compile time and is checked before program execution. With dynamic typing, the variable type is determined at runtime based on the value assigned to it. Static typing avoids errors associated with type mismatches, while dynamic typing provides greater flexibility and simplifies development.
Explicit typing assumes that each variable has a specified type in advance. This is a type of static typing where the variable types are determined at compile time. Unlike explicit typing, implicit typing allows developers to avoid specifying variable types, which simplifies the coding process but can lead to errors later. Explicit typing provides greater predictability and code safety, making it easier to identify errors at compile time.
In strongly typed languages, type conversion is only possible between compatible types. This means, for example, that in a strongly typed language, you cannot pass a string to a subroutine if its parameter expects a numeric type without explicit type casting. Strong typing helps avoid errors associated with implicit conversions and provides more precise control over data types in your code.
Weakly typed languages, such as JavaScript, provide greater flexibility in working with data types. In such languages, it is possible to perform operations that would cause errors in strongly typed languages. For example, in JavaScript, you can add a string to a number without an error. The number will be automatically converted to a string, allowing developers to easily manipulate data without having to explicitly specify types. This feature makes weakly typed languages convenient for rapid development and prototyping.
Gradient typing is the intersection of static and dynamic typing in programming languages. This concept allows developers to independently determine when they want to use static typing, while dynamic typing is applied by default. Gradual typing provides flexibility and convenience, allowing programmers to improve the readability and safety of code, depending on the needs of the project. It is one of the most attractive features of modern programming languages, which contributes to more efficient software development.
In many programming languages, gradual typing is achieved through explicit type declarations, which is typical of strongly typed languages. This allows developers to clearly define the data types of their variables and functions, improving code readability and reducing the likelihood of errors. Gradual typing combines the benefits of both dynamic and static typing, allowing developers to flexibly choose the right type depending on the context. This approach helps optimize performance and simplifies debugging, which is especially important when working on large projects.
This is a Python function in which the input and output parameter types are clearly specified; both are float. However, explicit type annotations are also possible. Using type annotations in Python improves code readability and allows developers to quickly understand what values are expected as input parameters and what the function will return. However, Python allows functions to be written without such annotations, making the language flexible and suitable for various programming styles.
Now nothing prevents you from passing any data to this function. However, the first implementation option provides static type checking, which allows you to use built-in static analysis tools in the IDE or external utilities. This makes it a solid choice for development.
I chose Python as an example, but I first encountered the concept of gradual typing when writing a Hello World program in Hack. This confirms that Facebook is truly committed to improving PHP's type system by implementing stronger and more flexible typing mechanisms.
What's Next
In the next installment, you'll find topics related to multiple dispatch, immutability, and inline assembly. In the meantime, you can delve deeper into lambda expressions, different typing approaches, and the Python and Java programming languages. These concepts will help you better understand modern trends in software development and improve your programming skills.

