December 2024 Edition

Spring AOP, Waking Up Early, Gifts For Programmer, December News

Happy New Year 🥳

Welcome to the Kacper Does Software Newsletter!

I hope you had an excellent December and the beginning of the year. December was busy with Christmas lights everywhere, but I didn’t forget about you. The Software Development world didn’t stop, and neither did I.

What you can find in this post 👇

Table of Contents

Latest News

Copilot is free for VS Code!

Microsoft announced that VS Code introduced a free plan for Github Copilot. All you need is a GitHub account. No trial, no subscription, no credit card is required.

With GitHub Copilot Free you get 2000 code completions/month. That's about 80 per working day. You also get 50 chat requests/month, as well as access to both GPT-4o and Claude 3.5 Sonnet models.

You can find more information about it in the official VS Code blog:

JetBrains State of Developer Ecosystem Report 2024

JetBrains published its annual report detailing significant trends in software development, highlighting the growing dominance of AI tools in developers' workflows.

It contains many interesting statistics like the one about the usage of programming languages in 2024:

You can find the full report here:

Spring AOP (Aspect-Oriented Programming)

Think of a typical business application.

You have the main business logic, the meat of the application, for example - for table reservation applications that would be (no surprise) table reservations.

However, many tasks need to happen alongside this core logic — these are things like logging, security checks, and transaction management.

Spring AOP (Aspect Oriented Programming) is an approach that helps you handle these cross-cutting concerns in a clean, organized way.

Little Theory (Sorry, but it is necessary 😉)

Aspect

This is the main building block of AOP. Think of an aspect as a special class that contains code you want to apply across multiple places in your application.

Join Point

It is a point where the aspect is applied. In Spring AOP it is a method.

Think of it like the moment when time stops and we can do whatever we want with the current state. It can be before a method, after a method or both.

Pointcut

It specifies exactly when to apply aspects. They’re pattern-matching expressions that tell Spring “apply this aspect here, but not there.”

For instance, you might create a point cut that matches all methods in your service layer or only methods marked with specific annotations.

Advice

Advice is the actual code that runs at join points. It’s what your aspects actually do when they’re triggered.

Installation

This is simple. Just add a dependency in Maven or Gradle:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

Real Word Usage Example

Imagine you’re building a banking application. You need to ensure that:

  1. All operations are logged

  2. Methods are wrapped in transactions

  3. Only authorized users can access certain operations

Without AOP, your business method might look cluttered:

public class BankServiceWithoutAOP {
    public void transferMoney(Account from, Account to, BigDecimal amount) {
        // Logging
        logger.info("Starting money transfer from " + 
            from.getId() + " to " + to.getId());
        
        // Security check
        if (!securityService.isAuthorized(getCurrentUser())) {
            throw new SecurityException("Unauthorized");
        }
        
        // Transaction management
        TransactionStatus status = transactionManager.beginTransaction();
        try {
            // Actual business logic
            from.deduct(amount);
            to.add(amount);
            transactionManager.commit(status);
            
            // More logging
            logger.info("Transfer completed successfully");
        } catch (Exception e) {
            // Error logging
            logger.error("Transfer failed", e);
            transactionManager.rollback(status);
            throw e;
        }
    }
}

Now, we will separate the concerns and decouple logic from logging, transaction management and authorization:

Logging Aspect

@Aspect
@Component
public class BankingLoggingAspect {
    
    // This pointcut matches all methods in our banking service
    @Pointcut("execution(* com.myapp.banking.BankService.*(..))")
    public void bankingOperations() {}
    
    @Around("bankingOperations()")
    public Object logOperation(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().getName();
        logger.info("Starting banking operation: " + methodName);
        
        try {
            // Execute the actual method
            Object result = joinPoint.proceed();
            
            logger.info("Successfully completed: " + methodName);
            return result;
        } catch (Exception e) {
            logger.error("Operation failed: " + methodName, e);
            throw e;
        }
    }
}

Security Aspect

@Aspect
@Component
public class SecurityAspect {
    
    @Before("execution(* com.myapp.banking.BankService.*(..))")
    public void checkSecurity(JoinPoint joinPoint) {
        if (!securityService.isAuthorized(getCurrentUser())) {
            throw new SecurityException("Unauthorized access");
        }
    }
}

Transaction

Here we do not need to write our custom aspect. Spring already provides @Transactional annotation that uses Spring AOP and handles transactions.

public class BankService {

    @Transactional
    public void transferMoney(Account from, Account to, BigDecimal amount) {
        // buissness logic
}

Summary

Spring Aspect Oriented Programming is a really powerful tool to decouple your business logic from other components. It can be used in many ways and many mechanisms implemented in Spring already use it under the hood.

Waking up early - you can learn it too!

I joined the 5 AM Club some time ago, and since then, I have been waking up at 5 AM. You may think that you are not an early bird and that it is not for you, but I think it can be learned as any other skill.

I wasn’t a morning person. I used to wake up a few minutes before I went to work and now…

The thing that changed my life was “The 5 AM Club” book by Robin Sharma.

You can find a review of it on my website:

This book goes far beyond just advocating for early rising. It presents transformative concepts that can completely reshape your life.

My mornings and late evenings are incomparably better without electronics. I makes a big difference, believe me. Look at my alarm clock:

Isn’t it cool? 😄

I love to do my work in the early morning when most people are still sleeping. I described it - how it changed my life in one of my articles:

Perfect gifts for a programmer

Christmas is gone and we already have a new year, but I managed to gather the 5 perfect gifts for programmers. You can note them and save them for when they may be needed.

1. Copy-paste 2 key keyboard

A good programmer writes code, but a great programmer knows exactly what to copy and paste! 

This minimalist keyboard would be perfect for those days when you’re feeling especially “efficient” and want to embrace the ancient programming wisdom that states “Why write it when someone else already has?”.

I found one on Amazon for around 20$😀 → link

2. Debugging Rubber Duck

This little yellow debugger is absolutely essential because it masters the art of silent judgment — just stare into those beady eyes while explaining your code line by line, and suddenly the solution becomes crystal clear! 

It’s amazing how often the bug reveals itself the moment you start explaining to the duck why your code should work.

I found the Rubber Duck on Amazon for around 10$link

3. Watch that displays time in binary

This is the ultimate programmer flex — nothing says “I live and breathe code” quite like having to do binary-to-decimal conversion just to know if you’re late for your stand-up meeting! 

It is available on a website called “getdigital” for 29 Eurolink

4. More stickers for the laptop

The more stickers you have on your laptop, the more experience you posses

That’s the unwritten rule in software development. With things being told this way, you can treat this purchase as an investment in programming knowledge.

Here I found a bundle of stickers on Amazon for around 6$link

5. Heated mug

And that is why you need to have your coffee hot all the time. This one can be treated as an investment in your code as well.

Just remember — a heated mug means you can no longer use the excuse “Let me grab a fresh coffee” when you need a break from that particularly nasty bug

Unfortunately, it is pretty expensive. I found it on Amazon for around 160$- > link

Thanks for reading!

I hope you enjoyed this month’s Kacper Does Software newsletter edition. I would love to hear back from You.

Feel free to respond to this email, share your thoughts and suggest what you would want me to cover in the next edition.

Happy New Year once again!

Connect with me!

See you on other platforms 😉

x / Twitter

LinkedIn

Medium

Instagram

Have a wonderful day ☀️