[Community Puzzle] MCxxxx microcontroller simulation

Very curious, I feel almost every Python solution I have looked at uses one or two classes with lots of functions for this puzzle.
While I understand making a nice function or class is fun, I didn’t really see any need for more than one or two functions in this puzzle. The solutions using those are all significantly longer than the others, which seems not only overkill but also kind of against the purpose of using a language like Python. Is there a significant advantage to solving this particular puzzle with classes that I don’t realize?

To me, the only real advantage is for practicing purposes or maybe just because it’s nice. I don’t see a any real advantages.

Personaly, i used : two functions, a while loop and some if cases.

The puzzle wasn’t meant for such complex things. But I might think about a more complex puzzle in the future. Stay tuned :wink:

In all programming jobs I engaged in, the first priority of “good” code is high maintainability.
To enable this goal, high readability and clarity is necessary.

Some features like conciseness, brevity, number of one-liner codes, etc are NOT considered at all.
We would be sceptical to accept codes that look very brief and compact, because often it causes the code to be too costly to maintain.

Performance (high execution speed) is often not a serious criteria unless in a few high frequency tight loops, or unless it would not reduce maintainability.

Regardless of CG like it or not, we would definitely reject any golf-coding style coder joining our team. Getting high CoC scores indicates in some ways you are good golfer. It might be poisonous to locating a job (in a team sharing similar belief as mine) .

I am not saying we value a good code by line count or word count. I value a code by readability.

Besides human-readable, a good code should be computer-testable. Unit testing small functions is much easier.

All these are opinions from my teams and from me. Read it in context.

So come back to dividing the code into several classes and writing plenty of small functions, my opinion is well-done in structure. We like many short functions rather than a few gigantic functions. Breaking down complex conditionals into smaller functions often helps maintainability and testability. It is generally true to python or js or php or java or C-family.


Yes, high code maintainability is always a high priority on my projects. In most of my puzzle solutions I try to make my code as readable as possible, both for future me and other poeples passing by.

For this puzzle, my solution is quite simplistic, but this is only due to the fact that I made it so I perfectly know the scope of the solution. And I also wanted the “official” solution to be as simple as possible, so that everyone could understand it.

1 Like

Oh, of course, I understand what you mean. Perhaps I should have phrased my comment differently. Of course I’m aware of the advantages of functions and classes, why it is good practice to go for legibility over brevity etc.

But usually people do not do this for CG puzzles which can be solved with a few lines of code. When I look at other people’s solutions in different puzzles, I rarely see a class. Especially in languages like Ruby or Python I see most people writing code that’s as short as possible. Only in this puzzle, I suddenly see the majority of solutions using classes with methods - in Python, while for example in Ruby I didn’t see those. So I asked myself what is it about this puzzle specifically that makes Python users try out using classes. :smiley:

And yet, while I accept all your arguments, and also agree that many community members put too much emphasis into writing the shortest possible code (including when reviewing the code of others), I’m not sure if using a dozen functions helps the readibility in the case of this puzzle. I was taught that one should write a function to avoid repeating code, but almost all of these functions are called only once - and what they do is only one line of code anyway. So I see why classes/functions would make it easier to expand the code later, I fail to see any advantages for legibility.

1 Like

In some circumstances, these classes and get/set functions are used to enable a framework to achieve maximum automation - in generation of codes, auto-wiring of objects, auto CRUD functions without code, and auto-everything - if you use java and Spring you should know these features.

These benefits are for real world projects, not for CG playground.

And yet, I agree with what you said the “best” code you can see are those filtered by “shortness is good” opinions. Many structurally good codes with classes and some functions could be hidden because they are longer than others.

Hehe, no, I have never used Java so far, and I’ve done programming only for fun or in college. So, I have never heard of Spring, auto-CRUD or auto-wiring, and I admit I don’t really know about frameworks either. :sweat_smile:

I guess CG puzzles are simply not made with the intention of having people use classes. :slight_smile: Do you know about any comparable platform where puzzles are built with this in mind? However, I’m definitely going to look at more of the less short/popular solutions by other members and see what I can learn from these.

Classes may be required in more advanced puzzles.

1 Like