Click here to Skip to main content
15,886,919 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Jeremy Falcon17-May-23 4:16
professionalJeremy Falcon17-May-23 4:16 
GeneralRe: In defense of spaghetti code. *ducks* Pin
honey the codewitch17-May-23 4:21
mvahoney the codewitch17-May-23 4:21 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Eusebiu Marcu18-May-23 0:21
Eusebiu Marcu18-May-23 0:21 
GeneralRe: In defense of spaghetti code. *ducks* Pin
honey the codewitch18-May-23 3:57
mvahoney the codewitch18-May-23 3:57 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Eusebiu Marcu18-May-23 6:24
Eusebiu Marcu18-May-23 6:24 
GeneralRe: In defense of spaghetti code. *ducks* Pin
honey the codewitch18-May-23 6:45
mvahoney the codewitch18-May-23 6:45 
GeneralRe: In defense of spaghetti code. *ducks* Pin
CPallini17-May-23 20:29
mveCPallini17-May-23 20:29 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Sander Rossel16-May-23 21:22
professionalSander Rossel16-May-23 21:22 
There's spaghetti and there's spaghetti.
C#
if (oldState == "state1" && newState == "state2")
{
}
else if (oldState == "state2" && newState == "state3")
{
}
else if .. else if .. else if .. else { }
// line 1000
This may feel like spaghetti, but as long as your code reaches one or more if-statement sequentially and it's readable and you can follow it's not so bad.
It's quite easy to refactor, should you ever want to.
C#
public class StateClass
{
public static string oldState;
public static string newState;
}

public class DifferentClass
{
//...
StateClass.oldState = "whatever";
StateClass.newState = "something";
//...
SomeControl.Text = StateClass.newState;
}

public class AnotherClass
{
private object field1;
//...
private object field90;
//...
if (StateClass.oldState == null) throw new Exception("State not set.");
if (StateClass.oldState == "state1" && StateClass.newState == "state2")
{
SomePublicOrStaticVar = "";
}
else if (StateClass.oldState == "state2" && StateClass.newState == "state3")
{
}
else if .. else if .. else if .. else { }
// line 1000
//...
}
Now it's going real spaghetti-like. You'll always have to wonder what will happen everywhere once you set or read either oldState or newState.
Also, your code has to run in a specific order, but in different classes so it's not at all obvious or even logical.
Not sure what SomePublicOrStaticVar does, but setting it or reading it at the wrong time is sure to mess up something somewhere.
Having 90 private fields is also a huge strain on your cognitive abilities.
Everything you do in such a class you have to wonder "will this mess up other methods that rely on this field?"
Believe me, I know Dead | X|
I've had code like this mess up Form1 because a user changed something on Form2 (which had no relation to Form1 whatsoever).

To me, it mostly comes down to this, how many variables do you have to worry about at any given time and how visible are those variables among your different classes?
Large code chuncks aren't the problem (although slicing them up can improve readability and maintainability).
Lots of if-statements aren't a problem either, as long as they don't work on too many different (public) variables.
When functions have their input and output and nothing else to worry about you can rewrite to your heart's contents if you wanted to and the function may remain a black box if it returns the correct output.

I think you're good enough to go for the first spaghetti.

GeneralRe: In defense of spaghetti code. *ducks* Pin
ACRowland18-May-23 1:21
ACRowland18-May-23 1:21 
GeneralRe: In defense of spaghetti code. *ducks* Pin
glennPattonWork316-May-23 22:48
professionalglennPattonWork316-May-23 22:48 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Jeremy Falcon17-May-23 1:40
professionalJeremy Falcon17-May-23 1:40 
GeneralRe: In defense of spaghetti code. *ducks* Pin
honey the codewitch17-May-23 2:24
mvahoney the codewitch17-May-23 2:24 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Jeremy Falcon17-May-23 4:09
professionalJeremy Falcon17-May-23 4:09 
GeneralRe: In defense of spaghetti code. *ducks* Pin
honey the codewitch17-May-23 4:10
mvahoney the codewitch17-May-23 4:10 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Jeremy Falcon17-May-23 4:15
professionalJeremy Falcon17-May-23 4:15 
GeneralRe: In defense of spaghetti code. *ducks* Pin
honey the codewitch17-May-23 4:18
mvahoney the codewitch17-May-23 4:18 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Jeremy Falcon17-May-23 4:24
professionalJeremy Falcon17-May-23 4:24 
GeneralRe: In defense of spaghetti code. *ducks* Pin
honey the codewitch17-May-23 4:30
mvahoney the codewitch17-May-23 4:30 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Jeremy Falcon17-May-23 4:57
professionalJeremy Falcon17-May-23 4:57 
GeneralRe: In defense of spaghetti code. *ducks* Pin
honey the codewitch17-May-23 4:58
mvahoney the codewitch17-May-23 4:58 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Jeremy Falcon17-May-23 5:02
professionalJeremy Falcon17-May-23 5:02 
GeneralRe: In defense of spaghetti code. *ducks* Pin
honey the codewitch17-May-23 5:03
mvahoney the codewitch17-May-23 5:03 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Nelek17-May-23 9:19
protectorNelek17-May-23 9:19 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Jeremy Falcon17-May-23 9:50
professionalJeremy Falcon17-May-23 9:50 
GeneralRe: In defense of spaghetti code. *ducks* Pin
Jeremy Falcon17-May-23 4:11
professionalJeremy Falcon17-May-23 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.