<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bishoy Abd on Medium]]></title>
        <description><![CDATA[Stories by Bishoy Abd on Medium]]></description>
        <link>https://medium.com/@bishoy-abd?source=rss-9e36054e0d8b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*yKkYmzrMqInBeXkGAzxK-w.png</url>
            <title>Stories by Bishoy Abd on Medium</title>
            <link>https://medium.com/@bishoy-abd?source=rss-9e36054e0d8b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 16:09:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bishoy-abd/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A/B Testing the Socratic Way]]></title>
            <link>https://bishoy-abd.medium.com/a-b-testing-the-socratic-way-0d15b6f395cf?source=rss-9e36054e0d8b------2</link>
            <guid isPermaLink="false">https://medium.com/p/0d15b6f395cf</guid>
            <category><![CDATA[hypothesis-testing]]></category>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[experiment]]></category>
            <dc:creator><![CDATA[Bishoy Abd]]></dc:creator>
            <pubDate>Sat, 15 Feb 2025 22:29:31 GMT</pubDate>
            <atom:updated>2025-02-15T22:29:31.539Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/450/0*Q8uepivAS4vofrud.png" /><figcaption><a href="https://www.seobility.net/en/wiki/AB_Testing">source</a></figcaption></figure><p><strong>Socrates:</strong> What is A/B testing?<br><strong>Me:</strong> A/B testing is a way to check if a change actually makes a difference by running an experiment with two or more groups.</p><blockquote><strong>Example 1:</strong> A company wants to know if a <strong>new website design</strong> will get more clicks. They show half the users the old design and the other half the new one. If more people click on the new design, it’s likely better.</blockquote><blockquote><strong>Example 2:</strong> A candy company wants to see if people like <strong>a slightly sweeter version</strong> of their product. They let one group try the original and another group try the sweeter version. If more people prefer the sweeter one, they might change the recipe.</blockquote><p><strong>Socrates:</strong> But what is a change?<br><strong>Me:</strong> A change can be anything you want to test. It could be a new design, a different price, a tweak in wording, or maybe a slight adjustment to a recipe. The goal here is to see if this change makes a real difference from a statistical point of view — something we call <strong>statistical significance</strong>.</p><p><strong>Socrates:</strong> So, how do we know if there’s really a difference?<br><strong>Me:</strong> We run the experiment, collect data from the two groups, and then compare the results. The question is: does the new version perform better than the original? Or are the differences we see just due to chance? That’s where statistics (AB Test) help us understand if the change actually matters.</p><p><strong>Socrates:</strong> What is a hypothesis?<br><strong>Me:</strong> A hypothesis is a clear statement about what we expect to happen when we introduce a change.</p><p><strong>Socrates:</strong> So, you’re saying a hypothesis is just a guess?<br><strong>Me:</strong> Not exactly. A hypothesis is more than just a guess — it’s an <strong>educated assumption</strong> based on logic, prior knowledge, or past data. It must be testable and clearly state the expected outcome.<br>testing. It helps the team focus on a specific question, making it easier to interpret the results.</p><blockquote><strong>Example:</strong> Suppose an e-commerce platform wants to test a new layout for the product detail page. The team’s hypothesis could be:<br>“If we use a simplified layout with fewer distractions and a more prominent ‘Buy Now’ button, then conversion rates will increase because customers can focus more on the primary call-to-action.”<br>This statement clarifies what change is being tested, what outcome is expected, and why.</blockquote><p><strong>Socrates:</strong> Can a hypothesis be wrong?<br><strong>Me:</strong> Absolutely! A hypothesis is <strong>not</strong> about being right or wrong; it’s about learning. Even if the test shows no improvement or a negative effect, that’s still valuable information. A failed hypothesis helps us understand what <strong>doesn’t</strong> work and refine future tests.</p><p><strong>Socrates:</strong> So, a failed A/B test isn’t a waste of time?<br><strong>Me:</strong> Not at all! A failed test helps us avoid bad decisions. It’s better to learn that a change doesn’t work <strong>before</strong> rolling it out to everyone. That’s why a good hypothesis isn’t just about getting a positive result — it’s about <strong>testing ideas with clear expectations</strong> and learning from the data.</p><h3>Measuring Success: The Role of Metrics</h3><p><strong>Socrates:</strong> Wait, wait, wait! <strong>What should we even measure?</strong> What if we <strong>can’t</strong> measure what we actually care about?<br>And what if the test shows that my <strong>new button is amazing</strong> — tons of people are clicking it — but… <strong>it’s actually making things worse</strong>?</p><blockquote><strong>Example:</strong> imagine we test a <strong>new “Buy Now” button</strong> that’s bigger and more colorful. The test results show a <strong>huge increase in clicks!</strong> Success, right?</blockquote><p>But then we check <strong>customer support tickets</strong> — suddenly, a lot more people are calling to ask <strong>how to cancel accidental purchases</strong>. The button was so big and eye-catching that users clicked it <strong>without meaning to buy</strong>!</p><p><strong>Me:</strong> Exactly! That’s why we need <strong>guardrail metrics</strong> — to make sure a test doesn’t just look good <strong>on the surface</strong> while secretly making things worse or hurting other metrics (Things we care about).</p><h4>Success Metrics (Primary Metrics)</h4><p><strong>Socrates:</strong> What are success metrics?<br><strong>Me:</strong> Success metrics (or <strong>primary metrics</strong>) are the key numbers we use to measure whether a test had the intended effect.</p><blockquote><strong>Example:</strong><br>If an <strong>e-commerce site</strong> tests a new checkout process, the primary success metric could be <strong>conversion rate</strong> — the percentage of visitors who complete a purchase.</blockquote><p><strong>Socrates:</strong> Can we just track one metric and call it a day?<br><strong>Me:</strong> Not really. Relying on a single metric can be risky because it doesn’t tell the whole story. That’s why we also use <strong>guardrail metrics</strong> to make sure we’re not causing unintended harm.</p><p><strong>Socrates:</strong> What happens if a test improves the success metric but hurts a guardrail metric?<br><strong>Me:</strong> Then we have to reconsider the change. If <strong>conversion rates go up but customer satisfaction drops</strong>, the test might not be worth implementing. That’s why <strong>guardrails help prevent bad business decisions</strong>.</p><h4>Proxy Metrics</h4><p><strong>Socrates:</strong> What if we can’t directly measure what we want?<br><strong>Me:</strong> That’s where <strong>proxy metrics</strong> come in. A <strong>proxy metric</strong> is something <strong>easier to measure</strong> that closely relates to the real goal.</p><blockquote><strong>Example:</strong><br>If we want to test if a <strong>new product page design</strong> increases sales, but we can’t track purchases in real-time, we might use <strong>“Add to Cart” clicks</strong> as a proxy metric.<br>If an <strong>education platform</strong> wants to measure <strong>student engagement</strong>, but tracking long-term learning is hard, a proxy metric could be <strong>time spent on lessons</strong> or <strong>quiz completion rates</strong>.</blockquote><p><strong>Socrates:</strong> But how do we know if a proxy metric really reflects what we care about?<br><strong>Me:</strong> We need to <strong>validate</strong> it first — by checking past data to see if it correlates well with the real outcome. If <strong>“Add to Cart” clicks</strong> don’t usually lead to purchases, then it’s a weak proxy.</p><h3>Variants and Randomization</h3><p><strong>Socrates:</strong> Alright, so we know how to measure success and avoid bad decisions. But tell me, why do we even need two groups? Can’t we just change something and see what happens?<br><strong>Me:</strong> If we don’t compare against a <strong>control group</strong>, we won’t know if the change actually made a difference or if something else caused it. Maybe sales increased because it was <strong>holiday season</strong>, not because of the new button.</p><p><strong>Socrates:</strong> So, what exactly are these groups?<br><strong>Me:</strong> Every A/B test has at least two <strong>variants</strong>:</p><ul><li><strong>Control Variant</strong> — The original, unchanged version.</li><li><strong>Test Variant</strong> — The version with the change we are testing.</li></ul><blockquote><strong>Example:</strong><br>If we’re testing a <strong>new homepage layout</strong>, the control group sees the <strong>old homepage</strong>, and the test group sees the <strong>new layout</strong>. If the test group engages more, the change is likely working.</blockquote><h4>Why Randomization Matters</h4><p><strong>Socrates:</strong> But how do we decide who sees which version?<br><strong>Me:</strong> We <strong>randomly assign</strong> users to either the control or test group.</p><p><strong>Socrates:</strong> Why random? Can’t we just test the new version on people who visit in the morning and the old one on those who visit at night?<br><strong>Me:</strong> That’s risky! What if people who shop in the morning behave differently from those at night? Then the difference wouldn’t be because of the new design — it would be because of <strong>user behavior.</strong></p><p><strong>Socrates:</strong> So, randomization makes sure the groups are <strong>similar</strong>?<br><strong>Me:</strong> Exactly! Random assignment ensures that the groups are <strong>statistically similar</strong>, so any differences we see in the results are likely due to the change itself — not external factors.</p><h3>Ensuring a Reliable Test: Sample Size &amp; Statistical Power</h3><p><strong>Socrates:</strong> Alright, so we have a test group, a control group, and we randomize them. But… how many people do we actually need in this experiment? Can’t we just test on, say, 20 people and see what happens?<br><strong>Me:</strong> Not quite. If our sample size is <strong>too small</strong>, we might <strong>miss real effects</strong> or mistake random noise for meaningful changes.</p><blockquote><strong>Example:</strong><br>Imagine flipping a coin 5 times — it might land on heads 4 times, making it seem like the coin is biased. But if you flip it 1,000 times, you’ll see it’s actually balanced. The same logic applies to A/B testing — <strong>small samples can be misleading</strong>.</blockquote><h4>What is Statistical Power?</h4><p><strong>Socrates:</strong> So, how do we make sure our test is reliable?<br><strong>Me:</strong> We need <strong>statistical power</strong> — the ability of a test to <strong>detect a real effect when one exists</strong>.</p><p><strong>Socrates:</strong> And what does that mean in practice?<br><strong>Me:</strong> It means we need <strong>enough people</strong> in our test to confidently say whether the change works or not. If we don’t have enough data, we might fail to detect a real improvement.</p><blockquote><strong>Example:</strong><br>Suppose we test a <strong>new “Subscribe Now” button</strong> and see a <strong>2% increase in sign-ups</strong>. If we only tested on <strong>100 people</strong>, that 2% could be random. But if we tested on <strong>100,000 people</strong>, we’d be more confident that it’s a real improvement.</blockquote><h4>What is the Minimum Detectable Effect (MDE)?</h4><p><strong>Socrates:</strong> But how do we decide how many people are “enough”?<br><strong>Me:</strong> That depends on how big of a change we want to detect — this is called the <strong>Minimum Detectable Effect (MDE)</strong>.</p><p><strong>Socrates:</strong> What does that mean?<br><strong>Me:</strong> MDE is the <strong>smallest improvement we care about detecting</strong>. The <strong>smaller the effect we want to detect, the more people we need in our test.</strong></p><blockquote><strong>Example: </strong>If we only care about <strong>big changes</strong> (e.g., a 20% increase in clicks), we don’t need a huge sample.<br>But if we want to detect <strong>tiny improvements</strong> (e.g., a 1% increase), we need <strong>a much larger sample</strong> to be sure it’s not random.</blockquote><p>Now that we have a <strong>hypothesis</strong>, we’ve set our <strong>metrics</strong>, and we have <strong>historical data</strong>, it’s time to answer a crucial question: <strong>How many users do we actually need in the test?</strong></p><h4>A Practical example</h4><p><strong>Anna (Product Manager):</strong> <em>“We’re ready to launch the test! How many users do we need?”<br></em><strong>Bishi (Senior Data Scientist):</strong> <em>“We need to calculate that. First, let’s look at how many users typically complete a purchase.”</em></p><p><strong>Bishi</strong> pulled data from the past <strong>30 days</strong>:</p><ul><li><strong>Total visitors per day:</strong> 200,000</li><li><strong>Checkout conversion rate:</strong> 5% (5% from users actually buy)</li><li><strong>Completed purchases per day:</strong> 10,000</li></ul><p><strong>Bishi:</strong> <em>“What’s the smallest improvement we care about?”<br></em><strong>Anna:</strong> <em>“Even a 1% increase in checkouts would be a win.”</em></p><p>Bishi needed three things:<br>✅ <strong>Baseline conversion rate</strong> (5%)<br>✅ <strong>Minimum Detectable Effect (1%)</strong><br>✅ <strong>Statistical power (80%) (Nothing special, just a rule of thumb)</strong></p><p>He ran the calculation in <strong>Python</strong>:</p><pre>from statsmodels.stats.power import NormalIndPower<br>power_analysis = NormalIndPower()<br>sample_size_per_group = power_analysis.solve_power(effect_size=0.01 / 0.05, alpha=0.05, power=0.8)<br>total_sample_size = sample_size_per_group * 2<br>print(f&quot;Total required sample size: {int(total_sample_size)}&quot;)</pre><p>The result? <strong>100,000 users total (50,000 per group).</strong></p><p><strong>Anna</strong>: “Can we get this sample size quickly?”<br><strong>Bishi</strong>: “Yes. We have 200,000 daily visitors, so we’ll get enough users in just one day. But we should run the test for at least a week to cover daily traffic patterns.”</p><h3>Understanding Statistical Significance and Effect Size</h3><p><strong>Socrates:</strong> Alright, we’ve got our test running with the right sample size. But how do we know if the results actually mean something?<br><strong>Me:</strong> Now we introduce <strong>the null hypothesis and the alternative hypothesis.</strong></p><p><strong>Socrates:</strong> The null… what now?<br><strong>Me:</strong> Don’t worry, it’s simple. Think of it like this:</p><ul><li>The <strong>null hypothesis (H₀)</strong> assumes <strong>nothing changed</strong> — that our new version has <strong>no real effect</strong> compared to the old one.</li><li>The <strong>alternative hypothesis (H₁)</strong> is what we <strong>hope</strong> is true — it says that the new version <strong>actually makes a difference</strong>.</li></ul><p><strong>Socrates:</strong> So in our checkout button test, the null hypothesis would be…?<strong>Me:</strong> <em>“The new checkout button does NOT improve conversions.” <br></em>And the alternative hypothesis would be:<em>“The new checkout button increases conversions.”</em></p><p><strong>Socrates:</strong> And our test is just trying to see if we have enough proof to reject the null hypothesis?<br><strong>Me:</strong> Exactly! If we <strong>reject the null</strong>, we can say the new version likely had an effect. But if we <strong>fail to reject it</strong>, we can’t confidently say the change made a difference.</p><p><strong>Socrates:</strong> So, if our test shows an increase in conversions, that means our change worked?<br><strong>Me:</strong> Not necessarily! Even if the test <strong>looks</strong> good, we need to check whether the difference is <strong>statistically significant</strong>.</p><blockquote><strong>Example:</strong><br> If we test a new checkout page and <strong>conversion rate increases from 5% to 5.2%</strong>, that might seem good. But if this change is <strong>not statistically significant</strong>, it could just be luck.</blockquote><h4><strong>The Role of the p-value</strong></h4><p><strong>Socrates:</strong> How do we check if a result is significant?<br><strong>Me:</strong> We use the <strong>p-value</strong> — it tells us how likely it is that the result happened by chance.</p><ul><li>A <strong>low p-value</strong> (e.g., &lt; 0.05) means the change is <strong>unlikely to be random</strong>, so we <strong>reject the null hypothesis</strong> and say the test likely had an impact.</li><li>A <strong>high p-value</strong> (e.g., &gt; 0.05) means we <strong>don’t have enough evidence</strong> to say the change made a difference.</li></ul><blockquote><strong>Example:</strong><br> If your <strong>p-value is 0.03</strong>, there’s only a <strong>3% chance</strong> that the results happened randomly, meaning the new version likely <strong>had a real effect</strong>.</blockquote><p><strong>Socrates:</strong> Hold on. You keep saying we reject the null hypothesis if the <strong>p-value is less than 0.05</strong>. But… why 0.05? Why not 0.06? Or 0.01?<br><strong>Me:</strong> Good question! The <strong>0.05 threshold</strong> is <strong>not a law of nature</strong> — it’s just a <strong>convention</strong> that statisticians have agreed upon over time.</p><p><strong>Socrates:</strong> So, it’s an arbitrary number?<br><strong>Me:</strong> Kind of. But it strikes a balance between <strong>avoiding false positives and false negatives</strong>.</p><ul><li>If we set <strong>α = 0.06</strong>, we’d be slightly more lenient, meaning we’d <strong>reject the null hypothesis more often</strong> — but that increases the chance of <strong>false positives</strong> (thinking something works when it doesn’t).</li><li>If we set <strong>α = 0.01</strong>, we’d be very strict, meaning we’d <strong>only accept results with very strong evidence</strong> — but that increases the chance of <strong>false negatives</strong> (missing real effects).</li></ul><h3>Statistical Significance vs. Practical Significance</h3><p><strong>Socrates:</strong> So, if my p-value is low, I should roll out the change immediately?<strong>Me:</strong> Not so fast! <strong>Statistical significance doesn’t mean the change is important.</strong></p><blockquote><strong>Example:</strong><br> If a test shows a <strong>0.1% increase in conversions</strong>, the p-value might say it’s significant, but if that tiny change <strong>doesn’t cover development costs</strong>, it’s not <strong>practically significant</strong>.</blockquote><p><strong>Socrates:</strong> So, how do I know if a result is truly useful?<br><strong>Me:</strong> That’s where <strong>effect size</strong> comes in — it tells us <strong>how big the impact is</strong>, not just whether it exists.</p><h4>Understanding Effect Size</h4><p><strong>Socrates:</strong> What exactly is effect size?<br><strong>Me:</strong> Effect size quantifies <strong>how meaningful a difference is</strong> between the test and control groups, <strong>typically the difference between a sample value and a null value</strong>. For example, the mean difference between a <strong>treatment</strong> group and a <strong>control</strong> group</p><blockquote><strong>Example:<br></strong>A <strong>12% revenue increase</strong> from a small change in <strong>ad wording</strong> — big effect!<br>A <strong>tiny 0.1% increase</strong> in conversions that costs <strong>millions to implement</strong> — not worth it.</blockquote><p><strong>Socrates:</strong> So, statistical significance tells me if a change is real, and effect size tells me if it’s worth it?<br><strong>Me:</strong> Exactly!</p><p><strong>Socrates:</strong> This all sounds precise. But do people ever misinterpret results?<strong>Me:</strong> All the time! Here are some common mistakes:</p><h3>Avoiding Common Mistakes</h3><p>🔴 <strong>Thinking non-significant results mean “no effect.”</strong><br>✅ A test might just be <strong>underpowered</strong> or need a <strong>larger sample size</strong> to detect an effect.</p><p>🔴 <strong>Misunderstanding the p-value.</strong><br>✅ A <strong>p-value of 0.05</strong> <strong>doesn’t</strong> mean there’s only a 5% chance you’re wrong. It just means that <strong>if the null hypothesis were true</strong>, you’d see results this extreme <strong>5% of the time by chance</strong>.</p><p>🔴 <strong>Thinking statistical significance means practical significance.</strong><br>✅ Just because a result is statistically significant <strong>doesn’t</strong> mean it’s <strong>meaningful or useful</strong> in the real world.</p><blockquote><strong>Example:</strong><br>Imagine an A/B test on an e-commerce site where a new product page increases conversions from <strong>2.00% to 2.5%</strong>. The result is <strong>statistically significant</strong> (p-value &lt; 0.05), but in reality, a <strong>0.5% increase is too small</strong> to justify the cost of redesigning the page across the entire website. <strong>The effect is real, but it’s not worth acting on.</strong></blockquote><h3><strong>Practical Example</strong></h3><p><strong>Socrates:</strong> Alright, we’ve talked about statistical significance, but how do we actually test it?<br><strong>Me:</strong> Let’s say a company is testing a <strong>sweeter version</strong> of its product to see if more people like it.</p><p><strong>Socrates:</strong> How do we check if the sweeter version actually <strong>makes a difference</strong>?<br><strong>Me:</strong> We collect data! Here’s how it looks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6weyd9qBNp6W5ajC9slRtQ.png" /><figcaption>Dummy data of an AB experiment.</figcaption></figure><p><strong>Socrates:</strong> Hmm… I see that more people liked the sweeter version, but how do we know if that’s just random chance?<br><strong>Me:</strong> That’s where we run a <strong>statistical test</strong>. In this case, we use a <strong>Z-test for proportions</strong>, which checks if the increase from <strong>15% to 20%</strong> is real or just noise.</p><p><strong>Socrates:</strong> And what did the test say?<br><strong>Me:</strong> We got a <strong>p-value &lt; 0.001</strong>, which is way below <strong>0.05</strong> — meaning the difference is <strong>statistically significant</strong>.</p><p><strong>Socrates:</strong> So the sweeter version is better? Case closed?<br><strong>Me:</strong> Not so fast! We also need to check the <strong>effect size</strong> to see if this change is actually meaningful.</p><p><strong>Socrates:</strong> How do we measure that?<br><strong>Me:</strong> We calculate <strong>Cohen’s h</strong>, which tells us how big the difference is. In this case, we found an <strong>effect size of 0.125</strong> — which is small, but meaningful, maybe not, it depends.</p><p><strong>Socrates:</strong> So, statistical significance tells us the change is real, and effect size tells us if it’s important?<br><strong>Me:</strong> Exactly! If the effect size were <strong>0.01</strong>, even a significant p-value wouldn’t mean much. But since <strong>0.125 is noticeable</strong>, this change might be worth rolling out.</p><p>Here is an example python code for the Test.</p><pre>import pandas as pd<br>import numpy as np<br>from statsmodels.stats.proportion import proportions_ztest<br><br># Simulated A/B test data<br>np.random.seed(42)<br><br>data = {<br>    &quot;Group&quot;: [&quot;Control&quot;] * 5000 + [&quot;Test&quot;] * 5000,<br>    &quot;Liked&quot;: np.concatenate([<br>        np.random.choice([0, 1], size=5000, p=[0.85, 0.15]),  # 15% liked in Control<br>        np.random.choice([0, 1], size=5000, p=[0.80, 0.20])  # 20% liked in Test (sweeter version)<br>    ])<br>}<br><br>df = pd.DataFrame(data)<br><br># Aggregating data<br>like_counts = df.groupby(&quot;Group&quot;)[&quot;Liked&quot;].sum().values<br>sample_sizes = df.groupby(&quot;Group&quot;)[&quot;Liked&quot;].count().values<br><br># Running a Z-test<br>stat, p_value = proportions_ztest(like_counts, sample_sizes, alternative=&#39;larger&#39;)<br><br># Calculating effect size (Cohen&#39;s h)<br>control_rate = like_counts[0] / sample_sizes[0]<br>test_rate = like_counts[1] / sample_sizes[1]<br>effect_size = 2 * (np.arcsin(np.sqrt(test_rate)) - np.arcsin(np.sqrt(control_rate)))<br><br># Display results<br>print(f&quot;Z-test Statistic: {stat:.2f}&quot;)<br>print(f&quot;P-value: {p_value:.5f}&quot;)<br>print(f&quot;Effect Size (Cohen&#39;s h): {effect_size:.3f}&quot;)</pre><p>I will also publish a new article that covers A/B testing in a more rigorous and in-depth way to help you design better experiments.</p><h3><strong>References</strong></h3><ul><li><strong><em>Kohavi, R., Tang, D., &amp; Xu, Y. (2020).</em></strong><em> Trustworthy Online Controlled Experiments: A Practical Guide to A/B Testing. Cambridge University Press.</em></li><li><strong><em>Hartshorn, S. (2021).</em></strong><em> Hypothesis Testing: An Intuitive Guide to Making Data-Driven Decisions. Independently published.</em></li><li><strong><em>Nassery, L. (2023).</em></strong><em> Practical A/B Testing: Creating Experimentation-Driven Products. Pragmatic Bookshelf.</em></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0d15b6f395cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sending Data between different parts of your application using RxJava2 subjects]]></title>
            <link>https://bishoy-abd.medium.com/sending-data-between-different-parts-of-your-app-using-rxjava2-5675153e37c?source=rss-9e36054e0d8b------2</link>
            <guid isPermaLink="false">https://medium.com/p/5675153e37c</guid>
            <category><![CDATA[rxandroid]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[rxjava2]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Bishoy Abd]]></dc:creator>
            <pubDate>Mon, 11 Dec 2017 09:43:44 GMT</pubDate>
            <atom:updated>2017-12-11T10:08:36.393Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*JxqWK5TMfVziwz1QPEznSw.png" /></figure><p>Sending data between activities (or whatever) is an important topic in android.I have come across many ways to do it,at first I used to use Intent extras to pass my primitive data.Things were going perfect till I needed to pass a big Object .I thought of using libraries like <a href="https://github.com/johncarl81/parceler"><strong><em>Parceler</em></strong></a><strong> </strong>but it did not satisfy my needs in some use cases.</p><h3><strong>The Rx World</strong></h3><p>RxJava2 introduced Subject to pass “publish” Data between activities in different ways,each way depends on how you want the data to be published and received.</p><p>Subject !! what on earth is a Subject.</p><h4>In plain words</h4><p>subject is a teacher that can give lessons to students (observable).he also can can go to another school to listen to new lessons (observer).</p><h4>Official definition</h4><blockquote>A Subject is a sort of bridge or proxy that is available in some implementations of ReactiveX that acts both as an observer and as an Observable. Because it is an observer, it can subscribe to one or more Observables, and because it is an Observable, it can pass through the items it observes by re-emitting them, and it can also emit new items.</blockquote><p>It comes with four flavors :</p><ul><li>Publish Subject</li><li>Replay Subject</li><li>Behavior Subject</li><li>Async Subject</li></ul><p>For our use case .I am going to use <strong>Behavior Subject</strong></p><h4>What is a <strong>Behavior Subject?</strong></h4><h4>In plain words</h4><p>You are going to the cinema to watch the movie “The bad behavioral subject”and you arrived late .do not worry because this movie will show a summary about what happened before you arrive.</p><p>When an observer subscribes to a BehaviorSubject, it begins by emitting the item most recently emitted by the source Observable (or a seed/default value if none has yet been emitted) and then continues to emit any other items emitted later by the source Observable(s).</p><h4>Time to code</h4><p>we first create a bus which will be responsible for sending and receiving our data :</p><pre><em>/**<br> * Created by Bishoy on 10/29/2017.<br> */<br>//this class does a similar job as the actual computer bus does.</em></pre><pre><strong>public final class </strong>RxBus {</pre><pre>//this how to create our bus<br>    <strong>private static final </strong>BehaviorSubject&lt;Object&gt; <strong><em>behaviorSubject <br>            </em></strong>= BehaviorSubject.<em>create</em>();<br><br><br>    <strong>public static </strong>Disposable subscribe(@NonNull Consumer&lt;Object&gt; action) {<br>        <strong>return <em>behaviorSubject</em></strong>.subscribe(action);<br>    }<br>//use this method to send data<br>    <strong>public static void </strong>publish(@NonNull Object message) {<br>        <strong><em>behaviorSubject</em></strong>.onNext(message);<br>    }<br>}</pre><h4>Note</h4><p>Your subject must be the same across all parts of your app.</p><p>Here is how we use this bus inside the sender activity:</p><pre>findViewById(R.id.goToAnotherActivityBtn)<br>        .setOnClickListener(<strong>new </strong>View.OnClickListener() {<br>            @Override<br>            <strong>public void </strong>onClick(View v) {<br>                <em>//the data to be based<br>                </em>MyData  data =getMyData();<br>                RxBus.<em>publish</em>( data);<br>                startActivity(<strong>new </strong>Intent(MainActivity.<strong>this</strong>, AnotherAct.<strong>class</strong>));<br><br><br>            }<br>        });</pre><p>inside the receiver activity :</p><pre>//inside onCreate</pre><pre><strong>disposable </strong>= RxBus.<em>subscribe</em>(<strong>new </strong>Consumer&lt;Object&gt;() {<br>    @Override<br>    <strong>public void </strong>accept(Object o) <strong>throws </strong>Exception {<br>        <strong>if </strong>(o <strong>instanceof </strong>MyData) {<br>            MyData data= (MyData) o;<br>          //do sth with the data .. you can populate a RecycleView for example<br>            Timber.<em>d</em>(data.toString());<br>        }<br>    }<br>});</pre><pre>//if you no longer need data &quot;very important&quot;<strong><br>disposable</strong>.dispose(); //unsubscribe </pre><p>last step is to unSubscribe to our bus to avoid memory leacks</p><pre>@Override<br><strong>protected void </strong>onDestroy() {<br>    <strong>super</strong>.onDestroy();<br>    <em>disposable.dispose(); //very important<br></em>}</pre><h4>References :</h4><p>about subjects : <a href="http://reactivex.io/documentation/subject.html"><em>http://reactivex.io/documentation/subject.html</em></a></p><p>about Rxjava : <a href="http://reactivex.io/documentation"><em>http://reactivex.io/documentation</em></a></p><p>Happy coding.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5675153e37c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>