Confused101's blog

By Confused101, history, 7 years ago, In English

Can someone suggest me what are good practices to write more readable codes? Indentation, variable naming, spaces, alignment etc.. also some users who write good looking codes?

thanks,

  • Vote: I like it
  • +7
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it +26 Vote: I do not like it

John Dethridge in TopCoder.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Its hard to find topcoder submissions, seems he is not much active these days as well, btw thanks, your codes are also elegant :)

»
7 years ago, # |
  Vote: I like it +3 Vote: I do not like it

competitive programming make us write hard-to-understand codes !

As the time is one of variables that affect your points ( the more faster your submission the higher rank you get ) so if you need to make variable Friend just write f , distance just write d .... etc.

in competitive , just make clear steps in your code and don't write many steps in one line. thus , any one read the problem and read your code will — almost — understand it.

the best way to write more readable codes is the real-life projects , almost we work in a team so you can't make a variable called f to refer to "friend" , and if you have to make a short name you will have to write a comment to explain what is this refer to.

also reading readable projects will help you.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You stop being able to manage badly written code as soon as its length reaches ~200-300 lines. It gets a lot harder to modify/extend it (yes, might need it in the contests).

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      And also keeping good coding style doesn't waste much time. Especially with modern IDEs that have autocompletion.

      So it's better to write more understandable code even in competitive programming. This way you can save time while debugging big codes.

»
7 years ago, # |
  Vote: I like it -12 Vote: I do not like it

Most of the IDE provide indentation. For spacing, you can use IDE like Pycharm because you can reformat your code according to pep8 convention in single click.

For good looking codes, you can codeforces filter + sorting feature.

Steps
  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    By your steps, you might end up getting python codes or codes which are compressed!. I randomly picked few problems and checked shortest solutions, here are some solutions 1 2

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yes ignore first few because people remove spaces to get on top.

»
7 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Did you miss Petr?

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I always write my variable names all the way out and spam comments. That's never a bad idea, especially when you are debugging.

»
7 years ago, # |
  Vote: I like it +11 Vote: I do not like it

I always thought that tourist solutions were simples and easy to read. I like his style very much. Take do some time to solve old contests which he took place. Compare your solutions with his.