0

I have 3 booleans:

Boat, Plane, Car

And 3 strings:

ReloadBoat, ReloadPlane, ReloadCar

Depending on these booleans, if false, I need to add commas between the strings.

string errorMessage = (Boat? "" : " " + ReloadBoat) + (Plane? "" : (errMessage) + ReloadPlane) + (Car? "" : ", " + ReloadCar);

For the above, the issue I am getting is, if both Boat and Plane are true, I am getting the errorMessage as ", ReloadCar".

I want it to be "ReloadCar" only.

Any idea on how to do this?

2 Answers 2

7

Breaking it out makes the code more readable and maintainable. It'll get really hard work to use your bools in the manner you have there. Instead I recommend you put the commas on your error messages as a matter of routine. Concatenate them together and remove any trailing commas:

string err="";

if(boatNeedsReload)
  err+="ReloadBoatErrorMessageWithComma, ";
if(planeNeedsReload)
  err+="ReloadPlaneErrorMessageWithComma, ";
if(carNeedsReload)
  err+="ReloadCarErrorMessageWithoutComma";

err = err.TrimEnd(new[]{' ',','});

So the method is:

  • Put punctuation between all your elements regardless
  • Trim off trailing redundant punctuation as the last operation
  • I didn't put any punctuation after ReloadCar because as the last item, it isn't strictly necessary. If this were extended in future to add another item at the end you'd have to remember to punctuate ReloadCar. You might thus wish to consider punctuating car right now and not have to remember to do it next time

If you use a stringbuilder you can interrogate the length and knock 2 off it if needs be:

StringBuilder err=new StringBuilder();

if(boat)
  err.Append("ReloadBoat, ");
if(plane)
  err.Append("ReloadPlane, ");
if(car)
  err.Append("ReloadCar, ");
if(err.Length>0);
  err.Length-=2;
  • this time I did punctuate reloadcar because this method wouldn't work properly without it

Don't try to do too much on one line in your code; you'll reach a point where it needs modifying in a months time and it'll take longer to work out how it works and how to extend it than just breaking it out to something readable and hence maintainable

As an example, this does the same as the first code block, but it's a bit "what the..?"

string err = (
  (boatNeedsReload ? "ReloadBoatErrorMessageWithComma, ":"")+
  (planeNeedsReload ? "ReloadPlaneErrorMessageWithComma, ":"")+
  (carNeedsReload ? "ReloadCarErrorMessageWithoutComma":""))
  .TrimEnd(new[]{' ',','});

Falco makes a good point, that you should strive to make your Boolean variables have a name that declares a truth, like "isTooYoung" or "boatNeedsReload". Make your Boolean have a positive spirit, as it starts to get confusing if you write if(boatDoesntNeedReload==false). Note also that classic advice is to not compare a Boolean with another Boolean to realise a Boolean, but consider that comparing with false can make code more readable than using ! to invert a truth

Sign up to request clarification or add additional context in comments.

3 Comments

prefixing the bools with "is" eg isBoat makes the code even more readable.
I take your point, but don't unilaterally prefix bools with is; in this case it would make less sense
you just made the edit when I wrote this. you are right, "is" is only best, if there is no further meaning known
3

I would create a list of strings with errors and appended to it based on the bools and string join them with a comma or whatever you want.

        var boatErrorMessage = "boatErrorMessage";
        var planeErrorMessage = "planeErrorMessage ";
        var carErrorMessage = "carErrorMessage";

        var isBoat = true;
        var isPlane = false;
        var isCar = true;

        var errorMessageList = new List<string>();

        if (isBoat)
            errorMessageList.Add(boatErrorMessage);

        if (isPlane)
            errorMessageList.Add(planeErrorMessage);

        if (isCar)
            errorMessageList.Add(carErrorMessage);

        Console.WriteLine(string.Join(", ", errorMessageList));

Output: boatErrorMessage, carErrorMessage

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.