liveBook · Manning

Conditional logic

As explained in The Art of Code

A conditional is a control-flow construct that selects whether or which instructions execute according to a Boolean condition.

Conditional logic selects which instructions execute, but its value depends on readable decisions. Dense Boolean expressions and deeply nested branches can increase cognitive load, so extract meaningful decisions into clearly named methods. section 2.3.5

Name the decision

Replace a condition such as checking whether a user is active, unexpired, and authorized with a named predicate such as isEligible(user). The name turns implementation details into a readable story; extract the surrounding permission handling when the branch carries another responsibility. section 2.3.5

Table 2.1 Guidelines for splitting methods
Approach
Guideline
Length based
Split a method if it grows too long.
Responsibility based
Each method should have one responsibility. At a low level (actions), the scope should be narrow (e.g., validating a single field). At higher levels, responsibilities can be broader (e.g., handling the full user registration process).
Simplicity driven
Extract code that feels cognitively heavy, such as complex Boolean conditions or deep nesting, into clearly named methods.

Use length, responsibility, and simplicity as complementary guides: split overgrown methods, keep responsibilities meaningful, and extract cognitively heavy conditions or nesting. section 2.3.5

Kotlin type-based branching

Kotlin if/else can select a promotional message by testing whether a Book is digital, pocket-sized, or hardcover. Smart casts avoid explicit casts, but an additional else is needed because the branch chain is not guaranteed to be exhaustive. section 5.1.3

How The Art of Code covers it

How deep it goes
Explains Boolean conditions through design guidance and a Kotlin implementation, without broader control-flow semantics.
Examples
Uses user eligibility predicates, nested permission handling, and Kotlin book-subtype promotional messages.
What it assumes
Assumes familiarity with methods, Boolean expressions, and Kotlin class hierarchies.
What you can do after
Apply it via named methods to simplify dense conditions and choose Kotlin branches for concrete book types.
How it connects
Connects conditional logic to method extraction, Single Responsibility Principle, cognitive complexity, smart casts, and exhaustive branching.

Also explained in

Compare how 21 books explain conditional logic →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.