this post was submitted on 13 Jun 2024
17 points (100.0% liked)

Learn Programming

22 readers
1 users here now

Posting Etiquette

  1. Ask the main part of your question in the title. This should be concise but informative.

  2. Provide everything up front. Don't make people fish for more details in the comments. Provide background information and examples.

  3. Be present for follow up questions. Don't ask for help and run away. Stick around to answer questions and provide more details.

  4. Ask about the problem you're trying to solve. Don't focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

founded 1 year ago
MODERATORS
 

I'm trying to make minesweeper using rust and bevy, but it feels that my code is bloated (a lot of for loops, segments that seem to be repeating themselves, etc.)

When I look at other people's code, they are using functions that I don't really understand (map, zip, etc.) that seem to make their code faster and cleaner.

I know that I should look up the functions that I don't understand, but I was wondering where you would learn stuff like that in the first place. I want to learn how to find functions that would be useful for optimizing my code.

top 9 comments
sorted by: hot top controversial new old
[–] CameronDev@programming.dev 13 points 5 months ago

Experience is the best teacher. Keep writing code, revisit old code and rewrite it.

Also, is worth knowing when not to optimise. Code you can read is code you can maintain, and some optimisations are not as readable.

Learn how to use a profiler. Its a bit of an artform, but learning to interpret the results will help you find slow code sections. It'll also help you determine if your optimisations are actually worthwhile. Measure first, optimise second.

[–] brisk@aussie.zone 8 points 5 months ago* (last edited 5 months ago)

The functions you've called out are higher order functions regularly associated with the functional programming paradigm. "In the first place" for a lot of people would be a functional programming course at a university.

For your specific case, rust (like a few other languages) implements these through iterator programming. There's a section in the rust book that might help.

Apart from academia you learn from experience, including a healthy amount of reading other people's code, just like you did to find out about these functions in the first place!

[–] Akrenion@programming.dev 7 points 5 months ago

Map, Filter, Reduce Those are the big three for data. More important than those however is mindset and patience with oneself. Writing code that works is the first and most impressive step. Optimizations are fun to think about but unless your computations are sluggish and repeat a lot of unnecessary steps they are rarely a priority.

Build something and shelf it. Trust me, in but a few months you will look back in bewilderment and realize how much you've grown.

[–] fourwd@programming.dev 4 points 5 months ago* (last edited 5 months ago)

When I started learning programming, I was like "tf is a map function?" and I always forgot about it. Then I tried the functional programming language Erlang and understood all these functions very well. But there is a downside, now most for-loops in C++ look terrible to me :)

[–] cflewis@programming.dev 4 points 5 months ago (1 children)

This is probably going to get downvoted into oblivion but: try writing some Haskell for a while. Learn You A Haskell is a good place to do it, just bail out when you get to monads.

When I was taught programming at university, we did one assignment in Java, then the next one was the exact same assignment but in Haskell. The idea was not to bias us towards imperative vs functional programming. I don't think it worked -- I would guess almost everyone preferred Java -- but over my career I've learned how much Haskell has offered me for writing imperative code for my day job. I think you will get what you are looking for by trying some Haskell for a while.

[–] ericjmorey@programming.dev 2 points 5 months ago

just bail out when you get to monads.

Isn't writing Haskell nearly equivalent to writing monads? How could they start without using monads?

[–] Vent@lemm.ee 4 points 5 months ago

I learned that type of stuff in college, so I can't personally recommend any online sources. However, I can tell you that what you're looking for falls under "Data Structures and Algorithms". IIRC my degree required 3 classes with that name. Lots of sorting algorithms in that field since they make great case studies.

You learn the various data structures and algorithms available, their strengths and weaknesses, how they work, when to use them, etc...

You also learn how to measure performance, like Big-O notation, the bane of many a CS student's existence.

[–] pooberbee@lemmy.ml 3 points 5 months ago

Some of this is probably just getting to know your tools. Learn the language, look at others' code and interrogate what they did and why. The higher-order functions (scary-sounding term, but they're not actually scary) you mentioned are useful, go learn them and use them.

Some of it might also be (I haven't seen your code) getting a better understanding of the problem you're trying to solve. Figure out what all the separate pieces you need are, and then break those pieces into their pieces and so on, until you've got simple, self-contained chunks of functionality that you can give simple names to. Some of those might be functions, or you may find that they're simple enough that they don't need to be. Refactor and think about how to make the problem simpler. I think a lot of it is just staring at your work and dreaming of ways the make it simpler and easier to read.

If you really want to optimize for performance, that can come later once you really have a feel for it.

[–] alsimoneau@lemmy.ca 3 points 5 months ago

Code more. Run profilers. Try different solutions.