Over a million developers have joined DZone.

Code Through the Looking Glass — Finding Today's Log File

DZone's Guide to

Code Through the Looking Glass — Finding Today's Log File

Spotting good code is usually much harder than spotting bad code because a really successful piece of code is going to be boring. It does what it's supposed to do and nothing more. And, then there's bloated code like the following...

· Performance Zone
Free Resource

Transform incident management with machine learning and analytics to help you maintain optimal performance and availability while keeping pace with the growing demands of digital business with this eBook, brought to you in partnership with BMC.

You might have noticed that we are looking for more people again. Mostly because I gripe about some of the bad ones here every now and then.

One of my absolute requirements is that I want to read a candidate's code before hiring them. Some of them have made significant contributions to open source code, but a large number don't have any significant body of code that they can show. That is why we ask candidates to send us a coding test.

We have had people flat out refuse to do the coding test, and some who thought that the questions were incredibly hard and unrealistic. We had people who sent in code that was so bad it caused migraines, code that wouldn't compile, read a 15TB file (16,492,674,416,640)! times. And, just to clarify, that is factorial (16492674416640). I don't know what the actual value of this number is, but it is big.

The nice thing is that you can usually tell right away when the code is bad. We also play a game called "guess the candidate's background". We have about an 89% success rate there*.

Spotting good code is much harder because a really successful submission is going to be boring. It does what needs to be done, and that is it. Our most recent hire's code submission was so boring, we had to analyze it using our standard metrics to find issues (our standard metrics are for production code running in a high-performance DB for months on end, not the same metrics we evaluate code submissions).

And then… then there is this candidate, whose code is so unique that I decided to dedicate a full week to explore it. The code looks good, it's documented, and there are explanations that show what is going on and they are going in the right direction on the surface of it.

And, then there is the devil in the details. I have quite a lot to write about this particular submission, but I'll just start with the following:

//Find today's log file in the directory
public string LogFileFinder()
    string[] files = Directory.GetFiles(LoggingAggregation.Program.GlobalVar.path, "*.log");
    for (int i = 0; i < files.Length; i++)
        int slash = files[i].LastIndexOf(@"\");
        int dot = files[i].LastIndexOf(".");
        string fileName = files[i].Substring(slash + 1, dot - slash - 1);
        string fileDate = DateTime.Now.ToString("yyyy-MM-dd");
        if (fileName.Equals(fileDate))
            return fileName + ".log";

    return null;

Now, another way to write this code would be:

Path.Combine(LoggingAggregation.Program.GlobalVar.path, DateTime.Now.ToString("yyyy-MM-dd") +".log") 

I literally stared at this piece of code for several minutes, trying to figure out what is actually going on there. I wasn't able to.

As an aside, we sent the candidate a rejection notice, along with a few pointers to some of the more egregious things that were wrong in the code.  They know how to code, it is just that it goes sideways in the middle.

* The game call to guess the candidate's default language settings. That is, someone who writes C# code, but has been brought up on C, and so has a C-style to the code.

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.


Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}