Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Justas Bieliauskas
    @justasbieliauskas

    Hi, everybody. I have a question regarding configurable objects.
    So, I have this class:

    public final class Cached<T>
    {
        private final Variable<T> variable;
        private final Origin<T> origin;
        public Cached(Variable<T> variable, Origin<T> origin) {
            this.variable = variable;
            this.origin = origin;
        }
        public T value() {
            if(!this.variable.initialized()) {
                this.variable.initialize(this.origin.value());
            }
            return this.variable.value();
        }
    }

    It's a class that caches a value. It needs to know what to cache and where to cache it. As you can see, I'm trying to simulate a concept of a variable, where I set a value to it, then reuse it when I need it.
    My problem is, I feel this is a configurable object. I'm injecting foreign behaviour in this class through Variable. It is not a black-box-solid object anymore, a lion's share of its logic is now in the hands of another object. I can pass anything in place of Variable<T> in the constructor and basically control how this object behaves.
    Furthermore, Variable interface itself reveals a lot of information about Variable. Since there's a method initialized and initialize, I'm almost certain there is an if sentence which checks if a Variable object holds a value and if it doesn't, sets it. I should not be aware of that.
    I discovered this problem while reading one of Yegor's drafts.
    Does anyone know what am I supposed to do here? Can't find an alternative.

    Fabricio Cabral
    @fabriciofx
    @justasbieliauskas What's the problem with:
    public final class Cached<T> implements Origin<T>
    {
        private final List<T> cache = new ArrayList<T>();
        private final Origin<T> origin;
        public Cached(Origin<T> origin) {
            this.origin = origin;
        }
        public T value() {
            if(this.cache.isEmpty()) {
                this.cache.add(this.origin.value());
            }
            return this.cache.get(0);
        }
    }
    Phantom190
    @Phantom190

    @yegor256 "What do you need a man for than?"
    Tell that to lesbians that can buy sperm and get pregnant, or simply adopt.

    @yegor256 "I got it, but I don't want my daughter to bench someone's desk with one hand and kick someone's ass with the other. I just don't want that :)"
    Who the fuck cares what you want? what you should care and what actually matters is what your daugher wants.

    @yegor256 And you believe that a society where anyone can be at any position is better than the one where certain people have certain limits?
    The only limit there is, is the one you put yourself. None should give a fuck about the limits society puts.

    James "Author is a fucking moron. Does he know that a woman invented the first programming language or"
    @yegor256 "You made it here: http://www.yegor256.com/tes... Thanks :)"
    /Testimonials.html "Author is a fucking moron."

    It's funny that in such a small sentence, you leave the good part. Considering some of your testimonials are made of like 15 lines.

    @yegor256"I don't have kids yet, but when I will have them I will raise them exactly like my parents raised me: in the gender inequality philosophy.
    Translated: In the year 2017, I will raise my kids as kids were raised back in the good old days, where racism, sexism, slavery, all those good things were the norm.

    @Yegor256 "We have to live by the laws of the Nature"
    No, not really. I live by the laws set by smart/dumb people in my country. That's the only one I actually care.

    Sorry for joining the party late !
    I also don't know why YOU living currently in the USA think women need your protection. They have guns bro. People don't need your "biceps".
    Phantom190
    @Phantom190
    By the way I bought your first book and read it for 10 mins. Then realized it was a copy paste from your website and threw it to the trash can. I REGRET IT THO, because I now own a dog and sometimes I need paper for him to shit on.
    Mihai A.
    @amihaiemil
    @Phantom190 why don't you copy the whole blog here while you're at it? Btw, good call on throwing the book after 10min read. It really is just a copy of the blog. Last 65 pages are just the images from each article. Your dog would have read more of it tho, so maybe go fetch it back for him
    Mihai A.
    @amihaiemil
    Second book also includes this sexism article plus a few blank pages just to keep the same page count. There werent enough articles you know... tough job being a writer
    Fabricio Cabral
    @fabriciofx
    By the way I bought your first book and read it for 10 mins. Then realized it was a copy paste from your website and threw it to the trash can. I REGRET IT THO, because I now own a dog and sometimes I need paper for him to shit on.
    @yegor256 A new testimonial to put here... :laughing:
    Silas Reinagel
    @SilasReinagel
    Somebody got their feelings super triggered
    That was pretty funny to watch
    RayoGundead
    @RayoGundead
    It's no laughing matter when the most liberal of tumblrettes get positions of power
    Mihai A.
    @amihaiemil
    What is a tumblerette and you mean liberal as in usa or eu?
    Bc depending on it, liberal means smth good or smth bad :))
    RayoGundead
    @RayoGundead
    a watered-down form of communist in it's media-presented form.
    Mihai A.
    @amihaiemil
    So american
    Well i don't think it's that. Yegor simply puts it bluntly, that's. It doesn't mean that he or you should apply it exaclty like that
    RayoGundead
    @RayoGundead
    all liberals of any country
    Mihai A.
    @amihaiemil
    Im referring to everything not just that sexism article
    Nah I dont imderstand that. By liberal I understand more freedom of action. The least interference with the state.
    I know in America it has an opposite meaning
    RayoGundead
    @RayoGundead
    not exclusive to America
    other countries have the term Liberal distorted
    Mihai A.
    @amihaiemil
    Well, as long as we each know what we mean, it's just semantics. Just pay attention to this word
    RayoGundead
    @RayoGundead
    see Canada and Germany
    Conservatives are practically underground there
    Mihai A.
    @amihaiemil
    Germany maybe, because they are taken as nazis?
    RayoGundead
    @RayoGundead
    the conservaives there, their mere existence gets labeled every derogatory name in the Liberal handboook
    Mihai A.
    @amihaiemil
    I know, it's bad in general to just label things. Everyone has their points and everyone exaggerates in general, making the others' bad headlines
    RayoGundead
    @RayoGundead
    so its either conform or get persecuted for wrongthink
    Mihai A.
    @amihaiemil
    When that happens, the "persecuted" one should stand up and debate. Nowadays we just have offended pussies and political correctness... same as in Yegor's article: everyone got offended and misunderstood the point. Everyone thought it was an article about women in tech, when women in tech was a mere example. He never said he would discriminate a woman. He just stated his opinion, used only the word "would"... "I would tell her not to work in tech" not "I would stop her"
    RayoGundead
    @RayoGundead

    When that happens, the "persecuted" one should stand up and debate.

    tell that to Garron Helm and Eron Gjoni

    RayoGundead
    @RayoGundead
    @yegor256 I remeber you mentioned treating HashMap as data (or was it container?) instead of object since it is for that purpose.
    I forgot where or when tho
    that's why I asked about Observables.
    Justas Bieliauskas
    @justasbieliauskas
    @fabriciofx Yes, this was my initial design, to have an ArrayList of 1 and save the value there. But it's not entirely flexible to always save it to a list. Perhaps I want to save it to a file instead or somewhere else etc. Having an abstraction Variable helps to specify different locations to save the value.
    Phantom190
    @Phantom190

    @amihaiemil

    When that happens, the "persecuted" one should stand up and debate. Nowadays we just have offended pussies and political correctness... same as in Yegor's article: everyone got offended and misunderstood the point. Everyone thought it was an article about women in tech, when women in tech was a mere example. r"

    Did you read the quotes? they are not even from the article. They are from the comments, from yegor - him trying hard to explain himself.

    He never said he would discriminate a woman. He just stated his opinion, used only the word "would"... "I would tell her not to work in tech" not "I would stop he

    No, I think it's a bit worse, he's fine discriminating any person, according to his ancient style of living.

    Fabricio Cabral
    @fabriciofx
    @fabriciofx Yes, this was my initial design, to have an ArrayList of 1 and save the value there. But it's not entirely flexible to always save it to a list. Perhaps I want to save it to a file instead or somewhere else etc. Having an abstraction Variable helps to specify different locations to save the value.
    I'm sorry, but I don't like this solution. Save in a file has a lot of overhead (I/O). But, could explain me better around the problem? I can't see the problem with ArrayList....
    Mihai A.
    @amihaiemil
    @Phantom190 this is not the good chat to debate that article in particular. Read my comments on the Disqus Thread there. I'm impartial, and I think everyone is making too big of a deal out of it. We have bigger issues, including the fact that we need to solve whole other issues between the sexes, besides gentelmen's need or will to over protect them.
    No one said anything there about violence, religios human rights abuse, etc etc
    All whining bitches, both men and women in that thread
    Rant over
    Yegor Bugayenko
    @yegor256
    @fabriciofx indeed a good new testimonial :) Will publish it now.
    @Phantom190 you made it, bro: yegor256/blog@ba8b012
    Phantom190
    @Phantom190
    @yegor256 While you are at it, please update the one from James. It's too lame to half-quote people. The original is "Author is a fucking moron. Does he know that a woman invented the first programming language or"
    Yegor Bugayenko
    @yegor256
    @Phantom190 and calling me a “fucking moron” is not lame? :)
    Phantom190
    @Phantom190
    @yegor256 It is, but countering a lame action with another lame action is what kids do, not adults.
    Yegor Bugayenko
    @yegor256
    @Phantom190 nah, I’m a kid.
    Justas Bieliauskas
    @justasbieliauskas

    @fabriciofx Well, since you put it that way, I'm starting to see nothing wrong with ArrayList as well :D But just to reiterate what I had in mind...
    There might be cases where using ArrayList is insufficient, where, let's say, you have a lot of such cached objects and thus you're dealing with memory issues, so you decide to serialize it to a file or send it to some server, hadoop storage, whatever. You're right, this would be slow to retrieve in common scenarios, but for some values still faster (what if the value we're after requires an entire game to be played, in declarative programmer that is very possible).
    There might also be other cases. Instead of using a plain ArrayList, you would use a decorated one, where in addition to add method it would also log the value or something... In that case, Cached class needs to allow users to specify where to cache stuff, be it a file, a cloud, etc.
    You asked for a problem description, so here it is. I would like to use Cached in classes like these:

    /** The class that computes a value. */
    public final class UrlHostNameStartIndex implements HostNameStartIndex
    {
        private final String url;
        public UrlHostNameStartIndex(String url) {
            this.url = url;
        }
        public int toInt() {
            // ...
        }
    }
    
    /** a decorator that caches the computed value */
    public final class CachedHostNameStartIndex implements HostNameStartIndex
    {
        private final Cached<Integer> index;
        public CachedHostNameStartIndex(UrlHostNameStartIndex index) {
            this(
                new Cached<>(
                    new Origin<>() {
                        public Integer value() {
                            return index.toInt();
                        }
                    }
                )
            );
        }
        public CachedHostNameStartIndex(Cached<Integer> index) {
            this.index = index;
        }
        public int toInt() {
            return this.index.value();
        }
    }

    I hope that it's easy to understand. Sure, for small objects like these two, it's enough to have an ArrayList. I just thought it'd be cool, if Cached allowed me to specify a different location of the value, for cases not yet discovered that may come up in the future. And that brings me back to my original question, is there a way to do that without making Cached look like a configurable object.

    Fabricio Cabral
    @fabriciofx
    @justasbieliauskas I'm sorry if I didn't your question well, but I believe you can create several decorators to the work properly, like new Logging(new Cached(new Abc())).value(). Or your problem is how Cached store the object (ArrayList)? In this case you can create another parameter to receive which implementation use to store the object into cached.