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 4095972
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:58:38+00:00 2026-05-20T19:58:38+00:00

I have a class which has a group of integers, say foo() { int

  • 0

I have a class which has a group of integers, say

foo()
{
  int a;
  int b;
  int c;
  int d;
  ....
  string s;
}

Now the question is for the best readbility, the init() function for foo(), should it look like

void init()
{
  a=b=c=d=1; //for some reason they are init to 1;
  s = "abc";
}

or

void init()
{
  a=1;
  b=1;
  c=1;
  d=1;
  s = "abc";
}

?

The reason for a string in class is a hint of other groups of same types might present and of course, the class might grow as requirement changes

EDIT: before this question goes too far, the intention of this question was simple:
In Effective C++ item 12 (prefer initialization to assignment in constructors), Scott uses chain assignment instead of a=c; b=c; I am sure he knows when to use what, but I also remembered the books I read also recommended to use int a; int b; which in similar case of assignments. In my program I have a similar situation of a group of related individual build-in types needs to be initialized and I have found by making a chain assignment does makes it easier to read especially if the class have many other different types instance variables. It seems to contradict with books I read and my memory, hence the question.

  • 1 1 Answer
  • 1 View
  • 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-20T19:58:39+00:00Added an answer on May 20, 2026 at 7:58 pm

    My personal preference is a=b=c=d for the following reasons:

    1. It is concise, saves lines
    2. It conveys the concept that (a/b/c/d) are initialized to the same thing, that they are related

    However, caveat:

    1. Don’t do that if a/b/c/d are not related (and just happens to be initialized to 1). You’ll reduce the readability of your code. Example:

      a=c=1; // Foo-function related

      b=d=1; // Bar-function related

    2. Chaining assignments like this reduces the flexibility for you in the future to assign different initial values to the variables — because then you’ll have to break them up again.

    Nevertheless, my personal recommendation is to chain assignments on variables that are related on concept/usage. In actual practice, the need to change an assignment usually doesn’t come up often so caveat #2 should not typically pose a problem.

    Edit: My recommendation may go against published guidelines. See the comments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which has two HashSet<String> collections as private members. Other classes
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have a class which has many small functions. By small functions, I mean
I have an class which has a enum property and a boolean property, based
I have a class which has a function to retrieve children elements from a
I have a class which has a private attribute which is a reference to
I have a class which has a private attribute vector rectVec; class A {
I have a class which has functionality to initialise opengl and run it in
I have a class which has a variety of details, as follows: Vehicle Name
I have a class which has implemented INotifyPropertyChanged . This class UserInfo has a

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.