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.

  • CameronDev@programming.dev
    link
    fedilink
    arrow-up
    2
    ·
    19 days 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.

    • hactar42@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      19 days ago

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

      This is one of my biggest pet peeves. So many people want to equate the number of lines as a sign of how well it’s programmed. But all they really do is chain a bunch of stuff together that makes it harder to debug.

  • 4wd@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    18 days 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 :)

  • grue@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    19 days ago

    What you’re asking about (how to learn clever techniques to e.g. turn your naive O(n2) algorithm into an O(n log n) one) is the kind of stuff taught in an algorithms / theory course as part of a CS degree. Here are some of the first search results for open content courses I found that look like they cover the right topics:

    https://ocw.mit.edu/courses/18-404j-theory-of-computation-fall-2020/pages/syllabus/

    http://openclassroom.stanford.edu/MainFolder/CoursePage.php?course=IntroToAlgorithms

    You also might want to consider a combinatorics / discrete math course.

  • Vent@lemm.ee
    link
    fedilink
    arrow-up
    1
    ·
    19 days 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.

  • brisk@aussie.zone
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    19 days 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
    link
    fedilink
    arrow-up
    0
    ·
    19 days 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.

    • grue@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      ·
      19 days ago

      but unless your computations are sluggish and repeat a lot of unnecessary steps

      In other words, the kind of optimizing that’s worth it is choosing a better algorithm to reduce its big-O complexity class.