0

I have a piece of software which has different users (admin/user) In the admin version you are able to upload the changes straight away. Whereas a user doesn't have this ability have to save it to get authorisation. To display the options I wrote my code like this.

        actionMode = ActionBarMode.SaveCancel;

        if (Config.ConfigVersion < 1)
        {
            //Allow the user to upload logs to trackserver
            this.ActionMode = ActionBarMode.SaveUploadCancel;
        }

actionMode is declared elsewhere and I am just changing it in this event handler. I was wondering what the cost implications of assigning the variable twice in the case of an admin user vs using an if else statement.

I know this cost (if any) is likely to be negligible, but I was just curious.

Edit to explain why its not a duplicate of that question: While yes my question is about Micro-Optimising, my question is more about a specific case which I was just curious about. I do acknowledge that at the end of my post but I do really appreciate the link because it is a great read.

Keithin8a
  • 161

1 Answers1

6

Unless you are obsessive over micro-optimising your code, then the cost is purely in terms of readability and complexity, which is after all at least as important as performance.

You are assigning a variable, performing a test and then potentially re-assigning that variable. You can simplify this down to a single check/set:

actionMode = Config.ConfigVersion < 1 
    ? ActionBarMode.SaveUploadCancel 
    : ActionBarMode.SaveCancel;

And you shouldn't be obsessive over micro-optimising your code, as you'll likely get it wrong anyway. Write clear, easy to read and understand code, then only optimise after properly performance-testing the app.

David Arno
  • 39,270
  • 1
    Yeah, I am not one to usually obsess over micro-optimisation, I just find it interesting to learn. I can't believe I never thought about a ternary statement. Thanks for your answer, readability is by far the most important factor in this. – Keithin8a Nov 07 '16 at 09:52