The Visitor Pattern: A Practical Guide to Adding New Operations

As software engineers, we often face a dilemma: we have a stable set of classes, but we constantly need to add new functionality that operates on them. Do we keep modifying these existing, stable classes? That can be risky and violates the Open/Closed Principle. Or is there a cleaner way?

Enter the Visitor Design Pattern. It’s a behavioral pattern that lets you add new operations to a set of objects without changing the classes of those objects. It’s like hiring a specialist to work on your existing equipment, rather than trying to rebuild the equipment every time you need a new task done.

In this guide, we’ll break down the Visitor pattern, understand the problem it solves, and walk through a practical Java example.

The Core Problem: Scattered Logic

Imagine you’re building an e-commerce platform. You have a shopping cart that can hold different types of items. Let’s keep it simple and say you have Book and Electronics items.

Continue reading The Visitor Pattern: A Practical Guide to Adding New Operations

Solved: java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider

Encountering a java.lang.NoClassDefFoundError can be one of the most frustrating issues when working with Java applications. This particular error — org/hibernate/cache/CacheProvider — is a common stumbling block for developers upgrading or mixing Hibernate versions. Let’s break down what it means and how to fix it.

Understanding the Error

The NoClassDefFoundError occurs when the JVM tries to load a class by its fully qualified name but cannot find its definition at runtime — even though the class existed at compile time. In this case, org.hibernate.cache.CacheProvider was the standard interface for Hibernate second-level cache integration in Hibernate 3.x. It was deprecated in Hibernate 4 and completely removed in Hibernate 5+, replaced by org.hibernate.cache.spi.RegionFactory.

Continue reading Solved: java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider

Setting the Classpath from the Command Line in Java

When you compile or run a Java program that depends on classes from other JAR files or locations, the Java runtime needs to know where to find those classes. The classpath is the list of paths that Java checks for classes and resources. In this guide we’ll explore how to set the classpath in a few different ways—via the java and javac commands, through the CLASSPATH environment variable and by using the -cp / -classpath switch.

What Is the Classpath?

The classpath is a logical list of directories, JAR archives, and other resources that the Java Virtual Machine (JVM) searches for class files. When the JVM loads a class, it looks through the entries in this list in order until it finds the class. If the class isn’t found, a ClassNotFoundException (compile time) or NoClassDefFoundError (runtime) is thrown.

Example of a classpath for a simple project might look like this:

/home/user/project/bin:/home/user/lib/commons-io-2.8.0.jar        
Continue reading Setting the Classpath from the Command Line in Java

A Developer’s Guide to Truthy and Falsy in TypeScript

As a TypeScript developer, you’ve almost certainly written code like if (myVariable) { ... } to check if a variable “exists” or has a “valid” value. But what’s really happening under the hood? This check isn’t just for null or undefined; it’s a core concept in JavaScript and TypeScript called “truthiness”.

Understanding the difference between truthy and falsy values is crucial for writing robust, bug-free code. It helps you avoid common pitfalls and write more concise, expressive logic. Let’s dive in!

What are Truthy and Falsy Values?

In TypeScript, every value has an inherent boolean quality. When used in a boolean context (like an if condition), a value will be coerced, or converted, into either true or false.

  • falsy value is a value that is considered false when encountered in a boolean context.
  • truthy value is any value that is considered true in a boolean context. Basically, if it’s not on the falsy list, it’s truthy!
Continue reading A Developer’s Guide to Truthy and Falsy in TypeScript

Handling Exceptions in JAX-RS Jersey with ExceptionMapper

When building RESTful web services, proper exception handling is crucial. Unhandled exceptions can lead to ugly stack traces being sent to the client, exposing internal server details and providing a poor user experience. The JAX-RS specification provides an elegant solution for this: the ExceptionMapper.

In this tutorial, we’ll explore how to use Jersey’s implementation of ExceptionMapper to create centralized, custom, and consistent error responses for our API. We’ll build a simple Spring Boot application with Jersey to demonstrate the concepts.

What is an ExceptionMapper?

An ExceptionMapper is a JAX-RS component that “maps” a Java exception to a javax.ws.rs.core.Response object. When an exception is thrown from a JAX-RS resource method, the framework checks if there’s a registered ExceptionMapper for that specific exception type (or any of its superclasses).

If a mapper is found, its toResponse() method is called. This gives you complete control over the HTTP response sent back to the client, including:

  • The HTTP Status Code (e.g., 404 Not Found, 400 Bad Request, 500 Internal Server Error)
  • The Response Body (e.g., a structured JSON error object)
  • Custom HTTP Headers
Continue reading Handling Exceptions in JAX-RS Jersey with ExceptionMapper

A Practical Guide to Hashing Passwords in Python with bcrypt

If you’re building any application that involves user accounts, there’s one security rule you absolutely cannot break: never, ever store passwords in plaintext. A single database breach could expose every user’s password, leading to a catastrophic loss of trust and security. The correct way to handle passwords is to hash them. In this guide, we’ll walk through the best way to do this in Python using the bcrypt library.

So, what’s wrong with common hashing algorithms like MD5 or SHA-1? They were designed to be fast. That’s great for verifying file integrity, but terrible for passwords. A fast algorithm means an attacker can try billions of password combinations per second against your leaked hashes. We need something that is intentionally slow. Enter bcrypt.

Bcrypt is a password-hashing function designed by Niels Provos and David Mazières. It has several key features that make it the gold standard for password security:

  • It’s Slow: By design, bcrypt is computationally expensive. This drastically slows down brute-force attacks.
  • It Includes Salt: Bcrypt automatically generates and incorporates a “salt” (a random string) into each hash. This means that even if two users have the same password, their stored hashes will be completely different.
  • It’s Adaptive: As computers get faster, you can increase the “cost factor” of bcrypt to make it even slower, keeping pace with hardware improvements.
Continue reading A Practical Guide to Hashing Passwords in Python with bcrypt

A Deep Dive into TypeScript Template Strings

If you’ve spent any time with modern JavaScript or TypeScript, you’ve likely encountered string concatenation. The traditional way, using the + operator, works, but it can quickly become clumsy, error-prone, and hard to read, especially when dealing with multiple variables and line breaks.

Fortunately, ES6 introduced a much more elegant and powerful solution: Template Strings (also known as template literals). TypeScript, being a superset of JavaScript, fully supports this feature, and it will fundamentally change the way you work with strings.

Let’s explore what makes them so special, from the basics to more advanced techniques.


What are Template Strings?

At their core, template strings are string literals that allow for embedded expressions. Instead of using single quotes (') or double quotes ("), you enclose them in backticks (`).

// Old way with single quotes
const singleQuoteString = 'This is a regular string.';

// Old way with double quotes
const doubleQuoteString = "This is also a regular string.";

// The modern way with backticks
const templateString = `This is a template string.`;

Continue reading A Deep Dive into TypeScript Template Strings