Sponsored Content
Top Forums Programming from where do i start? is there anything i should take care? Post 34242 by mbb on Tuesday 11th of February 2003 11:45:44 AM
Old 02-11-2003
Whenever I take on new code, whatever the language, I usually find that it has poor commenting and bad indentation.

Many programmers have their own styles it is true, but then again many programmers are untidy and inconsistent.

A good way of learning a new piece of code is to go through it and tidy it up.

By adding comments and correcting indentation you will find you have to think about what the code is doing on a line by line basis.

Although this may seem a trivial or pendantic exercise I have always found it a useful technique in learning some else's source code and most importantly How They Are Thinking!

Be careful though not to change the functionality of the code you are tarting up. At a minimum:

1. Always provide a module banner (comment block) describing the source file e.g source file name, author, date, desription, project

2. Always provide a function banner describing the function. e.g. function name, description, input and output parameters


Perdabo suggests looking at fuctions to understand how the program is constructed. Another method at looking at a program which might be a bit more immediate when fixing faults, is to look at a variable in the program and see what happens to it. Just pick on the variable you are interested in an use the editor to search for the name.

Although this may seem old fashioned now, some people, still prefer to print out a program to paper and read it and understand it away from the computer. This method does allow you to write notes against the code which may not be suitable for commenting. The print out can also be used as a basis for a peer review meeting where comments or critiques of the code can be written on the listing.

Hope this helps.

MBB
 

We Also Found This Discussion For You

1. UNIX for Dummies Questions & Answers

SAN and Disk I/O ... do we care?

Hi all, My main function is as a DBA. Another person manages the server and the SAN. I just want to know if I should be worried about high disk I/O or is it irrelevant as the I/O "load balancing" will be "taken care" of by the SAN? For example, I have hdisk1-5 and I can see that there are... (2 Replies)
Discussion started by: newbie_01
2 Replies
Moose::Cookbook::Roles::Restartable_AdvancedComposition(User Contributed Perl DocumentMoose::Cookbook::Roles::Restartable_AdvancedComposition(3pm)

NAME
Moose::Cookbook::Roles::Restartable_AdvancedComposition - Advanced Role Composition - method exclusion and aliasing VERSION
version 2.0603 SYNOPSIS
package Restartable; use Moose::Role; has 'is_paused' => ( is => 'rw', isa => 'Bool', default => 0, ); requires 'save_state', 'load_state'; sub stop { 1 } sub start { 1 } package Restartable::ButUnreliable; use Moose::Role; with 'Restartable' => { -alias => { stop => '_stop', start => '_start' }, -excludes => [ 'stop', 'start' ], }; sub stop { my $self = shift; $self->explode() if rand(1) > .5; $self->_stop(); } sub start { my $self = shift; $self->explode() if rand(1) > .5; $self->_start(); } package Restartable::ButBroken; use Moose::Role; with 'Restartable' => { -excludes => [ 'stop', 'start' ] }; sub stop { my $self = shift; $self->explode(); } sub start { my $self = shift; $self->explode(); } DESCRIPTION
In this example, we demonstrate how to exercise fine-grained control over what methods we consume from a role. We have a "Restartable" role which provides an "is_paused" attribute, and two methods, "stop" and "start". Then we have two more roles which implement the same interface, each putting their own spin on the "stop" and "start" methods. In the "Restartable::ButUnreliable" role, we want to provide a new implementation of "stop" and "start", but still have access to the original implementation. To do this, we alias the methods from "Restartable" to private methods, and provide wrappers around the originals(1). Note that aliasing simply adds a name, so we also need to exclude the methods with their original names. with 'Restartable' => { -alias => { stop => '_stop', start => '_start' }, -excludes => [ 'stop', 'start' ], }; In the "Restartable::ButBroken" role, we want to provide an entirely new behavior for "stop" and "start". We exclude them entirely when composing the "Restartable" role into "Restartable::ButBroken". It's worth noting that the "-excludes" parameter also accepts a single string as an argument if you just want to exclude one method. with 'Restartable' => { -excludes => [ 'stop', 'start' ] }; CONCLUSION
Exclusion and renaming are a power tool that can be handy, especially when building roles out of other roles. In this example, all of our roles implement the "Restartable" role. Each role provides same API, but each has a different implementation under the hood. You can also use the method aliasing and excluding features when composing a role into a class. FOOTNOTES(1) The mention of wrapper should tell you that we could do the same thing using method modifiers, but for the sake of this example, we don't. AUTHOR
Moose is maintained by the Moose Cabal, along with the help of many contributors. See "CABAL" in Moose and "CONTRIBUTORS" in Moose for details. COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Infinity Interactive, Inc.. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.14.2 2012-06-28 Moose::Cookbook::Roles::Restartable_AdvancedComposition(3pm)
All times are GMT -4. The time now is 02:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy