SlideShare a Scribd company logo
Programming Style GuidelinesNhat ‘Rich’ NguyenFuture  Computing  LabFebruary 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 codeUnderstandingSharingMaintainingBeing professional3
OverviewNaming ConventionOrganizationStatementsDocumentationExtra Guidelines4
1. Naming Convention5“What's in a name? That which we call a roseby any other name would smell as sweet.” Juliet / WilliamShakespeare
VariablesMixed case starting with lower case Small scope – short namesPrefix n represent number of objectsSuffix Array for plural conventionPrefix i represent single entity number 6
VariablesPrefixed i, j, k for iteratorNegated Boolean name should be avoidAcronyms should be mixed or lower caseAvoid keywords7
ConstantsAll UPPERCASE with underscore Prefixed by a common type name8
StructuresBegin with capital letterNeed not include structure name in field name9
FunctionsLower case (maybe with underscore)Meaningful namesSingle output - named for the outputNo output – named after what they doAvoid unintentional shadowing: unique name.10
Function Prefixesget/set  - reserved for accessing a property compute – where something is computedfind– where something is looked upinitialize – where object is establishedis – used for Boolean functionhas, can – alternative to is11
Function Symmetry12Reduce complexity by symmetry
2. Organization13"A place for everything, everything in its place." Benjamin Franklin
.m FilesBreak it down to functionsEnhance readability, understanding, testingMake interaction clearHave input, output argumentsUse structures to avoid long list of argumentsYour functions should do something very well14
.m FilesUse existing functionsQuicker to find an existing functionPackage any block of code as a function If it appears in more  than one .m-file.Write test scripts for every functionImprove quality of  the initial versionEnhance reliability of changed version15
Input and OutputMake input and output modulesInput is subject to change and often messyOutput is subject to change without noticeLocalize code to preprocess them.Format output for easy useFor 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. Statements17“A month from now, if I look atthis code, will I understand what it’s doing?” Steve Lord – The MathWorks Inc.
GeneralAvoid cryptic codeNumbers in expressions should be named constants insteadIndentation to reveal structure (Ctrl + I)Content should be kept in the first 80 columns18
Variables and constantsDocument important variables near the startDocument constant assignmentUse .m or .mat file for global constant19
20
LoopsLoops variables should be initialized immediately before the loopend lines of nested loops can have comments21
ConditionalsAvoid complex conditional expressions22
ConditionalsPut the usual case in the if-part and the exception in the else-partSwitch statement should include otherwise condition23
4. Documentation24“Good documentation is a sign of the professional pride a programmer puts into a program.” Steve McConnell
White SpaceSurround =, &, and | and operatorsFollow CommasOne or more blank lines among Block of statement Alignment25
CommentsCannot justify poorly written codeDo more than just restate  the codeShould be easy to readHave same indentation as codes referred toHeader comment should support the use of help and lookforhelp prints the 1st block of comment lines from the file
lookforsearches the first comment line of all m-files on the path. 26
27
28
DocumentationDescription: what the code is supposed to do Design: how it worksInterfaces: which functions it depends on and how it is used by other codeExamples: how it is tested.Credits:who wrote, modified, and when.29
30ExampleDescriptionDesignInterfaceExampleCredits
Documentation Tool: m2html31
5. Extra Guidelines32“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

More Related Content

PPTX
Clean code ch03
PPT
Codings Standards
ODP
Clean Code - Part 2
PDF
Python typing module
PPTX
Coding conventions
PPT
Oop interfaces
PPT
Chapter 09
PDF
Applicative style programming
Clean code ch03
Codings Standards
Clean Code - Part 2
Python typing module
Coding conventions
Oop interfaces
Chapter 09
Applicative style programming

Viewers also liked (9)

PDF
Programming style
PPTX
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
KEY
Douglas Crockford - Programming Style and Your Brain
PPT
CS201- Introduction to Programming- Lecture 01
PDF
Introduction to Computer and Programming - Lecture 01
PDF
Chapter 1. java programming language overview
PDF
New Programming Style of Java
PDF
Introduction to Java Programming
PPTX
Slideshare ppt
Programming style
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Douglas Crockford - Programming Style and Your Brain
CS201- Introduction to Programming- Lecture 01
Introduction to Computer and Programming - Lecture 01
Chapter 1. java programming language overview
New Programming Style of Java
Introduction to Java Programming
Slideshare ppt
Ad

Similar to Programming style guildelines (20)

PPT
Coding standard
PPTX
Coding standards
DOCX
Coding Best Practices.docx
PPTX
C# coding standards, good programming principles & refactoring
PPT
7-CodingAndUT.ppt
PPT
Best practices in enterprise applications
PDF
Software Craftmanship - Cours Polytech
PPT
9-Coding.ppt
PPT
Coding Standards
PPT
c-coding-standards-and-best-programming-practices.ppt
PPTX
Back-2-Basics: .NET Coding Standards For The Real World
ODP
Documenting code yapceu2016
PPTX
Back-2-Basics: .NET Coding Standards For The Real World
PPTX
Writing High Quality Code in C#
PDF
Crafting high quality code
PPTX
Combating software entropy 2-roc1-
ODP
Documenting Code - Patterns and Anti-patterns - NLPW 2016
PDF
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
PDF
Lotusphere 2007 AD505 DevBlast 30 LotusScript Tips
PPTX
Coding standard and coding guideline
Coding standard
Coding standards
Coding Best Practices.docx
C# coding standards, good programming principles & refactoring
7-CodingAndUT.ppt
Best practices in enterprise applications
Software Craftmanship - Cours Polytech
9-Coding.ppt
Coding Standards
c-coding-standards-and-best-programming-practices.ppt
Back-2-Basics: .NET Coding Standards For The Real World
Documenting code yapceu2016
Back-2-Basics: .NET Coding Standards For The Real World
Writing High Quality Code in C#
Crafting high quality code
Combating software entropy 2-roc1-
Documenting Code - Patterns and Anti-patterns - NLPW 2016
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
Lotusphere 2007 AD505 DevBlast 30 LotusScript Tips
Coding standard and coding guideline
Ad

More from Rich Nguyen (8)

PPTX
Improving pollen classification with less training effort
PPTX
An Accurate Cell Detection with Minimal Training Effort
PPTX
Wbc master thesisdefense
PPT
Wbc cmc talk
PPTX
Object tracking survey
PPTX
Wbc demo
PPT
Soccer Ball Tracking
PPTX
Tracking Colliding Cells
Improving pollen classification with less training effort
An Accurate Cell Detection with Minimal Training Effort
Wbc master thesisdefense
Wbc cmc talk
Object tracking survey
Wbc demo
Soccer Ball Tracking
Tracking Colliding Cells

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Electronic commerce courselecture one. Pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
KodekX | Application Modernization Development
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
Teaching material agriculture food technology
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Cloud computing and distributed systems.
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Empathic Computing: Creating Shared Understanding
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Approach and Philosophy of On baking technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Network Security Unit 5.pdf for BCA BBA.
Electronic commerce courselecture one. Pdf
Building Integrated photovoltaic BIPV_UPV.pdf
MYSQL Presentation for SQL database connectivity
Diabetes mellitus diagnosis method based random forest with bat algorithm
Chapter 3 Spatial Domain Image Processing.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
KodekX | Application Modernization Development
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Teaching material agriculture food technology
Dropbox Q2 2025 Financial Results & Investor Presentation
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Cloud computing and distributed systems.
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Empathic Computing: Creating Shared Understanding
20250228 LYD VKU AI Blended-Learning.pptx
Approach and Philosophy of On baking technology

Programming style guildelines