What is Legacy Code? – Legacy Coder Podcast #5

What is Legacy Code?

This podcast is called “Legacy Coder” but what exactly is legacy code? I talk about my definition of the term in the fifth episode of the Legacy Coder Podcast.

Legacy Code?

  • What is Legacy Code?
    • Definition by Michael Feathers: “Code without tests.”
    • Code of a certain age.
      • Brown field instead of green field.
    • “Old” languages or platforms.
      • Natural, COBOL, ABAP, Mainframes.
      • But also J2EE.
    • Code that’s hard to change or maintain.
      • You can write “new” legacy code.
      • You can also write legacy code in modern languages like Java or C#.
    • Big Balls of Mud, Monoliths.
      • Duplicated code.
      • Hard to separate into individual pieces of functionality for reuse.
      • Different concerns are bundled together (see title image).
    • Code that lacks certain quality characteristics.
      • Not readable, not modularized, not consistent, hard to understand, deeply nested, similar things are done differently, no patterns.
  • How can you get rid of legacy code?
    • Why would you want to get rid of the code in the first place?
      • “Don’t forget – having legacy software is often a sign of success. Your business was successful to last long enough for your software to become legacy.” [Sam Newman]
      • High maintenance costs, aging/retiring workforce, unable to implement new requirements.
    • A big rewrite is almost never the answer. But sometimes.
    • Gradually improve the quality of your codebase.
      • Introduce tests, e.g. compare log files before/after.
    • Integrate the legacy code base into your modern architecture, e.g. with webMethods and EntireX for Adabas/Natural applications.

A short piece of Legacy Code in (pseudo) Natural

Here’s how many of the old Natural modules I encounter in my day job look like:

DEFINE DATA
LOCAL USING DDMVIEW
END-DEFINE

READ IMPORTANT-DDM BY SUPERDESCRIPTOR

    IF IMPORTANT-DDM.FIELD EQ 1
        ADD 100 TO IMPORTANT-DDM.FIELD
        UPDATE
        END TRANSACTION
    ELSE
        ESCAPE TOP
    END-IF

    INPUT USING MAP 'OUTPUT'

END-READ

END

Database access, business logic, and the presentation of the results to the user (UI) are all bundled together into a single module. This becomes a maintenance nightmare quickly and is very hard to test because the individual concerns can’t be separated for testing.

This module could be split up into 5 different modules that only do one thing, can therefore be reused in different scenarios, and can easily be (unit) tested:

  • Reading the database (e.g. subroutine READ-DATA)
  • Processing the data, a.k.a. your “business logic” (e.g. subroutine PROCESS-DATA)
  • Saving data to the database (e.g. subroutine SAVE-DATA)
  • Showing the results to the user (e.g. subroutine DISPLAY-DATA)
  • Orchestrating the individual steps to implement the whole use case (the main program)

Here’s how the refactored main program would look like:

DEFINE DATA
LOCAL USING ARRDATA
END-DEFINE

PERFORM READ-DATA ARRDATA
PERFORM PROCESS-DATA ARRDATA
PERFORM SAVE-DATA ARRDATA
PERFORM DISPLAY-DATA ARRDATA

END

Recommended reading (and hearing)

In his book Working Effectively with Legacy Code* Michael Feathers shows different ways of introducing automated tests into a legacy code base. He uses C++ in his examples but the underlying ideas can be applied to any other programming language, too.

Michael Feathers - Working Effectively with Legacy Code (Robert C. Martin Series) (Affiliate)*

Robert C. Martin wrote my all time favourite book for software developers: Clean Code*. If you haven’t read it already, grab a copy now and read it from front to back! No matter what programming language you’re using, you will definitely find lots of ways to improve your existing code in here.

Robert C. Martin - Clean Code: A Handbook of Agile Software Craftsmanship (Affiliate)*

In the very first episode of this podcast I talked about how to unit test your Natural application. In my opinion, that’s a very important step in modernizing a legacy code base.

Unit Testing Natural Applications - Legacy Coder Podcast #1

Links

Modern Natural Development with NaturalONE – Legacy Coder Podcast #3

Modern Natural Development with NaturalONE

Why would anyone dismiss the good old Natural editor and switch over to NaturalONE? Let’s find out what features NaturalONE has to offer in the third episode of the Legacy Coder Podcast.

Why should you switch over to NaturalONE?

  • Increased developer productivity
    • Use Copy and Paste (duh!) for high quality Natural programming 😉
    • Syntax Highlighting will help you understand the source code faster.
    • Make use of intelligent search and replace features with regular expressions.
    • Code Completion makes you type way less than before.
      Code Completion in NaturalONE
    • Code Navigation (jump to a variable definition etc.) lets you explore the code base with a few keystrokes.
    • You can use Code Folding for focussing on the really important stuff.
    • Multiple compile errors are displayed instantly without uploading the source to the server.
      Multiple compile errors in NaturalONE
  • Repository based development
    • Finally develop software like the cool kids do!
    • Every code change is documented and can be rolled back with just a few commands. It can even be attached to an issue tracker like Jira or Redmine.
      Git integration in NaturalONE
    • You can try out new things in an isolated environment.
    • You can automate your coding with code generators like Xtext.
  • Modernize your development environment
    • Share knowledge and open up towards other (maybe younger) developers (Java etc.).
    • Be and stay attractive as a company to new employees.
    • Automate your build process
    • NaturalONE is Software AG’s platform of choice for the future.
    • The old Natural editor will be shut down!
  • Extendable with additional plugins
    • Software AG’s innovations will only take place in NaturalONE (e.g. Code Coverage, Screen Testing).
    • Attach your IDE to an issue tracker like Jira or Redmine with Mylyn.
    • Use Snipmatch for even less typing. Plugin Snipmatch used in NaturalONE
    • Create your own extensions (e.g. NatUnit) for whatever requirement you have.
      Our own NatUnit plugin used in NaturalONE

Recommended reading

  • NaturalOne Debugging

Links