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.

I am preparing for a programming contest where we have to code in C++ and it is all about producing working code in a short time. An example would be to use a macro to get the minimum of two ints or using memsets to initialize arrays (but I was told that you shouldn't use either here).

This leads to the question, what kind of coding techniques exist to use at a real job?

share|improve this question
24  
I would guess that the techniques you use in a coding competition may have nothing to do with the techniques you'd use at a real job. –  Doug T. Jun 21 '12 at 21:14
1  
I wasn't asking for techniques at a real job, I was asking for techniques in a coding competition. –  mcwise Jun 21 '12 at 21:16
1  
@DougT. I agree, tricks for a coding competition is not on topic, but I also think you could expand to talk about productivity in a work environment, and how do you best reduce time spent on monkey code –  James Jun 21 '12 at 21:17
1  
Why shouldn't you use memsets to initalise dynamic arrays? –  James Jun 21 '12 at 23:34
3  
Don't use c++. Use the highest level language you can. –  Kevin Jun 22 '12 at 18:36
show 6 more comments

9 Answers

up vote 47 down vote accepted

The way to produce working code fast is to... slow down. Take very small steps. Make sure you know what each step is for. Make sure that after each step your code compiles and runs.

Best of all, use Test-Driven Development. Write a failing test. Write just enough code to make the test pass. Refactor to make the code clean, making sure it still passes all the tests. Repeat.

If you do not do this, then it is very easy to write a big pile of code, which does not work. Then it will take you a very long time to figure out why it does not work.

share|improve this answer
2  
Thanks I think you are right and I will try to do this! –  mcwise Jun 21 '12 at 22:14
1  
+1 for recommending TDD! –  Marlon Jun 22 '12 at 17:31
19  
Are you serious? TDD for programming contest? –  Codism Jun 22 '12 at 18:05
2  
TDD definitely helps make my code better, but it has never helped me write the code faster. An interactive programming environment (e.g. Groovy console, Lisp REPL, etc) has been the biggest time saver in my experience. –  erturne Sep 9 '12 at 4:24
3  
-1 for TDD. That makes sense if the project is a long term, not a short hackathon. –  TheLQ Sep 10 '12 at 15:20
show 4 more comments

Review the standard libraries intensely, particularly the STL algorithms. This will save you many lines of code and a lot of time. The key to winning programming contests is in programming at as high a level as possible. In C++, without external libraries, this means STL calls instead of for loops.

share|improve this answer
    
+1 - I totally agree with this. Knowing how to quickly write code in STL is huge in these kinds of competitions. –  Jordan Parmer Sep 10 '12 at 13:38
add comment

If it's for an actual programming competition, don't use C++. Use a fast, interactive, RAD scripting language. This is something VB6 absolutely dominates at. Go pre-armed with libraries you prewrote. String splitting, array reversal, searching, sorting, rudementary graphics, console control.

If it's for work, do exactly the opposite. Learn what's there, use a solid language (C++ is fine), comment everything, and so forth. The two types of environments could not be farther apart.

share|improve this answer
3  
I wasn't one of the downvoters, but I assume they probably -1'd you because the question was about programming faster in C++. –  Gordon Gustafson Jun 22 '12 at 2:52
9  
VB6? Seriously? Are you from the past? –  Konamiman Jun 22 '12 at 8:46
2  
@Konamiman: heh, yes I am. I couldn't tell if he meant "I am preparing, in C++, for a programming contest" or "I am preparing for a C++ programming contest". Oh well, chips lie where they may. Still, nothing beats a very fast RAD environment for programming competitions, where literally every second counts. –  insta Jun 22 '12 at 14:45
    
I edited the question for clarification. –  mcwise Jun 22 '12 at 15:02
    
On RAD: Delphi 6 will be the best bet then, possibly. –  mlvljr Sep 10 '12 at 0:39
add comment

Well, it sounds to me like you only know C and not C++ at all. These questions are easily answered by someone with a basic working knowledge of the language.

An example would be to use a macro to get the minimum of two ints

Just use a function? std::min already exists.

or using memsets to initialize arrays

std::array<T, N> already initializes it's contents.

what kind of coding techniques exist to use at a real job?

The first step is to know the language you are using.

share|improve this answer
    
I am trying to get to know the language I am using, that's why I asked this question! Thanks for the input about this. –  mcwise Jun 21 '12 at 22:53
7  
Oh. I must have misunderstood your question then. I would recommend a book. We kindly present only the books worth reading, instead of those that teach C garbage. –  DeadMG Jun 21 '12 at 23:15
add comment

Think before you act. It's better to think and plan before writing a class implementation than after three failed ones. Also, think while you act - doing things in small steps greatly simplifies the development process.

Write only the code you need at the moment. That is, don't start off writing code with mentality like: "I'll need these functions later on so I might just aswell write their prototypes while I am at it". Things change, plans change. You'll end up with code which might not be even usable. It's waste of time.

Know what you're doing. There's nothing worse than trying to remember how basic functionalities and features of the language and/or library work. Get familiar with the tool(s) you use.

share|improve this answer
    
+1 for YAGNI. –  Brendan Long Jun 22 '12 at 3:55
add comment

I participate regularly in ACM competitions, hopefully some of these tips will help you:

  • As others said, familiarize yourself with the language, in C++ especially the STL, it has both common functions that you'd want to use (binary_search, min, max) and robust data structures to save you time (stack to avoid straight up recursion, queue for BFS, even priority_queue for Dijkstra if you like it that way).

  • Identify the category of the problem, if it's mathematical, dynamic programming, graph theory, etc. Ask yourself: How familiar are you with that type of problem? After doing this, you should make decisions regarding the order on which you'll solve them, which goes hand to hand with the next point...

  • You want to understand the problem completely before typing, solve the right problem, in my first competitions I thought that if I wasn't typing I was wasting my time; I later found that this was a mistake.

  • Don't think comments are a waste of time, at least in "clever" code, you don't want to go debugging line-by-line to see what went wrong (that is a real waste of time), value clarity.

  • Have fun.

share|improve this answer
5  
+1 for understand the problem completely before typing. Otherwise, you are wasting team's time (ACM competition is 3 person one computer) –  Codism Jun 22 '12 at 18:17
    
Nice answer. :) –  Jared Farrish Sep 8 '12 at 17:13
add comment

There have been many answers already, still I would like to add my opinion.

To speed up your coding, Become a touch typist to such a speed that while typing you are able to concentrate both on the logic and code. There are many software to learn typing (Typing Master, Rapid Typing, KTouch, gTypist for example), use them

I have seen many experienced programmers who are still two-finger typist. This leads to nasty code with no documentation and really cryptic code with macros and abbreviated type names.

share|improve this answer
    
Downvoted with an index finger :) –  mlvljr Sep 10 '12 at 0:38
    
Great answer. I'll add that folks who look at the keyboard instead of the screen often type garbage before they realize it and have to backtrack. Or they'll type into the wrong window. And the sad thing is, they don't realize how much this slows them down. –  Dave Markle Sep 10 '12 at 13:35
add comment

In addition to knowing the language, know your tools, especially your editor. I've been coding for over 15 years professionally, and I've seen otherwise great programmers slowed down because the only editor commands they use are cursor keys and crtl-x/c/v..

Does your editor support a single keystroke to delete a line? To re-indent a line? Move between functions/methods? block select/paste? bookmarks? Move by word? Can the editor auto generate classes/methods/functions? Does it suport templates that you can create ahead of time? Quick refactoring? Macro recording?

Knowing your editor can greatly speed up development time.

share|improve this answer
1  
amen. I'm just working with a new guy who is very fast... and uses vim. It works for him because he's taken the time to learn how to use it. –  gbjbaanb Sep 8 '12 at 21:10
    
Also... if your editor doesn't support almost every feature listed here — find a better editor. –  Abhi Beckert Sep 8 '12 at 22:23
    
interview in google docs... worst experience for this reason. –  UmNyobe Sep 10 '12 at 8:40
add comment

Software development is one of those classic 80-20 activities -- ie where the first 80% of the work takes 20% of the time, and the last 20% of the work takes 80% of the time.

The key to speeding up your work in general is, therefore, to work on improving your performance for that that last 20% of the effort. This is typically the debugging stages and the work to tie everything together and make it usable.

You can improve this by taking more care in the early stages. Planning and design work at the outset of a project may seem like a waste of time when you're tempted to dive into the hardcore programming, but they can save a vast amount of time later on.

People will talk about specific methodologies like TDD or BDD. These are great, but if you're not doing them now they will take you a bit of time to adjust to, so they're probably not something that you want to be starting out with for a programming contest. But stopping to plan what you're doing before you dive in and do it will reap rewards no matter what. And they're the first steps toward TDD and BDD anyway. This is where you want to start.

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.