Your SlideShare is downloading. ×
Best practices naming conventions
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Best practices naming conventions

1,144
views

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,144
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Best Practices Naming ConventionsMariano 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 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. sub attr { $_ = (caller(0))[3]; s/.*://; @_ > 1 ? $_[0]->{$_} = $_[1] : $_[0]->{$_}} vssub attr { my $self = shift; $self->{attr} = shift if(@_ ); return $self->{attr};}
  • 5. Namespaces
  • 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. 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. Subroutines or Methods
  • 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. 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. 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. 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. 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. Variables
  • 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. 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. 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. Common
  • 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. 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. Source: http://xkcd.org Thank You!