Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Featured What should be the rules to write excellent code?

  1. Mar 12, 2018 #1
    Hi.

    I know few rules that lets me write great code:

    1.
    "don't pollute the global namespace" when I sit down to write JavaScript code.

    2.
    SOLID

    3.
    KISS

    4.
    Liskov's Open-Closed principle

    Can someone help me complete the list of such rules?

    Thanks!

    Have a great day!
     
  2. jcsd
  3. Mar 12, 2018 #2
    I think subject independent from programming language. Minimum, functional and structured programming.
     
  4. Mar 12, 2018 #3
    I want to know the big words of programming so that I can look' em up on the internet and study about them.
     
  5. Mar 12, 2018 #4
    Test-driven development.

    Code has to be written in such a way that each component of it can be tested independently with only a few lines of setup.
     
  6. Mar 12, 2018 #5
    Okay.
     
  7. Mar 12, 2018 #6

    jedishrfu

    Staff: Mentor

  8. Mar 12, 2018 #7
    Ok. Then design patterns and software requirements.
     
  9. Mar 12, 2018 #8
    Wow! Okay. Thanks!
     
  10. Mar 12, 2018 #9
  11. Mar 12, 2018 #10
    Give your variables very detailed and specific names, including iterators such as i, this alone has saved me days of work
     
  12. Mar 12, 2018 #11

    phinds

    User Avatar
    Gold Member

    Documentation

    Documentation

    Documentation

    (And reread post #10)
     
  13. Mar 12, 2018 #12

    jim mcnamara

    User Avatar

    Staff: Mentor

    Steve McConnell 'Code Complete 2' has all of the above and a lot more.

    I think @Mark44 can verify that Microsoft had this book on its list of must reads - he was at the Redmond campus for years. I believe. His opinion of the book should count for a lot.
     
  14. Mar 13, 2018 #13
    Planning, documentation, experience - and an existing codebase to learn from...
     
  15. Mar 13, 2018 #14

    Borg

    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    100% agree. I would also add that code should be written with two other things in mind.

    1. Does your code follow coding and API standards that fall within the guidelines of the project?
    2. Will programmers who have to work on your code later be able to understand it? (that includes yourself)

    These come from two phrases that I often hear in the coding world that drive me crazy - job security and that will look good on my resume. Neither of these has the customer in mind. Those attitudes usually result in bloated, undocumented software in a language that nobody is familar with resulting in later programmers struggling to understand it and afraid to modify it.
     
  16. Mar 13, 2018 #15
    What is KISS?
     
  17. Mar 13, 2018 #16

    Borg

    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    Keep It Simple, Stupid

    Which goes back to my original statement about making things so complex and unusual that it can't easily be worked on by those who follow.
     
  18. Mar 13, 2018 #17

    Svein

    User Avatar
    Science Advisor

    When I was involved in Software Development, I always insisted on:
    1. A design document for the module (written in such a way that if it was inserted as comments in the source code, it would document the code)
    2. Do not publish any entry points to the code where you do not want to implement full parameter verification
    3. As we usually wrote in C: Run the code through lint until lint is satisfied.
     
  19. Mar 13, 2018 #18

    Mark44

    Staff: Mentor

    Anyone who writes code and aspires to be a better programmer should have this book.
    It was "Code Complete" at that time (about 2004 or so). Everyone in the Windows Division, about 7500 people, had to take a two-day class on strategies to protect the Windows OS from exploits such as buffer overruns and DOS (denial of service) attacks. Besides the class materials, each person received a copy of McConnell's book, which I still have. I didn't realize that this book is now in a second edition.
     
  20. Mar 13, 2018 #19
    Something I always make sure that I use:
    Code (Text):
    -Wall -pedantic-errors -Werror
    It sets the compiler to warn about everything that could possibly go wrong, not let you write non-standard code, and treat all warnings as compiler errors. Even before I go through QA, just setting these flags lets me find all sorts of stupid mistakes. Personally I like everything to be extremely explicit. Method takes a double? Better static_cast<double>() that float!
     
  21. Mar 13, 2018 #20

    QuantumQuest

    User Avatar
    Science Advisor
    Gold Member

    Think, design, code.
    Proper naming of variables, variable scope regarding context at hand, meaningful comments, use of efficient algorithms and efficient implementation and proper documentation are the simplest but unfortunately very often overlooked.
    As your experience in programming grows stronger KISS principle changes (i.e. is adapted) accordingly.
    Try to leverage the full power of the programming language, IDE, toolchain(s), libraries, frameworks, templates ... (the list goes on) - this also helps in not reinventing the wheel, but adapt and test properly.
    When you're about to break some rule(s) justify extensively and if you have to, do it with enough parsimony.
     
    Last edited: Mar 13, 2018
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted