In the real world, death is often sudden and unfair. Especially in a story with a dark tone, any character can die at any moment.
The issue is that stories are not reality. Killing off a fan favorite without any fore warning or buildup will leave the audience feeling cheated. At least one person in your audience probably liked that character and wanted them to reach the end.
Foreshadowing a death is good because it can give the audience time to grieve in advance. They know it's coming any day now, but they don't know exactly when. It builds tension and makes the eventual death feel more earned.
If there is no buildup, the death feels unearned. It came out of nowhere so fast that it left the audience feeling disoriented. That only works if the disorientation is the intent. You wanted to shake the audience up. Prove their favorites are not immune to consequences. Anyone can die in this world.
If there is buildup, it escalates the tension and lets us know that this is the beginning of the end for our hero.
There are downsides to building it up. The first is that the audience knows pretty soon that this character is going to die. Sometimes you don't want to reveal that too early. Death flags post the twist on a bright neon sign. Show a picture of the wife and you know that soldier is never coming home.
The other issue is that sometimes the author lays the death flags on a little too thick. They try to squeeze every last drop of sympathy that the audience has. It's better to make your audience cry than feel nothing at all, but you don't want them sobbing so hard that they won't turn the page. You also don't want to milk it for so long that the audience stops caring after a few pages.
Like for example, Tommy Mcgee is the character everyone knows is gonna die. He's a bright ray of sunshine in a wartorn world who's got a wife and three kids waiting for him back home. We know he's going to die. The narrative knows he's going to die. So let's milk it for all it's worth! Add in a tragic backstory about how his parents died, how his grandmother raised him only for her to die too, leaving him a jobless, homeless orphan having to beg on the streets before he met the love of his life around and got a fantastic job. Now he's enlisted in the military. And he tells the MC, "If I die, take care of my wife for me."
Great right? Well, let's repeat that tragic backstory every three pages. I really want you to feel bad for this poor man. Did you know he sold his kidney just to make ends meet? Did you know he kept a stray cat and nursed him back to health? Did you know he lost vision in his right eye because he was bullied a boy?
He's a pure soul too good for this lawless world. An angel among men. He's...Oh. He's dead. Do you feel bad yet? Should I go in depth about his funeral for twelve pages?