Hacker Newsnew | past | comments | ask | show | jobs | submitlogin



I don't know why so many people are saying that this is bad code.

Besides the redundant checks, it's really simple, so simple that an intern, maybe even someone who doesn't code, can understand and update it.

It's performant, most compilers will cache the strings.

People trying to justify more complex one-liners with "what if you change the symbol, or just show 5 characters" etc. These scenarios wouldn't take more than 5 minutes to adapt this code, and anyone could do it.

For me, this code with a good set of tests doesn't get much better.


It's easy to read, simple to maintain, and performant code. Maybe one of those newer switch expressions would make the code even clearer, but they already left the redundant lower bound checks in so I think the way this looks is quite intentional.

Much easier to read than `int count = (int)Math.Floor(percentage / 10); return new String("#", count) + new String("-", 10 - count));` in my opinion and not worth writing a custom progress component for.


I agree; there are things in the code base that I personally dislike (a number of classes which could be decomposed into smaller units) but overall it's pretty good.

I had the honour of being able to review this under NDA before it was made public (pro bono, and limited to static analysis and an hour poking around suspicious looking classes). I've seen a lot of .Net code in my time and this was surprisingly good. Sure there are things which could be improved, but you'll find an order of magnitude more issues in most other code (especially dynamic languages, which are magnets inadvertent issues affecting correctness).


LLMs can do better


This one's pretty fancy from the big GPT.

``` Here is an example of Python code that can print a loading bar at different completion percentages:

def print_loading_bar(percent): bar_length = 20 hashes = '#' * int(percent * bar_length / 100) spaces = ' ' * (bar_length - len(hashes)) print(f'\rLoading... [{hashes}{spaces}] {percent}%', end='')

for i in range(101): print_loading_bar(i) time.sleep(0.1) ```


Should be C#, though, and for just 11 steps.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: