Urgent.News

What's breaking now, across thousands of outlets.

Tech

Keep if clauses side-effect free

Avoid writing if clauses that have side effects. The purpose of an if statement is to determine whether a condition is true, not to execute code as a side effect of the test. When using the return value directly in an if statement, its meaning may become unclear. For example, the method enqueueMessage() could return true if the message was enqueued or true if the queue is full.

To make the code more readable, make the return value explicit using a variable. Methods that don't have side effects should have names that clearly indicate this, such as isEmpty(). Calling methods with side effects in if statements can make the code difficult to understand. If a reader skim-reading the code, they may not notice the call to a method with side effects.

Consider the following code from production: It was unclear where items were being added to the set. The code read as if the if statement had no side effects. However, the add() method was called, and it was unclear what this did. The Javadoc for the Set class states that the add() method returns true if the set did not already contain the specified element.

The code also uses extra convoluted logic due to the continue keyword. A more straightforward approach would be to use a variable to make the return value explicit. This would make the code more readable and avoid confusion. Be cautious when using methods with side effects in if statements. It's important to understand that short-circuit evaluation, while useful, should not be used to avoid calling methods with side effects.

If statements were created to handle these situations and should be used accordingly. Writing the code in a more explicit and straightforward manner may seem like an extra step, but it can save time and effort in the long run by making the code easier to follow for future readers.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at teamten.com →

More in Tech

BooleOS: building a bare-metal x86 OS without writing a single line of it myself

BooleOS is a hobby operating system for x86 (32-bit), written in C99 and NASM, no Linux or BSD underneath, no libc. It boots via Multiboot2, has its own heap manager (no raw malloc), a FAT16/VFS…

  • BooleOS is AI-generated bare-metal x86 OS without manual coding
  • Claude Code writes implementation, reviewed before each step
  • Project emphasizes strict 32-bit architecture and fixed-width types

My Life Isn't an OS — So I Built One

Another LifeOS? There are plenty of things called LifeOS now. At the simple end, there's ChatGPT plus scheduled tasks. Then there are Obsidian templates and manually maintained knowledge bases—I wrote…

  • Created LifeOS to address limitations of existing services
  • Uses SQLite for lightweight logging, n8n for automation
  • Agent layer powered by OpenClaw with OpenAI models

Your Exam Form Needs a 20 KB Photo. Now What?

Your exam form is almost done. You have your documents. You have your photo. Then you see: Upload photo: 20–50 KB, 200 × 230 px And suddenly, a simple application becomes a trip to the cyber cafe.

  • FormSaathi offers 9 tools for exam form requirements
  • Photo Resizer ensures photo size between 20-50 KB
  • Platform prioritizes privacy with offline functionality

Invariants are cheap. Silent corruption is not.

We had five numbers that were supposed to add up. Nothing in the codebase checked that they did. The relationship nobody wrote down A tracked journey produces five distance figures: original , cleaned…

  • Invariants are crucial for catching bugs in code
  • Silent corruption can occur without invariants
  • Validation function adds cost-effective error checks

More from Saturday 26 September →