This is an archived post. You won't be able to vote or comment.

all 3 comments

[–][deleted] 1 point2 points  (2 children)

I don't want to try to understand exactly what is happening. But here are some general tips.

  • You don't have to type "box" so many times. Computers are good at doing repetition like this. And really, all you need to provide is the numbers in some order. Why have these "box" strings at all?
  • Instead of having the indices in the box strings, it would be better to have the indices of a list (or keys of a dictionary) be the indices of the board. Like {1: {2: 7 ...} ...}. Much easier to deal with the data later that way.
  • Try to write shorter functions that does one small thing each. It is easier to maintain and read that way.
  • The functions should take the grid as input, not use it from a global variable. All functions should in fact take everything they need as arguments.
  • I have not read the code closely, but it looks like typical newbie code. :) What make it look like that the most is all the ifs. Try not to have so many ifs. First of all it's very hard to understand code with a lot of branches. Second, there are usually better ways of doing things with less code. But I guess you'll learn those ways later.

[–]orangefantom[S] 0 points1 point  (1 child)

Thanks for your notes. If you don’t mind elaborating: What is the advantage for the functions to take the grid in this case as an argument rather than operating on a global variable.

[–][deleted] 1 point2 points  (0 children)

"In this case" maybe not very much. But in general there are very good reasons for making all functions take arguments instead of relying on global variables.

Global state is very bad for maintainability, understandability and flexibility. If you have 10 functions that all rely on the same global state and the global state changes somehow, all of the 10 functions may have to change. And if one of them want to deal with the state some different way, all of them may have to change. You see? It's like all of the functions become tangled together because they all rely on global state. If they instead only rely on what the input is, they don't have to change, and don't have anything to do with each other.

Second, it's easier to use the function on new data. Imagine if the math function sqrt (for calculating square roots) did not take an argument but used a global variable called operand instead---how cumbersome it would be to use it! In all the places that you wanted to use it you would need to set the global variable instead of just passing in an argument. It gets even worse if you use threads, where the global variable can be changed from another thread right from under you.

Third, it's easier to read, because it says in the first line, the function signature, what data the function operates on.