The Podcast at Delphi.org

Beyond the being the longest running podcast about Object Pascal and Delphi programming languages, tools, news, and community, this is also Jim McKeeth's blog on other things related to programming and technology.

30 2018

Unexpected Benefit of Inline Variables: Conditional Blocks

by Jim McKeeth

Inline variables is one of the cool new feature coming in 10.3. The obvious huge use case is loop control variables, but I just discovered another great use case while reviewing some code. 

[delphi]procedure DoesSomething; var var1, var2: Integer; begin // use var1 {$IFDEF Something} // use var1 & var2 {$ENDIF Something} end;[/delphi]

This is a pattern I see a lot, and it generates a hint on var2 being unused based on the current compiler directive status.

[dcc32 Hint] myUnit.pas(123): H2164 Variable 'var2' is declared but never used in 'DoesSomething'

Now there are a number of ways to deal with this with more compiler directives, which is what I've done in the past, but I never like adding more compiler directives. It makes the code way more complicated and harder to maintain. Now with Inline Variables I can simplify it, make it easier to maintain, and hande the hint! (all of which makes me so happy!)

[delphi]procedure DoesSomething; var var1: Integer; begin // use var1 {$IFDEF Something} var var2: Integer; // use var1 and var2 {$ENDIF Something} end;[/delphi]

Happy dance commencing in T-minus 10 seconds. 

What are some interesting ways you see inline variables benefiting you?

tags: 10.3 Rio - clean code - delphi - language features - News - Source Code