Ternary Operator Guidelines

Short conversation at the office today. I declared that I wasn’t a big fan of ternaries and that I typically used them only for simple one line statements and almost never in returns and never nested. One of the developers challenged me on why the ternary operator is frowned on and I admitted I didn’t have much in the way of data. After some digging on the interwebs.

Github’s style guide has the most common approach to the ternary:

“Avoid the ternary operator (?:) except in cases where all expressions are extremely trivial.”

Github style guide

Support for using ternary’s in return statements is generally supported as well. There is general agreement that nesting ternaries is a bad practice. Static analyzers like Rubocop and Checkstyle include checks for nested ternary operators.

Historically ternary operators bothered me because I missed them sometimes when scanning through code. If and else are hard to miss on a quick scan. I’ve adjusted to using them for one simple one liners as the terseness is nice. Nesting them strikes me as abuse, much like single letter variables or predicates methods that have side effects.

Lucky Rubber Duck

I’ve had a pretty good run of luck grabbing a colleague to look at my code. The last four or five times it’s been a classic rubber duck case.

Me: So here I’m stubbing out the partials in this view spec since I only want to test this conditional logic.
Colleague: OK.
Me: So it keeps failing when I stub the partials and pulling the in anyway even though it works in similar cases. See I’m just pulling in ‘/users’.
Me: Oh, I probably don’t need that forward slash in users.
Me: Thanks again.

If you don’t regularly grab someone to look over your shoulder on a frustrating problem, you might want to start the practice. You can even use a real rubber duck and explain it to them, but for me it seems to work better with a real person.

Enterprise Architecture in An Agile World

In fact, your own ability to keep up with the latest trends have suffered because only you know how to keep things working. To help manage time, you have implemented universal standards and tried to funnel requests to architecture review boards or other planned meetings. Developers routinely work around the system, complaining that process holds them back, but you know that these things are there for the good of the company so you reinforce the policy to try to keep control.

Kevin Hickey

This is a good general description of several EA groups I’ve worked with in past companies. The EA is often far away from the day to day of development and looks to keep control by implementing layers of controls. Not much different then change control boards that attempt to stop change by adding a meeting/paperwork tax to any request. This is where so many EAs fail to add any value and end up becoming another impediment to producing working software. I hope this is changing as Agile has moved to the mainstream, but I fear it is not the case.

Rerun Jenkins Builds

Some consultants at our office needed to be able to roll back a Jenkins project that deployed to staging and production sites in case of an issue. The project had been setup to allow for a typical git checkout and then ended up using a php build tool, phing, to essentially rsync up to the servers.

Turns out ‘Parameterized Builds’ was able to resolve this. It took just a few steps:

  • Click ‘Configure’ on the project.
  • Check ‘This build is parameterized’.
  • Add a String Parameter with the name GIT_COMMIT and a default of ‘master’
  • Under Git > Branches to build, add the parameter as $GIT_COMMIT
  • Save

This allows you to specify the commit SHA for each release. If you need to roll back you just run it with the specified commit SHA. This can be configured to use git tags as well. Inspiration of this came from this post.

Javascript Goes Back to Class

Not long ago at a user group I saw a strange piece of sample code like this on an overhead projector:

1
2
3
4
5
6
7
8
9
10
11
12
class Person {

  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  fullName() {
    return this.firstName + ' ' + this.lastName;
  }

}

I chuckle a little bit inside. I’ve heard plenty of arguments over the years that Javascript’s prototypical inheritance was the right way to do things and trying to force traditional OO on Javascript was doing it all wrong:

If you’re creating constructor functions and inheriting from them, you haven’t learned JavaScript. It doesn’t matter if you’ve been doing it since 1995. You’re failing to take advantage of JavaScript’s most powerful capabilities. — Eric Elliot

It turns out ECMAScript 6 has officially added class style OO to the language. So the needs of many occasional Javascript developers to have a more familiar looking construct that would be at home in Java, C#, or Ruby eventually won.