what's in a name

25
OR WHAT’S IN A NAME ? enum ONE_FOUR_FLAG { ONE_FOUR_FLAG_1 = 1, ONE_FOUR_FLAG_2 = 2, ONE_FOUR_FLAG_3 = 3, ONE_FOUR_FLAG_4 = 4, };

Upload: koby-fruchtnis

Post on 22-Dec-2014

312 views

Category:

Software


0 download

DESCRIPTION

Why is it important to choose the correct name for a class, variable of function.

TRANSCRIPT

Page 1: What's in a name

OR

WHAT’S IN A NAME ?

enum ONE_FOUR_FLAG {

ONE_FOUR_FLAG_1 = 1,

ONE_FOUR_FLAG_2 = 2,

ONE_FOUR_FLAG_3 = 3,

ONE_FOUR_FLAG_4 = 4,

};

Page 2: What's in a name

WHY INVEST TIME IN NAMING ?

• Private functions and variables

• Benefits the maintainer

• Clearer meaning, leading to less bugs

• Easier debugging

• Namespaces, classes and public functions

• Benefits all users with easier reuse

• Benefits the maintainer with easier expanding

• Removes the need for costly API renaming and prototyping

Page 3: What's in a name

USE INTENTION REVEALING NAMES

• A proper name is better than a comment

int d; // duration of operation,

// in days

int duration_days; // of operation

bool verify(Dimentions); // within limits

bool withinLimits(Dimentions);

bool initialized(Dimentions);

Page 4: What's in a name

AVOID DISINFORMATION

• Avoid using common CS names s = getSize(idList); // Is this a list<> container?

int idList[100]; // Nope… confusing, I‟d say

int idArray[100]; // Same problem here

int ids[100]; // An improvement

int idGroup[100]; // better still

list<int> idList; // A proper use of “list”

• Lower case L, upper case o int a = l;

if ( O = l )

a = O1;

else

l = 01;

Page 5: What's in a name

AVOID DISINFORMATION

• Avoid small changes in long names class XYZControllerForEfficientHandlingOfStrings;

class XYZControllerForEfficientParsingOfStrings;

Page 6: What's in a name

MAKE MEANINGFUL DISTINCTION

• Avoid adding meaningless ‘noise’ words class Product; // Informative enough

class ProductData; // „noise‟ word Data

class ProductInfo; // Same deal

class ProductShipping; // Distinctive

class ProductInventory; // Distinctive

Page 7: What's in a name

MAKE MEANINGFUL DISTINCTION

• Same goes for object names struct MessageA {

Header hdr;

MsgBodyA data; // Really? Doesn‟t hdr

// contain data as well?

};

struct MessageA {

Header hdr;

MsgBodyA body;

};

Page 8: What's in a name

USE PRONOUNCEABLE NAMES

class DtaRcrdA {

private Date crtymdhms;

private Date modymdhms;

private final String pszrt = “type_A”;

};

class Customer {

private Date creation_timestamp;

private Date modification_timestamp;

private final String recordType = “Customer”;

};

Page 9: What's in a name

USE SEARCHABLE NAMES

• A variable named ‘e’ is hard to search for

• Also 30 Class types beginning with CTRL

• However, modern IDEs and their add-ons (Visual

Assist, ReSharper) make searching much easier.

• Still, the code will be more understandable if names

stand out without the assistance of external tools

Page 10: What's in a name

AVOID MENTAL MAPPING

• Being able to remember a lot of variable names, is no excuse for making it hard for those that can’t …

const char url[] = “http://www.google.com/index.html”;

char * r = getUrlFilePath(url); // ah… it‟s “index.html”

f = fopen(r); // what was r again?

char * starterWebPageName = getUrlFilePath(url);

f = fopen(starterWebPageName); // A bit clearer, isn‟t it?

Page 11: What's in a name

USE NOUN AND VERB PHRASES

• Combine nouns and verbs student.setName(“Mike”); // a good option

student.changeNameTo(“Mike”); // an actual phrase

• But not too much…

String name = person.getName();

String name = person.name();

Page 12: What's in a name

DON’T BE CUTE

void holyHandGrenade(); // One down, five to go

void deleteAll();

Page 13: What's in a name

PICK ONE WORD PER CONCEPT

• Be consistent addressList.push_front(address);

customers.add(customer);

groceries.insert(product);

cars.append(suv);

• STL/CLR can sometimes serve as a baseline insert // 1 or more elements, at certain

// location

push_front, push_back // 1 element, front or back

remove // remove all that match

clear // clear container from all elements

• Even if you dislike STL, remember the 1st paragraph

Page 14: What's in a name

AVOID ENCODINGS

class Carrier {

int iFreq_MHz_;

};

class Carrier {

float iFreq_MHz_; // Not true anymore…

};

class Carrier {

int freq_MHz_;

};

class Carrier {

float freq_MHz_; // Units are sufficient info

};

Page 15: What's in a name

SOLUTION DOMAIN NAMES

• Use when low level concepts are a must

• Because solution domain names might confuse coders,

making it harder for them to re-use the code

• Examples:

• gpioMux.setControl(3);

• vme.writeWordBE(0x0122AAAB, 15);

• receiver.writeReg(DTO_REG, freq);

Page 16: What's in a name

COMPUTER SCIENCE NAMES

• Include all programming language constructs

• Language building blocks, algorithms, containers

• Will be more understandable to other programmers

than solution domain names

• But, better to use problem domain names, when

applicable

Page 17: What's in a name

PROBLEM DOMAIN NAMES

• Use when computer-science names are

unclear

policy.list.find(pair.first) != policy.list.end()

policy.covers(vehicle)

bool Policy::covers(Vehicle vehicle) {

return carList_.find(vehicle.id) !=

carList_.end();

}

Page 18: What's in a name

MAKE CONTEXT MEANINGFUL

• Don’t add redundant prefixes // Gas Station Delux application

class GSDStation; // Try typing G and using

class GSDCustomer; // auto-complete after 30

class GSDTanker; // more of these…

namespace GSD { // Gas Station Delux

class Station;

class Customer;

class Tanker;

}

Page 19: What's in a name

MAKE CONTEXT MEANINGFUL

• Use short names when possible class BillingAddress;

class ShippingAddress; // Is it any different?

class MACAddress; // Different indeed

class Address;

class MACAddress;

Address shippingAddress, billingAddress;

MACAddress purchasedNIC;

Page 20: What's in a name

MAKE CONTEXT MEANINGFUL

• Use longer names for clarity for ( int i = 1; i < num; i++ ) {

meetsCriteria[i] = true;

}

for ( int i = 2; i < num / 2; ++i ) {

int j = i + i;

while (j <= num) {

meetsCriteria[j] = false;

j += i;

}

}

for ( int i = 1; i < num; ++i ) {

if (meetsCriteria[i]) {

Console.WriteLine(i + " is prime");

}

}

Page 21: What's in a name

MAKE CONTEXT MEANINGFUL

• Use longer names for clarity for ( int primeCandidate = 1; primeCandidate < num; primeCandidate++ ){

meetsCriteria[primeCandidate] = true;

}

for ( int factor = 2; factor < num / 2; ++factor ) {

int factorableNumber = factor + factor;

while (factorableNumber <= num) {

meetsCriteria[factorableNumber] = false;

factorableNumber += factor;

}

}

for ( int primeCandidate = 1; primeCandidate < num; ++primeCandidate ){

if (meetsCriteria[primeCandidate]) {

Console.WriteLine(primeCandidate + " is prime");

}

}

Page 22: What's in a name

HOW TO INVEST TIME IN NAMING

Choose a name

Does the name capture the idea behind a function or class?

Does the name describe what is a function supposed to do?

Does the name avoid implying what isn’t in a function’s to-do

list?

Does the name capture all the responsibilities a class has?

Does the name exclude tasks not required of the class?

Does the name make unit-test code clearer?

Ask co-worker to review the name

If you got a single “No”, reiterate the above points

Page 23: What's in a name

WHY INVEST TIME IN NAMING ?

• Private functions and variables

• Benefits the maintainer

• Clearer meaning, leading to less bugs

• Easier debugging

• Namespaces, classes and public functions

• Benefits all users with easier reuse

• Benefits the maintainer with easier expanding

• Removes the need for costly API renaming and prototyping

Page 24: What's in a name

REMEMBER !!!

• Even if you don’t remember anything else from this

presentation…

Invest time in naming