So I'm coding my first projects in C++ and it seems that it takes more effort to make the code "clean", rather than merely work. I.e. it seems as if C++ "allows" to write ugly, but working code.

Which got me thinking,

Can a programming language enforce clean code by design? Are there such languages already?

Also, how is this incorporated as design principles in programming language development/theory? What kind of measures are used?

  • 14
    Lots of languages have tried. None have remotely succeeded in my opinion.
    – user53141
    Commented Apr 4, 2016 at 15:55
  • 5
    Unfortunately this is entirely opinion-based since there is no objective definition of "clean code". Feel free to discuss it in our chat room though, I'm sure everyone in there will have some opinions to share.
    – Ixrec
    Commented Apr 4, 2016 at 16:03
  • 9
    No, you can write FORTRAN in any language. Commented Apr 4, 2016 at 16:16
  • 4
    You are asking if it is possible to idiot-proof a language. As they say, idiots are ingenious.
    – user53141
    Commented Apr 4, 2016 at 17:56
  • 2
    What characteristics does "clean code" have for the purposes of this question? You need to define that, otherwise any answer with any justification could be valid. Commented Apr 4, 2016 at 21:26

5 Answers 5


The main effect that language design has on "clean code" is at the syntactic level. Languages with a lot of shorthands and obscure operators (Perl/APL) lend themselves to "dirty" code, whereas languages with a smaller set of elements (say, Python) lend themselves to cleaner code.

Semantics, however, are a very different animal. There is no way to enforce that the semantics of a language are used in a clean way, particularly because you cannot, as the compiler, know what the user of the language is trying to accomplish. A powerful tool is simply that -- a powerful tool, for good or ill.

At the end of the day, semantics are more important than syntax. It is also the part that is the hardest to figure out as a maintenance developer (e.g. "what does this code actually mean? I see what it does...").

Consequently, I would say that there is no design to enforce clean code, but you can write simple syntax with clean semantics that makes it easier. For better or worse, clean code is primarily a matter of developer knowledge, motivation, discipline and skill.

  • 2
    I would say the type system can go a long way toward enforcing correct semantics. For example, strongly typed languages ensure that variables are assigned values of applicable types. A language that makes types cheap and easy encourages encoding more semantics in types. A strongly typed language makes programmers express intent about type conversion. A language that obscures semantics with boilerplate also makes it harder to reason about semantics. Now as for what "clean" semantics is, it isn't clear. But I would imagine it has significant overlap with correct semantics. Commented Apr 4, 2016 at 21:07

Languages can force or encourage programmers to address certain classes of bugs, which is part of the definition of clean code. For example, various languages do a relatively decent job of addressing:

  • Null pointer exceptions.
  • Shared state bugs.
  • Concurrency issues.
  • Unchecked exceptions.

That only gets you part of the way there, though, because clean code is primarily about human to human communication. Programming languages really only have one lever to aid in this, and that's their expressive power. That's a really difficult term to define, but basically it's easier for good programmers to write cleaner code in more expressive languages. They have more tools available to easily express an algorithm in terms that communicate well to other humans. Don't get me wrong, you can write clean code in (almost) any programming language. It's just some languages make it easier, and have a better relative result.

However, you can't just dial up the expressivity and magically people will start writing better code. With most programmers, you give them more knobs to turn in their language, and they won't know how to use them properly, so their code ends up actually worse. It takes discipline and good mentorship to improve your code quality. There are no silver bullets.


To some extent. Many languages are deliberately designed to encourage some forms of clean code according to the ideals of the language designers. It is certainly possible to write ugly and incomprehensible code in any language, but some languages do make more of an effort to discourage it.

As an example Python forces you to indent blocks according to the semantic structure of the language, while many other languages allows you to indent totally at random or not at all. This is an example of a language actively encouraging a certain ideal of cleanliness.


If you can quantify it, you can create a language that can optimize it.

While I don't know of any particular language that actually enforces a "clean code" policy, style cops that run on build are quite common.

The main reason that this is a separate step from being baked into the language is largely a function of priorities. It's in the best interest of a programming language to allow the most flexibility to programmers to get the broadest level of adoption. There are so many different programming languages and DSLs that restricting the user base artificially by being picky and opinionated about what input is allowed would likely get in the way of broader adoption.

For example, it's not in C#'s best interest to force people to write

if (condition)

instead of

if (condition) {

But style checkers can be opinionated an picky because that's what they're designed to do.

So, to answer the question

Can a programming language enforce clean code by design?

emphasis mine

Absolutely, so long as you provide a strong definition for what "clean code" means.

For example, I might define "clean code" to mean:

  • line length no greater than 80 characters
  • functions comprised of no more than 100 lines
  • indentation must be two spaces
  • open curly braces must follow at the end of the line preceded by exactly one space
  • no more than two operators per line

and you might disagree with some or all of these conventions, but at the end of the day these are quantifiable and can be enforced programmatically.


No, not in the sense that you describe. Detecting "ugliness" can't be done automatically!

However, language designers can do things to encourage good code (I don't want to say "clean" because sometimes the good, safe code is also long and "ugly"). For example, the designers of the language Rust looked at the things that disciplined C++ programmers tend to do (like giving heap-allocated values a single "owner"), and made it easier to do some of those things. This includes providing a typechecker that you can use to check that you didn't make certain common mistakes.

I would say that good language design is often reactive: designers look at what good programmers do and try to make that easier and "prettier".

Not the answer you're looking for? Browse other questions tagged or ask your own question.