21

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?

11
  • 26
    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, 2012 at 21:14
  • 2
    I wasn't asking for techniques at a real job, I was asking for techniques in a coding competition.
    – marktani
    Jun 21, 2012 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, 2012 at 21:17
  • 1
    Why shouldn't you use memsets to initalise dynamic arrays?
    – James
    Jun 21, 2012 at 23:34
  • 3
    Don't use c++. Use the highest level language you can.
    – Kevin
    Jun 22, 2012 at 18:36

9 Answers 9

53

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.

8
  • 21
    Are you serious? TDD for programming contest?
    – Codism
    Jun 22, 2012 at 18:05
  • 1
    @Codism: the questions was expanded to include techniques applicable to normal software development.
    – Dima
    Jun 22, 2012 at 19:30
  • 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, 2012 at 4:24
  • 3
    -1 for TDD. That makes sense if the project is a long term, not a short hackathon.
    – TheLQ
    Sep 10, 2012 at 15:20
  • 1
    @TheLQ Just because you'd rather spend all your time using a debugger doesn't mean that TDD is a bad suggestion.
    – byxor
    Dec 11, 2016 at 9:30
30

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.

1
  • +1 - I totally agree with this. Knowing how to quickly write code in STL is huge in these kinds of competitions. Sep 10, 2012 at 13:38
24

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.

2
  • 6
    +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, 2012 at 18:17
  • Nice answer. :) Sep 8, 2012 at 17:13
13

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.

2
  • 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.
    – marktani
    Jun 21, 2012 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, 2012 at 23:15
10

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.

3
  • 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, 2012 at 21:10
  • Also... if your editor doesn't support almost every feature listed here — find a better editor. Sep 8, 2012 at 22:23
  • interview in google docs... worst experience for this reason.
    – UmNyobe
    Sep 10, 2012 at 8:40
6

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.

1
4

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.

2
  • Downvoted with an index finger :)
    – mlvljr
    Sep 10, 2012 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. Sep 10, 2012 at 13:35
2

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.

-2

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.

5
  • 4
    I wasn't one of the downvoters, but I assume they probably -1'd you because the question was about programming faster in C++. Jun 22, 2012 at 2:52
  • 9
    VB6? Seriously? Are you from the past?
    – Konamiman
    Jun 22, 2012 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. Jun 22, 2012 at 14:45
  • I edited the question for clarification.
    – marktani
    Jun 22, 2012 at 15:02
  • On RAD: Delphi 6 will be the best bet then, possibly.
    – mlvljr
    Sep 10, 2012 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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