There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

programmer_humor

This magazine is from a federated server and may be incomplete. Browse more on the original instance.

FruitfullyYours , in You can certainly change it. But should you?

I’ve used it in the past when having flash memory blocks that could change but you need the compiler to put them into flash memory and not RAM. It’s mainly to get the compiler to stop assuming that it can optimize using the default value.

Lexam , in You can certainly change it. But should you?

When you set the port speed to no negotiate.

poopsmith , in You can certainly change it. But should you?
@poopsmith@lemmy.world avatar

If you have a memory-mapped peripheral where there’s a readonly register, I could see it being const volatile.

9point6 , in Functional bros be like

Const everything by default

If you need to mutate it, you don’t, you need to refactor.

noli ,

Dogmatic statements like this lead to bad, messy code. I’m a firm believer that you should use whatever style fits the problem most.

Although I agree most code would be better if people followed this dogma, sometimes mutability is just more clean/idiomatic/efficient/…

Corbin ,

Define your terms before relying on platitudes. Mutability isn’t cleaner if we want composition, particularly in the face of concurrency. Being idiomatic isn’t good or bad, but patterned; not all patterns are universally desirable. The only one which stands up to scrutiny is efficiency, which leads to the cult of performance-at-all-costs if one is not thoughtful.

9point6 ,

I agree somewhat, but I’d also say any codebase needs some level of “dogmatic” standard (ideally enforced via tooling). Otherwise you still end up with bad, messy code (I’d even say messier, as you don’t even get consistency)

sukhmel ,

I’d agree with the first half, but not the second. Sometimes mutability allows for more concise code, although in most cases it’s better to not mutate at all

9point6 ,

I feel like I should maybe have put a “probably” in there

After all “there’s no silver bullet”, but in anything but a few edge cases, the rule applies, IMO

BaardFigur ,

deleted_by_author

  • Loading...
  • whotookkarl ,
    @whotookkarl@lemmy.world avatar

    I think the general idea would be to take the original const, and create a new const with the new location applied. Destroy the original when it’s no longer needed or scoped. State maintained through parameters passed to the move function e.g. move(original const, new location) -> new const object instead of stateful members in the object like move(mutable, new location) -> updated mutable.

    Ironfacebuster ,

    A const object meets an immutable variable

    RageAgainstTheRich ,

    That is a… strange take.

    Random example, imagine a variable that holds the time of the last time the user moved the mouse. Or in a game holding the current selected target of the player. Or the players gold amount. Or its level. Or health. Or current position.

    frezik ,

    In all those cases, the answer is to swap in a new variable and throw the old one away.

    RageAgainstTheRich ,

    Legit question because i think I’m misunderstanding. But if its a const, how are you able to swap or replace it?

    frezik ,

    It’s only a const within a function. You can pass the value to another function and changing it as it’s passed. For example:

    
    <span style="color:#323232;">const int foo = 1
    </span><span style="color:#323232;">other_func( foo + 1)
    </span>
    

    In functional programming, you tend to keep track of state on the stack like this.

    madcaesar ,

    What is the advantage of this VS just overwriting the var?

    frezik ,

    Keeping state managed. The data for the function will be very predictable. This is especially important when it comes to multithreading. You can’t have a race condition where two things update the same data when they never update it that way at all.

    madcaesar ,

    Hm I’m having trouble visualizing this do you know a quick little example to illustrate this?

    frezik ,

    Rather than me coming up with an elaborate and contrived example, I suggest giving a language like Elixir a try. It tends to force you into thinking in terms of immutability. Bit of a learning curve if you’re not used to it, but it just takes practice.

    madcaesar ,

    Ok how about this then, I frequently do something like this:

    
    <span style="color:#323232;">let className = 'btn'
    </span><span style="color:#323232;">  if (displayType) {
    </span><span style="color:#323232;">    className += ` ${displayType}`
    </span><span style="color:#323232;">  }
    </span><span style="color:#323232;">  if (size) {
    </span><span style="color:#323232;">    className += ` ${size}`
    </span><span style="color:#323232;">  }
    </span><span style="color:#323232;">  if (bordered) {
    </span><span style="color:#323232;">    className += ' border'
    </span><span style="color:#323232;">  }
    </span><span style="color:#323232;">  if (classNameProp) {
    </span><span style="color:#323232;">    className += ` ${classNameProp}`
    </span><span style="color:#323232;">  }
    </span>
    

    How would this be made better with a functional approach? And would be more legible, better in anyway?

    frezik ,

    I’d say this example doesn’t fully show off what immutable data can do–it tends to help as things scale up to much larger code–but here’s how I might do it in JS.

    
    <span style="color:#323232;">function generate_class_name( display_type, size, bordered, class_name_prop ) 
    </span><span style="color:#323232;">{
    </span><span style="color:#323232;">  classes = [
    </span><span style="color:#323232;">      'btn',
    </span><span style="color:#323232;">      ( display_type ? display_type : [] ),
    </span><span style="color:#323232;">      ( size ? size : [] ),
    </span><span style="color:#323232;">      ( bordered ? bordered : [] ),
    </span><span style="color:#323232;">      ( class_name_prop ? class_name_prop : [] ),
    </span><span style="color:#323232;">  ];
    </span><span style="color:#323232;">
    </span><span style="color:#323232;">  return classes.flat().join( " " );
    </span><span style="color:#323232;">}
    </span><span style="color:#323232;">
    </span><span style="color:#323232;">console.log( "<"
    </span><span style="color:#323232;">    + generate_class_name( "mobile", "big", null, null )
    </span><span style="color:#323232;">    + ">" );
    </span><span style="color:#323232;">console.log( "<"
    </span><span style="color:#323232;">    + generate_class_name( "desktop", "small", "solid", "my-class" ) 
    </span><span style="color:#323232;">    + ">" );
    </span><span style="color:#323232;">console.log( "<"
    </span><span style="color:#323232;">    + generate_class_name( null, "medium", null, null ) 
    </span><span style="color:#323232;">    + ">" );
    </span>
    

    Results:

    
    <span style="color:#323232;"><btn mobile big>
    </span><span style="color:#323232;"><btn desktop small solid my-class>
    </span><span style="color:#323232;"><btn medium>
    </span>
    

    Notice that JavaScript has a bit of the immutability idea built in here. The Array.flat() returns a new array with flattened elements. That means we can chain the call to Array.join( " " ). The classes array is never modified, and we could keep using it as it was. Unfortunately, JavaScript doesn’t always do that; push() and pop() modify the array in place.

    This particular example would show off its power a little more if there wasn’t that initial btn class always there. Then you would end up with a leading space in your example, but handling it as an array this way avoids the problem.

    madcaesar ,

    Very interesting. Actually the part you mention about there being an initial ‘btn’ class is a good point. Using arrays and joining would be nice for that. I wish more people would chime in. Because between our two examples, I think mine is more readable. But yours would probably scale better. I also wonder about the performance implications of creating arrays. But that might be negligible.

    RageAgainstTheRich ,

    Aaah okay i get it now :) that makes a lot more sense.

    Magnetar ,

    Scala user unite! There are dozens of us, dozens!

    crispy_kilt ,

    Scala? Can we reimplement it in Rust?

    lemmesay ,
    @lemmesay@discuss.tchncs.de avatar

    sure, just make sure to add “blazingly fast” in the description and append “-rs” to the name

    Magnetar ,

    But does it do everything in anonymous functions and lambdas?

    crispy_kilt ,

    It can

    ByGourou ,

    Sorry, I want to make an app that works, not a perfect art piece.

    9point6 ,

    The app working isn’t good enough, it needs to be maintainable. From a professional perspective, unmaintainable code is useless code.

    Code that mutates everywhere is generally harder to reason about and therefore harder to maintain, so just don’t do it (unless there’s literally no other practical way, but genuinely these are very rare cases)

    ByGourou ,

    I personally disagree, forcing yourself to use non mutable variables only leads to longer and more convoluted code.

    9point6 ,

    Fair play, I guess we’re probably just gonna disagree.

    In my experience I’d say mutable code (larger than anything other than toy examples) always results in more time spent fixing bugs down the line, predominantly because it’s objectively harder for humans to reason about multiple one to many relationships rather than multiple one to one relationships. I’d say because you need to think about all possible states of the set of mutable variables in your code in order to completely understand it (and I don’t just mean understanding the intended purpose of the code, I mean understanding everything that code is capable of doing), that usually results in a more convoluted implementation than the pretty linear way you typically read functional code.

    Longer code is practically always better if it’s easier to understand than the shorter alternative. Software engineers aren’t employed to play code golf, they’re employed to write maintainable software. Though I’ll say ultra high performance applications might be the exception here—but 99% of engineers aren’t doing anything like that.

    I’m always happy to be convinced otherwise, but I’ve never seen a convincing argument

    jendrik , in After all, Why shouldn't i use Excel as my database?

    Shows up as Untitled 1 to Untitled 25 for me on spotify

    lemmesay , in Functional bros be like
    @lemmesay@discuss.tchncs.de avatar

    I oscillate between using more functional paradigms and more object-oriented ones. is that normal?
    I use a linter BTW(TypeScript) if that is a useful info.

    DickFiasco ,

    I use a combination of both. Objects are declared const, all members are set in the constructor, all methods are const. It doesn’t really work for some types of programs (e.g. GUIs) but for stuff like number crunching it’s great.

    lemmesay ,
    @lemmesay@discuss.tchncs.de avatar

    I heavily use classes while working on back end, and when I’m making a really self-contained logic, such as a logger or an image manipulation service.
    but since most frontend stuff heavily leans on functional side, I go with it

    jkrtn ,

    I think using both is normal. Closures and objects are duals of each other. Do whatever is understandable and maintainable, neither paradigm is magic.

    lemmesay ,
    @lemmesay@discuss.tchncs.de avatar

    that’s a nice way to look at it. thanks!

    kogasa ,
    @kogasa@programming.dev avatar

    Is the duality statement meant to be true in a technical sense?

    jkrtn ,

    Yeah! For example, if the language allows closures to capture state, they can act like properties on an instance.

    kogasa ,
    @kogasa@programming.dev avatar

    I don’t see the duality

    jendrik ,

    A closure is a function with captured state. An object is state with methods.

    crispy_kilt ,

    Avoid shared mutable state like the plague in any paradigm and you’ll be fine

    lemmesay ,
    @lemmesay@discuss.tchncs.de avatar

    state management crying in the corner

    crispy_kilt ,

    Functional state management is fine

    ZILtoid1991 OP ,

    I also do that. Very simple stuff, especially of those that are easy to optimize for the compiler, are often very close to functional programming paradigms.

    ChaoticNeutralCzech , in Exam Answer
    
    <span style="color:#323232;">86400000
    </span>
    
    xmunk , in traslation: i made that bug 15 years ago and have been waiting for it to matter.

    Ha, you remember 15 year old bugs? I’ve “fixed” bugs that were deliberate decisions because the fix was worse then the bug - so I’ve then unfixed the bug and said “7 years ago xmunk, that was really quite a good decision… SO Y U NO COMMENT!” Of course, since I’ve fixed the same fix twice it’s now burned into my memory so there’s no reason to leave a comment at this point.

    SpaceNoodle ,

    Maybe for the unlucky soul that inherits it after you get hit by a bus?

    xmunk ,

    Nah, they probably remember why I changed it.

    agentshags , (edited )
    @agentshags@sh.itjust.works avatar

    I love living vicariously. I feel this whole situation, and I barely ever did more then the (bare bones) intro to AMOS, or or hello world on c64 basic, but Lemmy and the hard R site (before the API mess) memes make me feel the situations at hand, even with very minimal understanding of coding.

    Blackmist ,

    Sounds like somebody else’s problem to me.

    Abnorc ,

    Coding requirements could be a lot less strict if we just solved this bus problem.

    SpaceNoodle ,

    I tried handling the Bus Fault but it didn’t work and I got my CDL suspended

    xmunk ,

    Would rm /var/run/kill solve this problem?

    wieson ,

    Getting hit by a star doesn’t sound that much better

    ExtraMedicated ,

    There’s been a few times where I had to look into an issue and found a comment I wrote much earlier with a ticket number or link to a previous ticket that explains exactly why this new issue is actually the intended behavior.

    It’s really helpful when the product owners clearly can’t make up their minds about what they want their apps to do.

    tatterdemalion , in Programming languages personified - leftoversalad
    @tatterdemalion@programming.dev avatar

    Erlang really is the necromancer’s language.

    Why is Swift… like that?

    cupcakezealot , in traslation: i made that bug 15 years ago and have been waiting for it to matter.
    @cupcakezealot@lemmy.blahaj.zone avatar

    having the same dev job you had 15 years ago? in this economy?

    prettybunnys ,

    Ive moved jobs 4 times in the last 10 years and only 1 of those jobs has actually moved me off the projects I’ve been working on.

    I’ve legitimately responded to my own Issue with a fix to the bug I put in against that code that I wrote at a previous place. It’s weird.

    I almost always get another set of eyes since it’s my old code but that’s always fun “hey I wrote this 6 years ago and it still works but it’s gross … please don’t judge me”

    NotMyOldRedditName ,

    “hey I wrote this 6 years ago and it still works but it’s gross … please don’t judge me”

    “hey I wrote this 5 years ago and it still works but it’s gross … please don’t judge me”

    “hey I wrote this 4 years ago and it still works but it’s gross … please don’t judge me”

    “hey I wrote this 3 years ago and it still works but it’s gross … please don’t judge me”

    “hey I wrote this 2 years ago and it still works but it’s gross … please don’t judge me”

    “hey I wrote this 1 years ago and it still works but it’s gross … please don’t judge me”

    “hey I wrote this last month and it still works but it’s gross … please don’t judge me”

    “hey I wrote this yesterday and it passed QA but it’s gross … please don’t judge me”

    Blackmist ,

    Been in mine 25 years. I could probably make more money elsewhere, but then I’d have to get a proper job rather than be the coding equivalent of an unmaintained fire extinguisher.

    cupcakezealot ,
    @cupcakezealot@lemmy.blahaj.zone avatar

    oh i just meant because usually tech companies go bust or merge in under five years these days :3 but that’s honestly so amazing and i’m happy that you are that happy where you are!

    sheepishly ,
    @sheepishly@kbin.social avatar

    Damn that sounds nice, I want a job like that...

    fibojoly ,

    My colleague and “squad leader” (ie boss without the salary) is a few months younger than me and has been in the same company for about 18 years. Meanwhile I think the longest I’ve been somewhere is about 2 years.

    “Where do you see yourself in 5 years?” has me giggling every time.

    SquishyPandaDev , (edited ) in Exam Answer
    @SquishyPandaDev@yiffit.net avatar

    Good thing this only uses ASCii characters, else you get into some fun discussions about UTF encoding

    qaz ,

    But does it count the null byte or not?

    SquishyPandaDev ,
    @SquishyPandaDev@yiffit.net avatar

    In most languages, length method doesn’t count the null terminator. Might result in some fun memory errors

    YoorWeb ,
    silasmariner , in Exam Answer

    They missed out the context code:

    
    <span style="color:#323232;">trait DoW { def length: FiniteDuration }
    </span><span style="color:#323232;">object Monday extends DoW { override def length = 24.hours }
    </span><span style="color:#323232;">...
    </span><span style="color:#323232;">implicit def toDoW(s: String): DoW = s match {
    </span><span style="color:#323232;"> case "Monday" => Monday
    </span><span style="color:#323232;">...
    </span><span style="color:#323232;">}
    </span><span style="color:#323232;">var day: DoW = _
    </span>
    

    (Duration formatting and language identification are left as an exercise for the reader)

    Magnetar ,

    Upvote for using Scala.

    silasmariner ,

    Implicit was too much of a give away wasn’t it?

    Magnetar ,

    I’ve literally seen code that does something awfully similar. But you could have used an Enumeration.

    Fuck, I think you just gave me an idea for an issue in my code that has bugged me for days.

    silasmariner , (edited )

    I could’ve used a lot of things, but I’m on my phone and I wanted fewer characters to render it, whilst being sure it would work without having to run it.

    Also, I am pleased to have maybe helped. Perhaps we can be friends, you and I. Perhaps not. Idk, maybe you punch dogs, why would you do that? Seems mean.

    Have you ever just, like, edited a comment? How do people know when you did it? I guess if I were writing a thing to check it I’d use a registry of timestamps and checksums… So, like, ok, you can track, but why, how does it look?

    Anyway sorry I had some drinks between now and first post, goodnight

    paholg ,

    Works even better in Ruby, as the code as given is valid, you just need to monkey patch length:

    
    <span style="color:#323232;">#!/usr/bin/env ruby
    </span><span style="color:#323232;">
    </span><span style="color:#323232;">module DayLength
    </span><span style="color:#323232;">  def length
    </span><span style="color:#323232;">    if ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"].include? self
    </span><span style="color:#323232;">      "24 hours"
    </span><span style="color:#323232;">    else
    </span><span style="color:#323232;">      super
    </span><span style="color:#323232;">    end
    </span><span style="color:#323232;">  end
    </span><span style="color:#323232;">end
    </span><span style="color:#323232;">
    </span><span style="color:#323232;">class String
    </span><span style="color:#323232;">  prepend DayLength
    </span><span style="color:#323232;">end
    </span><span style="color:#323232;">
    </span><span style="color:#323232;">day = "Monday"
    </span><span style="color:#323232;">
    </span><span style="color:#323232;">x = day.length
    </span><span style="color:#323232;">
    </span><span style="color:#323232;">print(x)
    </span>
    
    silasmariner , (edited )

    Code as given can be made valid in scala I believe. My starter was based on that assumption. I think raku can do it too, but you would probably have to x = $ to make it work…

    Edit: misread your comment slightly, CBA to change mine now. It is what it is

    kubica , in Exam Answer
    @kubica@kbin.social avatar

    The future is not yet young man.

    takeda , (edited ) in Exam Answer

    For 1 hour = 4^(-1) characters

    mox , in Exam Answer

    hours = 0.25

    There. I fixed it! :)

  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • random
  • lifeLocal
  • goranko
  • All magazines