Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 821133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:35:34+00:00 2026-05-15T02:35:34+00:00

In the code base I was maintaining I found this exact class, pasted below.

  • 0

In the code base I was maintaining I found this exact class, pasted below. In logPath property, gets does some work. I would think that it is better to do the work in set – that way it will be done only once. However, partly because this is how the class is written, partly because it is an xml-mapped property, and partly because I am afraid that I might miss something in a debugger, I have doubts.

Additionally, if an element never existed in the xml, and it happened to be optional, then I think I will get a null for the value. I might actually want to differentiate between having no element and receiving empty value. I suppose I can have a private bool member which can help me detect that – that would be an argument for doing work in set rather than get. So, code optimizers work hard these days, so performance is rarely a true concern. It is more of a “figure this out once and do not think about it later” things. This is just one example, and properties frequently do some massaging.

Would you say that it is always better to do work in set? In get? It depends? A mixed style would not bother you a single bit as long as it works?

Thanks.

namespace MyNamespace
{
    using System;
    using System.Xml.Serialization;

    /// <summary>
    /// The LoggingListener class encapsulates the "logListener" 
    ///      element of config file, and puts the "logPath" 
    ///      attribute in a file path string.
    /// </summary>
    public class LoggingListener
    {
        private string logPathValue;

        /// <summary>
        /// Gets or sets the LOCAL file path to a log file 
        /// which will be written during operation of the Updater.
        /// </summary>
        [XmlAttribute("logPath")]
        public string LogPath
        {
            get
            {
                return this.logPathValue == null ? 
                         String.Empty : this.logPathValue;
            }

            set
            {
                this.logPathValue = value;
            }
        }
    }
}

EDIT: In this given sample … if the log file is not there, then no logging should take place.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:35:35+00:00Added an answer on May 15, 2026 at 2:35 am

    With getters and setters I follow a few rules:

    1. No side effects – don’t modify ‘B’ when you put a value in ‘A’
    2. What I put in ‘A’ comes out of ‘A’, if you need to modify ‘A’ expose a new read-only property
    3. If you don’t like my value for ‘A’, tell me now not later when I call a method
    4. Outside of a data model, do not accept or return null

    For your example I prefer seeing the following:

    public class LoggingListener
    {
        private string logPathValue = String.Empty;
    
        [XmlAttribute("logPath")]
        public string LogPath
        {
            get { return logPathValue; }
            set
            {
                if(value == null) throw new ArgumentNullException();
                this.logPathValue = value;
            }
        }
    }
    

    Yet it’s clear to me why you ask, it really about the behavior of “XmlAttribute” and the XmlSerializer. You can use the “DefaultValueAttribute” from the ComponentModel namespace, or use an XSD to provide the defaults, or expose a new property and/or method. You could also try creating an interface to separate concerns, something like the following:

    public class LoggingListener : ILogListenerSettings
    {
        private string logPathValue;
    
        [XmlAttribute("logPath")]
        public string LogPath
        {
            get { return logPathValue; }
            set { logPathValue = value; }
        }
    
        string ILogListenerSettings.FullLogPath
        {
            get
            {
                string path = logPathValue;
                if(String.IsNullOrEmpty(path))
                    path = Environment.CurrentDirectory;
                path = Path.GetFullPath(path);
                Directory.Create(path);
                return path;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 512k
  • Answers 512k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should always use the title attribute for tooltips. With… May 16, 2026 at 5:32 pm
  • Editorial Team
    Editorial Team added an answer You cannot link those two pages, they have different purposes:… May 16, 2026 at 5:32 pm
  • Editorial Team
    Editorial Team added an answer For non case sensitive comparisons try int strcmp ( const… May 16, 2026 at 5:32 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am maintaining a large code base and am using a combination of forward
We are currently maintaining a huge code base with hundreds of *.proj and perhaps
In our C++ code base we keep 99 column lines but 79-some-odd column multiline
I have the following HTML (excerpt from larger code-base) <div class=diary-event ui-corner-all title=[title]> <span
How do people manage permissions between their code base and the database? For example,
I'm reviewing our Visual C++ code base and see lots of helper functions with
I am migrating my app code base to iPhone SDK 4. In one of
We have a fairly large code-base. The vast majority of the code is compiled
I am porting a 2.7.7 scala code base over to 2.8 and was wondering
We have a team of 40+ engineers working on a common code base. We're

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.