SlideShare a Scribd company logo
Good Code
20160411, Enzo
Outlines
● Principles
● Readability
● DRY
● Naming
● Function
● Comments
● Keep Good Quality
● Demo
● Take Out
Principles
● What is good code?
○ Easy to read (and understand)
○ Easy to modify
○ Easy to extend
○ (If easy to ready, then) Easy to debug
○ Proven accuracy
Readability
“Indeed, the ratio of time spent reading vs. writing is well over 10:1.
….. so making it easy to read actually makes it easier to write.”
- Clean Code
Readability
● Abstraction can enhance readability
● Abstraction = Get big picture and hide details
if (u32Year == 2016 &&
u8Month == 4 &&
u8Day == 11) {
goodCode();
}
if (dateIs(2016, 4, 11)) {
goodCode();
}
Readability
● Don’t Make Me Think: Avoid nested decision statement
Readability
if (A == u8Val) {
if (B == u16Val) {
if (C == u32Val) {
if (D == iVal) {
helloWorld();
}
}
}
}
if (checkABCD(A, B, C, D)) {
helloWorld();
}
● Don’t Make Me Think: Use straight-forward decision statement
Readability
if ( sheSayYes() != NO )
{
soWhatDoYouMean();
}
if ( sheSayYes() == YES }
{
bingo();
}
● Don’t Make Me Think: Meaningful #define instead of numeric
value
Readability
switch (u8Temperature) {
case 1: fanSpeed(Low);
break;
case 2: fanSpeed(Mid);
break;
case 3: fanSpeed(High);
break;
case 4: fanSpeed(SuperHigh);
break;
default: fanSpeed(Low);
break;
}
switch (u8Temperature) {
case TEMP_LOW: fanSpeed(Low);
break;
case TEMP_MID: fanSpeed(Mid);
break;
case TEMP_HIGH: fanSpeed(High);
break;
case TEMP_OVERHEAT: fanSpeed(SuperHigh);
break;
default: fanSpeed(Low);
break;
}
DRY (Dont Repeat Yourself)
● Repeating in code is VERY VERY VERY EVIL!
○ Hard to debug
○ Hard to extend
○ Hard to maintain
○ Larger code size
○ Not well structured
● When feeling you are repeating something, be VERY AWARE!
This is the right moment to refactor.
DRY (Dont Repeat Yourself)
● Avoid double-confirm comment
● Avoid same code pattern (by using function)
// sdk init
sdkInit();
// sdk init
sdkInit();
if (u32Year == 2016 &&
u8Month == 4 &&
u8Day == 11) {
goodCode();
}
if (u32Year == 2016 &&
u8Month == 4 &&
u8Day == 32) {
areYouOK();
}
if (dateIs(2016, 4, 11)) {
goodCode();
}
if (dateIs(2016, 4, 32)) {
areYouOK();
}
DRY (Dont Repeat Yourself)
● Create common utility
● Something else?
linux # ./addImgaeHeader_ET6848 rtimg
linux # ./addImgaeHeader_FJSRS rtimg
linux # ./addImgaeHeader rtimg ET6848
linux # ./addImgaeHeader rtimg FJSRS
linux # ./addImgaeHeader uboot FJSRS
Naming
● Naming is everything
○ variable
○ function
○ file
○ utility
○ directory
● Naming in consistent way, precisely!
○ easy to understand (ex: u8Val, u16Val, …)
○ easy to group (ex: bspCpuInit(), bspCpuI2cInit(), …)
○ easy to refactor
● Good naming implies logic correct code
Naming
● Good naming can help logic debug
if ( u8Animal == ANIMAL_DOG } {
bark();
}
ICOS/bsp/cpu/webserver.c ICOS/bsp/cpu/i2c.c
if ( u8Mode == VLAN_ENABLE ) {
disableVlan();
}
if ( u8Mode == VLAN_ENABLE } {
enableVlan();
}
if ( u8Animal == FRUIT_APPLE ) {
areYouOK();
}
Function
● Function is a power tool for abstraction
● Function should do one thing at a time
● Function should have no side effect
● Function should be as small as possible
int portDisableAll()
{
int port = 1;
do {
portDisableByPort(port);
port++;
} while (port <= PORT_COUNTS);
vlanDisableAll();
}
int portDisableAll()
{
int port = 1;
do {
portDisableByPort(port);
port++;
} while (port <= PORT_COUNTS);
vlanDisableAll();
}
Comments
● Comment can be out-dated
● Documentation in code, not in comment
● Less comment is better!
Keep Good Quality
● Fix “Broken Window”
○ Broken window theory
● Prove your code by testing
○ “All code is guilty until proven innocent”
Demo
● Too many nested statement: tskpsaving.c, poeschedule.js
● Repeated code block: poeschedule.js
● Use #define instead of numeric value: poeplus.c
● Funtion power: firstbuild_ag7448.sh vs firstbuild_fjsrs.sh
Take Out
● Abstraction
● Don’t Make Me Think
● DRY
● Naming precisely, consistently
● Less comment is better
● Fix “Broken Window”
● Prove your code by testing
Reference
1. Clean Code: A Handbook of Agile Software Craftsmanship, 1st
Edition, Robert C. Martin
2. The Pragmatic Programmer: From Journeyman to Master, 1st
Edition, Dave Thomas, Andy Hunt
3. Don't Make Me Think: A Common Sense Approach to Web
Usability, 2nd Edition, Steve Krug
Q&A || Share
Appendix
● Applying the Linus Torvalds “Good Taste” Coding Requirement
soso better

More Related Content

PDF
Understanding Gradle Syntax
PDF
Groovy MOPping
PDF
HelsinkiJS - Clojurescript for Javascript Developers
PDF
Coding dojo
PDF
OpenPonk (formerly DynaCASE). The open modeling platform
PPTX
Why do we need TypeScript?
PDF
Typescript - MentorMate Academy
PDF
Professional development
Understanding Gradle Syntax
Groovy MOPping
HelsinkiJS - Clojurescript for Javascript Developers
Coding dojo
OpenPonk (formerly DynaCASE). The open modeling platform
Why do we need TypeScript?
Typescript - MentorMate Academy
Professional development

What's hot (7)

PPTX
Kickstarting Your Mongo Education with MongoDB University
PDF
TEI ODD support in oXygen
PDF
#2 CZ KUG meetup
PDF
TDD with BabyMock 2
PDF
The Future is Here: ECMAScript 6 in the Wild
PPT
Compartilhando código entre Windows Phone e o Windows 8
PDF
Contract Testing of Web Sockets: Functional Programming is taking the Stage
Kickstarting Your Mongo Education with MongoDB University
TEI ODD support in oXygen
#2 CZ KUG meetup
TDD with BabyMock 2
The Future is Here: ECMAScript 6 in the Wild
Compartilhando código entre Windows Phone e o Windows 8
Contract Testing of Web Sockets: Functional Programming is taking the Stage
Ad

Similar to Good code (20)

PPTX
TDD in Go with Ginkgo and Gomega
PPTX
Writing clean scientific software Murphy cleancoding
PDF
TDD in Python With Pytest
PDF
Contest Tips and Tricks
PPTX
Javascript Programming according to Industry Standards.pptx
PDF
Dart the better Javascript 2015
PDF
Power Leveling your TypeScript
PDF
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
PDF
Test Driven Development en Go con Ginkgo y Gomega
PDF
Top Tips Every Notes Developer Needs To Know
PDF
JSHint: Learning JavaScript the Hard Way
PPTX
Introduction to React Native
PDF
Tools and libraries for common problems (Early Draft)
PPTX
Pen Testing Development
PDF
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
PDF
7 lessons learned building high availability / performance systems - CM2015
PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
PPTX
Dart the Better JavaScript
PDF
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
TDD in Go with Ginkgo and Gomega
Writing clean scientific software Murphy cleancoding
TDD in Python With Pytest
Contest Tips and Tricks
Javascript Programming according to Industry Standards.pptx
Dart the better Javascript 2015
Power Leveling your TypeScript
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
Test Driven Development en Go con Ginkgo y Gomega
Top Tips Every Notes Developer Needs To Know
JSHint: Learning JavaScript the Hard Way
Introduction to React Native
Tools and libraries for common problems (Early Draft)
Pen Testing Development
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
7 lessons learned building high availability / performance systems - CM2015
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
Dart the Better JavaScript
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Ad

Recently uploaded (20)

PDF
medical staffing services at VALiNTRY
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
System and Network Administraation Chapter 3
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPT
Introduction Database Management System for Course Database
PDF
AI in Product Development-omnex systems
PDF
top salesforce developer skills in 2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
medical staffing services at VALiNTRY
ManageIQ - Sprint 268 Review - Slide Deck
System and Network Administraation Chapter 3
PTS Company Brochure 2025 (1).pdf.......
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Design an Analysis of Algorithms II-SECS-1021-03
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Operating system designcfffgfgggggggvggggggggg
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Understanding Forklifts - TECH EHS Solution
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
How to Choose the Right IT Partner for Your Business in Malaysia
Introduction Database Management System for Course Database
AI in Product Development-omnex systems
top salesforce developer skills in 2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
How Creative Agencies Leverage Project Management Software.pdf
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx

Good code

  • 2. Outlines ● Principles ● Readability ● DRY ● Naming ● Function ● Comments ● Keep Good Quality ● Demo ● Take Out
  • 3. Principles ● What is good code? ○ Easy to read (and understand) ○ Easy to modify ○ Easy to extend ○ (If easy to ready, then) Easy to debug ○ Proven accuracy
  • 4. Readability “Indeed, the ratio of time spent reading vs. writing is well over 10:1. ….. so making it easy to read actually makes it easier to write.” - Clean Code
  • 5. Readability ● Abstraction can enhance readability ● Abstraction = Get big picture and hide details if (u32Year == 2016 && u8Month == 4 && u8Day == 11) { goodCode(); } if (dateIs(2016, 4, 11)) { goodCode(); }
  • 7. ● Don’t Make Me Think: Avoid nested decision statement Readability if (A == u8Val) { if (B == u16Val) { if (C == u32Val) { if (D == iVal) { helloWorld(); } } } } if (checkABCD(A, B, C, D)) { helloWorld(); }
  • 8. ● Don’t Make Me Think: Use straight-forward decision statement Readability if ( sheSayYes() != NO ) { soWhatDoYouMean(); } if ( sheSayYes() == YES } { bingo(); }
  • 9. ● Don’t Make Me Think: Meaningful #define instead of numeric value Readability switch (u8Temperature) { case 1: fanSpeed(Low); break; case 2: fanSpeed(Mid); break; case 3: fanSpeed(High); break; case 4: fanSpeed(SuperHigh); break; default: fanSpeed(Low); break; } switch (u8Temperature) { case TEMP_LOW: fanSpeed(Low); break; case TEMP_MID: fanSpeed(Mid); break; case TEMP_HIGH: fanSpeed(High); break; case TEMP_OVERHEAT: fanSpeed(SuperHigh); break; default: fanSpeed(Low); break; }
  • 10. DRY (Dont Repeat Yourself) ● Repeating in code is VERY VERY VERY EVIL! ○ Hard to debug ○ Hard to extend ○ Hard to maintain ○ Larger code size ○ Not well structured ● When feeling you are repeating something, be VERY AWARE! This is the right moment to refactor.
  • 11. DRY (Dont Repeat Yourself) ● Avoid double-confirm comment ● Avoid same code pattern (by using function) // sdk init sdkInit(); // sdk init sdkInit(); if (u32Year == 2016 && u8Month == 4 && u8Day == 11) { goodCode(); } if (u32Year == 2016 && u8Month == 4 && u8Day == 32) { areYouOK(); } if (dateIs(2016, 4, 11)) { goodCode(); } if (dateIs(2016, 4, 32)) { areYouOK(); }
  • 12. DRY (Dont Repeat Yourself) ● Create common utility ● Something else? linux # ./addImgaeHeader_ET6848 rtimg linux # ./addImgaeHeader_FJSRS rtimg linux # ./addImgaeHeader rtimg ET6848 linux # ./addImgaeHeader rtimg FJSRS linux # ./addImgaeHeader uboot FJSRS
  • 13. Naming ● Naming is everything ○ variable ○ function ○ file ○ utility ○ directory ● Naming in consistent way, precisely! ○ easy to understand (ex: u8Val, u16Val, …) ○ easy to group (ex: bspCpuInit(), bspCpuI2cInit(), …) ○ easy to refactor ● Good naming implies logic correct code
  • 14. Naming ● Good naming can help logic debug if ( u8Animal == ANIMAL_DOG } { bark(); } ICOS/bsp/cpu/webserver.c ICOS/bsp/cpu/i2c.c if ( u8Mode == VLAN_ENABLE ) { disableVlan(); } if ( u8Mode == VLAN_ENABLE } { enableVlan(); } if ( u8Animal == FRUIT_APPLE ) { areYouOK(); }
  • 15. Function ● Function is a power tool for abstraction ● Function should do one thing at a time ● Function should have no side effect ● Function should be as small as possible int portDisableAll() { int port = 1; do { portDisableByPort(port); port++; } while (port <= PORT_COUNTS); vlanDisableAll(); } int portDisableAll() { int port = 1; do { portDisableByPort(port); port++; } while (port <= PORT_COUNTS); vlanDisableAll(); }
  • 16. Comments ● Comment can be out-dated ● Documentation in code, not in comment ● Less comment is better!
  • 17. Keep Good Quality ● Fix “Broken Window” ○ Broken window theory ● Prove your code by testing ○ “All code is guilty until proven innocent”
  • 18. Demo ● Too many nested statement: tskpsaving.c, poeschedule.js ● Repeated code block: poeschedule.js ● Use #define instead of numeric value: poeplus.c ● Funtion power: firstbuild_ag7448.sh vs firstbuild_fjsrs.sh
  • 19. Take Out ● Abstraction ● Don’t Make Me Think ● DRY ● Naming precisely, consistently ● Less comment is better ● Fix “Broken Window” ● Prove your code by testing
  • 20. Reference 1. Clean Code: A Handbook of Agile Software Craftsmanship, 1st Edition, Robert C. Martin 2. The Pragmatic Programmer: From Journeyman to Master, 1st Edition, Dave Thomas, Andy Hunt 3. Don't Make Me Think: A Common Sense Approach to Web Usability, 2nd Edition, Steve Krug
  • 23. ● Applying the Linus Torvalds “Good Taste” Coding Requirement soso better