0
Best Practices   Naming ConventionsMariano Wahlmann
Greatly inspired in the contents of...                           & ISBN-13: 978-0596001735       ISBN-13: 978-0131655492
“Always code as if the person who will maintainyour code is a maniac serial killer that knowswhere you live”●   Software E...
sub attr {   $_ = (caller(0))[3];   s/.*://; @_ > 1 ? $_[0]->{$_} = $_[1] : $_[0]->{$_}}                         vssub att...
Namespaces
Guideline        namespace → Noun [ :: Adjective ]*Begin packages names with an uppercase, if compoundwords are used each ...
GuidelineAvoid names that relate to implementation details,there might be special cases where you dont want todo that such...
Subroutines or Methods
Guidelineroutine ➝ imperative_verb [ _ adjective ]? _ noun _ preposition        | imperative_verb [ _ adjective ]? _ noun ...
GuidelineBegin subroutine or method names with is_ or has_ ifthey return a boolean, there might be special caseswhere you ...
GuidelinePrefix “internal use only” subroutines or “Private”methods with an underscore                                    ...
GuidelineUse nouns for subroutines or methods which return aspecific object or value                                      ...
GuidelineUse imperative for subroutines or methods thatperforms an action                                                 ...
Variables
Guideline            Variable ➝ [ adjective _ ]* nounUse lowercase for variables and if compound words areuse separate eac...
GuidelineName arrays in plural and hashes in singular                                                   Goodmy @errors;my ...
Guidelinelookup_variable ➝ [ adjective _ ]* noun _ prepositionAdding a preposition to the end of lookup variablesmake name...
Common
GuidelineAvoid ambiguous abbreviations or names, is preferableto use short full names. If you have to abbreviateuse well-a...
GuidelineUse named constants, using the constant pragma. Fornamed constants use uppercase                                 ...
Source: http://xkcd.org                          Thank You!
Upcoming SlideShare
Loading in...5
×

Best practices naming conventions

1,204

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,204
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Best practices naming conventions"

  1. 1. Best Practices Naming ConventionsMariano Wahlmann
  2. 2. Greatly inspired in the contents of... & ISBN-13: 978-0596001735 ISBN-13: 978-0131655492
  3. 3. “Always code as if the person who will maintainyour code is a maniac serial killer that knowswhere 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. 4. sub attr { $_ = (caller(0))[3]; s/.*://; @_ > 1 ? $_[0]->{$_} = $_[1] : $_[0]->{$_}} vssub attr { my $self = shift; $self->{attr} = shift if(@_ ); return $self->{attr};}
  5. 5. Namespaces
  6. 6. Guideline namespace → Noun [ :: Adjective ]*Begin packages names with an uppercase, if compoundwords are used each word must be capitalized, alsowhen using acronyms each letter must be capitalized Goodpackage XML::Simple;package Car::Electric;Package Devel::NYTProf;package Moose; Badpackage Hotel::Inventory::HotelInventory;package Big::House;Package Rich::People;package Html::tag;
  7. 7. GuidelineAvoid names that relate to implementation details,there might be special cases where you dont want todo that such as packages that provide bindings to anspecific library Goodpackage Plane::Jet;package Dog::Beagle;Package XML::LibXML;package Gtk2; Badpackage XML::SAXServiceParser;package People::CachedList;Package Moose::People;package HTTP::LWPAgent;
  8. 8. Subroutines or Methods
  9. 9. Guidelineroutine ➝ 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. 10. GuidelineBegin subroutine or method names with is_ or has_ ifthey return a boolean, there might be special caseswhere you dont 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. 11. GuidelinePrefix “internal use only” subroutines or “Private”methods with an underscore Good$self->_build_list;$self->_calculate_price;$self->_store($element);$self->_initialize(@_); Badsub internal_method {sub private_function {
  12. 12. GuidelineUse nouns for subroutines or methods which return aspecific 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. 13. GuidelineUse imperative for subroutines or methods thatperforms an action Good$car->start_engine;$face->smile;$list->remove_all;$person->clone; Bad$car->engine;$face->happy;$list->clear;$person->new_instance;
  14. 14. Variables
  15. 15. Guideline Variable ➝ [ adjective _ ]* nounUse lowercase for variables and if compound words areuse separate each word with underscore Goodmy $name = Doe;my $first_name = John;my $total_price = $net_rate + $taxesmy $next_node; Badmy $name_first;my $name_last;my $node;my $sum;
  16. 16. GuidelineName arrays in plural and hashes in singular Goodmy @errors;my %author_of;my @brands;my %config; Badmy @record;my %authors;my %places;my @user;
  17. 17. Guidelinelookup_variable ➝ [ adjective _ ]* noun _ prepositionAdding a preposition to the end of lookup variablesmake names more readable Goodmy @sales_for;print “$sales_for[$month]n”;my %title_of;print “$title_of{$book}n”; Badmy @sales;print “$sales[$index]n”;my %titles;print “$titles{$isbn}n”;
  18. 18. Common
  19. 19. GuidelineAvoid ambiguous abbreviations or names, is preferableto use short full names. If you have to abbreviateuse well-accepted abbreviations Goodmy $io_stream;my $tcp_sock;my $udp_socket;sub gmt_timestamp { Badmy $left;my $tcp_st;my @e;sub g_tmstmp {
  20. 20. GuidelineUse named constants, using the constant pragma. Fornamed constants use uppercase Gooduse constant PI => 3.14;my $area = PI * $radius**2 ;use constant BASE_URL => http://a.com;my $home_url = “${BASE_URL}/home”; Badmy $area = 3.14 * $radius**2;my $base_url = http://a.com;my $home_url = http://a.com/home;if ( $#results < 50 ) {
  21. 21. Source: http://xkcd.org Thank You!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×