SlideShare una empresa de Scribd logo
1 de 37
Programming Style Guidelines Nhat ‘Rich’ Nguyen Future  Computing  Lab February 2010
2 “You got to know the rules before you can break them. Otherwise it’s no fun.” Sonny Crockett (Miami Vice)
Benefits of well-written code Understanding Sharing Maintaining Being professional 3
Overview Naming Convention Organization Statements Documentation Extra Guidelines 4
1. Naming Convention 5 “What's in a name? That which we call a roseby any other name would smell as sweet.”  Juliet / WilliamShakespeare
Variables Mixed case starting with lower case  Small scope – short names Prefix n represent number of objects Suffix Array for plural convention Prefix i represent single entity number  6
Variables Prefixed i, j, k for iterator Negated Boolean name should be avoid Acronyms should be mixed or lower case Avoid keywords 7
Constants All UPPERCASE with underscore  Prefixed by a common type name 8
Structures Begin with capital letter Need not include structure name in field name 9
Functions Lower case (maybe with underscore) Meaningful names Single output - named for the output No output – named after what they do Avoid unintentional shadowing: unique name. 10
Function Prefixes get/set  - reserved for accessing a property  compute – where something is computed find– where something is looked up initialize – where object is established is – used for Boolean function has, can – alternative to is 11
Function Symmetry 12 Reduce complexity by symmetry
2. Organization 13 "A place for everything, everything in its place." Benjamin Franklin
.m Files Break it down to functions Enhance readability, understanding, testing Make interaction clear Have input, output arguments Use structures to avoid long list of arguments Your functions should do something very well 14
.m Files Use existing functions Quicker to find an existing function Package any block of code as a function  If it appears in more  than one .m-file. Write test scripts for every function Improve quality of  the initial version Enhance reliability of changed version 15
Input and Output Make input and output modules Input is subject to change and often messy Output is subject to change without notice Localize code to preprocess them. Format output for easy use For human: make it self descriptive and easy to read. For another function: make it easy to use as input. For both: make it easy to use as input and write a formatter function to produce a human readable version. 16
3. Statements 17 “A month from now, if I look at this code, will I understand what it’s doing?” Steve Lord – The MathWorks Inc.
General Avoid cryptic code Numbers in expressions should be named constants instead Indentation to reveal structure (Ctrl + I) Content should be kept in the first 80 columns 18
Variables and constants Document important variables near the start Document constant assignment Use .m or .mat file for global constant 19
20
Loops Loops variables should be initialized immediately before the loop end lines of nested loops can have comments 21
Conditionals Avoid complex conditional expressions 22
Conditionals Put the usual case in the if-part and the exception in the else-part Switch statement should include otherwise condition 23
4. Documentation 24 “Good documentation is a sign of the professional pride a programmer puts into a program.”  Steve McConnell
White Space Surround =, &, and | and operators Follow Commas One or more blank lines among Block of statement  Alignment 25
Comments Cannot justify poorly written code Do more than just restate  the code Should be easy to read Have same indentation as codes referred to Header comment should support the use of help and lookfor ,[object Object]
lookforsearches the first comment line of all m-files on the path. 26
27
28
Documentation Description: what the code is supposed to do  Design: how it works Interfaces: which functions it depends on and how it is used by other code Examples: how it is tested. Credits:who wrote, modified, and when. 29
30 Example Description Design Interface Example Credits
Documentation Tool: m2html 31
5. Extra Guidelines 32 “Measuring programming progress by lines of code is like measuring aircraft building progress by weight.”Bill Gates   
Design Guidelines	 Elegance always paid off. First make it work, then make it fast. Remember ‘divide and conquer’. Automate everything. Write the test code first. Make function as atomic as possible. Don’t repeat yourself. 33
Implementation Guidelines	 Follow coding conventions. Group standardize code. Use ‘get’, ‘set’, ‘is’ naming convention.  Don’t fall for premature optimization. Robust components make robust program. Avoid using ‘magic number’. Code is read much more than it is written. 34
Documentation Guidelines	 Write documentation first! Define what each function should do.  Define how it interact with other function. Include case testing scripts. 35
Use this presentation as a guide,  Refer to it often, and … Practice, practice, practice. Ask the group if you need a code review.  Good luck! 36

Más contenido relacionado

Destacado

Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 01 - Introduction to Concurrency ProgrammingConcurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 01 - Introduction to Concurrency ProgrammingSachintha Gunasena
 
Douglas Crockford - Programming Style and Your Brain
Douglas Crockford - Programming Style and Your BrainDouglas Crockford - Programming Style and Your Brain
Douglas Crockford - Programming Style and Your BrainWeb Directions
 
CS201- Introduction to Programming- Lecture 01
CS201- Introduction to Programming- Lecture 01CS201- Introduction to Programming- Lecture 01
CS201- Introduction to Programming- Lecture 01Bilal Ahmed
 
Introduction to Computer and Programming - Lecture 01
Introduction to Computer and Programming - Lecture 01Introduction to Computer and Programming - Lecture 01
Introduction to Computer and Programming - Lecture 01hassaanciit
 
Chapter 1. java programming language overview
Chapter 1. java programming language overviewChapter 1. java programming language overview
Chapter 1. java programming language overviewJong Soon Bok
 
New Programming Style of Java
New Programming Style of JavaNew Programming Style of Java
New Programming Style of JavaYuichi Sakuraba
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java ProgrammingRavi Kant Sahu
 

Destacado (10)

Applicative style programming
Applicative style programmingApplicative style programming
Applicative style programming
 
Programming style
Programming styleProgramming style
Programming style
 
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 01 - Introduction to Concurrency ProgrammingConcurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
 
Douglas Crockford - Programming Style and Your Brain
Douglas Crockford - Programming Style and Your BrainDouglas Crockford - Programming Style and Your Brain
Douglas Crockford - Programming Style and Your Brain
 
CS201- Introduction to Programming- Lecture 01
CS201- Introduction to Programming- Lecture 01CS201- Introduction to Programming- Lecture 01
CS201- Introduction to Programming- Lecture 01
 
Introduction to Computer and Programming - Lecture 01
Introduction to Computer and Programming - Lecture 01Introduction to Computer and Programming - Lecture 01
Introduction to Computer and Programming - Lecture 01
 
Chapter 1. java programming language overview
Chapter 1. java programming language overviewChapter 1. java programming language overview
Chapter 1. java programming language overview
 
New Programming Style of Java
New Programming Style of JavaNew Programming Style of Java
New Programming Style of Java
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similar a Programming style guildelines

Coding standards
Coding standardsCoding standards
Coding standardsMimoh Ojha
 
Coding Best Practices.docx
Coding Best Practices.docxCoding Best Practices.docx
Coding Best Practices.docxShitanshuKumar15
 
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
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applicationsChandra Sekhar Saripaka
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytechyannick grenzinger
 
c-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptc-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptVinayakHospet1
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldDavid McCarter
 
Documenting code yapceu2016
Documenting code yapceu2016Documenting code yapceu2016
Documenting code yapceu2016Søren Lund
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldDavid McCarter
 
Writing High Quality Code in C#
Writing High Quality Code in C#Writing High Quality Code in C#
Writing High Quality Code in C#Svetlin Nakov
 
Crafting high quality code
Crafting high quality code Crafting high quality code
Crafting high quality code Allan Mangune
 
Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Hammad Rajjoub
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Søren Lund
 
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...DevDay.org
 
Lotusphere 2007 AD505 DevBlast 30 LotusScript Tips
Lotusphere 2007 AD505 DevBlast 30 LotusScript TipsLotusphere 2007 AD505 DevBlast 30 LotusScript Tips
Lotusphere 2007 AD505 DevBlast 30 LotusScript TipsBill Buchan
 
Coding standard and coding guideline
Coding standard and coding guidelineCoding standard and coding guideline
Coding standard and coding guidelineDhananjaysinh Jhala
 

Similar a Programming style guildelines (20)

Coding standard
Coding standardCoding standard
Coding standard
 
Coding standards
Coding standardsCoding standards
Coding standards
 
Coding Best Practices.docx
Coding Best Practices.docxCoding Best Practices.docx
Coding Best Practices.docx
 
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
 
7-CodingAndUT.ppt
7-CodingAndUT.ppt7-CodingAndUT.ppt
7-CodingAndUT.ppt
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
9-Coding.ppt
9-Coding.ppt9-Coding.ppt
9-Coding.ppt
 
Coding Standards
Coding StandardsCoding Standards
Coding Standards
 
c-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptc-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.ppt
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Documenting code yapceu2016
Documenting code yapceu2016Documenting code yapceu2016
Documenting code yapceu2016
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Writing High Quality Code in C#
Writing High Quality Code in C#Writing High Quality Code in C#
Writing High Quality Code in C#
 
Crafting high quality code
Crafting high quality code Crafting high quality code
Crafting high quality code
 
Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Combating software entropy 2-roc1-
Combating software entropy 2-roc1-
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016
 
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
 
Lotusphere 2007 AD505 DevBlast 30 LotusScript Tips
Lotusphere 2007 AD505 DevBlast 30 LotusScript TipsLotusphere 2007 AD505 DevBlast 30 LotusScript Tips
Lotusphere 2007 AD505 DevBlast 30 LotusScript Tips
 
Coding standard and coding guideline
Coding standard and coding guidelineCoding standard and coding guideline
Coding standard and coding guideline
 

Más de Rich Nguyen

Improving pollen classification with less training effort
Improving pollen classification with less training effortImproving pollen classification with less training effort
Improving pollen classification with less training effortRich Nguyen
 
An Accurate Cell Detection with Minimal Training Effort
An Accurate Cell Detection with Minimal Training EffortAn Accurate Cell Detection with Minimal Training Effort
An Accurate Cell Detection with Minimal Training EffortRich Nguyen
 
Wbc master thesisdefense
Wbc master thesisdefenseWbc master thesisdefense
Wbc master thesisdefenseRich Nguyen
 
Object tracking survey
Object tracking surveyObject tracking survey
Object tracking surveyRich Nguyen
 
Soccer Ball Tracking
Soccer Ball TrackingSoccer Ball Tracking
Soccer Ball TrackingRich Nguyen
 
Tracking Colliding Cells
Tracking Colliding CellsTracking Colliding Cells
Tracking Colliding CellsRich Nguyen
 

Más de Rich Nguyen (8)

Improving pollen classification with less training effort
Improving pollen classification with less training effortImproving pollen classification with less training effort
Improving pollen classification with less training effort
 
An Accurate Cell Detection with Minimal Training Effort
An Accurate Cell Detection with Minimal Training EffortAn Accurate Cell Detection with Minimal Training Effort
An Accurate Cell Detection with Minimal Training Effort
 
Wbc master thesisdefense
Wbc master thesisdefenseWbc master thesisdefense
Wbc master thesisdefense
 
Wbc cmc talk
Wbc cmc talkWbc cmc talk
Wbc cmc talk
 
Object tracking survey
Object tracking surveyObject tracking survey
Object tracking survey
 
Wbc demo
Wbc demoWbc demo
Wbc demo
 
Soccer Ball Tracking
Soccer Ball TrackingSoccer Ball Tracking
Soccer Ball Tracking
 
Tracking Colliding Cells
Tracking Colliding CellsTracking Colliding Cells
Tracking Colliding Cells
 

Último

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
 
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
 
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
 
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
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
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
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
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
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 

Último (20)

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 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
 
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
 
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
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
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
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
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...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 

Programming style guildelines

  • 1. Programming Style Guidelines Nhat ‘Rich’ Nguyen Future Computing Lab February 2010
  • 2. 2 “You got to know the rules before you can break them. Otherwise it’s no fun.” Sonny Crockett (Miami Vice)
  • 3. Benefits of well-written code Understanding Sharing Maintaining Being professional 3
  • 4. Overview Naming Convention Organization Statements Documentation Extra Guidelines 4
  • 5. 1. Naming Convention 5 “What's in a name? That which we call a roseby any other name would smell as sweet.” Juliet / WilliamShakespeare
  • 6. Variables Mixed case starting with lower case Small scope – short names Prefix n represent number of objects Suffix Array for plural convention Prefix i represent single entity number 6
  • 7. Variables Prefixed i, j, k for iterator Negated Boolean name should be avoid Acronyms should be mixed or lower case Avoid keywords 7
  • 8. Constants All UPPERCASE with underscore Prefixed by a common type name 8
  • 9. Structures Begin with capital letter Need not include structure name in field name 9
  • 10. Functions Lower case (maybe with underscore) Meaningful names Single output - named for the output No output – named after what they do Avoid unintentional shadowing: unique name. 10
  • 11. Function Prefixes get/set - reserved for accessing a property compute – where something is computed find– where something is looked up initialize – where object is established is – used for Boolean function has, can – alternative to is 11
  • 12. Function Symmetry 12 Reduce complexity by symmetry
  • 13. 2. Organization 13 "A place for everything, everything in its place." Benjamin Franklin
  • 14. .m Files Break it down to functions Enhance readability, understanding, testing Make interaction clear Have input, output arguments Use structures to avoid long list of arguments Your functions should do something very well 14
  • 15. .m Files Use existing functions Quicker to find an existing function Package any block of code as a function If it appears in more than one .m-file. Write test scripts for every function Improve quality of the initial version Enhance reliability of changed version 15
  • 16. Input and Output Make input and output modules Input is subject to change and often messy Output is subject to change without notice Localize code to preprocess them. Format output for easy use For human: make it self descriptive and easy to read. For another function: make it easy to use as input. For both: make it easy to use as input and write a formatter function to produce a human readable version. 16
  • 17. 3. Statements 17 “A month from now, if I look at this code, will I understand what it’s doing?” Steve Lord – The MathWorks Inc.
  • 18. General Avoid cryptic code Numbers in expressions should be named constants instead Indentation to reveal structure (Ctrl + I) Content should be kept in the first 80 columns 18
  • 19. Variables and constants Document important variables near the start Document constant assignment Use .m or .mat file for global constant 19
  • 20. 20
  • 21. Loops Loops variables should be initialized immediately before the loop end lines of nested loops can have comments 21
  • 22. Conditionals Avoid complex conditional expressions 22
  • 23. Conditionals Put the usual case in the if-part and the exception in the else-part Switch statement should include otherwise condition 23
  • 24. 4. Documentation 24 “Good documentation is a sign of the professional pride a programmer puts into a program.” Steve McConnell
  • 25. White Space Surround =, &, and | and operators Follow Commas One or more blank lines among Block of statement Alignment 25
  • 26.
  • 27. lookforsearches the first comment line of all m-files on the path. 26
  • 28. 27
  • 29. 28
  • 30. Documentation Description: what the code is supposed to do Design: how it works Interfaces: which functions it depends on and how it is used by other code Examples: how it is tested. Credits:who wrote, modified, and when. 29
  • 31. 30 Example Description Design Interface Example Credits
  • 33. 5. Extra Guidelines 32 “Measuring programming progress by lines of code is like measuring aircraft building progress by weight.”Bill Gates   
  • 34. Design Guidelines Elegance always paid off. First make it work, then make it fast. Remember ‘divide and conquer’. Automate everything. Write the test code first. Make function as atomic as possible. Don’t repeat yourself. 33
  • 35. Implementation Guidelines Follow coding conventions. Group standardize code. Use ‘get’, ‘set’, ‘is’ naming convention. Don’t fall for premature optimization. Robust components make robust program. Avoid using ‘magic number’. Code is read much more than it is written. 34
  • 36. Documentation Guidelines Write documentation first! Define what each function should do. Define how it interact with other function. Include case testing scripts. 35
  • 37. Use this presentation as a guide, Refer to it often, and … Practice, practice, practice. Ask the group if you need a code review. Good luck! 36
  • 38. References Richard Johnson – MATLAB Programming Style Guidelines. Bruce Eckel– Thinking in Java. Piotr Dollar – Image&Video Toolbox. 37