SlideShare ist ein Scribd-Unternehmen logo
1 von 38
Variables

1
 General Issues in Using Variables:
 Guidelines for Initializing Variables.
 Scope
 Persistence.
 Binding time.
 Using each variable for exactly one purpose.
 Variables declaration.

2
 The Power of Variable Names:
 Considerations in choosing good names.
 The power of naming conventions.
 Informal naming conventions.
 Standardized prefixes.
 Creating short names that are Readable.
 Kinds of names to avoid.

3
General Issues in Using Variables

4
 Improper initialization problem.

 Reasons:
 The variable has never been assigned a value.
 Part of the variable has been assigned a value and part has
not.
 The value in the variable is outdated .

5
 Solutions:
 Initialize each variable as it's declared.
 Use const when possible.
 Pay attention to counters and accumulators.
 Initialize a class's member data in its constructor.
 Check the need for re-initialization.
 Take advantage of your compiler's warning messages
 Check input parameters for validity .
 Use a memory-access checker to check for bad pointers.
 Initialize working memory at the beginning of your program.

6
 Scope is variable visibility.

 We can measure scope by:
 Live time.
 Span.

7
 live time is total number of statements over which a

variable is live .
1 int recordIndex = 0;
2 bool done = false;
...
...
26 while ( recordIndex < 10)
{...}
64 while ( !done )
{ ... }

1 int recordIndex = 0;
2 while ( recordIndex < 10)
{...}
...
...
63 bool done = false;
64 while ( !done )
{ ... }
8
 Why we try to restrict live time?
 You can concentrate on a smaller section of code.
 Code more readable.
 reduces the chance of initialization errors .
 Easy to modify.

9
 Span is how you keep variable references close.
1.

2.
3.
4.

1.
2.
3.

4.

a = 0;
a++;
a+= 10;
a = 3;

Life time = (4-1 +1) = 4
Average span = (0+0+0)/3 = 0;

a = 0;
b= 1;
c = 0;
a = b + c;

Life time = (4-1 +1) = 4
Average span = 2/1 = 2;

10
11
 Initialize variables used in a loop immediately






before the loop .
Don't assign a value to a variable until just before
the value is used.
Break groups of related statements into separate
routines .
Begin with most restricted visibility, and expand
the scope if necessary .
Keep both span and live time as short as you can.
12
 Persistence is life span of a piece of data .
 Problem:

When you assume variable is persist but it's not .
 Solution:
 Set variables to "unreasonable values" when they doesn’t
persist.
 Use assertions to check critical variables.
 Declare and Initialize all data right before it's used.

13
 The time at which the variable and its value are

bound.
titleBar.color = 0xFF;
titleBar.color = TITLE_BAR_COLOR;
titleBar.color = ReadTitleBarColor( )

Code-Writing Time
Compile Time

Run Time

The later you bind, the more flexibility you get and
the higher complexity you need .
14
 Use each variable for one purpose only.
// Compute roots of a quadratic equation
temp = Sqrt( b*b - 4*a*c );
root[0] = ( -b + temp ) / ( 2 * a );
root[1] = ( -b - temp ) / ( 2 * a );
...
// swap the roots
temp = root[0];
root[0] = root[1];
root[1] = temp ;

Creating unique variables for each purpose makes code more
readable.
15
 Avoid hybrid coupling.

Double use is clear to you but it won't be to someone else.
 Make sure all declared variables are used.

16
Initializing Variables
 Does each routine check input parameters for validity?
 Does the code declare variables close to where they're first

used?
 Does the code initialize variables as they're declared, if
possible?
 Are counters and accumulators initialized properly and, if
necessary, reinitialized each time they are used?
 Does the code compile with no warnings from the compiler?

17
Other General Issues in Using Data
 Do all variables have the smallest lift time possible?
 Are references to variables as close together as possible?
 Are all the declared variables being used?
 Are all variables bound with balance between the
flexibility and the complexity associated?
 Does each variable have only one purpose?
 Is each variable's meaning explicit, with no hidden
meanings?

18
The Power of Variable Names

19
x = x - xx;
xxx = fido + SalesTax( fido );
x = x + LateFee( x1, x ) + xxx;
x = x + Interest( x1, x );

balance = balance - lastPayment;
monthlyTotal = newPurchases + SalesTax( newPurchases );
balance = balance + LateFee( customerID, balance ) +
monthlyTotal;
balance = balance + Interest( customerID, balance );

A good variable name is readable, memorable, and
appropriate .
20
 The Most Important Naming Consideration:
 Good name is to state what the variable represents.

 Optimum Name Length.

Don’t be maximumNumberOfPointsInModernOlympics or np But
be teamPointsMax
21
 Are short names always bad?

 Common Opposites in Variable Names.
 maxPoints OR pointsMax OR both ?
 Avoid the confusion between maxPoints and

pointsMax .
 The most significant part of the variable name is at the
front.

22
 loop variables:
 Don’t use i, j, and k if:
1. variable is to be used outside the loop.
2. the loop is longer than a few lines.
3. you write nested loops.
Try to avoid altogether .

Code is so often changed, expanded, and copied
into other programs.

23
 Naming Status Variables:
 A flag should never have flag in its name .
 Use constants and enumerator for its value.
reportType == ReportType_Annual is more meaningful
than reportflag == 0x80 .
 Naming Temporary Variables:
 Use descriptive variable name instead of temp.

24
 Naming Boolean Variables:
 Give Boolean variables names that imply true or false.
(done , error ,found ,success)
 Don’t use negative boolean variable names .
(if not notFound)

 Naming Enumerated Types:
 Using group prefix if your language doesn’t support.
Enum Color {Color_Red , Color_Green }
 enum type itself can include prefix (enColor).

25
 When Should Have a Naming Convention
 When multiple programmers are working on a project
 When you plan t turn a program over to another
programmer
 When programs are reviewed by other programmers
 When program is so large that you think about it in
pieces
 When the program will be long-lived enough

26
 Why we Have Conventions?
 One global decision rather than many local ones .
 They help you transfer knowledge across projects.
 They reduce name proliferation.
 They compensate for language weaknesses.
 You work with a consistent code.

27
 Differentiate between classes and objects via:
 Initial Capitalization
Widget widget.
 All Caps

WIDGET widget.
 "t_" Prefix for Types

t_Widget Widget.
 More Specific Names for the Variables

Widget employeeWidget .

 Identify member variables.
with an m_ prefix.
28
 Identify named constants .
Via uppercase letters.

 Identify elements of enumerated types.
 Use all caps or an e_ or E_ prefix for the name of the type.
 Use a prefix based on the specific type like Color_Red.

 Format names to enhance readability.
CIRCLEPOINTTOTAL is less readable than circlePointTotal or
circle_point_total.

29
 Guidelines for Language-Specific Conventions:
 i and j are integer indexes.
 p is a pointer.
 Constants and macros are in ALL_CAPS.
 Class and other types are in MixedUpperAndLowerCase().
 Variable and function names as variableOrRoutineName.
 The underscore is not used as a separator within names, except

for names in all caps and certain kinds of prefixes

30
 As avgSize, maxNum, firsIndex.

 Advantages of Standardized Prefixes:
 Naming conventions advantage +
 You have fewer names to remember.
 Make names more compact.

31
 General Abbreviation Guidelines:
 Remove all nonleading vowels (screen becomes scrn).
 Remove and, or, the, and so on.
 Use the first/first few letters of the name.
 Keep the first and last letters of each word.
 Remove useless suffixes—ing, ed, and so on.
 Keep the most noticeable sound in each syllable.

Be sure not to change the meaning of the variable.

32
Comments on Abbreviations:
 Don't abbreviate by removing one character from a word.

 Abbreviate consistently.

Num everywhere or No .
 Create names that you can pronounce

Use xPos rather than xPstn.
 Avoid combinations that result in misreading .

Use bEnd/b_Endrather than bend
 Use a thesaurus to resolve naming collisions.
 Document all abbreviations in a document.
 A reader of the code might not understand the abbreviation.
 Other programmers might use multiple abbreviations to refer
to the same word.

33
 Avoid misleading names or abbreviations.
As FALSE as abb for "Fig and Almond Season.“.
 Avoid names with similar meanings.
As fileNumber and fileIndex .
 Avoid variables with different meanings but similar names.
As clientRecs and clientReps .
 Avoid names that sound similar.
As wrap and rap.
 Avoid numerals in names.
As file1,file2

34
 Avoid words that are commonly misspelled in English.
As occassionally, acummulate, and acsend.

 Don't differentiate variable names solely by capitalization.
As count and Count.
 Avoid multiple natural languages
As "color" or "colour”.

35
?

programmers over the lifetime of a system
spend more time reading code than writing
code.
36
 Steve McConnell, June 2004, Code Complete: A Practical Handbook of

Software Construction, 2nd edn.

37
38

Weitere ähnliche Inhalte

Was ist angesagt?

Applying Generics
Applying GenericsApplying Generics
Applying GenericsBharat17485
 
C# language basics (Visual studio)
C# language basics (Visual studio)C# language basics (Visual studio)
C# language basics (Visual studio)rnkhan
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual BasicTushar Jain
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answerVasuki Ramasamy
 
Generics Tutorial
Generics TutorialGenerics Tutorial
Generics Tutorialwasntgosu
 
Lecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageLecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageSURAJ KUMAR
 
Python Interview questions 2020
Python Interview questions 2020Python Interview questions 2020
Python Interview questions 2020VigneshVijay21
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++ shammi mehra
 
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++  Langauage Training in Ambala ! BATRA COMPUTER CENTREC++  Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTREjatin batra
 
over all view programming to computer
over all view programming to computer over all view programming to computer
over all view programming to computer muniryaseen
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsEng Teong Cheah
 
Chapter 13.1.1
Chapter 13.1.1Chapter 13.1.1
Chapter 13.1.1patcha535
 
Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov
 

Was ist angesagt? (16)

Applying Generics
Applying GenericsApplying Generics
Applying Generics
 
C# language basics (Visual studio)
C# language basics (Visual studio)C# language basics (Visual studio)
C# language basics (Visual studio)
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual Basic
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answer
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
Generics Tutorial
Generics TutorialGenerics Tutorial
Generics Tutorial
 
Lecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageLecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming Language
 
Python Interview questions 2020
Python Interview questions 2020Python Interview questions 2020
Python Interview questions 2020
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++
 
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++  Langauage Training in Ambala ! BATRA COMPUTER CENTREC++  Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
 
over all view programming to computer
over all view programming to computer over all view programming to computer
over all view programming to computer
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
 
python and perl
python and perlpython and perl
python and perl
 
C basics by haseeb khan
C basics by haseeb khanC basics by haseeb khan
C basics by haseeb khan
 
Chapter 13.1.1
Chapter 13.1.1Chapter 13.1.1
Chapter 13.1.1
 
Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2
 

Andere mochten auch

MOST_OpenFoundry_version control system_Git
MOST_OpenFoundry_version control system_GitMOST_OpenFoundry_version control system_Git
MOST_OpenFoundry_version control system_GitSu Jan
 
Defencive programming
Defencive programmingDefencive programming
Defencive programmingAsha Sari
 
Code tuning techniques
Code tuning techniquesCode tuning techniques
Code tuning techniquesAsha Sari
 
程序员发展漫谈
程序员发展漫谈程序员发展漫谈
程序员发展漫谈Horky Chen
 
Design in construction
Design in constructionDesign in construction
Design in constructionAsha Sari
 
程序员实践之路
程序员实践之路程序员实践之路
程序员实践之路Horky Chen
 
Java scriptcore brief introduction
Java scriptcore brief introductionJava scriptcore brief introduction
Java scriptcore brief introductionHorky Chen
 
A Guideline to Test Your Own Code - Developer Testing
A Guideline to Test Your Own Code - Developer TestingA Guideline to Test Your Own Code - Developer Testing
A Guideline to Test Your Own Code - Developer TestingFolio3 Software
 
高品質軟體的基本動作 101 + 102 for NUU
高品質軟體的基本動作 101 + 102 for NUU高品質軟體的基本動作 101 + 102 for NUU
高品質軟體的基本動作 101 + 102 for NUUSu Jan
 
Design in construction
Design in constructionDesign in construction
Design in constructionAsha Sari
 
代码大全(内训)
代码大全(内训)代码大全(内训)
代码大全(内训)Horky Chen
 
Code tuning strategies
Code tuning strategiesCode tuning strategies
Code tuning strategiesAsha Sari
 
高品質軟體的基本動作 101 for NTHU
高品質軟體的基本動作 101 for NTHU高品質軟體的基本動作 101 for NTHU
高品質軟體的基本動作 101 for NTHUSu Jan
 
Code Tuning
Code TuningCode Tuning
Code Tuningbgtraghu
 
The pseudocode
The pseudocodeThe pseudocode
The pseudocodeAsha Sari
 
Rm 1 Intro Types Research Process
Rm   1   Intro Types   Research ProcessRm   1   Intro Types   Research Process
Rm 1 Intro Types Research Processitsvineeth209
 

Andere mochten auch (19)

Coding Style
Coding StyleCoding Style
Coding Style
 
MOST_OpenFoundry_version control system_Git
MOST_OpenFoundry_version control system_GitMOST_OpenFoundry_version control system_Git
MOST_OpenFoundry_version control system_Git
 
Integration
IntegrationIntegration
Integration
 
Defencive programming
Defencive programmingDefencive programming
Defencive programming
 
Code tuning techniques
Code tuning techniquesCode tuning techniques
Code tuning techniques
 
程序员发展漫谈
程序员发展漫谈程序员发展漫谈
程序员发展漫谈
 
Design in construction
Design in constructionDesign in construction
Design in construction
 
程序员实践之路
程序员实践之路程序员实践之路
程序员实践之路
 
Java scriptcore brief introduction
Java scriptcore brief introductionJava scriptcore brief introduction
Java scriptcore brief introduction
 
A Guideline to Test Your Own Code - Developer Testing
A Guideline to Test Your Own Code - Developer TestingA Guideline to Test Your Own Code - Developer Testing
A Guideline to Test Your Own Code - Developer Testing
 
高品質軟體的基本動作 101 + 102 for NUU
高品質軟體的基本動作 101 + 102 for NUU高品質軟體的基本動作 101 + 102 for NUU
高品質軟體的基本動作 101 + 102 for NUU
 
Design in construction
Design in constructionDesign in construction
Design in construction
 
代码大全(内训)
代码大全(内训)代码大全(内训)
代码大全(内训)
 
Code tuning strategies
Code tuning strategiesCode tuning strategies
Code tuning strategies
 
高品質軟體的基本動作 101 for NTHU
高品質軟體的基本動作 101 for NTHU高品質軟體的基本動作 101 for NTHU
高品質軟體的基本動作 101 for NTHU
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
Code Complete
Code CompleteCode Complete
Code Complete
 
The pseudocode
The pseudocodeThe pseudocode
The pseudocode
 
Rm 1 Intro Types Research Process
Rm   1   Intro Types   Research ProcessRm   1   Intro Types   Research Process
Rm 1 Intro Types Research Process
 

Ähnlich wie Variables

Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practicesAnilkumar Patil
 
Programming concepts By ZAK
Programming concepts By ZAKProgramming concepts By ZAK
Programming concepts By ZAKTabsheer Hasan
 
5. using variables, data, expressions and constants
5. using variables, data, expressions and constants5. using variables, data, expressions and constants
5. using variables, data, expressions and constantsCtOlaf
 
Variable and constants in Vb.NET
Variable and constants in Vb.NETVariable and constants in Vb.NET
Variable and constants in Vb.NETJaya Kumari
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringEyob Lube
 
PYTHON NOTES
PYTHON NOTESPYTHON NOTES
PYTHON NOTESNi
 
Coding standard and coding guideline
Coding standard and coding guidelineCoding standard and coding guideline
Coding standard and coding guidelineDhananjaysinh Jhala
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++Muhammad Hammad Waseem
 
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptxChapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptxrajinevitable05
 
Introduction to c
Introduction to cIntroduction to c
Introduction to cAjeet Kumar
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfIntroduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfAbrehamKassa
 

Ähnlich wie Variables (20)

Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practices
 
Chap10
Chap10Chap10
Chap10
 
Coding standards
Coding standardsCoding standards
Coding standards
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
12.6-12.9.pptx
12.6-12.9.pptx12.6-12.9.pptx
12.6-12.9.pptx
 
Programming concepts By ZAK
Programming concepts By ZAKProgramming concepts By ZAK
Programming concepts By ZAK
 
5. using variables, data, expressions and constants
5. using variables, data, expressions and constants5. using variables, data, expressions and constants
5. using variables, data, expressions and constants
 
Variable and constants in Vb.NET
Variable and constants in Vb.NETVariable and constants in Vb.NET
Variable and constants in Vb.NET
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
 
PYTHON NOTES
PYTHON NOTESPYTHON NOTES
PYTHON NOTES
 
Coding standard and coding guideline
Coding standard and coding guidelineCoding standard and coding guideline
Coding standard and coding guideline
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
Lecture No 13.ppt
Lecture No 13.pptLecture No 13.ppt
Lecture No 13.ppt
 
Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4
 
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptxChapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
 
Coding standard
Coding standardCoding standard
Coding standard
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfIntroduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdf
 
Codings Standards
Codings StandardsCodings Standards
Codings Standards
 

Kürzlich hochgeladen

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 

Kürzlich hochgeladen (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

Variables

  • 2.  General Issues in Using Variables:  Guidelines for Initializing Variables.  Scope  Persistence.  Binding time.  Using each variable for exactly one purpose.  Variables declaration. 2
  • 3.  The Power of Variable Names:  Considerations in choosing good names.  The power of naming conventions.  Informal naming conventions.  Standardized prefixes.  Creating short names that are Readable.  Kinds of names to avoid. 3
  • 4. General Issues in Using Variables 4
  • 5.  Improper initialization problem.  Reasons:  The variable has never been assigned a value.  Part of the variable has been assigned a value and part has not.  The value in the variable is outdated . 5
  • 6.  Solutions:  Initialize each variable as it's declared.  Use const when possible.  Pay attention to counters and accumulators.  Initialize a class's member data in its constructor.  Check the need for re-initialization.  Take advantage of your compiler's warning messages  Check input parameters for validity .  Use a memory-access checker to check for bad pointers.  Initialize working memory at the beginning of your program. 6
  • 7.  Scope is variable visibility.  We can measure scope by:  Live time.  Span. 7
  • 8.  live time is total number of statements over which a variable is live . 1 int recordIndex = 0; 2 bool done = false; ... ... 26 while ( recordIndex < 10) {...} 64 while ( !done ) { ... } 1 int recordIndex = 0; 2 while ( recordIndex < 10) {...} ... ... 63 bool done = false; 64 while ( !done ) { ... } 8
  • 9.  Why we try to restrict live time?  You can concentrate on a smaller section of code.  Code more readable.  reduces the chance of initialization errors .  Easy to modify. 9
  • 10.  Span is how you keep variable references close. 1. 2. 3. 4. 1. 2. 3. 4. a = 0; a++; a+= 10; a = 3; Life time = (4-1 +1) = 4 Average span = (0+0+0)/3 = 0; a = 0; b= 1; c = 0; a = b + c; Life time = (4-1 +1) = 4 Average span = 2/1 = 2; 10
  • 11. 11
  • 12.  Initialize variables used in a loop immediately     before the loop . Don't assign a value to a variable until just before the value is used. Break groups of related statements into separate routines . Begin with most restricted visibility, and expand the scope if necessary . Keep both span and live time as short as you can. 12
  • 13.  Persistence is life span of a piece of data .  Problem: When you assume variable is persist but it's not .  Solution:  Set variables to "unreasonable values" when they doesn’t persist.  Use assertions to check critical variables.  Declare and Initialize all data right before it's used. 13
  • 14.  The time at which the variable and its value are bound. titleBar.color = 0xFF; titleBar.color = TITLE_BAR_COLOR; titleBar.color = ReadTitleBarColor( ) Code-Writing Time Compile Time Run Time The later you bind, the more flexibility you get and the higher complexity you need . 14
  • 15.  Use each variable for one purpose only. // Compute roots of a quadratic equation temp = Sqrt( b*b - 4*a*c ); root[0] = ( -b + temp ) / ( 2 * a ); root[1] = ( -b - temp ) / ( 2 * a ); ... // swap the roots temp = root[0]; root[0] = root[1]; root[1] = temp ; Creating unique variables for each purpose makes code more readable. 15
  • 16.  Avoid hybrid coupling. Double use is clear to you but it won't be to someone else.  Make sure all declared variables are used. 16
  • 17. Initializing Variables  Does each routine check input parameters for validity?  Does the code declare variables close to where they're first used?  Does the code initialize variables as they're declared, if possible?  Are counters and accumulators initialized properly and, if necessary, reinitialized each time they are used?  Does the code compile with no warnings from the compiler? 17
  • 18. Other General Issues in Using Data  Do all variables have the smallest lift time possible?  Are references to variables as close together as possible?  Are all the declared variables being used?  Are all variables bound with balance between the flexibility and the complexity associated?  Does each variable have only one purpose?  Is each variable's meaning explicit, with no hidden meanings? 18
  • 19. The Power of Variable Names 19
  • 20. x = x - xx; xxx = fido + SalesTax( fido ); x = x + LateFee( x1, x ) + xxx; x = x + Interest( x1, x ); balance = balance - lastPayment; monthlyTotal = newPurchases + SalesTax( newPurchases ); balance = balance + LateFee( customerID, balance ) + monthlyTotal; balance = balance + Interest( customerID, balance ); A good variable name is readable, memorable, and appropriate . 20
  • 21.  The Most Important Naming Consideration:  Good name is to state what the variable represents.  Optimum Name Length. Don’t be maximumNumberOfPointsInModernOlympics or np But be teamPointsMax 21
  • 22.  Are short names always bad?  Common Opposites in Variable Names.  maxPoints OR pointsMax OR both ?  Avoid the confusion between maxPoints and pointsMax .  The most significant part of the variable name is at the front. 22
  • 23.  loop variables:  Don’t use i, j, and k if: 1. variable is to be used outside the loop. 2. the loop is longer than a few lines. 3. you write nested loops. Try to avoid altogether . Code is so often changed, expanded, and copied into other programs. 23
  • 24.  Naming Status Variables:  A flag should never have flag in its name .  Use constants and enumerator for its value. reportType == ReportType_Annual is more meaningful than reportflag == 0x80 .  Naming Temporary Variables:  Use descriptive variable name instead of temp. 24
  • 25.  Naming Boolean Variables:  Give Boolean variables names that imply true or false. (done , error ,found ,success)  Don’t use negative boolean variable names . (if not notFound)  Naming Enumerated Types:  Using group prefix if your language doesn’t support. Enum Color {Color_Red , Color_Green }  enum type itself can include prefix (enColor). 25
  • 26.  When Should Have a Naming Convention  When multiple programmers are working on a project  When you plan t turn a program over to another programmer  When programs are reviewed by other programmers  When program is so large that you think about it in pieces  When the program will be long-lived enough 26
  • 27.  Why we Have Conventions?  One global decision rather than many local ones .  They help you transfer knowledge across projects.  They reduce name proliferation.  They compensate for language weaknesses.  You work with a consistent code. 27
  • 28.  Differentiate between classes and objects via:  Initial Capitalization Widget widget.  All Caps WIDGET widget.  "t_" Prefix for Types t_Widget Widget.  More Specific Names for the Variables Widget employeeWidget .  Identify member variables. with an m_ prefix. 28
  • 29.  Identify named constants . Via uppercase letters.  Identify elements of enumerated types.  Use all caps or an e_ or E_ prefix for the name of the type.  Use a prefix based on the specific type like Color_Red.  Format names to enhance readability. CIRCLEPOINTTOTAL is less readable than circlePointTotal or circle_point_total. 29
  • 30.  Guidelines for Language-Specific Conventions:  i and j are integer indexes.  p is a pointer.  Constants and macros are in ALL_CAPS.  Class and other types are in MixedUpperAndLowerCase().  Variable and function names as variableOrRoutineName.  The underscore is not used as a separator within names, except for names in all caps and certain kinds of prefixes 30
  • 31.  As avgSize, maxNum, firsIndex.  Advantages of Standardized Prefixes:  Naming conventions advantage +  You have fewer names to remember.  Make names more compact. 31
  • 32.  General Abbreviation Guidelines:  Remove all nonleading vowels (screen becomes scrn).  Remove and, or, the, and so on.  Use the first/first few letters of the name.  Keep the first and last letters of each word.  Remove useless suffixes—ing, ed, and so on.  Keep the most noticeable sound in each syllable. Be sure not to change the meaning of the variable. 32
  • 33. Comments on Abbreviations:  Don't abbreviate by removing one character from a word.  Abbreviate consistently. Num everywhere or No .  Create names that you can pronounce Use xPos rather than xPstn.  Avoid combinations that result in misreading . Use bEnd/b_Endrather than bend  Use a thesaurus to resolve naming collisions.  Document all abbreviations in a document.  A reader of the code might not understand the abbreviation.  Other programmers might use multiple abbreviations to refer to the same word. 33
  • 34.  Avoid misleading names or abbreviations. As FALSE as abb for "Fig and Almond Season.“.  Avoid names with similar meanings. As fileNumber and fileIndex .  Avoid variables with different meanings but similar names. As clientRecs and clientReps .  Avoid names that sound similar. As wrap and rap.  Avoid numerals in names. As file1,file2 34
  • 35.  Avoid words that are commonly misspelled in English. As occassionally, acummulate, and acsend.  Don't differentiate variable names solely by capitalization. As count and Count.  Avoid multiple natural languages As "color" or "colour”. 35
  • 36. ? programmers over the lifetime of a system spend more time reading code than writing code. 36
  • 37.  Steve McConnell, June 2004, Code Complete: A Practical Handbook of Software Construction, 2nd edn. 37
  • 38. 38

Hinweis der Redaktion

  1. %