patternModerate
Analyzing genetic tags
Viewed 0 times
geneticanalyzingtags
Problem
I've gone back and forth a few times recently on my Perl coding style when it comes to module subroutines. If you have an object and you want to call the method
For example, consider this block of code. For the method calls that require arguments (
```
while( my $feature = $gff3->next_feature )
{
if($type eq "cds")
{
if( $feature->primary_tag eq "mRNA" )
{
my($gene_id) = $feature->get_tag_values("Parent");
my($mRNA_id) = $feature->get_tag_values("ID");
next unless( $list eq '' or $genes_to_extract->{$gene_id} );
$subseq_locations->{ $feature->seq_id }->{ $mRNA_id } = Bio::Location::Split->new();
}
elsif( $feature->primary_tag eq "CDS" )
{
my($mRNA_id) = $feature->get_tag_values("Parent");
if( $subseq_locations->{ $feature->seq_id }->{ $mRNA_id } )
{
$subseq_locations->{ $feature->seq_id }->{ $mRNA_id }->add_sub_Location( $feature->location );
}
}
}
else
{
if( $feature->primary_tag eq $type )
{
my $feat_id;
if( $list ne '')
{
($feat_id) = $feature->get_tag_values("ID") if($feature->has_tag("ID"));
next unless( $feature->has_tag("ID") and $genes_to_extract->{$feat_id} );
}
$subseq_locations->{ $f
bar with no arguments, then you can either do $foo->bar() or $foo->bar. At one point I started favoring the latter because I felt it cleaned up the code and made it more readable. However, sometimes I question whether it would be better to be fully explicit, especially considering the possibility that someone else will have to look at my code later — someone who almost certainly will not be an expert Perl programmer.For example, consider this block of code. For the method calls that require arguments (
get_tag_values and has_tag), there is no question about the parentheses. But what about next_feature and primary_tag? Is the readability I gain from dropping the parens worth losing the explicit syntax? Is one better than the other for long term maintainability? Or is this simply a subjective judgment call?```
while( my $feature = $gff3->next_feature )
{
if($type eq "cds")
{
if( $feature->primary_tag eq "mRNA" )
{
my($gene_id) = $feature->get_tag_values("Parent");
my($mRNA_id) = $feature->get_tag_values("ID");
next unless( $list eq '' or $genes_to_extract->{$gene_id} );
$subseq_locations->{ $feature->seq_id }->{ $mRNA_id } = Bio::Location::Split->new();
}
elsif( $feature->primary_tag eq "CDS" )
{
my($mRNA_id) = $feature->get_tag_values("Parent");
if( $subseq_locations->{ $feature->seq_id }->{ $mRNA_id } )
{
$subseq_locations->{ $feature->seq_id }->{ $mRNA_id }->add_sub_Location( $feature->location );
}
}
}
else
{
if( $feature->primary_tag eq $type )
{
my $feat_id;
if( $list ne '')
{
($feat_id) = $feature->get_tag_values("ID") if($feature->has_tag("ID"));
next unless( $feature->has_tag("ID") and $genes_to_extract->{$feat_id} );
}
$subseq_locations->{ $f
Solution
I prefer to make a distinction on semantic level: there are no functions or methods, but instead there are properties and actions. Every property is an object (in real-world sense) and an action is something done on an object. A good mnemonic is probably to read
Consider a variable:
You know by the name it's an object, since it's a noun.
Even though
Again,
Again, a function/method, but a property of a document object, not an action, so no parens. A boolean value or an integer is strictly speaking not a real-world object, but it's more like an object than an action.
() as do it!.Consider a variable:
$documentYou know by the name it's an object, since it's a noun.
$document->authorEven though
author is probably a function/method, an author is an object and a noun, so it's a property and thus no parens here.$document->send()
$document->author->write_book()Again,
send and write_book are functions/methods, but since those are actions (as designated by verbs), we write parens behind them. $document->author_count
$document->has_reviewsAgain, a function/method, but a property of a document object, not an action, so no parens. A boolean value or an integer is strictly speaking not a real-world object, but it's more like an object than an action.
Code Snippets
$document->author$document->send()
$document->author->write_book()$document->author_count
$document->has_reviewsContext
StackExchange Code Review Q#906, answer score: 13
Revisions (0)
No revisions yet.