Tech bites

SQLAlchemy vs Hibernate: A Deep Dive into Python and Java ORMs

SQLAlchemy vs Hibernate: A Deep Dive Into Python and Java ORMs

Introduction

When developing applications that interact with databases, Object-Relational Mappers (ORMs) help bridge the gap between relational databases and object-oriented programming. Two of the most popular ORMs are SQLAlchemy (for Python) and Hibernate (for Java). While both serve the same purpose, they have different approaches, strengths, and best use cases.

Comparing Persistent Data Sources on AWS: Choosing the Right Storage Solution

Comparing Persistent Data Sources on AWS: Choosing the Right Storage Solution

Introduction

Amazon Web Services (AWS) offers a variety of persistent storage solutions designed to meet different needs, from simple object storage to high-performance databases. Choosing the right data source depends on factors such as scalability, durability, cost, and access patterns. This guide compares the most commonly used persistent data storage options on AWS to help you make an informed decision.

Write Unit Tests First and Use AI to Generate Code That Passes Them

Write Unit Tests First and Use AI to Generate Code That Passes Them

Introduction

Many developers turn to AI to generate unit tests for existing code, but what if we flipped the process? Instead of using AI to write tests, we can write unit tests first and use AI to generate the implementation that satisfies them. This approach aligns with test-driven development (TDD), ensuring that our code meets predefined requirements and is robust from the start.

Walrus Operator

Walrus Operator

The walrus operator (:=) in Python, introduced in Python 3.8, is also known as the assignment expression. It returns the value as well as assigning it. This can reduce many lines of code and greatly simplifies the language.

Managing Hatch Dependencies in VS Code

Managing Hatch Dependencies in vs Code

I recently started using hatch for python projects, and even though hatch will manage and install dependencies for you (just modify the pyproject.toml file), I noticed the import statements could not find the dependencies. This has to do with how Python and Hatch create virtual environments.

Git Hooks

Git Hooks

Git hooks are scripts that Git automatically executes before or after specific events, such as committing changes or pushing to a repository. They allow you to customize and automate tasks related to these events, such as enforcing code style rules, running tests, or sending notifications. Hooks are useful for maintaining code quality and ensuring consistent workflows across a team. They can be set up in the .git/hooks directory of your repository and include both client-side and server-side hooks.

Recursion

Recursion

Definition of Recursion:

Recursion is a programming technique where a function calls itself in order to solve smaller instances of the same problem until it reaches a base case that does not require further recursion.

Safely retrieve properties using Supplier and Optional in Java

Safely Retrieve Properties Using Supplier and Optional in Java

The supplier allows you to pass something but not retrieve it right away. One use case is retrieving a property an object that could throw a null pointer exception. By not retrieving right away, but instead using a supplier, the supplier can be later executed inside a try/catch block and handle any exceptions thrown.

Maintaining List Order in Jpa

Maintaining List Order in Jpa

If you want to maintain the order of a list of objects retrieve from the database using hibernate/jpa, use the @OrderColumn annotation. An additional column will be created in the database (if ddl is set to update) that will keep track of the order/position of an item in a list.

SQL Injection

SQL Injection

SQL injection occurs when SQL code can be injected into API input. In this injection attack, valid input has SQL commands concatened with SQL execution commands. When the SQL code is executed, the commands are run. In this process data can be mutated and returned to the attacker.

YAGNI

YAGNI

YAGNI is an acronym for “You Aren’t Gonna Need It”.

Kubernetes: Restarting Pods

Kubernetes: Restarting Pods

When making changes to config maps or other things - it’s tempting to restart all the pods for a particular service (select them all, delete, and let them come back up). This may work in the majority of cases, but if something goes wrong, it’ll be hard to tell if the recent change caused it or if it’s unrelated.

Convert String into LocalDate

Convert String Into LocalDate

Code


   public static LocalDate parseDate(String date) {
        DateTimeFormatter formatter =
                 DateTimeFormatter.ofPattern("M/d/y");
        return LocalDate.parse(date,formatter);
    }

Unit Tests

    @Test
    public void testDateParsing() {
        String testDate;
        testDate ="01/01/2022";
        LocalDate expected = LocalDate.of(2022, 1, 1);
        assertEquals(expected, DateParser.parseDate(testDate));
    }

    @Test
    public void testDateParsingSingleDigitMonthAndYear() {
        String testDate;
        testDate = "1/1/2022";
        LocalDate expected = LocalDate.of(2022, 1, 1);
        assertEquals(expected, DateParser.parseDate(testDate));
    }

A "Simple" Change to a Web App

A "Simple" Change to a Web App

Imagine a basic web app (SPA, REST API, Sql database) with a new-user registration form. The form collects a first and last name of a new user. It is decided that the middle name needs to be collected on the form as well.

Check Spring Profiles in Rancher

Check Spring Profiles in Rancher

Spring profiles can be used to toggle behaviors in a spring app. If your app is running in a container in Rancher (Kubernetes) you may need to see what spring profiles are being applied. To check what spring profiles a service is running in Rancher:

Spring Batch: Running Batch Jobs Asynchronously

Spring Batch: Running Batch Jobs Asynchronously

When running multiple batch jobs in Spring Batch, the default JobLauncher waits to launch a job until the previous job running is COMPLETE. In order to run multiple batch jobs simultaneously, the JobLauncher must be configured to run jobs asynchronously.

Spring Batch: Limit How Frequent a Batch Job Runs

Spring Batch: Limit How Frequent a Batch Job Runs

Often in designing batch jobs you want to limit the frequency that a job can run. For example, if you want a batch job to run once a day, you can use @Scheduled annotation to run the batch job once a day. This is enough if you only run one instance of the batch program and there are no other ways to launch the job.

Java Tip: Method Reference

Java Tip: Method Reference

Instead of

for(String name: names) {
	System.out.println(name);
}

Do

// This is called a method reference
names.foreach(System.out::println);

Rules of Thumb for Software Developers

Rules of Thumb for Software Developers

Fetching Data

  • Remember Performance issues go unnoticed during development because the data set is too small - Vlad Mihalcea
  • A transaction should fetch only as much data as required by the current executing business logic - Vlad Mihalcea

Optimization

  • Premature optimization is the root of all evil - Donald E. Knuth
  • Measure performance before and after each attempted optimization. J Bloch
  • Programs spend 90 percent of their time in 10 percent of the code.

Design

  • The components of design that are most difficult to change after the fact are those specifying interactions between components and with the outside world. Cheif among these design components are APIs, wire-level protocols, and persistent data formats. Bloch
  • Strive to write good programs rather than fast ones. J Block

Install Heroku CLI and Setup  Heroku Autocomplete

Install Heroku CLI and Setup Heroku Autocomplete

On a Mac command line

brew tap heroku/brew && brew install heroku

Enable heroku autocomplete

heroku autocomplete
printf "$(heroku autocomplete:script bash)" >> ~/.bashrc; source ~/.bashrc

To use autocomplete type: Heroku and Tab,Tab

Java Tip: ComputeIfAbsent

Java Tip: ComputeIfAbsent

Often we have to loop through a list of items and convert it into a map, with the key being some sort of category and the value being a set of the associated items ie Map<Key, Set<Object>>. The old way of doing this looped through the list and before adding checked if the key was present. Using computeIfAbsent we can clean up this syntax considerably.