Code Like a Poet

By

Most people don’t expect to become great poets, or poets at all, even if they consider themselves to be good writers. Writing poetry requires more than skill, but also a certain way of thinking about the world. Instead of trying to write it, most of us use what we learn about poetry to help us enjoy reading it. For similar reasons, not everyone should write code, but everyone should learn as much as they can about it and take time to read code every chance they get.

Studying both computer science and imaginative writing in college taught me that there is a lot of overlap between good poetry and good code. Both rely on form and language to evoke a desired reaction: in poetry, the reaction comes from the individual reading the poem; in code the reaction comes from the computer reading the code. Some might say that both can, at times, be difficult to understand. Let’s look at the similarities.

Form

Poetry and code both rely on structure and syntax as fundamental to the way they communicate. ee cumings’s poem about a leaping grasshopper is a great example of poetry’s dependence on form to influence interpretation. More familiar structures for poetry include haiku and sonnet and the poet chooses the form best suited for his intent.

Similarly, code written for an Apex Trigger will have a slightly different structure than code written for an Apex Class, but overall they are shaped the same. Code written for a user interface with Visualforce or Javascript will have a very different structure from Apex classes.

Triggers use curly brackets to group related code and square brackets around queries.

Classes use curly brackets and square brackets the same as Triggers, both use indention like an outline would.

Organization

Organization is important. All writers, poets and coders alike, have to organize their ideas in a way that makes sense for what they are trying to accomplish. Take the acrostic form, which Lewis Carroll used when he wrote poems about people he knew. The first letter of each line must be a letter, in sequence, from the name and the poem must describe the person named.

Coders also organize what their code needs to accomplish in as straightforward a fashion as possible. And, ideally, they incorporate the names of the Salesforce objects important to the code into the names of the classes and methods. A class that automates business rules regarding deleting Account records might be called AccountDeletionHandler and might contain a method like blockWithContacts.

Language

Like poetry, code relies on very precise use of the chosen language. Matsuo Basho’s eight-word frog haiku has been translated many hundreds of times, yet the original, arguably, remains the most effective in its use of language to evoke a specific time and place in nature.

Code also has efficient and effective ways to use language. For example, Apex and other object-oriented languages rely on dot notation to reference an object’s attributes, like Account.name and Account.id. Dot notation also enables code to reference other existing code for efficiency so there is no need for repetition. That code described above to automate Account business rules around record deletion when the Account has related Contacts or Opportunities can be called using dot notation: AccountDeletionHandler.blockWithContacts.

Trigger using dot notation at line 4 to call a separate class.

Desired Reaction

Writers start out knowing what they intend to evoke with their poetry, but sometimes that changes over the course of the writing. Finally, when the poem is complete, each reader reacts based on their own individual experiences. Edgar Allen Poe famously described writing his gothic poem “The Raven” to appeal to a wide audience.

Likewise, programmers know what the expected outcome should be for their code, but sometimes requirements change over the course of a project. Finally, when the code is deployed, different inputs and scenarios cause different reactions as well. Poets ask for feedback from trusted readers; coders ask for feedback from trusted users and from automated test code to validate the expected results of what they’ve written. With Salesforce unit tests, system.assert statements validate the expected results of Apex code. A unit test for AccountDeletionHandler.blockWithContacts would need to assert that Accounts aren’t deleted if they have related records.

Test Methods use System.assert statements to check reactions to code.

Read Every Day

Readers, and the best writers, spend time reading what others have written because the more we read, the easier it is for us to understand even the most difficult writers. We can all benefit from reading code, or reading about code, a little bit every day. You might start with blogs and user manuals and work your way up to open source projects. Salesforce also offersdeveloper trails in Trailhead. For those who want to read code and read about how code works, you don’t even have to complete the Trailhead challenges to learn from all the great material they offer.

Learning about structure, syntax and language are important for learning to read code. And the ability to read code is fundamental, whether or not you take that ability further and learn to edit code and write it yourself. Like poetry, you don’t have to learn to write it, but having the ability to read it can enrich your life and add clarity to the inner workings of your Salesforce org.

Intro to the Salesforce Command-Line Interface Webinar Recap

One of our latest webinars, Introduction to the Salesforce Command-line Interface for Admins, is part of our Back to School webinar series where we cover technical skill development as well as the latest product updates. Director of Trailhead Content Engineering (Salesforce) Kieren Jameson, Developer Evangelist Peter Chittum (Salesforce), and Senior Admin Evangelist Marc Baizman (Salesforce) […]

READ MORE

Decoding Triggers for Admins: Webinar Recap

Our latest webinar, Decoding Triggers for Admins, is based on Kieren Jameson’s extremely popular Dreamforce ’18 breakout session and covers how #AwesomeAdmins can decode the basic structure of a trigger, learn how to get started with triggers, and understand the role they play in your org. Take a look at the recording:   Webinar summary: […]

READ MORE

Learning Salesforce.com Workbench Part 2 (Guest Post)

“Last week I introduced you to fellow Salesforce MVP Brian Kwong. He is the Salesforce Wizard and has the hat to prove it. He has been a Salesforce Administrator for six years. In addition to being a ButtonClick Admin, Brian also utilizes Salesforce Apex and Visualforce. You know he is a driven MVP because he took […]

READ MORE