Blog of Rob Galanakis (@robgalanakis)

There’s idiomatic, and there’s just being respectful

I work in mixed language environments. Python, C#, C++, and more, can all make their rounds. It isn’t uncommon to have someone focused on C++ have to write something in another language, and it isn’t uncommon that I come across their code some point in the future.

It is easy to learn a language’s syntax but difficult to learn its idioms. Good luck trying to explain what ‘pythonic’ means to someone who is new to python or programming! So I forgive the transgressor when I see non-idiomatic code.

Usually.

There are some errors I find unforgivable. Errors that indicate a complete lack of understanding of the platform you are writing on. Errors like this (C#):

var foo = new Foo()
if (foo != null) {...}

Creating an instance is probably the most basic operation you can perform in an OO language, and the author clearly did not understand it.

Another unforgivable type of error is when someone tries to fix a bug but does not bother to understand what’s actually going on.

class Foo {
private bool _somevar;

...
}

There was some bug in the code somewhere, I can’t remember what. A developer changed ‘private bool _somevar’ to ‘private bool _somevar = False’ and declared the bug fixed (spoiler: it wasn’t).

Probably the best example comes from memory management, as the least understood things in programming tend to:

try { someUIControl.SetText(someGiantString); }
except OutOfMemoryException {
someUIControl.Clear();
GC.Collect()
someUIControl.SetText(someGiantString);
}

The only thing this did is change the stack trace. The problem was due to a .NET garbage collection implementation detail- the Large Object Heap and huge strings- and the ‘fixer’ just tried something every authority tells you not to do, which is catch an OOME.

If you’re going to leave your domain to write code in another language- I applaud you. It can show an endeavouring personality! But please have some respect for the language you are writing in- read a book, read a blog, ask for help. It’ll make you a better programmer, I promise.

4 thoughts on “There’s idiomatic, and there’s just being respectful

  1. MrPingouin says:

    Hmmm… So what’s the point in the first example ?
    It should be catched as a NullException ?

    1. It can never be null, the guard is always True.

  2. Phill Van Horn says:

    I think the point in the first example is that a c++ person clearly wrote the code because they are checking for successful DMA, but C# uses garbage collection and the new call will never return null. It’s evidence that the writer has no idea of even the basic differences between C# and C++.

    Although, they probably weren’t a great C++ programmer either because new is practically a dirty word in C++. var foo = new foo() actually calls the constructor twice… unless foo is a pointer.

Leave a Reply