Can you make small changes in an unknown language?

If you’re tasked to make minor changes to an existing program in a programming language that you’ve never used before, can you do it? Consider the following fictitious interview question:

You’re writing code in a programming language that you’ve never used before. You have no documentation about the language except the other code. Complete the following function: …

Let’s make this easier. Can you do it if you’re only slightly unfamiliar with the programming language?

When I learned new programming languages, I looked for how to:

  • declare and initialise variables
  • do loops (such as for and while in the C family)
  • do conditionals (the if statement)
  • do multi-conditionals (switch for C#, select case for VB.NET) if available
  • take in input and print output
  • write and use functions

You might have a different list, and that’s ok. It’s how I look for patterns.

With that in mind, if you had sufficient lines of code, you should be able to figure out what the code did. Even if you’ve never seen the programming language before. Then you could make the minor changes you’re tasked with some level of confidence.

It’s not like you code in assembly, right? … Are you?

I don’t have a code sample to show you. Alright fine, I’m too lazy to think up one… Do you have something to show? Post in the comments, and I’ll rack my brains on it…

  1. Ben Barden

    Depending on the language, I might be able to do it, but creating new code based on existing code has a couple of drawbacks. First, you may replicate any faults or bad practices that are present in the existing code. Second, if you’re writing something that isn’t already written for the system you’re working on, you might have difficulties finding other code that will accomplish what you want.

    The second point really depends on what you’re doing, but what if – to give a specific example – you need to access the Windows API in VB6 and no other function has done that yet? You can’t do that in “pure” code.

    On the other hand, writing code in the same style as existing code keeps things consistent, even if it continues with bad practices that have been used in the past.

  2. Vincent Tan

    Replication of existing faults is unavoidable if you just use the existing code as a learning template. I sometimes come across this situation when maintaining legacy code. My colleague and I discussed on it, and the best solution, other than rewriting large chunks of hard-to-find or hidden-in-obscure-places code, is to let it be.

    Our rationale? Be consistently wrong. This way, if we ever put our minds to go on a mass code correction rampage, the editing process is idiotically and mechanically consistent.

    As for your second point, it is hard to write something new with only a small code sample, a subset of its “true” language. In your example, I’d probably go learn the Windows API and use an existing system function if there is. I wouldn’t know I have to write a new function if I didn’t learn the ins and outs of the API.

    I was actually thinking of minor changes when I wrote the post. Code changes that can be extrapolated and intuited from surrounding code. This tests one’s understanding of basic programming skills and grasp of business logic.

Comments are closed.