DEV Community

Cover image for HackerRank Is Teaching You to Write Terrible Code
Devon Campbell
Devon Campbell

Posted on • Originally published at raddevon.com on

HackerRank Is Teaching You to Write Terrible Code

In case you’re not familiar with HackerRank, it’s a platform for practicing coding, and, increasingly, it has become a platform for companies to test their engineering candidates. This is why it’s a problem that they’re encouraging people who practice with them or interview using their platform to write bad code.

function Rectangle(a, b) {

}
Enter fullscreen mode Exit fullscreen mode

This is the starting point of a function they ask you to write for their 10 Days of Javascript. Creating a rectangle object is day 4 of those 10 days. Most coders will take this starting point as it is and fill in the space between the brackets. That’s probably what HackerRank expects you to do with their problems: leave the starting point they’ve given you as-is and fill in what’s missing.

If this is all they ever gave you, you could probably work out that a is one side of the rectangle and b is the other side. If you read the problem description, they’ll tell you explicitly that a is the length of the rectangle and b is the width.

Why All the Secrecy?

Batman

Batman needs to keep his identity secret. He spends his evenings breaking the law. Even though he’s doing it for good, he could still be arrested. In order to stay on the streets delivering vigilante justice, he can’t let anyone know that he is actually Bruce Wayne.

But, as far as I can tell, a and b don’t need to keep their true identities (the length and width of the rectangle) hidden. In fact, all they’re ever going to do in this function is be the length and width of the rectangle. That means they don’t even need to be a and b.

The names length and width are not reserved by Javascript. So, why not just call a spade a spade? Why can’t the length be length and the width be width?

Yeah, but What's the Harm?

In this HackerRank exercise, you’re going to write a few lines of code at most. Not to mention the only two logical parameters to create a rectangle and its length and width. You’re probably not going to permanently lose track of what a and b are. Even so, there’s still going to be a split second where you don’t know what a and b are.

This might even make you feel cool. Like you’re part of a fun club with a secret code. Muggles look at a and b and throw their hands into the air, but you… you can decipher it in a fraction of a second. It feels like a superpower. And if HackerRank does it like this, it must be right right way… right? !😰

In your career, you’ll never write an application that’s this short and that does so little. Imagine the split second it takes to remember what a and b are in the Rectangle function. Now put that in a context where the meaning of the parameters are not so clear. Maybe you have a function that displays an alert on your page. It takes three parameters: a, b, and c. Any idea what those parameters are? Yeah, me neither.

Now take this new naming issue and blow it up to an application with 10,000 lines of code. Each function has parameters with cryptic single-letter names. Since you learned this is OK, your variables are probably not thoughtfully named either. You’re going to spend most of the time you work on this application just trying to figure out what it does. I pity anyone else who has to pick this app up and work on it. I hope they have plenty of aspirin.

You might think I’ve cherry picked the only example or maybe the worst example of this on HackerRank. You’d be very very wrong. Not every exercise uses this opaque variable and parameter naming, but many do and some are even worse than my first example. Here’s your starting code for another indecipherable exercise:

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the minimumBribes function below.
function minimumBribes(q) {


}

function main() {
    const t = parseInt(readLine(), 10);

    for (let tItr = 0; tItr < t; tItr++) {
        const n = parseInt(readLine(), 10);

        const q = readLine().split(' ').map(qTemp => parseInt(qTemp, 10));

        minimumBribes(q);
    }
}
Enter fullscreen mode Exit fullscreen mode

The variable naming in that main function is seriously making my head spin. t? tItr? n? q? qTemp? Why are these practice exercises not more readable?

Hey there. I help people like you start their web development careers. It starts with identifying a goal. Take my free course to get started! 🎉

Code Is For Humans

You may think that code is for computers. It’s not. Code is for humans. If it was exclusively for computers, we’d have no need for a high-level language like Javascript or Python.

Take this to heart as you’re writing your code. Write it to be read by someone other than you. Variable, parameter, and function names should encapsulate as much context as possible to help the reader understand what they’re reading. a is bad. length is good. lengthInInches is even better (if, in fact, you’re expecting the measurement to be in inches). If your code changes, make sure you update your names to reflect what they now represent.

It’s easy to look at HackerRank and, because they’re a huge, well-established, and, if the number of interviews administered through them is any indicator, well-respected site, assume they’re starting you out with good code. Unfortunately, that’s not the case.

If you practice on HackerRank, do yourself a favor and start your exercise by refactoring their terrible meaningless names to something that conveys the meaning and context needed to read the code. When you’re working on projects outside the platform, make sure their bad habits don’t poison your code and give you a reputation as a developer who writes indecipherable code.

By writing your code for the next developer coming behind you, you make it easy not only for other developers to work with you but for your clients to re-hire you and recommend you to other companies who need your help.

Latest comments (34)

Collapse
 
mikeytown19 profile image
Michael Harvey

Doing some of the Javascript challenges, the instructions are so terrible. I cannot even follow what they want me to do, it's like they need to be re-written. They want me to print out my result, or assign a member variable and the debugging experience is trash.

Collapse
 
bfmcneill profile image
Ben McNeill

Hacker rank taught me that when you are given a question and you write code to solve the question your code still sucks because you don't sort your data to pass the tests even thought they don't ask you to sort the data in the question.

Collapse
 
clemo97 profile image
Clemo97

I also found their wording of the instructions to be too difficult to understand.

Collapse
 
mikeytown19 profile image
Michael Harvey

I think that understanding the instructions is harder than the actual coding! its honestly terrible.

Collapse
 
colinmcgraw profile image
ColinMcGraw

I just ran into this issue myself after my first job hunt in 5 years, and even found that some of the answers for these type of sites are “wrong”. For instance, a problem asking you to ensure unique file naming given an array of file names:

FileName
FileName
FileName(1)

...the problem suggests appending “(#)” to the end. A more experienced programmer would write code to deliver a solution that produces the output:

FileName
FileName(2)
FileName(1)

...because it minimizes file name changes as a convenience to the user. However, the expected solution is actually:

FileName
FileName(1)
FileName(1)(1)

...and by the time you realize they asked for that contrived form of answer, the timer runs out and you’re left with an “incomplete” solution.

Of course, in the real world, a good programmer would question the requirement as written, but sites like HackerRank encourages blind, unquestioning adherence to written instructions even if it’s bad design.

Collapse
 
dyland profile image
Dylan Davenport • Edited

What is the reason behind HackerRank using Node.js instead of vanilla JS? The first time I saw that I was thrown off but I'm curious as to the actual reason. If there is one. It just seems overly complicated.

 
sz332 profile image
Zsolt Sandor

Totally agree. I have a CS (actually msc) background and after 14 years working for an international company I wanted to move along and find a new job (yes, I am not the typical jobhopper). To my greatest surprise most of the companies here asked for HackerRank tests. I started working on hacker rank exercises and eventually passed them. However, it was like practicing for an exam, and totally removed from real-life problems. Now, either every company needs to solve complex algorithmic problems on a daily basics (which I am pretty sure they don't) or this is some kind of quick test just because it seems easy for the recruiters. The code I had to write in hacker rank was ugly and would never pass any of my own code review for sure.

Collapse
 
farahanjum profile image
Farah Anjum

This is so true!! I remember joining my first job and being surprised when micro optimisations, clever tricks that save lines and obscure logic that you think is super cool are not applauded, and rather discouraged.

Being in college, hackerrank and countless other similar platforms taught me to value the wrong things and continue to do so. Smh

Rant over.

Collapse
 
mongopark profile image
Ola' John Ajiboye • Edited

One thing I also noticed as someone who has appplied to hundreds of jobs. 100% of the companies that have used Hackerrank on me never even followed up about the solution. If Hackerrank was the first step. You'd expect to discuss some of your implementation and maybe extend functionalities just to confirm you actually wrote the code. Never has this happen.NEVER. I mean not just once. They usually just move you to the next stage and pretend there was never an Hackerrank test.

It means employers just used this to judge if you can write any code at all. The problem is I could have employed anyone to write the code.Why don't you just take a cursory look at my github. That gives more info than Hackerrank.

There is literarily no Haackerrank problem you can't google if you search enough. Except few ones that are custom made by companies. It is a waste of everybodys time.

Collapse
 
dyland profile image
Dylan Davenport

The problem is I could have employed anyone to write the code. Why don't you just take a cursory look at my github. That gives more info than Hackerrank.

This 100%! If my resume, cover letter, portfolio, projects, github, linkedin, etc. aren't enough then what are we even doing?

The hiring process really needs to take a step back and reevaluate itself. What's worse is the fact that it's a well known issue and nobody does anything...

Collapse
 
sz332 profile image
Zsolt Sandor

The best part is that if you google you can find the tests of the company/job you apply for. They don't even bother to iterate the questions.

Collapse
 
brunooliveira profile image
Bruno Oliveira

As a former competitive programmer, I can't completely agree with the premise of the article for a simple reason:
Hackerrank and similar websites are NOT teaching you code.

The purpose of those websites is to test for and practice algorithms and algorithmic thinking and, last time I checked, the code editor there allows you to delete everything entirely and write your own.
Their premise is that if the core of the problem is to test your knowledge of say... Dijkstra, the way you'll parse your input files and read input and write output are a distraction with respect to knowing whether you can or not write a Dijkstra based solution.
Some websites choose to let you upload your code entirely or others give you these placeholders. I'd dare say that competitive programmers would be smart enough to never take that code as production-ready at face value.
Also as a sidenote, I never, ever needed to read/parse files in a professional setting in any non-trivial way besides reading the file as a list of lines.

Collapse
 
raddevon profile image
Devon Campbell • Edited

Here's the problem with that: junior developers are using HackerRank to practice because they're being sent there by recruiters and companies to complete coding problems as part of job interviews. If that's the site that gets them the job, it seems natural to practice there as well.

It's easy for you and I to look at this with a more robust perspective and say, "Well, of course this isn't the code you're supposed to write in production," but that's not made clear to a new developer. They don't have the tools yet to be critical of the efficacy of this code in other contexts. Competitive coders will have those tools, but HackerRank does nothing to keep out those who are not competitive coders and it doesn't make it clear that the platform is only intended for them or even just that the code is not production-quality.

If I were inexperienced, interviewing for a job and they sent me to test for the job on HackerRank, I would assume the code used on HackerRank reflects the code they will want me to write on the job. Why would they want to test whether or not I can write code they won't accept from me if I'm an employee?

So, HackerRank may not claim to teach people to code, but they are teaching people to code, for better or for worse (in my opinion, for worse).

Thanks for reading and sharing your perspective, Bruno.

Collapse
 
wojtekidd profile image
Wojtek Cichoń

Thank you for this post. I'm 3 months into a Python Bootcamp and started doing a basic Python path there. The code they suggest in their basic arithmetic tasks is really weird and questions all I've learned so far. It's good to hear that I'm not the only confused person around.

Collapse
 
brunooliveira profile image
Bruno Oliveira

Wow, thank you so much for your reply! :) (I assume you're also far more experienced than me, so this is a great perspective)
I agree with what you say indeed, junior devs or people just "going at it for the job", even if they're good at algorithms, can definitely pick up very bad habits from that scaffolding code that lies there.
Unfortunately competitive programming is literally a competition so writing unsafe, sluggish but blazingly fast I/O routines and taking some dubious shortcuts it's the norm there (top competitors using C/C++ would have hundreds of lines amongst macros and custom I/O routines just to shave off ms on reading and processing input) and unfortunately some junior devs can definitely approach those platforms completely misguided and start their career with the left foot there... Ultimately experience and long exposure to production code will fix all of these quirks but I agree it's a big problem with more and more companies "outsourcing" code challenges on these platforms nowadays. Thanks a lot for your reply

Collapse
 
kamilliano profile image
kamilliano

I use sometimes hackerrank but never thought about this. In everyday code, I am trying to be explicit and least ambiguous software as though as if I was woken up at 12 at night and had to debug it. But I noticed that comes with experience I think and after eating a few humble pies myself and writing crap code :).

Collapse
 
official_alkin profile image
Alkin

What's the best language for programming softwares?

Collapse
 
raddevon profile image
Devon Campbell

The least helpful and also only accurate answer to this question is, "It depends." If you want to write software that runs in a web browser, you have no choice but Javascript. If you want to write native mobile applications with full access to device APIs, Swift or Objective C for iOS or Java for Android. If you want to write software that automates some task, Python might be a good fit. If you want to write a web API, you've got lots of good choices.

Depends on the kind of software you want to write and your priorities.

Collapse
 
bugb profile image
bugb • Edited

IMO, if you want to learn algorithm in pro way, you should go with hackerrank, topcoder, codeforces, kattis, Atcoder, SPOJ, codewar ...

If you want to learn code as a beginner you can learn from pluralsight, ... (sorry I do not learn this way so I do not have much experience about site like this, I often learn from language's docs instead)

I love algorithm, code golf (one line, unreadable code) but in my production code, I write everything pretty clear so anyone can get it.

Everything exist for a reason, you may wonder but almost winners in Olympiad for Programming or Google CodeJam, Facebook hacker cup have profiles in the sites that I mentioned above with highest rating.

You can get benefit from them if using in smart ways and open-minded, and you can leverage your critical thinking in the next level.

Btw: my favorite sites is: dwitter.net/ and codegolf.stackexchange.com/

Collapse
 
bradtaniguchi profile image
Brad

I say the best code is "dumb code".

Code that is so blatantly obvious, so simple, and so boring to read that you don't need comments. I'm not saying this is always possible, but it should always be the goal.

Collapse
 
mongopark profile image
Ola' John Ajiboye

This is the part I hate the most. The problem description is so academic and pointlessly long. I have no CS background and find this off putting.

Collapse
 
michaeldrawe profile image
Michael

I agree I'm the way that anyone writing about software development should be guiding others to be the kind of person that adds finesse to a team, not the one bringing speed bumps to the team. Help others create positive software engineer habits!