Navigate/Search

Archive for the '30 second tasks' Category

30 Second Tasks… Rant

Friday, April 3rd, 2015

So if you got back a month or so I used to write two or three 30 second tasks a week.  Then I decided to collect my tasks up into a single application.  It seemed like a great idea.  Now, I have gone weeks without creating a single 30 second task.  All I ever do is build, maintain, and refactor the applications.  I litterly go weeks on in before a task is made and the tasks are driven by that application, not my needs.

This is exactly what happens in software development all of the time.  See when you’re building an application there is a huge risk that you will end up with 1000s of lines of totally useless code.  Actually, it’s very likely that you will end up the a big hunk of useless code.  It’s actually very, very sad, as a developer you pour your heart into a project and eventually you just have to give up on your baby.

When you build and actual application your have to account for a lot of things.  Most of the things you have to account for are double edged swords.  Almost every good thing in software development has an evil twin.

The most frustrating thing is when you use frameworks.  A good framework will get you a very quick start and provide guidelines that promote good practices, but once the framework is in place update usually take much longer because you have to work with in the framework.

Just some thoughts…

30 Second Task… Create a build directory in my Perl List – Part 3

Monday, March 30th, 2015

I had a refactoring detour that used up two 30 second tasks.  Now it’s time to return to the creating a build directory task.

At this point I always need to take a step back and remember what I was and and why I was doing it.  If you lose track of what you’re doing or why you’re doing it you will write some strange code.

What, simple update my script to create a build directory.

Why, I write a couple dozen perl scripts a year.  The majority of the scripts follow a pattern.  Usually, take some files from somewhere, doing something to the files, move the files to somewhere else.

I usually call the “some files from somewhere” directory my build directory.  Sometimes my script needs to make the build directory.

The code to make the directory is simple, but it always takes me a few minutes to google it and make one.  After I put the code for that operation in this script I will have a simple place to refer to.  That will save time and it will make my implementations more consistent.

The first step will be to make a variable that contains the build directory path.  It’s important to think carefully about the name because as this application grows the name needs to make sense.

I think $buildDirectoryPath will be a good name for a long while.  So here’s what it’s going to look like:

$buildDirectoryPath = ‘/home/marty/tech/temp/build’

… oh oh I think I forgot something.  I need a function that checks for the existence of a directory.  I could build it straight into the create directory subroutine, but I am pretty sure I will need the functionality as part of other tasks.  I think that is functionality that will be used over and over again.  If I build it into the Create A Build Directory subroutine then there is a good chance that I will keep rebuilding the checking for existence functionality and then I will end up with lots of duplicate code that is very similar, but slightly different.  This is the kind of things that really upsets project managers.  This project has been stalled for weeks and we just hit another detour.

Detour two create a function that determines if a directory exists…

30 Second Task… Perl Detour Complete

Friday, March 27th, 2015

Last time I hit a refactor detour.  Basically, I ran into a variable name that quit making sense as the project grew.  I was chomping at the bit to add new functionality, but I decided to take the time to refactor in order to avoid confusion down the road.

After thinking things through it was fun, simple, and easy:

 

#!/usr/bin/perl
 use Cwd;
 
################################################
# This section has all of the global variables #
################################################ 

$currentDirectory     = "";
$consolePrintTitle         = "";

#############################################################
# This section should read like description of the behavior #
#############################################################

setCurrentDirectory();
displayContentsOfRootDirectory();
displayEntireContentsOfCurrentDirectory();
displayContentsOfCurrentDirectoryMinusDots();

############################################################
# This section is where the high level implementations are #
############################################################

sub displayContentsOfRootDirectory{
    $consolePrintTitle = "print contents of root directory";
    printCurrentTitle();
    printContentsOfRootDirectory();
}

sub displayEntireContentsOfCurrentDirectory{
    $consolePrintTitle = "print contents of current directory";
    printCurrentTitle();
    printEntireContentsOfCurrentDirectory();
}

sub displayContentsOfCurrentDirectoryMinusDots{
    $consolePrintTitle = "print contents of current directory minus dots";
    printCurrentTitle();
    printContentsOfCurrentDirectory();
}

###########################################################
# This section is where the low level implementations are #
###########################################################

sub setCurrentDirectory{
    $currentDirectory = getcwd();
}

sub printCurrentTitle{
    print "\n\n* $consolePrintTitle *\n";
}

sub printContentsOfRootDirectory{
    opendir($dh, "/") || die;
      while(readdir $dh) {
        print "$_\n";
      }
    closedir $dh;
}

sub printContentsOfCurrentDirectory{
    opendir($dh, $currentDirectory) || die;
      while(readdir $dh) {
          next if ($_ =~ m/^\./);
        print "$_\n";
      }
    closedir $dh;
}

sub printEntireContentsOfCurrentDirectory{
    opendir($dh, $currentDirectory) || die;
      while(readdir $dh) {
        print "$_\n";
      }
    closedir $dh;
}

30 Second Tasks… Perl List Refactor Detour

Tuesday, March 24th, 2015

So now I am all set.  I have my task, to create a build directory with my script.  I have thought it all out.  I have even figured out exactly where the code goes.

This morning I was getting ready to start coding, and bang and detour.  The variable name “currentTitle” doesn’t make a lick of sense.  It did when I wrote it, but as this application grows the word went from useful, to confusing.  This happens all of the time in code.

You see, when I wrote the code the variable was right next to the call.  Then as part of a refactor the variable got pulled into a variables area.  So where:

var currentTitle = "Blah blah blah";
printCurrentTitle();

Makes a lot sense.  This:

var currentTitle = "";
code
code
code
currentTitle = "Blah blah blah";
printCurrentTitle();

Makes a lot less sense.  It’s pretty good at the point of use, but amongst the other variables it doesn’t make sense.

As a developer, this situation is tricky.  Usually you have timelines and stress to complete your task. Also you’re usually working on a ticket or story that doesn’t include refactoring.

Since I am doing this coding for recreational purposes I can take the time to refactor.  My future self will appreciate not have to wonder WTH currentTitle means.  I am going to rename it to consolePrintTitle, so that when ever I look that the variable listing for this code I will immediately remember that there is a section of code that prints titles to the console.

This will be an easy fix, the fix should impact 4 lines.  This fix will probably need to be fixed again during the next major refactor.  It may also be a problem if this system ever starts printing to something other than the console.  Implementing and testing this fix should take about 30 seconds…  It’s a 30 second task :)

30 Second Task… Create a build directory in my Perl List – Part 2

Saturday, March 21st, 2015

In my last post about this I mentioned that I thought adding a create build directory tidbit would be 5 lines.

1) 1 line in the variables.

Basically, a variable to declare the path and name of the build directory.  Something like “buildDirectoryPathName”

2) 1 line in the high level description

Something like “createBuildDirectory.”

3) 3 lines in the low level implementations.

Basically a sub routine that creates a directory.

If this was in Java or JavaScript this would be a good time to write unit tests.

30 Second Task… Create a build directory in my Perl List

Wednesday, March 18th, 2015

At this point I have a 77 line Perl script that I can read very easily.  As I mentioned in my last 30 second post I was to add some new tidbit/references.  The first one I want to add is an example of creating a build directory.  So what I want is a 80is line Perl script that includes an example of creating a directory named “build” and what I absolutely don’t want is for it to be less readable.

Right now I have four poorly named layers.

Top – variables

Second – functions that describe the behavior

Third – high level implementations

Fourth – low level implementations

So the very first step will be to guess which levels should have new code:

Top – Yes, I should create a variable that I will be able to easily read in the future and that will help me know what the system is doing.  Something like “buildDirectoryName”

Second – Yes, always

Third – Probably not, in Perl writing a sub routine that creates a directory is just a couple of lines of code.

Fourth – Yes

It’s also a good idea to practice guessing the lines of code at this point:

Top:1, Second:1, Third:0, and Fourth: 3

So I am guessing there will be 5 new lines of code.

note: I suspect I will need to make the fourth more generic in the future and then add code to the third.  For new I will make the fourth very specific.

In real life this whole analysis would take about 30 seconds to accomplish :)

30 Second Tasks… Perl List Next Steps

Sunday, March 15th, 2015

When you write a computer program you have to take a step back once in a while.  You have to think about what the purpose was when you started program, what the purpose is now, is the work we are doing true to the purpose…

Once you’ve wrestled with those thoughts it time to think about the current state of the program.  Should you go through a refactoring stage or does the current architecture support the near term plans.

The point of my Perl List is to be a simple reference so I can find useful Perl tidbits.

What’s the next tidbit?  The suite of functionality that I want to document/reference/build is creating directories, moving files, copying files, backing up files…

Right now the application is 77 lines of code and has 4 layers.  It’s is very, very easy for me to find code.

Top layer – data listing.  The data can/should be a good indicator of what the system is capable of.

Second layer – description of behavior

Third layer – high level implementations

Bottom layer – low level implementations

I think it’s ok for the top and bottom layers to grow.  I think the middle layers should never grow to more than about 10 lines of code before they are split into some kind of smaller unit.

The second layer is 4 lines of code so it’s good for now.

The third layer is 9 lines of code (not including signatures) so starting to get bad.

The third layer is going to be a problem soon, it’s probably going to need to name other than the third layer.  But not now… It will probably be easier to to come up with good name after the next set of changes are implemented.

Here is the next set of tidbits:

  • Create a build directory
  • Create a destination directory
  • Create a backup directory
  • Create a year month directory in the backup directory
  • Create a file in the build directory
  • Move a file from the build directory to the backup directory
  • Copy a file from the build directory to the destination directory
  • Backup file by date

That will be nice and I will definitely need to some restructuring when this is done.

 

 

 

30 Second Tasks… Perl List Refactor

Thursday, March 12th, 2015

Here’s the refactor I mentioned a couple of days ago.

 

#!/usr/bin/perl
use Cwd;

################################################
# This section has all of the global variables #
################################################

$currentDirectory     = “”;
$currentTitle         = “”;

#############################################################
# This section should read like description of the behavior #
#############################################################

setCurrentDirectory();
displayContentsOfRootDirectory();
displayEntireContentsOfCurrentDirectory();
displayContentsOfCurrentDirectoryMinusDots();

############################################################
# This section is where the high level implementations are #
############################################################

sub displayContentsOfRootDirectory{
$currentTitle = “print contents of root directory”;
printCurrentTitle();
printContentsOfRootDirectory();
}

sub displayEntireContentsOfCurrentDirectory{
$currentTitle = “print contents of current directory”;
printCurrentTitle();
printEntireContentsOfCurrentDirectory();
}

sub displayContentsOfCurrentDirectoryMinusDots{
$currentTitle = “print contents of current directory minus dots”;
printCurrentTitle();
printContentsOfCurrentDirectory();
}

###########################################################
# This section is where the low level implementations are #
###########################################################

sub setCurrentDirectory{
$currentDirectory = getcwd();
}

sub printCurrentTitle{
print “\n\n* $currentTitle *\n”;
}

sub printContentsOfRootDirectory{
opendir($dh, “/”) || die;
while(readdir $dh) {
print “$_\n”;
}
closedir $dh;
}

sub printContentsOfCurrentDirectory{
opendir($dh, $currentDirectory) || die;
while(readdir $dh) {
next if ($_ =~ m/^\./);
print “$_\n”;
}
closedir $dh;
}

sub printEntireContentsOfCurrentDirectory{
opendir($dh, $currentDirectory) || die;
while(readdir $dh) {
print “$_\n”;
}
closedir $dh;
}

30 Second Tasks… Time to refactor the Perl list

Sunday, March 8th, 2015

I am trying to create a single script that has a couple dozen useful perl 30 second tasks.  When I am all done I hope to have an easy reference so I can do lots of useful things like read directories, files, ftp files, download files, screen scrape websites…

I think the script I made for the last post is pretty cool.  I have found that the narrative part of my script shouldn’t exceed 10 lines of code.  I usually start rethinking it around 6 lines.

The narrative part of my code currently reads like:

setCurrentDirectory();

$currentTitle = "print contents of root directory";
printCurrentTitle();
printContentsOfRootDirectory();

$currentTitle = "print contents of current directory";
printCurrentTitle();
printEntireContentsOfCurrentDirectory();

$currentTitle = "print contents of current directory minus dots";
printCurrentTitle();
printContentsOfCurrentDirectory();

So that is exactly 10 lines.  So it’s time to refactor the code so it is readable.

I think the next step it the group the code so the narrative reads:

setCurrentDirectory();
displayContentsOfRootDirectory();
displayEntireContentsOfCurrentDirectory();
displayContentsOfCurrentDirectoryMinusDots();

That will get the narrative part of the script down to four lines of code.

30 Second Task All The Perl Stuff…

Thursday, March 5th, 2015

The whole point of my 30 second task series is to save time for my future self.  I am going to combine all of them into a single file…

#!/usr/bin/perl
 use Cwd;

################################################
# This section has all of the global variables #
################################################ 

$currentDirectory     = "";
$currentTitle         = "";

#############################################################
# This section should read like description of the behavior #
#############################################################

setCurrentDirectory();

$currentTitle = "print contents of root directory";
printCurrentTitle();
printContentsOfRootDirectory();

$currentTitle = "print contents of current directory";
printCurrentTitle();
printEntireContentsOfCurrentDirectory();

$currentTitle = "print contents of current directory minus dots";
printCurrentTitle();
printContentsOfCurrentDirectory();

#################################################
# This section is where the implementations are #
#################################################

sub setCurrentDirectory{
    $currentDirectory = getcwd();
}

sub printCurrentTitle{
    print "\n\n* $currentTitle *\n";
}

sub printContentsOfRootDirectory{
    opendir($dh, "/") || die;
      while(readdir $dh) {
        print "$_\n";
      }
    closedir $dh;
}

sub printContentsOfCurrentDirectory{
    opendir($dh, $currentDirectory) || die;
      while(readdir $dh) {
          next if ($_ =~ m/^\./);
        print "$_\n";
      }
    closedir $dh;
}

sub printEntireContentsOfCurrentDirectory{
    opendir($dh, $currentDirectory) || die;
      while(readdir $dh) {
        print "$_\n";
      }
    closedir $dh;
}