Usina BRAdvPL Guide
Sign in
← All topics
PRACTICAL GUIDEUnder review

Variable practices: scope, initialization and clear names

Declares temporary data and avoids implicit dependencies between calls.

Variable practices: scope, initialization and clear namesAdvPL
01 · OVERVIEW

Overview

Declare and initialize variables before use. An uninitialized numeric-intent Local holds Nil and cannot be added as though it were zero. In AdvPL, assigning an unknown name can implicitly create a Private: a typo may change logic without the expected error. Prefer Local for temporary data; reserve Static for state that must survive calls and document framework-required Private/Public dependencies. Names should express intent while expected types remain consistent throughout the flow. (TOTVS, n.d., refs. 1–2)

Return value

No single return value: this is a composite workflow.

03 · PRACTICAL EXAMPLE

Commented example

#Include "Protheus.ch"
User Function EdVars()
    Local nQuantity := 3
    Local nUnitPrice := 10
    Local nTotal := 0
    // Every operand is initialized.
    nTotal := nQuantity * nUnitPrice
    ConOut(cValToChar(nTotal))
Return Nil
Expected result

Expected result: 30; no global state created. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • Review spelling and dynamic dependencies. Do not use Public to carry secrets or Static as a cache without an invalidation policy.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. TOTVS. Criação e Atribuição de Variáveis. TDN. Accessed on 2026-10-26.
  2. TOTVS. O Contexto de Variáveis dentro de um Programa. TDN. Accessed on 2026-10-26.
Status
Under review
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha