Skip to content

Commit

Permalink
introduce a code paragraph smell
Browse files Browse the repository at this point in the history
  • Loading branch information
emilybache committed Jun 18, 2024
1 parent a267289 commit 9d907c4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
19 changes: 19 additions & 0 deletions _code_smells/paragraph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
layout: code_smell
title: Paragraph of Code
source: Emily Bache
wikipedia_source: false
---

# Paragraph of Code

A paragraph is a section of code within a longer function or method that belongs together, does something specific, and might make sense to extract and name as a method. Giving the section of code a name and explicitly stating the arguments, dependencies and return value would almost certainly be easier to read and reason about than an unnamed section of code.

You can spot code paragraphs without reading the code in any detail, they have one or more of these visual characteristics:

* begin with a short comment explaining what the next lines of code do
* are in a code block (ie they are between a pair of curly braces or at the same indentation level)
* begin with a for, if, try, switch or while statement
* have a line of whitespace before and after
* a cluster of statements using the same variable name or recurring word

3 changes: 0 additions & 3 deletions _layouts/code_smell.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ <h2>Sources</h2>
{% if page.source == 'Adam Tornhill' %}
<li>This smell comes from a respected technical leader, {{ page.source }}</li>
{% endif %}
{% if page.wikipedia_source == true %}
<li> This smell is included among wikipedia's <a href="https://en.wikipedia.org/wiki/Code_smell">code smells</a></li>
{% endif %}
{% if page.source != 'Martin Fowler' and page.source != 'Adam Tornhill' %}
<li>This smell was contributed to this site by {{ page.source }}</li>
{% endif %}
Expand Down
8 changes: 1 addition & 7 deletions _learning_hours/refactoring/identify_paragraphs.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ A [Sparrow Deck](http://llewellynfalco.blogspot.com/p/sparrow-decks.html) is a w

Alternatively, if you don't want to use these prepared materials, you could collect some of your own code samples and put boxes around parts which may or may not be paragraphs. Give them to participants and ask them to classify them as "paragraph" or "not a paragraph" just by looking.

A paragraph is a section of code within a long method that hangs together and might make sense to extract as a method. You can spot them without reading the code in any detail. They have one or more of these visual characteristics:

* begin with a short comment explaining what the next lines of code do
* are in a code block (ie they are between a pair of curly braces or at the same indentation level)
* begin with a for, if, try, switch or while statement
* have a line of whitespace before and after
* a cluster of statements using the same variable name or recurring word
A [code paragraph]({% link _code_smells/paragraph.md %}) is a section of code within a long method that hangs together and might make sense to extract as a method.

Once you think you've spotted a paragraph, it may take a little more investigation to see whether it really would be possible and/or desirable to extract as a method. If you have a refactoring tool, it can do this analysis for you. Ask it to do "extract method" on the paragraph. It should be able to show you if it's possible and what the method signature would be.

Expand Down
4 changes: 3 additions & 1 deletion reference/code_smells/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ title: Code Smells

# Code Smells

This is a collection of descriptions of [Code Smells](https://www.martinfowler.com/bliki/CodeSmell.html). The descriptions are original to this site although many of the names are taken from Martin Fowler's book "Refactoring" 2nd Edition.
This is a collection of descriptions of Code Smells intended as a reference to use during learning hours and other technical coaching activities. Although these descriptions are original and authored specifically for this site, many of the names are taken from Martin Fowler's book "Refactoring" 2nd Edition.

You can read more about code smells in general in Martin Fowler's article [Code Smells](https://www.martinfowler.com/bliki/CodeSmell.html) and on [Wikipedia](https://en.wikipedia.org/wiki/Code_smell).

0 comments on commit 9d907c4

Please sign in to comment.