<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/keep-if-clauses-side-effect-free-uujsadlll" -->

---
title: Keep if clauses side-effect free | daily.dev
description: Argues against writing if clauses that call methods with side effects, since the returned value's meaning is often ambiguous and the side-effecting call can be...
canonical: https://daily.dev/posts/keep-if-clauses-side-effect-free-uujsadlll
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Keep if clauses side-effect free | daily.dev
og:description: Argues against writing if clauses that call methods with side effects, since the returned value's meaning is often ambiguous and the side-effecting call can be...
og:url: https://daily.dev/posts/keep-if-clauses-side-effect-free-uujsadlll
og:image: https://api.daily.dev/og/posts/uujsadLLl.png
og:image:alt: Keep if clauses side-effect free
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Keep if clauses side-effect free

**[Lobsters](https://daily.dev/sources/lobsters)** · 3 min read · 0 upvotes · 0 comments

## Summary

Argues against writing if clauses that call methods with side effects, since the returned value's meaning is often ambiguous and the side-effecting call can be missed by a reader skimming code. Uses examples like enqueueMessage(), flushQueue(), and a real production example with Set.add() combined with continue and negation, showing how convoluted the logic becomes. Recommends always assigning the return value to a clearly named boolean or variable before testing it in the if statement, and avoiding short-circuit evaluation as a way to hide side-effecting calls.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.teamten.com/lawrence/programming/keep-if-clauses-side-effect-free.html>

## Community take

How the wider developer community reacted, aggregated from 2 discussions and 12 comments across lobsters, hackernews (as of 2026-09-26).

**TL;DR:** Reactions are split: some agree side-effecting conditionals should be avoided or at least clearly signposted, while others push back that side effects in if-statements are fine as long as naming and conventions make them obvious, with several offering language-specific alternatives (Rust's insert, .NET's TryAdd, Go's short form).

**Sentiment:** 30% positive · 55% mixed · 15% skeptical

**The case for**

- Some found the post's argument compelling, agreeing that hidden side effects in conditionals reduce clarity.
- One commenter argued a related rule (single way to fail per line) helps with tracing and diagnostics.
- Language features like Go's short-form if, .NET's TryAdd, and Rust's Option-returning insert were praised as good ways to keep side effects legible.

**The pushback**

- Several argued side effects in if-statements are fine as long as function names clearly signal them, making strict avoidance unnecessary.
- One questioned the premise that an if statement's only purpose is testing a condition, calling it an unsupported assertion.
- Another suggested the real problem in the Set.add() example is the ambiguous boolean return value rather than the side effect itself.

**By community**

- lobsters (mixed): Discussion splits between defending disciplined side-effect-free conditionals and arguing side effects are acceptable if clearly named, with several tangents into language-specific alternatives.
- hackernews (mixed): A single commenter shares a real Ruby pattern and asks whether it should be refactored, without further discussion to confirm a lean.

**Hottest debate:** Whether side effects inside if-statement conditions are inherently bad practice or acceptable so long as the function naming makes the side effect obvious.

**Open questions**

- Is there a good general convention (like Rust's or .NET's) that most languages could adopt to make side-effecting conditionals unambiguous?
- Is refactoring a same-line conditional assignment (like the Ruby find_by example) into a separate statement actually clearer or just longer?

**Highlights**

> This is one of the things that I've thought about the most while developing my programming style and I think I've landed on the side of "side effects are fine, actually, you just have to indicate that they're happening". So something like `if(handle_event(x) == EV_OK)` having side effects is fine, but `if(user_callback(x) == EV_OK)` having side effects is not. A comment is usually OK, but fixing the names of the functions is better. If statements being able to have side effects is just too useful in terms of structure/formatting/etc, so programmers have to be mindful that they might happen there. But there's still a bias in the brain towards interpreting if statements as purely logical, so programmers should also take steps to plant reminders that they actually aren't in the shape/flavor of their code.
> — [wareya on lobsters · 4 points, 1 comments](https://lobste.rs/s/buroqq/keep_if_clauses_side_effect_free#c_k4loaf)

> >     boolean isNewCategory = categorySeen.add(categoryID); I’d say the problem is more about `add` returning a bool. If it were instead “added” or “already in set”, things would be clear. Rust’s [`HashSet::insert`](https://doc.rust-lang.org/stable/std/collections/struct.HashSet.html#method.insert) returns `bool` in much the same way. I wish it instead defined an enum, but the ergonomics of such things aren’t great due to needing extra imports for things like `== Insert::Inserted`; you’d end up defining methods on the enum to convert it to bool, e.g. `.is_inserted()`. Something like `== _::Inserted` could be nicer for referring to the variant in such cases. Its [`HashMap::insert`](https://doc.rust-lang.org/stable/std/collections/struct.HashMap.html#method.insert), on the other hand, returns `Option<V>`, the value replaced, if any. That’s not ambiguous.
> — [chrismorgan on lobsters · 2 points, 1 comments](https://lobste.rs/s/buroqq/keep_if_clauses_side_effect_free#c_tusp3y)

> I think at least the first "`success`" example is unnecessarily verbose. I assume it's usually obvious that if you're testing a function in an if statement, it'll be related to whether it was successful.  > The only function of an if statement is to test whether a condition is true. It’s not for executing code as a side-effect of the test. Says who?
> — [prayerie on lobsters · 9 points](https://lobste.rs/s/buroqq/keep_if_clauses_side_effect_free#c_s6kguh)

> I do this often: ```ruby if (user = User.find_by(email: 'abc@example.com'))   user.update(status: 'active') end ``` Is it better to do this instead? ```ruby user = User.find_by(email: 'abc@example.com') user.update(status: 'active') if user.present? ```
> — [a\_w on hackernews](https://news.ycombinator.com/item?id=44083564)

**Source threads**

- [lobsters](https://lobste.rs/s/buroqq/keep_if_clauses_side_effect_free) · 8 points · 10 comments
- [hackernews](https://news.ycombinator.com/item?id=44083529) · 2 points · 2 comments

## Similar posts on daily.dev

- [assert\(\): A modern How To](https://daily.dev/posts/assert-a-modern-how-to-mky6fdnmw) · Hacker News · 0 upvotes · 0 comments
- [Quadrupling code performance with a "useless" if](https://daily.dev/posts/quadrupling-code-performance-with-a-useless-if-r4n2jo4ig) · Lobsters · 1 upvotes · 0 comments
- [To test, or not to Test? Part 2 – Make it hard to make mistakes](https://daily.dev/posts/to-test-or-not-to-test-part-2-make-it-hard-to-make-mistakes-fnfxbqcof) · We Are .NET · 0 upvotes · 0 comments

---

Tags: [#java](https://daily.dev/tags/java), [#code-review](https://daily.dev/tags/code-review)

[View this post on daily.dev](https://daily.dev/posts/keep-if-clauses-side-effect-free-uujsadlll)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Keep if clauses side-effect free","url":"https://daily.dev/posts/keep-if-clauses-side-effect-free-uujsadlll","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/keep-if-clauses-side-effect-free-uujsadlll"},"datePublished":"2026-09-26T14:05:28.758Z","dateModified":"2026-09-26T14:06:02.078Z","description":"Argues against writing if clauses that call methods with side effects, since the returned value's meaning is often ambiguous and the side-effecting call can be...","image":"https://media.daily.dev/image/upload/s--CxzD6vbw--/f_auto/v1722860399/public/Placeholder%2005","thumbnailUrl":"https://media.daily.dev/image/upload/s--CxzD6vbw--/f_auto/v1722860399/public/Placeholder%2005","isAccessibleForFree":true,"articleSection":"Lobsters","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Lobsters","logo":"https://media.daily.dev/image/upload/s--tl8v_Fku--/f_auto,t_logo/v1698841318/logos/lobste.jpg","url":"https://daily.dev/sources/lobsters"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/keep-if-clauses-side-effect-free-uujsadlll","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java,code-review","timeRequired":"PT3M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Lobsters","item":"https://daily.dev/sources/lobsters"},{"@type":"ListItem","position":3,"name":"Keep if clauses side-effect free"}]}
```

