61
Code Smells Catalog (luzkan.github.io)
you are viewing a single comment's thread
view the rest of the comments
[-] Boomkop3@reddthat.com 1 points 4 days ago

There are arguments to be made either way, but normally you’d scope your variables in a way that the ones specific to a particular bit of code are not accessible from elsewhere.

They're arguing to do this:

int field = 1;
void may() {
   do(field);
}

int field = 3;
void you() {
   do(field);
}

int field = 3;
void be() {
   do(field);
}

int field = 7;
void happy() {
   do(field);
}

rather than

int field = 1;
int field = 3;
int field = 3;
int field = 7;

void may() {
   do(field);
}
void you() {
   do(field);
}
void be() {
   do(field);
}
void happy() {
   do(field);
}

A bad example of encapsulation would be:

class AClass {
    private class HelloThere {
         int a = 1;
         int b = 3;
         int c = 3;
         int d = 7;
         void DoStuff(AClass self) {
              Do(a, b);
         }
    }
    private HelloThere field = new();
    void World() {
        field.DoStuff(this);
    }
}

Of course, there is nuance here. Is this class encapsulating enough that it's got a right to exist? That'll depend on the situation.

Also, c has local static variables. Depending on your use case, it might just be easier in c than in C# and similar.

// a method with a state, horrid in some contexts, great in others
void PrintCounter() {
    static int count = 0;
    Print(count);
    count += 1;
}

And just in case you're still reading and curious:

#region PingPong
    // hi! I am in a region, collapse me using your ide!
#endregion
[-] FizzyOrange@programming.dev 1 points 4 days ago

// a method with a state, horrid in some contexts, great in others

Definitely another code smell!

https://luzkan.github.io/smells/global-data

[-] Boomkop3@reddthat.com 1 points 4 days ago* (last edited 4 days ago)

In most cases it's a bad idea, yes.

Also, have another look at that example code snippet though: that static variable is local to that function. It's a weird feature in c.

I've used it quite often in embedded code where a single variable was only for one function, and only for that one app/device. Wrapping it in a struct would've made the code needlessly more complex (that's a code smell). And yet, these static locals are very easy to refactor to one local to a struct. May the situation change, that's still an option.

[-] FizzyOrange@programming.dev 2 points 4 days ago

that static variable is local to that function

Yes I know how static storage durations work. It's still global state, which is a code smell. Actually I'd go as far as to say global state is just bad practice, not just a smell. Occasionally it's the only option, and it's definitely the lazy option which I won't claim to never take!

[-] Boomkop3@reddthat.com 1 points 4 days ago

Aaand.. you didn't even bother to google it :/

This is not about storage durations, and it's local to a function

[-] FizzyOrange@programming.dev 1 points 3 days ago

I don't need to Google anything. I have 30 years experience writing C & C++.

This is not about storage durations

Yes it is.

https://en.cppreference.com/w/c/language/storage_duration

it’s local to a function

Only the visibility is local. The data is still global state. You can call that function from anywhere and it will use the same state. That's what global state means.

https://softwareengineering.stackexchange.com/a/314983

Some of the biggest issues with global state are that is makes testing difficult and it makes concurrent code more error-prone. Both of those are still true for locally scoped static variables.

[-] Boomkop3@reddthat.com 1 points 3 days ago* (last edited 3 days ago)

Again, it's an easy refactor to make it not global. There are cases where that extra abstraction work simply does not add value.

With your background, you should know that

[-] FizzyOrange@programming.dev 1 points 2 days ago

it’s an easy refactor to make it not global

I have enough experience to know that making global state non-global is usually anything but easy.

[-] Boomkop3@reddthat.com 0 points 2 days ago

Damn, suppose I won't just pass it as a pointer from the call site. That'd be so difficult to add an int to a struct

30 years my ass

[-] FizzyOrange@programming.dev 1 points 1 day ago

Yes, and then pass the context from the call sites of that function, and all the way up to main(). Oh look you're refactored the entire app.

That's best cases too, you'd better hope your program isn't actually a shared library running in a SystemVerilog simulator with state instantiated from separate modules via DPI, or whatever.

30 years my ass

lol when you have 30 years experience you will have actually tried to do this a few times and realised it isn't usually as trivial as you hope it would be.

[-] Boomkop3@reddthat.com 0 points 1 day ago

That's not a great approach, but you do you

this post was submitted on 05 Jan 2025
61 points (90.7% liked)

Programming

17788 readers
173 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS