Docsity

Best Practices for Variable Usage: Naming, Initialization, and Use, Slides of C Sharp Programming

Toggle sidebar
bg1
Best Practices for
Variables
docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download Best Practices for Variable Usage: Naming, Initialization, and Use and more Slides C Sharp Programming in PDF only on Docsity!

Best Practices for

Variables

Introduction

 Don’t use implicitly declared variables (VB allows this – C# does not)  Do use naming conventions  Remove unused variables  Use all variables that you declare

Variable Names (1)

 Variables should “accurately” and “fully” describe the entity  Don’t use obscure abbreviations  1 character variable names are OK for scratch values  i,j,k are common  Put modifier at the end of names (not the beginning)  Total, Sub, Average, Max

Variable Names (2)

 Use opposites precisely  Avoid the use of the word flag  Use boolean names  done / error / found / success  The name should imply true or false

Variable Initialization

 Declare and initialize variables together  A variable’s declaration and first use should be together (or close)  Use constants where possible  Don’t use literal values

Variable Use

 Use variables for a single purpose  Don’t reuse counters and accumulators  If you do, tell the reader  Minimize scope visibility  Use local variables over global variables where possible  Minimize persistence  Avoid names with hidden meanings