Clean Code Meaningful names Masaki Hayashi

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

1 C++ Syntax and Semantics The Development Process.
Coding Standard: General Rules 1.Always be consistent with existing code. 2.Adopt naming conventions consistent with selected framework. 3.Use the same.
Software Engineering Reading Group: Clean Code Chapter 2 Led by Nicholas Vaidyanathan Lead Visionary, Visionary Software.
Road Map Introduction to object oriented programming. Classes
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Programming Introduction to C++.
110-D1 Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need.
INTRODUCTION TO VC++ As the Microsoft Windows 3.X and then 5.5 operating system was becoming popular, many programmers were interested in creating graphical.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Working with Strings Lecture 2 Hartmut Kaiser
GENERAL GUIDELINES FOR CODING. RULE #1 The most important aspect of the code is the readability If your code is incorrect it can be fixed (if it is readable)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
CPS120: Introduction to Computer Science
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Introduction to programming in the Java programming language.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Even More Java. Java Packages What is a package? Definition: A package is a grouping of related types providing access protection and name space management.
CPS120: Introduction to Computer Science Variables and Constants.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope.
Learning Programming Windows API Morpheus Feb-28, 2008.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Primitive Types Four integer types: Two floating-point types:
Thinking about programming
2016 API for Game Development
ECE 382 Lesson 21 Readings Lesson Outline
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Working with Java.
Reviewing Code A guide to smelling another developer’s source code.
Introduction to the C Language
Windows Programming Lecture 09.
Variables and Primative Types
Thinking about programming
Nov. 11, Visualization with 3D CG
Lecture Set 4 Data Types and Variables
By: Syed Shahrukh Haider
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Introduction to the C Language
Java Programming Language
Working with Strings Lecture 2 Hartmut Kaiser
Unit-1 Introduction to Java
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Chapter 1: Computer Systems
Govt. Polytechnic,Dhangar
Coding Concepts (Basics)
C# Language Basics Imran Rashid CTO at ManiWeber Technologies.
Coding Concepts (Data- Types)
Programming in JavaScript
Programming in JavaScript
Chapter 2: Introduction to C++.
Programming Introduction to C++.
Fundamental Programming
Coding practices For IT and Computing students 2014.
CISC101 Reminders All assignments are now posted.
Introduction to Programming
Chap 2. Identifiers, Keywords, and Types
“Name carefully- as if a first-born child.”
Variables and Constants
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

Clean Code Meaningful names Masaki Hayashi 2016 API for Game Development Clean Code Meaningful names Masaki Hayashi

5 Lectures from me Meaningful names Functions Workshop Comments Formatting + workshop (assignment) Mostly from a book "Clean Code"

Masaki HAYASHI(林正樹), PhD. Self introduction Masaki HAYASHI(林正樹), PhD. Associate Professor Department of Game Design, Uppsala University 1986 - 2006 NHK Science and Technology Research Laboratories (20 years) 2006 - 2011 Game and IT companies 2012 - Uppsala University Astrodesign, Inc. Engineering Researcher on Content

Clean code is pretty important in a long term. The book is based on Java, here we use Unity with C# The book is strongly related to OO programming Code should be easy to understand for other people For group work, and even for yourself Important in industry (if wanting to be a professional) An episode (about a genius programmer) The lectures introduce just cases. Try and think about it yourself (most important) Clean code is pretty important in a long term.

Meaningful names

Names... Names, names, names... Variables Functions Arguments Classes Projects Source files Directories Executions etc etc.....

The bad example... // リヴォルヴのアルゴリズム ========================= // リヴォルヴのアルゴリズム ========================= void Camera::initRevolve(float sx, float sy, float sz, float tx, float ty, float tz) { rsx = sx; rsz = sz; rt0 = - atan2((tx-sx), (sz-tz)); dist = sqrt((sx-tx)*(sx-tx) + (sy-ty)*(sy-ty) + (sz-tz)*(sz-tz)); rthe = 0.0f; rdest = destry * 3.14159265f / 180.0f; rry = camdata.ry; if(destry > 0.0f) rdifthe = destspeed * 0.03f; // 目分量で決めたスピード=0.03 else rdifthe = - destspeed * 0.03f; status = 1; }

Use intention-revealing names Good name is very important, seriously. Change them when you find better ones. eg. float d; // distance in meters "d" doesn't reveal anything. It needs what is measured and the unit. float distanceInMeters; float distanceInCentiMeters; float metersFromOrigin; float miliMetersFromGround;

Avoid disinformation Avoid false clues that would obscure the meaning of code. Don't use letters which means something in the platform. eg. int characterList; // serial number of group of characters "List" means "std::list" to programmers. Don't use it unless it's actually a " std::list" int serialNumberOfCharacterGroup; int characterGroup; .....

Avoid disinformation (cont.) Don't use "l" and "O" as variable names. ( lower case of "L" upper case of "o" ) int l = 1; if ( p == 0 ) a = O1; else l = 01; int l = 1; if ( p == 0 ) a = O1; else l = 01; ( Font: Verdana ) ( Font: SimSun ) It's annoying, really...

Make meaningful distinctions Don't make a name in an instant way in order to distinguish one to another. eg. int Camera::closeup() { int status, status1; ................... } Number-series naming should be avoided. int statusOfCameraPosition, statusOfCameraRotation;

Make meaningful distinctions (cont.) Noise words added to naming. eg. a1, a2, a3,... Camera, CameraData, CameraInfo,... klass, thiss, ... Classic example: void copyValue(int a1, int a2) { a1 = a2; } void copyValue(int destination, int source) { destination = source; }

Use pronounceable names If you can't pronounce it, you can't discuss it. Abbreviation is sometimes annoying, too. eg. int qt1ksu; int adjdstcam; int kokusaitenjijou; int numberOfQuestion; int adjustedDistanceOfCamera; int businessExpo;

Use searchable names Single-letter names and numeric constants have a trouble when to be searched. eg. "e" in a wide scope (eg. global variable) is impossible to locate by search function. If it's a local variable in a small method, it would be OK. eg. for(int e=0 ; e < maxNumberOfCamera ; e++) {...} for (int e=0 ; e < 5 ; e++) {...} const int WORK_DAYS_PER_WEEK = 5; for (int e=0 ; e < WORK_DAYS_PER_WEEK ; e++) {...}

Avoid encoding No more Hungarian notation. eg. int m_MyDevice; // member of a structure/class LPCTSTR lpszSourceName; // long pointer to a null-terminated string Reasons of no need Richer type system Compiler evolved Better IDEs (Integrated Development Environment) Trend of shorter classes and functions Just a clutter and a mark of old type code....

Avoid encoding Hungarian notation e.g. Win32 API HWND CreateWindow( LPCTSTR lpClassName , LPCTSTR lpWindowName, DWORD dwStyle , int x , int y , int nWidth , int nHeight , HWND hWndParent , HMENU hMenu , HANDLE hInstance , LPVOID lpParam ); Microsoft's Design Guidelines discourage developers from using Hungarian notation in .NET. https://msdn.microsoft.com/en-us/library/ms229045(v=vs.110).aspx

Excerpt from the Microsoft's Framework Design Guidelines Avoid encoding Excerpt from the Microsoft's Framework Design Guidelines Word Choice ✓ DO choose easily readable identifier names. For example, a property named HorizontalAlignment is more English-readable than AlignmentHorizontal. ✓ DO favor readability over brevity. The property name CanScrollHorizontally is better than ScrollableX (an obscure reference to the X-axis). X DO NOT use underscores, hyphens, or any other nonalphanumeric characters. X DO NOT use Hungarian notation. X AVOID using identifiers that conflict with keywords of widely used programming languages.

Avoid mental mapping Smart people is not a professional, sometimes. eg. Smart people sometimes want to show off their smartness via the code. however Professionals know "Clarity is king". Also, don't be humorous.... Kissaway ( = delete ) addsonofabitchheadershit() ( = AddHeader() )

Class names / method names Class names should be noun or noun phrase Method names should be verb or verb phrase eg. Class / objects: Camera, Account,... Method: getActiveCamera, setId, movePosition,...

Pick one word per concept Don't use multiple names for just one abstract concept eg. fetch, retrieve, get controller, manager, driver If those are the same functioning, unify it.

Final words... Try doing this for your code and see what happens. Don't fear other developers. It will pay off in a long term.