You may have heard of the term “Code Golf” used before in programming competitions or programming challenge sites like Code Signal (formally known as Code Fights). But what does this term mean? And what’s wrong with it?

So what is it?

Code Golf refers to attempting to solve a problem using the least amount of characters possible i.e. to have the smallest source code. The term is derived from the sport golf where the player’s goal is to hit their ball in the hole using the least number of shots possible. In Code Golf the developer that solves the problem (usually writing a function) using the fewest number characters possible wins. Usually Spaces and line breaks don’t count as characters, just so that judges can actually read the solution properly. As for everything else, the goal is to use as few characters as possible to solve the problem provided.

Bạn đang xem: The new razer gold & silver

Code Golf is not limited to just one specific programming language. You can play Code Golf using any programming language you want as long as it gets the job done. However, you cannot compare a solution in one language to the same solution in another language in Code Golf. That’s just unfair, code golf competitions should always only compare solutions written in the same language.

An Extremely Simple Example:

Imagine you are given the task to write a function that will return the sum of two numbers. Since I’m a JavaScript guy, I am going to provide the logical solution that I would provide in a job interview here below:

function sumOfTwoNumbers(number1, number2) { return number1 + number2;}As you can see I’ve written a function that solves the problem which is clear and concise. Any developer can understand it very quickly without having to question their decisions in life that led them to becoming a developer. This solution consists of 64 characters – remember that spaces and line breaks do not count!

Now, we do live in a modern day where modern JavaScript is a thing. This gives us some fabulous ES2015 (AKA ES6) features that we can use to simplify this function which will help us win the ultimate Code Golfing challenge. So let’s give that a shot below:

const sum = (num1, num2) => num1 + num2;Only 32 characters in this solution, literally half of the amount of characters use for the previous solution. Great! So we’ve simplified our function to all fit on one line and our fellow JS developers still understand what’s going on without questioning their sanity – provided they’re fluent in ES2015.

But we’re playing Code Golf and that filthy overachiever developer friend of yours has gone and removed even more characters – thus coming up with this even shorter solution below:

const x = (a, b) => a + bThe bastard even left out a closing semicolon because it still gets the job done and that semicolon was a waste of a character. And there we go, your filthy friend wins the code golf competition with 17 characters! Congrats you monster…

What good does Code Golf serve?

Before I go on my rant about why Code Golf is a terrible concept to celebrate. Let me point out that it does have it’s perks.

The first argument that anyone would tell you is that smaller source code is good for performance. Less code means a smaller file size which means downloading the file will be quicker. But these days we have code minifier tools and transpilers which makes this argument redundant.

A better argument for Code Golf is that it often encourages developers to think out of the box and make use of language features that are less commonly used, thus making them add tools to their developer toolbox. Forcing developers to learn and expand on their skills is definitely a positive outcome.

So there we go, there’s some good that comes out of it. But is it good enough?

Why does it suck?

Here’s a real world scenario that actually happened in my working career a few years ago.

Solutions Architect to Me:“Hey Barry, Good Job on implementing that complicated feature with ‘Other Developer’. Just one thing though. I was reviewing the code you guys wrote and I came across your complicatedFunction function. I don’t really understand what’s going on there with its arguments since they’re labeled p, v and w (the function was written like this: complicatedFunction(p, v, w) { ... }). Could you please provide understandable variable names to complicatedFunction so that we don’t get confused when we come across this later?”

Me to Solutions Architect:“Oh yeah you’re right! That’s a weird one. Let me ask ‘Other Developer’ what his thought process was since he wrote it yesterday. I’ll update it with more readable variable names as soon as I can identify what their purposes are.”

Me to “Other Developer”:“Hey dude, the big boss and I don’t understand how complicatedFunction works since the arguments you passed to it are just random characters. Could you let me know what the variables p, v and w are meant to represent?”

“Other Developer” to Me:“Hey dude, the big boss and I don’t understand how complicatedFunction works since the arguments you passed to it are just random characters. Could you let me know what the variables p, v and w are meant to represent?”

This was an absolutely frustrating situation to be in because I had my solutions architect expecting this to be a quick and simple 5 minute fix. But no, “Other Developer” literally could not remember what the variables were meant to represent. And when you’ve got a big complicated function with randomly named variables used all over the show while struggling to keep track of everything. You get a group of sad developers that don’t understand why their code works.

This isn’t a good thing…

Code Golf in the Workplace

The scenario above is a clear example why certain aspects of Code Golf are not cool! “Other Developer” thought he was being a legend by making his complicated function look small and compact without caring about it’s readability.

Xem thêm: Chơi Game Bán Hàng Quần Áo, Game Cửa Hàng Bán Quần Áo: Wedding Shopping

Regardless of whether or not you are working with others. It just makes sense to write code in a way that it can be understood quickly rather than trying to figure it out every time you come back to it later. People spend 10 times more time reading code than actually writing code. So from a productivity point of view, writing clean, maintainable and understandable code is a huge advantage in the workplace and on personal projects.

Ironically, writing cryptic code it will bite you in the butt later. The amount time it takes to decipher code golf formatted code adds up. And we all know that time in the development world is a big deal. If you can save time accomplishing business goals, you could justify raising your hourly rate or pursuing that promotion you’ve had your eye on for a while.

Code Golf in Interviews

In a job interview, the interviewer wants to see your ability to come up with solutions that are actually understandable. Your interviewer is probably going to be working with you if you get the job. They want to be sure that you’re going to make their life easier by being around, not more complicated!

The goal of a whiteboard interview test is to test if you can come up with a high performance solution. Cutting characters off your source code isn’t the what they meant when they asked you to implement the most efficient solution.

If they tell you that your variable names should be simplified to single characters then it’s probably a test to see if you care about writing clean code. Now if that wasn’t a test then you probably don’t want to work there anyway. You certainly wouldn’t enjoy your life there if that was the case.

In Practice

Remember my simple example at the top of this post where we wrote a function to take two arguments and return the sum of them? In the real world. These functions need to be called somewhere right? Let’s see what that looks like in each of those examples mentioned above. We’ll demonstrate it in order from filthy code golf winner to respectable code gold loser.

Filthy Code Golf Winner

x(1, 2); // returns 3What is this? I pretty much have no idea. I need to run the function to see what it does. And even then, I don’t know how many arugments to provide to the function without checking out the actual function.

Code Golf Second Place

sum(1, 2); // returns 3This is good. I can clearly read the function and see that it has two arguments in it. Given the function’s name sum, I can naturally assume that it will return the result of the two arguments added together. However, if the function was given to me with no context. I might still be confused, since I’m not really sure how many arguments the function is expecting.

Xem thêm: Tải Và Chơi Long Vũ 3D Trên Pc (Máy Tính) Và Mac Bằng Giả Lập

Respectable Code Golf Loser

sumOfTwoNumbers(1, 2); // returns 3This is excellent! Even if I didn’t know how many arguments to enter into the function, the name sumOfTwoNumbers would certainly help clarify that. It’s obvious that this is a function that takes two numbers as an input and adds them together.