Talking code, breathing air

Person underwater

Image credit: Jacob Walti

“Do you have a minute to check out this code?” I ask my colleague next to me. Early morning is approaching midday. I’m on my second or third cup of coffee and I’m about to dive into implementing a new feature and I reckon it would be nice to bounce ideas.

With coffee in hand, my colleague considers the code in my editor.

{% for event in coursesToShow.order('startDateTime asc') %}
  <article>
    … code for displaying events …
  </article>
{% endfor %}

“I’m working on the conference page which will display all the events,” I explain. The goal now is to display only the events that will happen in the future. Furthermore I would also like to add a show older button at the bottom of the event list to show older events.

“Hmm. I believe you can do this within the loop,” my colleague says and types out a suggestion.

{% for event in coursesToShow.order('startDateTime asc')
 if event.startDateTime >= now %}
   <article>
     … code for displaying events …
   </article>
{% endfor %}

Profit! With if event.startDateTime >= now in place, the code nicely fetches events that are in the future.

“Thanks! I think I have what I need.”

Since I now know about that neat trick with the if-clause within the loop, I go about hammering out the code for showing the old events as well.

{% set showOld = false %}
{% if craft.request.getQuery('includeEvents') == 'earlier' %}
  {% set showOld = true %}
{% endif %}
{% for event in coursesToShow.order('startDateTime asc')
   if event.startDateTime >= now || showOld %}
   … code for displaying events …
{% endfor %}

“What are you writing there?”

From the sideline my colleague cannot help but inject, “that’s way too verbose. Why not use a ternary statement instead?”

“Ternary statements are not my favorite,” I retort.

“Oh come on. Ternary statements aren’t all that bad. Give it a try.”

{% set showOld = craft.request.getQuery('includeEvents') == 'earlier' ? true : false %}
{% for event in coursesToShow.order('startDateTime asc')
if event.startDateTime >= now or showOld %}
 … code for displaying events …
{% endfor %}

“Instead of simply checking for the value earlier in the request, you could use a date instead. That way you can provide a more fine-grained control of what periods of events you want to show.”

“That’s totally possible, but that kind of fine-grained control is not on the plate yet.” Yes, we could invest time at this point to create a very configurable loop for listing out events according to all sorts of user-defined criteria. But that would introduce complexity and we don’t even know if it would be a user need.

Still a bit unsatisfied with the code, I poke at it some more.

“How about this?”

{% set showOld = craft.request.getQuery('includeEvents') == 'earlier' %}
{% for event in coursesToShow.order('startDateTime asc')
   if event.startDateTime >= now or showOld %}
   … code for displaying events …
{% endfor %}

There are times where I’d happily use a ternary statement, but for this specific implementation it turned out that it was not needed.

In the end the customer said that they had no need for showing older events. After some more coding and code reorganizing, this became the current implementation for listing out events. With time it will probably have to evolve again as most code eventually has to.

{% for event in coursesToShow.order('startDateTime asc')
 if event.startDateTime >= now %}
   {% include "shared/eventListEntry.module" with {
     event: event
   } %}
{% endfor %}

Find a place where you can talk code #

It was just a fifteen-minute conversation but valuable for a number of reasons. Firstly, I was able to get started implementing my feature right away. I might have found the solution by reading the documentation, or I might have wasted time exploring alternative approaches. Secondly, having my code challenged resulted in a better implementation. Lastly, my colleague and I got to exchange ideas and experiences around a very concrete use-case.

I’m fast approaching a year working at Netlife Research Bergen now. And one nice thing I’d like to highlight is how I can ask my colleagues for advice without fear of being ridiculed. What’s more, I can give feedback without colleagues shunning me afterwards.

After having experienced workplaces where constructive critique is neither valued nor nurtured this is like tasting air after suffocating underwater.