ProgrammingPro #47: CISA’s OSS Action Items, C# Genetic Algorithms, Python’s GIL Disablement, & AI-Powered Code Optimization
Bite-sized actionable content, practical tutorials, and resources for programmers
Welcome to this week’s edition of ProgrammingPro!
In today’s Expert Insight, we bring you an excerpt from the recently published book, C# Data Structures and Algorithms - Second Edition, which offers insight into implementing genetic algorithms in C#, and teaches you how to apply concepts like fitness, crossover, and mutation to solve problems through evolving solutions.
News Highlights: CISA's security summit and Biden-Harris administration's government software requirements set new standards. Python's GIL disablement paves the way towards faster concurrancy, while TypeScript 5.4's introduction of NoInfer, and Swift 5.10's data-race safety mark significant advancements.
My top 5 picks from today’s learning resources:
But there’s more, so dive right in.
Stay Awesome!
Divya Anne Selvaraj
Editor-in-Chief
PS: If you have any food for thought, feedback, or would like us to find you a specific Programming learning resource for our next issue, take the survey! As a thank you, we will send you one free Packt credit to buy a book of your choice.
🗞️News and Analysis🔎
CISA concludes two-day summit on open source security with three action items: At the Open Source Software Security Summit, CISA outlined three initiatives: adopting security principles for package repositories, enhancing cyber defense information sharing with OSS maintainers, and distributing tabletop exercise materials for improved security practices. Read to learn more.
Biden-Harris Administration to require secure software development attestation form for government software: Developed by CISA and OMB, the form mandates environment separation, multi-factor authentication, encryption, and more. Read to learn about how this impacts software suppliers.
Python progresses towards faster concurrency - option to disable GIL merged into main code: This change, set for Python 3.13, allows running Python with free threading but requires cautious implementation to minimize disruption. Read to learn how this benefits performance-sensitive applications.
TypeScript 5.4 adds NoInfer utility type: The additon aligns with JavaScript enhancements, introducing declarations for Object.groupBy and Map.groupBy methods. Read to learn more about the new functionalities and improvements.
Modernizing legacy code at LinkedIn: how big bang versus gradual approach caused conflict: This article incorporates reflections from a former senior staff engineer at LinkedIn on what led them to leave. Read for insights into the complexities of updating large codebases, the impact of technical debt, and more.
Netlify AI-assisted deployment aims to reduce log-diving: Netlify has launched an AI-assisted deployment feature designed to analyze failures and suggest fixes, aiming to minimize developers' time spent troubleshooting. Read for more.
Swift language achieves data-race safety: Swift 5.10 introduces full data isolation in its concurrency model, ensuring safety against data races by preventing concurrent threads from accessing the same memory simultaneously. Read to learn how you can enable complete concurrency checking at compile time.
Rust-based WinterJS web server reaches 1.0: Touted as the fastest JavaScript web server, WinterJS now supports Cloudflare apps and React Server Components. Read to learn how it can handle 150,000 requests per second natively.
🎓Tutorials and Learning Resources💡
Python
🎓Tutorial | How to Use VS Code as Your Python IDE: This artilcle takes you through setting up the environment, installing necessary extensions, and creating and running a basic Python application. Read to learn how to configure VS Code for Python development.
Scientific Python Lectures: This comprehensive guide covers numerics, science, and data analysis from beginner to expert levels through tutorials on tools and techniques. Read to learn the fundamentals of data manipulation and more.
Optimize Your Code With This 1 Line in Python: This article introduces functools.lru_cache, a Python decorator for optimizing code efficiency by caching the results of functions based on their arguments. Read to learn how to
optimize recursive functions or those with heavy computations.
For more Python resources go to PythonPro
C# and .NET
.NET/C# instrumentation for search-based software testing: This paper introduces a novel search-based approach and an open-source tool for white-box testing of C# applications on the .NET platform, utilizing .NET bytecode instrumentation Read to learn how this technique surpasses gray-box testing tools.
🎓Tutorial | Digging deeper into DbContext in Entity Framework Core: The article advises against disposing DbContext instances with using statements due to potential issues like pending database modifications and interference with change tracking. Read to learn advanced practices for managing DbContext in EF Core.
🎓Tutorial | Virtual in C# - How to Use it?: This article demystifies the 'virtual' keyword in C#, illustrating its role in enabling polymorphism by allowing methods, properties, or indexers in a base class to be overridden in derived classes. Read to learn how to enhance flexibility and extensibility in object-oriented programming.
C and C++
C++ safety, in context: Herb Sutter discusses the pressing need for C++ language safety improvements amidst rising cybersecurity and safety threats, emphasizing C++'s current shortcomings in type, bounds, initialization, and lifetime safety. Read to gain insights into the significance of enhancing C++ language safety.
🎓Tutorial | How can I force a copy of a C++ value?: With C++23's introduction of "auto decay copy," directly making copies using auto(x) is now possible, simplifying the syntax and eliminating the need for the copy_of function. Read to learn efficient strategies for safely passing copies of objects in C++.
🎓Tutorial | Implementing scoped defer in C: This article presents a macro-based solution for implementing scoped defer in C, aimed at simplifying error handling and resource management. Read to discover a practical solution to streamline error handling and resource management.
Java
1BRC merykitty’s Magic SWAR - 8 Lines of Code Explained in 3,000 Words: This blog post delves into an optimization technique called SWAR (SIMD Within A Register) applied during the 1BRC. Read to discover how bitwise operations and arithmetic can solve complex problems with exceptional efficiency.
Update on String Templates (JEP 459): The update proposes that StringTemplate and String remain unrelated types, with API-driven template processing, enhancing interoperability and reducing complexity. Read to learn how the update aims to make handling string templates more intuitive and efficient in Java.
🎓Tutorial | The number of places you can find the Java version in Intellij: In IntelliJ, managing Java versions across different projects can lead to confusion and vague errors. Read to manage Java versions effectively, ensuring compatibility and leveraging the IDE's features to its fullest.
JavaScript and TypeScript
📖Open Book | Eloquent JavaScript - 4th edition: This book by Marijn Haverbeke explores the language's application in the digital world, and is structured into three parts focusing on core JavaScript language features, Node.js for server-side development, and more. Read to learn through practical examples.
Comparing JavaScript Frameworks part 1 - templates: This comprehensive article compares the template languages of four major JavaScript frameworks: Vue.js, React, Angular, and Svelte. Read to learn about the nuances and practical considerations of choosing between JavaScript frameworks for web development.
Ceiling is being raised: analyzing our "superhuman code" that broke twitter (and why it's actually good code!): This article discusses the creation of complex, conditional recursive type expressions in TypeScript to enhance the development experience within a framework called CopilotKit. Read to gain insights into the complexities and advantages of using advanced type systems.
Go
💼Case Study | You don't need a database, a queue, a distributed system: Go is enough: This article critiques the common belief that extensive infrastructure must be established before coding begins. Read for insights into Go's strength in creating efficient, performant applications with minimal infrastructure.
🎓Tutorial | For Loops and More in Go: This article provides an in-depth examination of looping constructs in Go, highlighting the flexibility and nuances of for loops. Read to gain insights that can help prevent bugs and optimize code performance.
Rust
🎓Tutorial | How to Use Rust Procedural Macros to Replace Panic with syn’s Fold: Procedural macros in Rust offers the capability to manipulate code, such as converting panics into Errs, making code manipulation more manageable and less repetitive. Read to learn how to elegantly manipulate and transform Rust code, particularly for error handling improvements.
🎓Tutorial | Using Stripe Payments with Rust: This article details setting up one-off payments, managing customers, handling products and prices along with subscriptions, and utilizing webhooks for event-driven actions. Read to learn how to implement Stripe Payments in your web services.
PHP
Stop Using These 5 Outdated Optimization Tricks in PHP: This article highlights five outdated PHP optimization tricks that developers should avoid, emphasizing that such micro-optimizations are largely irrelevant with the advancements in PHP 7 and beyond. Read to learn why the author advises against sacrificing readability.
SQL
🎓Tutorial | Raster Data Analysis With Spatial SQL And Apache Sedona: This article covers how to load, manipulate, and analyze raster data, showcasing the capabilities of Sedona in handling geospatial information efficiently. Read to enhance your geospatial data processing skills.
Ruby
What Does the Frozen String Literal Comment Do in Ruby?: The # frozen_string_literal: true magic comment in Ruby automatically freezes all string literals in the file, preventing their modification and optimizing memory use. Read to learn how freezing strings leads to better memory management and application efficiency.
Swift
Byte-sized Swift - Building Tiny Games for the Playdate: Highlighting Swift's memory safety and interoperability with C, this article explores the language’s fit for embedded systems beyond traditional Apple platforms. Read to learn how Swift can be applied to game development for embedded systems.
Kotlin
Kotlin Design Patterns - Abstract Factory Explained: This pattern in Kotlin is used to create families of related objects without revealing their concrete classes, delegating object creation to specific factories. Read to learn the advantages of using the Abstract Factory pattern to maintain code flexibility.
🌟Best Practices and Code Optimization🚀
🎥So You Think You Know Git - FOSDEM 2024: In this video talk, Scott Chacon, a GitHub co-founder, explores the nuances of Git, including configurations, aliasing, and advanced features like Git maintenance and partial cloning. Watch to learn advanced Git techniques, optimization strategies for managing large repositories, and discover the latest Git and GitHub features.
Agile Architecture, Lean Architecture, or Both?: This article highlights the complementary nature of Agile and Lean practices in software architecture. Read to learn the importance of a balanced application to navigate the complexities of developing viable, sustainable software products.
OWASP Top 10 Explained: SQL Injection: This article delves into SQL Injection (SQLi), detailing its mechanisms, types (In-band, Inferential, Out-of-band), and preventive measures across frontend and backend development. Read to gain insights into the pervasive threat of SQL Injection.
1276 GPTs for Code Optimization Powered by AI for Free: These tools are versatile, supporting multiple programming languages and environments, and offer benefits like efficiency improvements and error reduction. Read to learn more.
Clean Code - The Art of Clean Naming: Clean naming involves choosing meaningful, context-rich names over generic ones like 'x' or 'day,' which can lead to confusion. Read to learn why descriptive, full names, and consistent abbreviations, are essential for writing maintainable and understandable code.
🧠 Expert Insight 📚
Here’s an exclusive excerpt from “Chapter 9, See in Action” in the book, C# Data Structures and Algorithms - Second Edition by Marcin Jamro.
Title guess
It is high time to change a type of applied algorithm to a heuristic one, which has many applications and also subtypes. Here, we focus only on genetic algorithms, which are adaptive heuristic search algorithms. They are related to the Darwinian theory of evolution and natural selection. According to it, individuals in a population compete, and the population evolves to create next
generations that are better suited to survive. The genetic algorithms operate on strings that evolve to receive possibly the highest value of fitness, complying with the rule of survival and passing on the genes of the fittest parents, also based on a randomized data exchange. The algorithm ends its operation when a suitable value of fitness is reached or when the maximum number of generations is reached.
Where can you find more information?
You can find a lot of content about genetic algorithms in the internet, such as in the article published here. The simple approach to a genetic algorithm, which is shown in this chapter, is based on the solution presented here.
Let's take a look at an example of a genetic algorithm application to guess the title of this book. The first part of the code is as follows:
const string Genes = "abcdefghijklmnopqrstuvwxyz
#ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
const string Target = "C# Data Structures and Algorithms";
Random random = new();
int generationNo = 0;
List<Individual> population = [];
for (int i = 0; i < 1000; i++)
{
string chromosome = GetRandomChromosome();
population.Add(new(chromosome,
GetFitness(chromosome)));
}
First, you create an initial population, with 1,000 individuals. Each individual has a random chromosome, represented by a random string whose length is equal to the target string, which is a title of the book. Let’s go further:
List<Individual> generation = [];
while (true)
{
population.Sort((a, b) =>
b.Fitness.CompareTo(a.Fitness));
if (population[0].Fitness == Target.Length)
{
Print();
break;
}
generation.Clear();
for (int i = 0; i < 200; i++)
{
generation.Add(population[i]);
}
for (int i = 0; i < 800; i++)
{
Individual p1 = population[random.Next(400)];
Individual p2 = population[random.Next(400)];
Individual offspring = Mate(p1, p2);
generation.Add(offspring);
}
population.Clear();
population.AddRange(generation);
Print();
generationNo++;
}
The most interesting part is located in the infinite while loop. Here, you sort the population from the best fitted to survive – that is, by fitness in decreasing order. To explain it in detail, fitness is equal to 0 when no chars in the chromosome string match the following chars in the target string. In turn, fitness is equal to 33 (i.e., the number of chars in the book title), when the chromosome string is equal to the target string. For this reason, if the first element from the population (namely the fittest) has a fitness equal to the target string length, it means that the solution is found, so you just print it and exit the loop.
Otherwise, you clear the list with data of a new generation and add 200 best-fitted individuals to it. This means that 20% of the best-fitted individuals are moved automatically to the next generation. For the remaining 800 places in the new generation, you perform crossover and randomly choose parents, from 40% of the best-fitted individuals, to generate new individuals. Then, you replace the current population with the new generation and proceed to the next iteration.
It’s worth mentioning the Individual record, the code for which is as follows:
record Individual(string Chromosome, int Fitness);
It contains two properties, namely Chromosome and Fitness. The first stores the string adjusted in the evolution, while the other is the number indicating how this particular individual is fit to survive. Of course, a higher value is better.
The Mate method is used to generate a new individual using two parents:
Individual Mate(Individual p1, Individual p2)
{
string child = string.Empty;
for (int i = 0; i < Target.Length; i++)
{
float r = random.Next(101) / 100.0f;
if (r < 0.45f) { child += p1.Chromosome[i]; }
else if (r < 0.9f) { child += p2.Chromosome[i]; }
else { child += GetRandomGene(); }
}
return new Individual(child, GetFitness(child));
}
The most interesting part of this method is the for loop in which the chromosome of the child is created, according to the following rules:
Approximately 45% of genes are taken from the first parent
Approximately 45% of genes are taken from the second parent
The remaining 10% are randomized
And how can you get a random single gene or generate a random whole chromosome? You just take a look at the code:
char GetRandomGene() => Genes[random.Next(Genes.Length)];
string GetRandomChromosome()
{
string chromosome = string.Empty;
for (int i = 0; i < Target.Length; i++)
{
chromosome += GetRandomGene();
}
return chromosome;
}
The next necessary method is named GetFitness, which simply returns the number of characters that matches the target book title. Its code is as follows:
int GetFitness(string chromosome)
{
int fitness = 0;
for (int i = 0; i < Target.Length; i++)
{
if (chromosome[i] == Target[i]) { fitness++; }
}
return fitness;
}
Finally, let’s take a look at the Print method:
void Print() => Console.WriteLine(
$"Generation {generationNo:D2}:
{population[0].Chromosome} / {population[0].Fitness}");
When you run the code, the best-fitted individual from each generation is presented, as shown in the following output:
Generation 00: UvWvvtycVTYAsJYxXZpanLkj#rDrmDIEI / 4
Generation 01: sXDGuQQDPnbjpRvWZs evqRNlg#yiwIPL / 5
Generation 02: j#TvvtmKToXuTjxBegpaCLkmNsornzg R / 7
Generation 03: fZCUBIT QrnuzwuWTskTOf bezodQwhmM / 8
Generation 04: CyDwafZZpinLziuPgs yID AevGrGf bs / 9
Generation 05: C# ZaBawSWwLoturSXOcIq wLeSgQOhme / 12 (...)
Generation 10: Sboats ttrDcterus Mnt jmvGrifhms / 17 (...)
Generation 15: C kData ltrCkteres entbAagorZthmD / 21 (...)
Generation 20: C#VDatahStrdcturessanU Al#orithmd / 26 (...)
Generation 25: CZ Data StrunturOs awd Algorithms / 29 (...)
Generation 30: C# Data Structures Qjd Algorithms / 31 (...)
Generation 35: C# Data Structures and Algorothms / 32 (...)
Generation 37: C# Data Structures and Algorithms / 33
Is this magic? No, it’s just the algorithm you wrote that manages the following generations and evolves the individuals, giving you the expected result.
Packt library subscribers can continue reading for free here. C# Data Structures and Algorithms - Second Edition by Marcin Jamro was published in February 2024. You can buy the book here!
🛠️ Useful Tools ⚒️
OpenHouse: an open-sourced tool from LinkedIn for managing data lakehouse tables, which enhances governance and developer experience.
Heat.js: a lightweight JavaScript library designed for creating customizable heat maps, charts, and statistics to visualize date-based activity and trends.
Husky: a modern tool that simplifies the use of git hooks, automatically linting commit messages, code, and running tests with each commit or push.
That’s all for today.
We have an entire range of newsletters with focused content for tech pros. Subscribe to the ones you find the most useful here. Complete ProgrammingPro archives can be found here. Complete PythonPro archives are here.
📢 If your company is interested in reaching an audience of developers, software engineers, and tech decision makers, you may want to advertise with us.
If you have any feedback, take the survey, or leave a comment below.





