While I love PHP I find its biggest weakness is that it allows and even almost encourages programmers to write bad code.

Is there a language that encourages good programming practices? Or, more specifically, a web-related language that encourages good practices.

I'm interested in languages who have either a stated goal of encouraging good programming or are designed in such a way as to encourage good programming.

+3  A: 

You can follow good practices in any language.

Yet you can carry out bad practices in any language as well.

Agreed but it doesn't really answer the question.
Darrell Brogdon
That's because there is no answer. I don't mean for that to sound like something out of the Matrix, but there really isn't IMO.
Wrong, the designers of Python did make decisions about the nature of the language specifically to encourage good coding habits. No language requires someone to follow such encouragement, but if one follows the design ideas of Python, then good habits are reinforced.
Not really. Simply having consistent indention is not good practice. You can get python examples that use bad practices, poor variable names, exception handling etc...
+21  A: 

I think Python have some ideas for good coding practices. At least programs are forced to look the same way.

But don't forget that Larry Wall said:

Real programmers can write assembly code in any language

You'd better think about creating a coding style convention.

Ivan Nevostruev
True, but there is the 'Pythonic' way of doing things, and the other way of doing things so this is not always the case.
+1 Python for readability via enforced indenting.
Love it. Stupid comment length cap....
My opinion: Despite the enforced (and otherwise superficial) visual style, Python (like most dynamic languages) is really easy to write unmaintainable code in. The language itself doesn't encourage you to follow good practices. If anything, it requires *more* discipline to write good code in a dynamic language.
Steve S
@Steve S: That isn't my experience with Common Lisp, which is a dynamic language (dynamically typed, anyway - I don't know exactly what a "dynamic language" is). I do agree that it's really easy to write unmaintainable code in Python. Just like literally every other programming language I've seen.
David Thornley
"Dynamic language" is intentionally vague. ;-) What I'm getting at is this: In Python (for example) mistakes that could be detected by static analysis (i.e. at compile time) in other languages will go unnoticed until they generate a run-time error at best (possibly in a rarely followed code path). The programmer generally compensates by following unenforced conventions and exercising extra care when deviating from them.
Steve S
Hmm... my previous comments do sound pretty harsh and dramatic. I'm not trying to say that Python is a bad language (I actually like it a lot). I just think that encouraging safer coding isn't one of its strong points.
Steve S
+5  A: 

PASCAL is one of the programming languages that encourages good programming practices

and Delphi does do web stuff (compiled).
Not when I used it. There may well be implementations that encourage good programming practices, but Niklaus Wirth's version wasn't one of them.
David Thornley
Linus Torvalds doesn't agree:
Roberto Bonvallet
**Why**? Your statement could have been written for *any* language.
+4  A: 

This is pretty subjective, but I would have to say Python. Readability counts and there is only one way to do things make the language very easy to work with.

Frameworks can also reinforce good practices. Python's Django framework is based off the "MTV" pattern and lends itself to very friendly code.

Dan Lorenc
I think you meant the MVC pattern.
John at CashCommons
Maybe he's saying Python ROCKS!
MTV is right, in Django it stands for Model Template View.
tstenner, John W and Chuck, Django uses the MTV pattern, not the MVC.
Dan Lorenc
Eh, it uses MVC and mislabels it. "The view isn't about how the data looks." Yes, yes it is — that's why it's called a *view*.
+3  A: 

As for writing "bad" have to learn not to do that ;)

As for writing "well styled" code - grab a copy of Visual Studio, the ReSharper plugin, and StyleCop. Intellisense and autoformatting (Visual Studio) will help you lay things out nicely, the ReSharper plugin will restructure your code into a "preferred" structure, and StyleCop will help you follow a standard.

+1  A: 

I'd throw C# out there. It encourages good techniques, readable code and provides tools for ensuring your comments are useful and targetted.

They also provide static analysis tools out of the box to make sure the code is good.

Does C# provide tools for making good comments, or does Visual Studio do this? And what good techniques does the language itself encourage?
C#'s compiler will provide warnings if you have screwed up the commenting on the API (the infamous /// comments on each member in a class). I believe on higher warning levels you can actually have a warning for no comments on a public member.
I disagree. C# is not a bad language, but anything C based has a ridicules amount of noise in it
Matt Briggs
the question seemed to me to ask about good coding practices, which isn't related to noise level of symbols used.
The Visual Studio IDE is what provides the tooling for robust comments.
C# most definately does encourage good coding.
if C# is good, then so is VB.Net - think about what you're saying guys :)
@gbjbaanb VB.Net with Option Strict Option Explicit enabled might qualify. But normal VB allows you to do evil evil things which are not good at all but still considered "standard" technique by most coders.
@Spence, you mean like C#'s extension methods....
+1  A: 

to be clear You can write bad code in any language

anyway C# is really good for following good coding practices

+1  A: 

C# undoubtedly... good base and constantly improving.

Shankar Ramachandran
I think this could use further explanation.
how does C# encourage good practices? o.O
Matt Briggs
+1  A: 

Yes, Python. Many of its design goals are for the stated purpose of encouraging good coding practices. Read the Python tutorial, and the Zen of Python (type "import this" at a Python prompt).

+14  A: 

Well, it depends on what you consider a web language. Smalltalk definitely encourages best practices, and there's even a crazy web framework written in it (Seaside). It's very different and you can still do bad things. You're simply encouraged to do otherwise.

Eiffel's big thing is Design By Contract. It's a nifty organizational requirement that encourages testing and assertions everywhere.

Python is great, but it doesn't really encourage good practices. Well, if indentation is a best practice, then Python definitely enforces it.

Other languages don't really encourage you to do bad things like PHP does. You can also write great (and proper) code in PHP. People often forget that you can disable much of the nastiness in the interpreter (globals, slashes, etc.). You needn't jump ship just because PHP merely entices you to the dark side.

+1: I totally agree about Eiffel, Python and PHP.
Eiffel would be nice but it's never going to catch on. Why? A: it costs to buy it and B: it costs waaaaaaay too much to buy it. I'll stick to C#, thank you.
There is a free (GPL) implementation of Eiffel:
Cobra has design-by-contract and is a Python derivative. See
Sarah Vessels
Definitely Seaside on Smalltalk.
Stephan Eggermont
Should it be the responsibility of the language to ensure best practices and what would be the cost of building this concept into a language?
To a certain extent. A good example would be native support for functions. Sure, you can always `gosub 20583` or `jmp eax`, but it's a tad error prone.

Boo language for the same reasons as Python.

Maybe I'm a bit biased...?

+1  A: 

Coding practices are external to a language. You can just about muck up source code in any language and what is a good practice is subjective.

For example, in C# at the function level you can declare any variable using var and the compiler will enforce type safety, however many people don't like var and think it lends itself to making the code harder to decipher. I personally love var especially when the type is mentioned on the right:


var firstName = new string();

is better to me than ...

string firstName = new string();

... because why do I need to say string firstName when I know it's a string based on the right hand instantiation? Of course, once again this is subjective.

Standards and using code analysis tools coupled with code reviews can really make a difference though.

Here's a list of good analysis tools:

Nissan Fan
+6  A: 

Short answer? No. But the question uses English-language ideas that don't lend themselves to computer programming. What does "encourage" mean? What is "good programming practice"? Syntax highlighting and auto-indent? Surely any language can do that. Lint-style warnings? Not terribly hard. Style policing? Well, we'd had to admit C if that was the criteria.

Good programming practice arises naturally from good design. It's quite hard for clever coders to write bad code in any language if the design is clean and simple. Conversely bad practice follows from bad design. The tricky part is working out where a design stops being good and starts being bad. This is the #1 question in software design and sadly there is no silver bullet. Those lovely Design Patterns start looking awfully ugly once you have several of them implemented in your app.

+3  A: 

If I'm reading the question correctly, what you're looking for is a language where the "easy" thing to do and the "Best practices" thing to do are closely aligned.

If that's the case, what are the "best practices?" Gotta start with that :)

And as a slightly bizarre suggestion, might I suggest LISP or Scheme?

Actually I mean a language where the bad things to do are either actively discourage or just outright difficult.
Darrell Brogdon
Bad things are actively discouraged in most languages. Ask a C question on how to use `gets()` for proof.
Chris Lutz
@Darrell Brogdon: I'm not sure there's a huge difference between encouraging the good things to do, and discouraging the bad ones. Either way, the result is that the language is engineered so that the better practices take less effort to use than dangerous ones.
+2  A: 


  • It makes you write pure code separately
  • It uses a tab syntax, forcing whitespace
  • The module system allows effortless organization of code
Nothing like syntax mandated whitespace :)
It doesn't force whitespace. You can use curly brackets and semicolons. It's just in whitespace mode by default.
Thank you Chuck, I think we know. Nobody really uses that unless they have to though, and even then, anyone who uses haskell loves programming enough to use consistent whitespace =P
+2  A: 

Why are you asking? The question seems to come up a lot after I deal with some nasty code hairball. I spend hours picking apart spaghetti logic that trails through about 6 subroutine levels. Finally track down the niggling error. And my first thought is why can't this be easier?

Code is written for the computer - not humans. It's optimized for the machine. Machines require exact instructions. At that level of detail, the complexity of any code base goes up fast. And that complexity is what typically drives this question.

Programming languages are not intended to control complexity. Programming tools can help you do that. How you write your code matters a lot more than in what language you write your code.

Take a look at literate programming. It controls complexity through organization. I personally like the visual structure provided by the Leo editor.

Robert Wohlfarth
+1  A: 

Assembler fits the bill of "a language where the bad things to do are either actively discourage or just outright difficult" - having your program segfault at runtime is rather discouraging, and writing badly structured web software in assembler would be outright difficult.

Pete Kirkham
+11  A: 

There is No Silver Bullet. Technology will not make a better programmer of you.

Ivan Nevostruev
+13  A: 

This is way too subjective. If by "good programming practices," you mean consistent indentation, then sure, Python works for that. But indentation is something most programmers learn the first time they write a program. I think good programming goes well beyond that.

Over the years, the idea of what is good programming practice has changed. Python, other than the indentation thing, doesn't really excel in any one category. It's not the most object-oriented language around, nor does it (yet) offer full support for functional programming.

Here are some possible definitions of good programming practices, and a representative language for each:

- Pure object oriented design: SmallTalk was the first real OO language, and in some ways still the purest. Literally everything is a class.

- Pragmatic object oriented design: Java, for all it's detractors, was a huge step forward in terms of encouraging good programming practices. It is strongly typed, almost everything is a class, and it requires self-documenting class design, to a certain extent (by explicitly listing thrown exceptions, making functions private by default, etc.) Java enforces disciplines that are valuable for large development teams.

- Functional programming Haskell is often praised as being the most purely functional language around. Assignment is verboten, and side effects are impossible unless your function explicitly asks for them.

- Robust concurrency Erlang (another language that encourages the functional programming paradigm) is known for its robustness in a concurrent environment. Never having studied or used it, I can't personally vouch for this, but its track-record seems impressive.

- Iterative development There are a ton of Lisp evangelists out there. They can't all be wrong, can they?

- Quick 'n' dirty Sometimes you just need a script done quickly. Thank you, Perl. ;)

Eddie Sullivan
+1  A: 

I think an aspect of a language / framework that isn't often talked about is the community around it.

I think Rails does a great job of encouraging better practices not only in the technology itself - which is very much focused on good practices eg. creation of unit tests when you generate scaffolding, but in the community which encourages best practices etc

Kevin Davis
+3  A: 

I think really what you're looking for is not a programming language that encourages best practices, but an opinionated web development framework.

Ruby on Rails for instance is very opinionated, and "out of the box" gives you a strong indication of how the framework expects you to interact with it - it automatically creates a project structure, and even provides some initial unit tests. Testing is a very integral part of rails development, and you'll find a lot of resources to support BDD, TDD and other good "agile" development practices.

Other frameworks like Django (python) and Seaside (smalltalk) probably have their own conventions, although I'm not personally familiar with them.

Bayard Randel
+1  A: 

You might want to read the seaside book to get some ideas on how to DRY web apps. The elimination of templates, consequent construction of DSLs for html and javascript frameworks (a.o. jQuery, Scriptaculous, Prototype, RaphaelJs) and even css (Phantasia, not in the base distribution), combination with an OODB for persistence (Gemstone) provides a lot of why is the current state of the art in php, java, ruby or c# so much worse?

Stephan Eggermont
+3  A: 

I'm going to attract flames and suggest Perl.

'WTF?' you may say. Perl has a reputation as a write only language. It is, when in the hands of people who use it as merely a super-shell language. You can write crap in any language. Perl gives you massive freedom and flexibility. So, in Perl, logically, you have more freedom to write crappier crap.

The freedom and flexibility can also be used in the cause of readability and maintainability.

But how do you learn to produce good code and not crap? In the past the answer was "Experience, and community involvement." Over the last few years, some new tools and resources have arrived to help as well.

  • Experience

    • If you write a turd, and have to maintain that turd, you will learn from the experience.
    • Lovely. How do you avoid writing turds in the first place?

  • Community

    • Instead of enforcing arbitrary rules in the language, Perl has cultural norms that help improve code.
    • Post code on perlmonks without strict and warnings, and you will hear about why you should use them.
    • Great, so to do a decent job, I need to spend hours reading websites and newsgroups? Well, you're here now. But this is still a sub-optimal solution.

  • Tools: This is where we hit paydirt -

    • A few years ago, Perl Best Practices was published. It codified and collected the ideas of many leaders in the Perl community. It provides detailed discussion of the reasoning behind each of the 256 suggested practices. It offers practices, and a way of thinking about the practice of programming.
    • perltidy is a great reformatter for Perl that promotes code readability by automatically enforcing formatting style.
    • Perl::Critic and supporting modules provide a highly configurable system for analyzing Perl code. PC goes way beyond basic lint.
    • Unit testing is a pervasive part of Perl culture. Perl offers great unit testing tools as part of the 5.10 distribution. Additional testing tools are available from CPAN.
    • Moose is a new object system for Perl 5. It is inspired by the Perl 6 object system. Moose provides advanced object oriented capabilities like roles and traits. Roles and traits provide powerful new ways of handling code composition and reuse beyond what is possible with inheritance, interfaces and mix-ins. Inheritance models an "IS A" relationship. Roles and traits model "DOES". I can't emphasize enough how useful this is.

+2  A: 

I can't believe nobody has mentioned Objective-C / Cocoa. You are encouraged throughout to follow MVC patterns, and loose typing allows for some seriously decoupled OO designs.

Kenny Winker