SlideShare a Scribd company logo
Best Practices
   Naming Conventions

Mariano Wahlmann
Greatly inspired in the contents of...




                           &

 ISBN-13: 978-0596001735       ISBN-13: 978-0131655492
“Always code as if the person who will maintain
your code is a maniac serial killer that knows
where you live”



●   Software Engineers spend most of their
    time reading code NOT writing it!
●   Code is the only complete, accurate
    and updated documentation
●   Easy to read code has fewer bugs
sub attr {
   $_ = (caller(0))[3];
   s/.*://; @_ > 1 ? $_[0]->{$_} = $_[1] : $_[0]->{$_}
}




                         vs
sub attr {
   my $self = shift;
   $self->{attr} = shift if(@_ );
   return $self->{attr};
}
Namespaces
Guideline
        namespace → Noun [ :: Adjective ]*

Begin package's names with an uppercase, if compound
words are used each word must be capitalized, also
when using acronyms each letter must be capitalized


                                                       Good
package XML::Simple;
package Car::Electric;
Package Devel::NYTProf;
package Moose;


                                                        Bad
package Hotel::Inventory::HotelInventory;
package Big::House;
Package Rich::People;
package Html::tag;
Guideline
Avoid names that relate to implementation details,
there might be special cases where you don't want to
do that such as packages that provide bindings to an
specific library


                                                       Good
package Plane::Jet;
package Dog::Beagle;
Package XML::LibXML;
package Gtk2;


                                                        Bad
package XML::SAXServiceParser;
package People::CachedList;
Package Moose::People;
package HTTP::LWPAgent;
Subroutines or Methods
Guideline
routine ➝ imperative_verb [ _ adjective ]? _ noun _ preposition
        | imperative_verb [ _ adjective ]? _ noun _ participle
        | imperative_verb [ _ adjective ]? _ noun
 Pick function and method names that describe what
 they do and not how they do it


                                                            Good
 $list->is_empty;
 sub calculate_net_rate {
 sub get_record {
 sub build_profile_using {


                                                             Bad
 $date->ok;
 sub end_tag {
 $file->backup;
 sub using {
Guideline
Begin subroutine or method names with is_ or has_ if
they return a boolean, there might be special cases
where you don't want to do that


                                                       Good
$element->is_valid;
$node->has_children;
$hotel->room_available_for(@dates);
$list->contains($element);


                                                        Bad
$date->valid;
$record->bad_record;
$list->list_empty;
$process->running;
Guideline
Prefix “internal use only” subroutines or “Private”
methods with an underscore


                                                      Good
$self->_build_list;
$self->_calculate_price;
$self->_store($element);
$self->_initialize(@_);



                                                       Bad
sub internal_method {
sub private_function {
Guideline
Use nouns for subroutines or methods which return a
specific object or value


                                                      Good
$car->engine;
$face->mouth;
$list->next_item;
$person->name;


                                                       Bad
$car->get_engine;
$list->item;
$plane->retrieve_model;
$dog->leg;
Guideline
Use imperative for subroutines or methods that
performs an action


                                                     Good
$car->start_engine;
$face->smile;
$list->remove_all;
$person->clone;


                                                      Bad
$car->engine;
$face->happy;
$list->clear;
$person->new_instance;
Variables
Guideline
            Variable ➝ [ adjective _ ]* noun
Use lowercase for variables and if compound words are
use separate each word with underscore


                                                    Good
my $name = 'Doe';
my $first_name = 'John';
my $total_price = $net_rate + $taxes
my $next_node;


                                                        Bad
my $name_first;
my $name_last;
my $node;
my $sum;
Guideline
Name arrays in plural and hashes in singular



                                                   Good
my @errors;
my %author_of;
my @brands;
my %config;


                                                    Bad
my @record;
my %authors;
my %places;
my @user;
Guideline
lookup_variable ➝ [ adjective _ ]* noun _ preposition
Adding a preposition to the end of lookup variables
make names more readable


                                                      Good
my @sales_for;
print “$sales_for[$month]n”;
my %title_of;
print “$title_of{$book}n”;


                                                        Bad
my @sales;
print “$sales[$index]n”;
my %titles;
print “$titles{$isbn}n”;
Common
Guideline
Avoid ambiguous abbreviations or names, is preferable
to use short full names. If you have to abbreviate
use well-accepted abbreviations


                                                    Good
my $io_stream;
my $tcp_sock;
my $udp_socket;
sub gmt_timestamp {


                                                        Bad
my $left;
my $tcp_st;
my @e;
sub g_tmstmp {
Guideline
Use named constants, using the constant pragma. For
named constants use uppercase



                                                      Good
use constant PI => 3.14;
my $area = PI * $radius**2 ;
use constant BASE_URL => 'http://a.com';
my $home_url = “${BASE_URL}/home”;


                                                       Bad
my $area = 3.14 * $radius**2;
my $base_url = 'http://a.com';
my $home_url = 'http://a.com/home';
if ( $#results < 50 ) {
Source: http://xkcd.org




                          Thank You!

More Related Content

PPT
PPT
Smarty Template
PPT
Smarty Template
PPT
Perl Intro 7 Subroutines
PPTX
Php & my sql
PDF
Drupal - dbtng 25th Anniversary Edition
PPTX
Subroutines
PPT
Php variables
Smarty Template
Smarty Template
Perl Intro 7 Subroutines
Php & my sql
Drupal - dbtng 25th Anniversary Edition
Subroutines
Php variables

What's hot (20)

PDF
Web app development_php_04
PPTX
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
PPTX
PHP Powerpoint -- Teach PHP with this
PDF
Benchmarking Perl (Chicago UniForum 2006)
PDF
Drupal 8: Forms
PPT
06 Php Mysql Connect Query
PPTX
Let's write secure Drupal code! - Drupal Camp Poland 2019
PPTX
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
PPT
07 Php Mysql Update Delete
PDF
Your code sucks, let's fix it (CakeFest2012)
PDF
8時間耐久CakePHP2 勉強会
PDF
Drupal 8: Entities
PDF
PDF
Dependency injection in Drupal 8
PPT
Drupal Lightning FAPI Jumpstart
PPT
Oop php 5
PDF
Zend Framework 2 : Dependency Injection
PDF
Drupal 8: Routing & More
ODP
Extending Zend Framework
Web app development_php_04
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
PHP Powerpoint -- Teach PHP with this
Benchmarking Perl (Chicago UniForum 2006)
Drupal 8: Forms
06 Php Mysql Connect Query
Let's write secure Drupal code! - Drupal Camp Poland 2019
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
07 Php Mysql Update Delete
Your code sucks, let's fix it (CakeFest2012)
8時間耐久CakePHP2 勉強会
Drupal 8: Entities
Dependency injection in Drupal 8
Drupal Lightning FAPI Jumpstart
Oop php 5
Zend Framework 2 : Dependency Injection
Drupal 8: Routing & More
Extending Zend Framework
Ad

Similar to Best practices naming conventions (20)

DOC
Coding standards php
KEY
Better Perl Practices
PDF
Marc’s (bio)perl course
PDF
Perl Best Practices Reference Guide version 1.01.00
PDF
PerlScripting
ODP
Red Flags in Programming
PPTX
On Coding Guidelines
KEY
Introduction to Perl Best Practices
PDF
PPT
You Can Do It! Start Using Perl to Handle Your Voyager Needs
PPTX
Bioinformatica p4-io
PPT
codingtechniques1.ppt
PDF
Scripting3
PDF
Perl intro
PDF
Practical approach to perl day2
PDF
Top 10 Perl Performance Tips
ODP
The Essential Perl Hacker's Toolkit
PDF
Perl best practices v4
PDF
Crafting Custom Interfaces with Sub::Exporter
Coding standards php
Better Perl Practices
Marc’s (bio)perl course
Perl Best Practices Reference Guide version 1.01.00
PerlScripting
Red Flags in Programming
On Coding Guidelines
Introduction to Perl Best Practices
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Bioinformatica p4-io
codingtechniques1.ppt
Scripting3
Perl intro
Practical approach to perl day2
Top 10 Perl Performance Tips
The Essential Perl Hacker's Toolkit
Perl best practices v4
Crafting Custom Interfaces with Sub::Exporter
Ad

Recently uploaded (20)

PPTX
TLE Review Electricity (Electricity).pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Mushroom cultivation and it's methods.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Getting Started with Data Integration: FME Form 101
PPTX
A Presentation on Artificial Intelligence
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
A Presentation on Touch Screen Technology
PDF
Encapsulation theory and applications.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
Tartificialntelligence_presentation.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
TLE Review Electricity (Electricity).pptx
NewMind AI Weekly Chronicles - August'25-Week II
Mushroom cultivation and it's methods.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
A novel scalable deep ensemble learning framework for big data classification...
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Enhancing emotion recognition model for a student engagement use case through...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Getting Started with Data Integration: FME Form 101
A Presentation on Artificial Intelligence
Unlocking AI with Model Context Protocol (MCP)
A Presentation on Touch Screen Technology
Encapsulation theory and applications.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Group 1 Presentation -Planning and Decision Making .pptx
A comparative analysis of optical character recognition models for extracting...
Web App vs Mobile App What Should You Build First.pdf
Tartificialntelligence_presentation.pptx
Programs and apps: productivity, graphics, security and other tools

Best practices naming conventions

  • 1. Best Practices Naming Conventions Mariano Wahlmann
  • 2. Greatly inspired in the contents of... & ISBN-13: 978-0596001735 ISBN-13: 978-0131655492
  • 3. “Always code as if the person who will maintain your code is a maniac serial killer that knows where you live” ● Software Engineers spend most of their time reading code NOT writing it! ● Code is the only complete, accurate and updated documentation ● Easy to read code has fewer bugs
  • 4. sub attr { $_ = (caller(0))[3]; s/.*://; @_ > 1 ? $_[0]->{$_} = $_[1] : $_[0]->{$_} } vs sub attr { my $self = shift; $self->{attr} = shift if(@_ ); return $self->{attr}; }
  • 6. Guideline namespace → Noun [ :: Adjective ]* Begin package's names with an uppercase, if compound words are used each word must be capitalized, also when using acronyms each letter must be capitalized Good package XML::Simple; package Car::Electric; Package Devel::NYTProf; package Moose; Bad package Hotel::Inventory::HotelInventory; package Big::House; Package Rich::People; package Html::tag;
  • 7. Guideline Avoid names that relate to implementation details, there might be special cases where you don't want to do that such as packages that provide bindings to an specific library Good package Plane::Jet; package Dog::Beagle; Package XML::LibXML; package Gtk2; Bad package XML::SAXServiceParser; package People::CachedList; Package Moose::People; package HTTP::LWPAgent;
  • 9. Guideline routine ➝ imperative_verb [ _ adjective ]? _ noun _ preposition | imperative_verb [ _ adjective ]? _ noun _ participle | imperative_verb [ _ adjective ]? _ noun Pick function and method names that describe what they do and not how they do it Good $list->is_empty; sub calculate_net_rate { sub get_record { sub build_profile_using { Bad $date->ok; sub end_tag { $file->backup; sub using {
  • 10. Guideline Begin subroutine or method names with is_ or has_ if they return a boolean, there might be special cases where you don't want to do that Good $element->is_valid; $node->has_children; $hotel->room_available_for(@dates); $list->contains($element); Bad $date->valid; $record->bad_record; $list->list_empty; $process->running;
  • 11. Guideline Prefix “internal use only” subroutines or “Private” methods with an underscore Good $self->_build_list; $self->_calculate_price; $self->_store($element); $self->_initialize(@_); Bad sub internal_method { sub private_function {
  • 12. Guideline Use nouns for subroutines or methods which return a specific object or value Good $car->engine; $face->mouth; $list->next_item; $person->name; Bad $car->get_engine; $list->item; $plane->retrieve_model; $dog->leg;
  • 13. Guideline Use imperative for subroutines or methods that performs an action Good $car->start_engine; $face->smile; $list->remove_all; $person->clone; Bad $car->engine; $face->happy; $list->clear; $person->new_instance;
  • 15. Guideline Variable ➝ [ adjective _ ]* noun Use lowercase for variables and if compound words are use separate each word with underscore Good my $name = 'Doe'; my $first_name = 'John'; my $total_price = $net_rate + $taxes my $next_node; Bad my $name_first; my $name_last; my $node; my $sum;
  • 16. Guideline Name arrays in plural and hashes in singular Good my @errors; my %author_of; my @brands; my %config; Bad my @record; my %authors; my %places; my @user;
  • 17. Guideline lookup_variable ➝ [ adjective _ ]* noun _ preposition Adding a preposition to the end of lookup variables make names more readable Good my @sales_for; print “$sales_for[$month]n”; my %title_of; print “$title_of{$book}n”; Bad my @sales; print “$sales[$index]n”; my %titles; print “$titles{$isbn}n”;
  • 19. Guideline Avoid ambiguous abbreviations or names, is preferable to use short full names. If you have to abbreviate use well-accepted abbreviations Good my $io_stream; my $tcp_sock; my $udp_socket; sub gmt_timestamp { Bad my $left; my $tcp_st; my @e; sub g_tmstmp {
  • 20. Guideline Use named constants, using the constant pragma. For named constants use uppercase Good use constant PI => 3.14; my $area = PI * $radius**2 ; use constant BASE_URL => 'http://a.com'; my $home_url = “${BASE_URL}/home”; Bad my $area = 3.14 * $radius**2; my $base_url = 'http://a.com'; my $home_url = 'http://a.com/home'; if ( $#results < 50 ) {