Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Code Complete states that you should aways code into a language as opposed to code in it. By that, they mean

Don't limit your programming thinking only to the concepts that are supported automatically by your language. The best programmers think of what they want to do, and then they assess how to accomplish their objectives with the programming tools at their disposal. (chapter 34.4)

Doesn't this lead to using one style of programming in every language out there, regardless of the particular strengths and weaknesses of the language at hand?

Or, to put the question in a more answerable format:

Would you propose that one should try to encode one's problem as neatly as possible with the particulars of one's language, or should you rather search the most elegant solution overall, even if that means that you need to implement possibly awkward constructs that do not exist natively in one's language?

share|improve this question
5  
+1 good question. I can write in Perl in half a dozen different languages, at this point. –  Dan Ray Feb 27 '12 at 13:18
    
@Dan Ray -- weird! I always write C in Perl. –  James Anderson Aug 21 '13 at 3:47
    
See also: Programming "in" a language vs programming "into" a language by Jon Skeet –  MainMa Aug 21 '13 at 6:11
add comment

3 Answers

There is an even better approach: forget about your pathetic fixed programming language at all. Encode your problem in a language you've just invented, derived from the very terms of the relevant problem domain, encode it as naturally as possible, and only then think of implementing this new programming language or dumbing your code down to the limitations of the existing language.

This approach is called Language-oriented programming. There are many techniques of implementing the domain-specific languages efficiently, and it is an especially hot topic for the Ruby community.

share|improve this answer
1  
The Haskell community also embraces domain-specific languages, and the Haskell programming language is particularly suitable for implementing them. –  tdammers Feb 27 '12 at 9:26
    
Is implementing a tailor made scripting system based on an embedded language like Lua or Tkl considered a writing a DSL? If so, how do you deal with the deficiencies of e.g. Lua? –  bastibe Feb 27 '12 at 9:31
    
@Paperflyer, in some cases it makes sense implementing languages on top of something like Lua (especially if it is Metalua), but it is easier to write a proper compiler for the most of the typical DSLs. –  SK-logic Feb 27 '12 at 12:53
    
@tdammers, yes, Haskell and Scala are all about DSLs. But I'm from the darker side of the Force: my preferred approach is metaprogramming. I believe that ad hoc interpreters are almost always inferior to the compilers. –  SK-logic Feb 27 '12 at 12:54
2  
@tdammers, a DSL implemented on top of high order functions is, practically, an ad hoc interpreter. You can't extend Haskell syntax same way you'll extend, say, Lisp. Even with Template Haskell. It's a totally different (and, I'd say, a limited one) way of implementing DSLs. It's ok in many cases, but for anything really complex it leads to the totally unreadable implementations, whereas a multi-stage metaprogramming is just trivial, no matter how big and alien your DSL is. –  SK-logic Feb 27 '12 at 14:36
show 4 more comments

I believe the correct answer, and the one intended by the book is:

one should try to encode one's problem as neatly as possible with the particulars of one's language

By programming into a language, I always assumed it was to use techniques outside of the normal style of the language where it would lead to a benefit. This is a key difference to writing in one style in all languages.

For example, learning Haskell greatly improved my skill at using higher order functions. Now when programming in c#, I use the various IEnumerable methods such as Select more often, as using these methods leads to cleaner code than writing in for loops. I also tend to use pass and use functions (ie Func<int, int>) more often due to my haskell experience. My use of inheritance has dropped due to this, and most of the time the result is simpler code.

However, I don't use concepts like monads, or algebraic data types in c#. This is because neither are clearly representable in c#, and lead to little benefit in exchange for a lot of obscurity.

So I use the tools of the language to use the skills I have to the best effect. I believe that is programming into the language.

share|improve this answer
add comment

Would you propose that one should try to encode one's problem as neatly as possible with the particulars of one's language, or should you rather search the most elegant solution overall, even if that means that you need to implement possibly awkward constructs that do not exist natively in one's language?

The point is that good programmers don't have a language. The quote from the book talks about "programming tools at their disposal" - that means if you know perl and Java, then maybe you should use perl for that quick string manipulation. Programming languages aren't boxes to limit us, but tools we use to solve problems. This is (imo) what Code Complete is getting at. Don't code in a programming language/environment box, put the best solution into the best programming language/environment for you, your problem, and your solution.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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