Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How complex should code be?

People also ask

What is the complexity of a code?

Defining Code Complexity The complexity of a given piece of code has to do with just how complicated and unwieldy it is for a developer to understand. This idea makes intuitive sense. For example, the picture at the top of the article is way more complex than, say, a picture of a single rose on a white background.

What is the most complex code?

Malbolge. Malbolge was invented in 1998 by Ben Olmstead. This esolang is considered to be the most complicated programming language. It is said that the author of the Malbolge programming language never wrote any program using the language.

What is a complex in coding?

Programming complexity (or software complexity) is a term that includes many properties of a piece of software, all of which affect internal interactions. According to several commentators, there is a distinction between the terms complex and complicated.


As Einstein said:

Make everything as simple as possible, but not simpler.

It applies to code as well as to physics.

Use your judgement - will it get easier to maintain? Often by reducing large if/else messes into something smaller you remove corner cases that didn't need to be there, preventing bugs that may have popped-up in the future. Of course by reducing a clear set of conditions into an obscure twist of boolean logic that only happens to work, you may be making things much more difficult to maintain when something changes.

EDIT:
With the case you cited, if the formula is going to work, then it's probably the better option - you can even leave a comment citing the source. It's entirely possible though, that the formula was there before, but was dropped to allow a specific case to be worked around. This is the sort of thing comments in your versioning repository should help with.

Since no one's posted a link yet, here's a description of the PID algorithm he's referring to.


Code is written once, and read 10 times. So you should try to make it as easy to understand as possible.

Also, debugging is much harder than writing code. So how can you ever debug your code when you already put all of your brainpower into writing complex code?

Just try to follow The Three Laws of Software Development:

  1. A developer must write code that creates value.
  2. A developer must make their code easy to maintain, except where such expenditure will conflict with the first law.
  3. A developer must reduce their code to the smallest size possible, as long as such reduction does not conflict with the first two laws.

Robert C. Martin uses this comic as an introduction to his book Clean Code:

The only valid measurement of code quality: WTFs/minute
(source: osnews.com)


Remember that code should primarily be understood by humans...compilers take care of making the computer understand.


The level of "complex" can be a little dicey here, but as long as the algorithm doesn't require a PhD in mathematics to solve,

I'd say go ahead and use the algorithms. Make sure to put a decent level of documentation about the name of the algorithm, and maybe a short description of how it works or a reference to a document about it.

It will keep the amount of code down, hopefully give a little extra performance to the application, and it will hopefully encourage a few programmers around you to learn some new things to add to their repertoire. Plus there's always the chance that a programmer that does know these things won't be coming in later on.


Good old quote....

Any fool can write code that a computer can understand. Good programmers write code that humans can understand

  • Martin Fowler