The present invent
Gastric mucosal pH
[Tumors of the med
Q: Add to cart bu
Rome was once the
NBA commissioner A
/* * Driver for S
Synthesis, propert
1. Field of the In
1. Technical Field

1. Field of the In
Q: Is there a way
1. Field of the In
The present invent
Q: How can I inse
// ---------------
Q: How to create
/** * (C) Copyrig
Inhibition of huma
// Licensed to Ela
Q: When and how should I test my code, to improve it's readability? I usually test my code when it becomes a bigger block. Usually I start a new method and add some comments, and I test my code as it grows. However, for some reason, some of the code that I write is a lot harder to read than other code. Maybe it's because I made it a lot harder to understand and there's less comments. But this confuses me, I'm using test driven development, so shouldn't it make my code more readable? How should I approach this problem? For example I started to learn python recently, so I wrote a method to calculate the factorial of an integer. def fact(n): if n==0: return 1 elif n==1: return 1 else: return n*(n-1)*(n-2) ... *(n-i+1) Even though I think it is simple enough, I realized I don't understand it really well. I would like to make my code more understandable. However when I test my code, it is hard to test out these cases to make sure that I don't write something weird, so my code looks like this: def fact(n): if n==0: return 1 elif n==1: return 1 else: f = (n)*(n-1)*(n-2) ... *(n-i+1) return f Is there anyway I can think of this problem in a different way to help me make the code more readable? A: I feel that you are mixing up your test of understanding with your actual test of correctness. If you are unable to understand the code, there is little chance of you writing reliable unit tests for it. At least with python you can usually read an if statement like a switch statement if you look hard enough. If I may quote from Robert C. Martin's book Clean Code: One of the hardest jobs we have as programmers is to figure out what the code does. The author of the book goes on to describe five different steps you could take to improve your code. Two of them are Make it work, then make it right. Don't make it work and then try to make it right. When you're done, have fun. You will have a much easier time the next time you are in this situation if you've just enjoyed yourself in the process of solving a problem than if you've just slogged through the problem to solve it. Try to approach your problem by improving the code. Writing code that works is a very basic thing. If you do not understand your own code, you will most likely not be able to write tests for it either. A: There are a number of ways to test your code to increase its understandability. For instance, for your code to be correct, the conditions need to be true. That's pretty easy to test, and is an easy step in understanding your code. So, write a test which ensures those conditions hold. Also, once you understand the conditions, it might be a good idea to comment it out, then re-read the commented code. Does it make sense to you? Does it make sense in reverse? Does the test prove out the code? (Doing a unit test without writing tests on the non-code parts that compose the unit is difficult) I also find that when I want to test the logic of my code, it's hard to think of ways I can break my code, so that it will fail, and that makes it hard to test. So, sometimes I think of an edge case I would like to be sure it covers. A: Is there anyway I can think of this problem in a different way to help me make the code more understandable? If you use python, probably you should read Zen of Python: There should be one-- and preferably only one --obvious way to do it. With most programming languages, I find myself spending the most amount of time reading code of things that I can get done relatively quickly and then spend most of the time reading/rewriting it into a more elegant form so it reads cleaner. Python is generally very readable. So much so that code that is written without comments tends to read like the author's inner dialog. This is a pretty good sign that it is readable as is. I find this to be the case in most of my code as I am a terrible writer. When in doubt, I like to remove comments in favor of keeping the code clean and readable. I know you do too. Edit: You asked, How should I approach this problem? One way is to try and re-think about your design. If you are doing more than the minimum, your code is likely to grow out of control. In these cases, look at your code and ask yourself if you really need to be doing what you are trying to do. Try to make it as easy to read as possible. Try to simplify as much as possible. There is a reason that the language is called python for goodness sakes. This is a very minimal and simplistic language. Use it. Another way to solve the problem is to rewrite your code. Maybe rewriting it will enlighten you to the fact that you really shouldn't be solving this problem with so much code. If it is that easy to see a problem with the code, others will certainly see it too. I am always trying to write code to be read. I think this is even more true when you are using my code. Keep your methods as small as possible and only keep the functionality required. The fewer things there are, the less code it will take to make something happen. If the code is easy to understand, then you should easily be able to tell how the method is supposed to work simply by reading the method. If you have a really long method with lots of code in it, then there is a high chance that there is more than one way to implement the method. This makes it difficult to read and harder to understand the method. Avoid using one liner functions (unless you are writing python with no class like functionality and just using dictionaries and such). I think this is true of all programming languages.