Posts

Refactoring 2nd Edition: Chapter 11: Refactoring APIs

Refactoring: Improving the Design of Existing Code (2nd Edition) Cover Book cover copyright Martin Fowler

Overview

This chapter explains several methods for refactoring functions and their parameters.

My One Takeaway

I felt like most of this chapter just clicked with me. I do love the “Remove Flag Argument” refactoring because I worked on a piece of software were it was used all the time and I constantly had to look up what the function was doing.

Finding All (or most) Commits to a Feature

git Logo

The other day my supervisor asked me an interesting question. He wanted to know how much time we spent developing a new feature. The feature was massive and we started working on it late last year and is just now about to come out of beta. There were a bunch of issues trying to figure this out: nobody was dedicated full time to it, it’s been worked on in fits and starts during that time, and we don’t currently track our time at this level of detail. It became an interesting thought process.

Read More

Refactoring 2nd Edition: Chapter 10: Simplifying Conditional Logic

Refactoring: Improving the Design of Existing Code (2nd Edition) Cover Book cover copyright Martin Fowler

Overview

This chapter explains several refactoring methods for simplifying conditional logic.

My One Takeaway

Another two takeaway chapter:

  1. Decompose Conditional is a huge win for me for readability
  2. “I often find I use Replace Nested Conditional with Guard Clauses when I’m working with a programmer who has been taught to have only one entry point and one exit point from a method. … Clarity is the key principal: If the method is clearer with one exit point, use one exit point; otherwise don’t.” I love “Clarity is the key principal” and it should be on every blackboard in every programming class ever.

Refactoring 2nd Edition: Chapter 9: Organizing Data

Refactoring: Improving the Design of Existing Code (2nd Edition) Cover Book cover copyright Martin Fowler

Overview

This chapter explains several refactoring methods for moving code that accesses data around so it’s easier to work with

My One Takeaway

I’ve become a big fan of “Change Value to Reference” as I’ve worked with more complex integration tests. It’s made testing results a lot easier in some cases and has improved performance in others.

Using clearstatcache To Fix Unit Tests

PHP Logo

The other day I ran into an interesting problem. In one of our integration tests we did the following:

$this->assertFalse(is_file($fullPath));

$item->runProcessThatCreatesFileInADifferentProcess();

$this->assertTrue(is_file($fullPath));

The assertTrue() check always failed and everything we did showed that the file existed. In order to get around this we had to use PHP’s clearstatcache() function before the assertTrue() call.

The reason for this can be seen in this section from https://www.php.net/manual/en/function.clearstatcache.php

When you use stat(), lstat(), or any of the other functions listed in the affected functions list (below), PHP caches the information those functions return in order to provide faster performance. However, in certain cases, you may want to clear the cached information. For instance, if the same file is being checked multiple times within a single script, and that file is in danger of being removed or changed during that script’s operation, you may elect to clear the status cache. In these cases, you can use the clearstatcache() function to clear the information that PHP caches about a file.

PHP was caching the result of is_file() and causing the assert to fail. To fix the tests we had to add the following call to clearstatcache().

$this->assertFalse(is_file($fullPath));

$item->runProcessThatCreatesFileInADifferentProcess();

clearstatcache(false, $fullPath);
$this->assertTrue(is_file($fullPath));

Link Post and Podcast Roundup: September 2019 Edition

Link Post Logo

September’s links.

Read More

Refactoring 2nd Edition: Chapter 8: Moving Features

Refactoring: Improving the Design of Existing Code (2nd Edition) Cover Book cover copyright Martin Fowler

Overview

This chapter explains several refactoring methods for moving code around and making it easier to clean them up.

My One Takeaway

Two this time:

  1. I love how much easier to read the “Replace Loop with Pipeline” refactoring makes the example. I think I’ll definitely be spending some time looking into how to do this with PHP.
  2. “Remove Dead Code” is my favorite refactor ever. In an earlier job, we had a functions.php that was included in every file and it had an if statement that could never resolve to true. Everyone was afraid of removing it for fear that it would break something. I wasn’t. :-)

Refactoring 2nd Edition: Chapter 7: Encapsulation

Refactoring: Improving the Design of Existing Code (2nd Edition) Cover Book cover copyright Martin Fowler

I fell off of this process really bad due to some family reasons but I’m going to start back on this process.

Overview

This chapter explains several refactoring methods that involve moving items into classes, out of classes, and into new classes.

My One Takeaway

I think these are all valuable refactorings to learn but I’m still not sure on how I’ll know to use them when I’m working. I guess I’ll just need to keep reviewing these. :-)

Mass Deleting Remote Git Branches

Git Logo Git Logo from https://git-scm.com/downloads/logos

The other day at work I realized how many open branches we had in our Git repo that had been merged into the master branch but weren’t deleted from our BitBucket repo. I started looking for ways to quickly cleanup the branches without automating the process so I didn’t accidentally delete something that was still needed.

My initial search found that you can delete a remote branch using the following command:

git push origin --delete name-of-branch

My issue was that I needed to delete hundreds of of branches. Luckily you can specify multiple branches for this command like so:

git push origin --delete name-of-branch1 name-of-branch2 name-of-branch3 name-of-branch4

I was able to use git branch --merged to get a quick list of all the branches that had been merged into master and then I ran the command above on them once I verified they were all “old”.

Link Post and Podcast Roundup: August 2019 Edition

Link Post Logo

August’s links.

Read More
RSS

Join Our Mailing List!

View previous campaigns.

Top Posts

  1. Working With Soft Deletes in Laravel (By Example)
  2. Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
  3. Upgrading to Laravel 8.x
  4. Get The Count of the Number of Users in an AD Group
  5. Multiple Vagrant VMs in One Vagrantfile
  6. Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
  7. Changing the Directory Vagrant Stores the VMs In
  8. Accepting Android SDK Licenses From The OSX Command Line
  9. Fixing the 'Target class [config] does not exist' Error
  10. Using Rectangle to Manage MacOS Windows

subscribe via RSS

All content copyright This Programming Thing 2012 - 2021
Blogging about PHP, MySQL, Zend Framework, MySQL, Server Administration and Programming in general