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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:23:49+00:00 2026-06-10T13:23:49+00:00

Normally, you define init or initWith… methods and call them inside convenient constructors like

  • 0

Normally, you define init or initWith… methods and call them inside convenient constructors like this:

@implementation MyClass1 : NSObject
@synthesize n, s;

-(id)init
{
    self = [super init];
    if (self) {
        self.n = 1;
    }
    return self;
}

-(id)initWithString:(NSString *)s
{
    self = [self init];
    if (self) {
        self.s = s;
    }
    return self;
}

+(id)myClass
{
    return [[self alloc] init];
}

+(id)myClassWithString:(NSString *)s
{
    return [[self alloc] initWithString:s];
}
@end

But I think it is possible to define convenient constructors without defining init or initWith… methods like this:

@implementation MyClass2
@synthesize n, s;

+(id)myClass
{
    MyClass2 *obj = [[self alloc] init];
    obj.n = 1;
    return obj;
}

+(id)myClassWithString:(NSString *)s
{
    MyClass2 *obj = [self myClass];
    obj.s = s;
    return obj;
}

@end

Is it bad practice to define convenient constructors without defining init method?
If it is bad practice, could you tell me the disadvantage or problems?

  • 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-06-10T13:23:51+00:00Added an answer on June 10, 2026 at 1:23 pm

    I’m not sure if it’s actually a bad practice. Generally, when I write convenience constructors they look like this:

    + (FSClub *)clubWithXMLElement:(SMXMLElement *)element;
    {
        FSClub *club = [[FSClub alloc] init];
        if (club)
        {
            club.identifier = [element integerValueWithPath:@"id"];
            club.name       = [element valueWithPath:@"naam"];
            club.referer    = [element URLWithPath:@"referer"];
        }
        return club;
    }
    

    The code still takes into account possible memory issues (initialisation failure) like in a ‘normal’ init constructor. Values will only be set if initialisation is successful.

    The interface file is defined as such:

    @interface FSClub : NSObject
    
    @property (nonatomic, assign, readonly) NSInteger identifier;
    @property (nonatomic, copy, readonly)   NSURL    *referer;
    @property (nonatomic, copy, readonly)   NSString *name;
    
    + (FSClub *)clubWithXMLElement:(SMXMLElement *)element;
    
    @end
    

    Please note the properties are readonly. I prefer creating immutable objects, since they’re easier to deal with in e.g. threaded code. The only way to have the properties set in this situation is by using the convenience constructor.

    When I create the convenience constructors, these are generally the only methods I use to instantiate the objects. That is to say, the -init method will not be used most of the time. Also, writing lots of initialisers even if you don’t use them takes lots of developer time. I wouldn’t create methods that I don’t use.

    When you create code as part of a framework -code that you share with lots of people that you don’t know- in such situations you might want to write both the convenience constructors as well as all the normal constructors, because you can’t be sure how the code will be used in people’s own project. For example creating lots of objects using convenience constructors in tight loops might be bad for performance, since the objects are added to the autorelease pool. I think this is also true in an ARC scenario. In such situations one might have the option to use ‘normal’ constructors to create objects.

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

Sidebar

Related Questions

Normally the XML files I have to parse are like this: <row id=1> <title>widget<title>
Normally, I've seen prototype functions declared outside the class definition, like this: function Container(param)
We have a macro for error-checking that goes like this: #define CheckCondition( x )
I have a C header file like this: #ifndef RENDERER_H #define RENDERER_H static int
This question comes from issues raised by this answer . Normally, we define copy
Normally this would be something like: glutAddMenuEntry(-, <opid>); But it doesn't work. After looking
I have a query (a view actually) the normally should define a unique mapping
Normally you make a call to a service, it blocks until a thread in
Normally, if I want to start a new activity I can use StartActivity(typeof(foo)); This
It's possible to define a pointer to a member and using this later on:

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.