Learning From Other Coding Styles

Will learning multiple coding styles make you a better programmer? And, does it make sense to import those coding styles into your primary language?

Learning From Other Coding Styles

In yesterday's article, Learning From Other Languages, I wrote about the benefits of learning a second (or third or fourth...) programming language:

Even if you only write code professionally in a single language, learning the concepts and idioms of other languages will make you a better programmer.

In the comments of that article, reader Huy Truong asked a followup question:

Does coding style count? For example, when naming a function, I use camel case. However, when writing the content of that function, I use snake case. For some reason, I find it much easier to read, but that could be because I first learned how to program in python. Actually, another reason for my preference is that using snake case helps me distinguish between my variables and stuff from VBA. I'm not really sure if it's a good thing though :(

A Two-Part Answer

I'm going to answer two different questions: the explicit question Huy asked–Does learning the coding style of another language make you a better programmer?–and the implied followup question–Is it a good idea to "import" coding styles from other languages?

Will Learning Multiple Coding Styles Make You a Better Programmer?

As I said in yesterday's article, real growth comes not from learning how to write code in another language; it comes from learning how to think in that language.

It's like the difference between porting and rewriting code from one language into another.

A port is simply a translation from one language to another.  In general, the same logic is maintained as much as possible.
Think of a rewrite as a port with a lot of refactoring.  The goal is to take advantage of features of the native language to improve the library in some way (efficiency, readability, etc.)
Deciding whether to do a simple port or a full re-write usually comes down to one question: is there a better way to implement the features of the external library in the native language?

If all you ever do when writing code in another language is try to figure out the equivalent syntax in that language, then you are missing out on most of the benefits.  

In the same way, you'll get little benefit from copying a coding style from one language into another without understanding why the styles are different.  In some cases, you might even make things worse.  An example of one such bad practice would be adopting the naming conventions of a case-sensitive language, like C#, in a language that is case-insensitive, like VBA.

So to answer the original question, YES, learning multiple coding styles will make you a better programmer...but ONLY if you understand the reasoning behind each style.

Is it a Good Idea to Import Coding Styles from Other Languages?

It can be, but....

One thing you need to be careful about is every language tends to have its own style.  Some languages enforce that styling more than others.  For example, Go has gofmt, Python has the PEP-8 style guide, and Perl has random keyboard mashing.

Fun Fact! FJAFJKLDSKF7JKFDJ is actually an excerpt from a 10-line Perl script that takes the full text of a Beatles song, rearranges it into iambic pentameter, translates it into Aramaic, then publishes it as a LaTeX file that follows the MLA style guide.

While it's OK to bring in small bits of styling from other languages where it enhances readability, in general, you're going to want to stick to what everyone else is doing.  So long as "everyone else" isn't using Systems Hungarian, of course.

The key to any coding convention is consistency.  

And, as with code itself, stylistic consistency is most important within the narrowest scope.  When I talk about scope as it relates to coding convention, I'm thinking of the following scopes, from narrowest to broadest:

  • A code procedure (Sub, Function, Property, etc.)
  • A code module (class module, standard module, etc.)
  • A project/application (e.g., a single Microsoft Access front-end file)
  • An organization (e.g., Grandjean & Braverman, Microsoft, etc.)
  • A programming language (e.g., VBA, C#, Python, etc.)
  • A language family (e.g., functional, procedural, object oriented, etc.)

In other words, while coding styles may vary greatly within a programming language, they should be very consistent within the same module or procedure.

So, by all means, import coding styles from other languages.  Just make sure that:

  1. You know why you are doing it.
  2. You maintain consistency with existing code.
  3. You use the technique sparingly.

Referenced articles

Learning From Other Languages
Even if you only write code professionally in a single language, learning the concepts and idioms of other languages will make you a better programmer.

Image by Daniel Sampaio Donate if you want (Paypal) from Pixabay