()

Rant: On Long If Statements

Posted: 8/8/2020
I really hate how long if statements look. For example:
if (*mouse_x > marker[i].x + map_pos->x - 10 &&
    *mouse_x < marker[i].x + map_pos->x + 10 &&
    *mouse_y > marker[i].y + map_pos->y - 10 &&
    *mouse_y < marker[i].y + map_pos->y + 10) {
    marker_hover_idx = i;
    break;
}
This breaks my indentation! So I want to do this:
if (*mouse_x > marker[i].x + map_pos->x - 10 &&
    *mouse_x < marker[i].x + map_pos->x + 10 &&
    *mouse_y > marker[i].y + map_pos->y - 10 &&
    *mouse_y < marker[i].y + map_pos->y + 10) {
        marker_hover_idx = i;
        break;
}
But now it's like there's no visual separation on the left hand side of the screen between the if conditions and the contents of the if statement. So what about this?
if (*mouse_x > marker[i].x + map_pos->x - 10 &&
    *mouse_x < marker[i].x + map_pos->x + 10 &&
    *mouse_y > marker[i].y + map_pos->y - 10 &&
    *mouse_y < marker[i].y + map_pos->y + 10
) {
        marker_hover_idx = i;
        break;
}
My brain does not want to see ") {" at the beginning of a line. It makes me angry! That doesn't appear anywhere else in my code.
Maybe this?
if (*mouse_x > marker[i].x + map_pos->x - 10 &&
    *mouse_x < marker[i].x + map_pos->x + 10 &&
    *mouse_y > marker[i].y + map_pos->y - 10 &&
    *mouse_y < marker[i].y + map_pos->y + 10
    ) {
        marker_hover_idx = i;
        break;
}
But now the indent line between the brackets is completely off! What the hell.
So here's an if statement I'm experimenting with:
if (*mouse_x > marker[i].x + map_pos->x - 10)
if (*mouse_x < marker[i].x + map_pos->x + 10)
if (*mouse_y > marker[i].y + map_pos->y - 10)
if (*mouse_y < marker[i].y + map_pos->y + 10) {
    marker_hover_idx = i;
    break;
}
My brain loves to look at this!
But the downside is, imagine if you add an else at the end.
if (*mouse_x > marker[i].x + map_pos->x - 10)
if (*mouse_x < marker[i].x + map_pos->x + 10)
if (*mouse_y > marker[i].y + map_pos->y - 10)
if (*mouse_y < marker[i].y + map_pos->y + 10) {
    marker_hover_idx = i;
    break;
}
else {
    // do something else
}
You'd get very unexpected results, because this is the equivalent of 4 nested if statements, and an else added to the end, will only connect with the innermost if statement. So it's basically like this:
    if (condition1) {
        if (condition2) {
            if (condition3) {
                if (condition4) {
                    // do something
                }
                else {
                    // do something else
                }
            }
        }
    }
So yeah, not recommend this, and still figuring out what I really want to do...
I think my favorite pattern I've used so far came up in a previous project:
int is_condition(int value) {
    if (value != condition1) return 0;
    if (value != condition2) return 0;
    if (value != condition3) return 0;
    if (value != condition4) return 0;
    return 1;
}
Positives:
• Easy to read and parse
• very clean and simple
Downsides:
• You have to write the logic "backwards" which can often be harder to reason about.. Particularly when thinking about collision.. It's a little weird to be setting the conditions by which something does not collide!
• If this isn't used in multiple places, then the benefit of a nice looking if statement is likely negated by additional complexity in logic.
On a positive note - if this is used in a for loop, you no longer needed to split it out into a function to get the benefit.. Here's an approximate rewrite of my hover function
for (int i = 0; i < *marker_idx; ++i) {
    if (*mouse_x < marker[i].x + map_pos->x - 10) continue;
    if (*mouse_x > marker[i].x + map_pos->x + 10) continue;
    if (*mouse_y < marker[i].y + map_pos->y - 10) continue;
    if (*mouse_y > marker[i].y + map_pos->y + 10) continue;
    marker_hover_idx = i;
    break;
}
I don't mind this! As a sidenote, I'd really love to be able to use "break" without using a for loop, so I could do stuff like this:
{
    if (value != condition1) break;
    if (value != condition2) break;
    if (value != condition3) break;
    if (value != condition4) break;
    // do something
}
Right now in order to get this behavior, I'd have to do something like:
for (;;) {
    if (value != condition1) break;
    if (value != condition2) break;
    if (value != condition3) break;
    if (value != condition4) break;
    // do something
    break;
}
Which feels really wrong!
Or:
do {
    if (value != condition1) break;
    if (value != condition2) break;
    if (value != condition3) break;
    if (value != condition4) break;
    // do something    
} while(0)
Which feels super hacky. I guess I could go with a goto statement, but I concede that gotos do add some ambiguity to the code for any future developer of the code, especially when I don't need something that powerful.
The switch statement would be perfect for this, if it allowed more flexible conditions in the "case" statements..
Oh well..
File this under features I would include in a custom programming language.

What I hate about Open Source

posted: 8/8/2020
First, let me say all the things I love about open source:
1. Being able to reference other people's code in order to extract the code useful for solving the problem I am having, and learn from others.
There. Now that we've gotten that out of the way, here's all the things I hate about open source.
...Well really this is more tied to the current culture of open source which can be described as:
"Hey! Stop coding! Someone has already done the thing you are trying to do! So just build your thing on top of their thing!" Which is a disaster.
1. Code rot. Someone creates a package for React Native. Then React Native changes something requiring an update. Someone else helpfully submits a pull request to update the package. The maintainer of the package has lost interest in the project. Even the greatest of packages will eventually rot, leaving you with an overlay complex pile of code that you can't easily fix or add features too. Any time you initially gained from adding this project to your project instead of stealing just the part you needed, is now lost.
2. Encourages complexity. There's this idea that projects are capable of sustaining infinite complexity, and therefore it is not a concern, so you might as well pick the most generic packages that add the most features to your project on day one. Well that's fine if you don't care about performance, security, being able to easily add features or catch bugs, stability, user experience, and general power of the programmer to design solutions that best solve the problems you have.
3. No responsibility + No expectation of quality.
Quote from some React-Native devs: "Ok this issue is clearly getting out of hand so I'll lock it.We test the releases before rolling out, and I would like to [remind] everyone that this is an Open Source project. The first way you can fix things is by submitting PRs to resolve your issues or fork it. You are entitled to the support that you pay for, and that's zero. Being harsh has only the effect of making feel bad the people that spend their free time helping with this project."
4. Everything is always broken, all the time, forever.
5. Even well made packages will eventually break. This may seem obvious, but the trend of software requiring "maintenance" is actually fairly new. There was a time when a piece of quality software could be made and then never updated and it would always work forever. Think about software before the Internet - there was no way to release a patch! Now things have to be patched every 3-6 months or they straight up don't work. This is not a feature.
Note that I'm blaming the developers of open source! I just feel that this model of reuse is fundamentally broken.
I'd rather we move to a model where sharing code is for transparency, education, and equity - not for create huge dependency maps of incredibly complex, always broken code.

Not pretty

Posted: 7/21/2020
React native render functions can get a little hairy sometimes, but sometimes attempts to improve them can fix one thing, but create a bigger problem.

Raw Version

Here's some example react code:
    return (
    <Modal>
        <View>
        <SafeAreaView>
            <BackButton />
            <View>
                <Icon />
                <View>
                    <TextInput />
                </View>
                { textLength && <CancelButton />}
            </View>
            <View>
                <SearchFilterModalButton />
            </View>
        </SafeAreaView> 
        <SearchSectionHeader>Locations</SearchSectionHeader>
        <FlatList
            renderItem={({ item }) => (
            <TouchableOpacity>
                <Text>{item.place_name}</Text>
            </TouchableOpacity>
        )} />
        <SearchSectionHeader>Trails and Recreation</SearchSectionHeader>
        <FlatList
            renderItem={({ item }) => (
            <TouchableOpacity>
                <Text>{item.place_name}</Text>
            </TouchableOpacity>
        )} />

        </View>
    </Modal>
    );
It's fairly linear, but imagine all of these elements having large style tags etc..
I'd say it's:
• Trivial to change, debug, add features
• Easy to deeply understand consequences of a change
• Hard to read - requires concentration to figure out what is what
• Difficult to parse out the discreet sections in the code
Seeing that, a programmer might decide to extract functionality out to separate abstracted components..

Abstract Version

The end result of an attempt to extract and make the code more abstract could look like this:
    return (
    <Modal>
        <TopSearchBar />
        <SearchSection title="Locations" item={this.renderItem} />
        <SearchSection title="Trails and Recreation" item={this.renderSecondItem} />
    </Modal>
    );
How elegant, right? This is the kind of code the corporate managers love! But it has these characteristics:
• Pretty / easy to read
• Very difficult to deeply understand and therefore change, debug, or add features to
• Complexity is hidden, and therefore likely to only ever increase over time.
The reality is, code being pretty is of very little help to anyone who has to actually work with the code, and having the property of being difficult to deeply understand is simply not worth it. Every time you touch this code, in order to grasp what's going on, you need to open up multiple panes, and your eyes need to jump between the screens to understand the flow.

Commented Version

Here's a better version, using inline comments:
    return (
    <Modal>
        <View>
    {/************ Top Bar ******************/ }
        <SafeAreaView>
            <BackButton />
        {/************ Search Bar ******************/ }
            <View>
                <Icon />
                <View>
                    <TextInput />
                </View>
                { textLength && <CancelButton />}
            </View>
        {/************ End Search Bar ******************/ }
            <View>
                <SearchFilterModalButton />
            </View>
        </SafeAreaView> 
    {/************ End Top Bar ******************/ }

    {/************ Locations ******************/ }
        <SearchSectionHeader>Locations</SearchSectionHeader>
        <FlatList
            renderItem={({ item }) => (
            <TouchableOpacity>
                <Text>{item.place_name}</Text>
            </TouchableOpacity>
        )} />
    {/************ End Locations ******************/ }
    {/************ Trails and Recreation ******************/ }
        <SearchSectionHeader>Trails and Recreation</SearchSectionHeader>
        <FlatList
            renderItem={({ item }) => (
            <TouchableOpacity>
                <Text>{item.place_name}</Text>
            </TouchableOpacity>
        )} />
    {/************ End Trails and Recreation ******************/ }

        </View>
    </Modal>
    );
This has the characteristics of being:
• Ugly, but clear and quick to scan
• Trivial to change, debug, add features
• Easy to parse out the discreet sections in the code
• Complexity is transparent
All this adds up to great benefits - as the code gets more complex, the coder will be in the best position to actually deal with the complexity as it comes up, rather than stuffing it away to cause problems overtime.
It's hard to fully explain just how important it is to keep code linear and complexity at a minimum - when you work on a project long enough, it pays back dividends.
Pretty code, as time goes by, just incurs a greater and greater cost.

An alternative to a global stylesheet in React Native

Posted: 7/7/2020

Problem: Stylesheets

The traditional method for dealing with this problem is to extract all your styling, and put them in a stylesheet file and reference that everywhere.. (think html + a global css stylesheet) I find that creates two problems:
1. Styling becomes harder to tinker with
I can’t make changes to appearance without taking into consideration the structure of the code, so now I have to open up two files, and hold the mental connection between the style code and the structure code in my head while I make changes.
But also, I have to simultaneously hold in my head everywhere this stylistic code could be used (For example, a button color) in order to make sure I’m not creating a problem somewhere else in the app. In practice this becomes an exponentially difficult problem the more the style code is used.
2. The stylesheet file becomes a mess of rotten code and redundancy
In order to delete a style, you’d have to search everywhere it’s used, and inline style code for classes who are used too infrequently to be useful, this is a highly bug prone and tedious task so in practice, old styles rarely get deleted.
The global stylesheet becomes a scary mess of styles that can not be deleted + new redundant styles that are created to compensate for this. Individual colors + font size can be redundant too, since Stylesheet properties can not reference other properties created within the same object, requiring the creation of local variables.
These two problems can override any benefit of style conformity this method offers.

Solution: Collection of Components

Fortunately, I've landed on a way I like much better for now: Creating component files that are collections of similar elements (like Buttons.js, or Text.js for labels and such). This method comes with a number of benefits.
1. Easy to delete
These can be named specifically to indicate where they are used, which makes it much easier to delete unused components as needed.
2. Simple reuse
Components can be made up of elements of other components, or can just include variables (referencing colors, fontsizes, etc) used in other components. This gives much more flexibility than the stylesheet method, while not enforcing reuse where not helpful.
3. Easy Hackability
Want to create a new button? Just copy an existing Button that matches the design and paste it at the bottom of the file + tweak way. There is no unnecessary coupling!
Example:
Buttons.js

// Confirm Button
const ConfirmButton = ({bgColor, textColor, children, onPress}) => {
    return(
        <TouchableOpacity style={{backgroundColor:bgColor}} onPress={onPress}>
            <Text style={{fontSize:confirmFontSize, color:textColor }}>{children}</Text>
        </TouchableOpacity>
    );
};
export const NoConfirmButton = ({children, onPress}) => {
    return(
        <ConfirmButton bgColor="#ffffff" textColor={confirmNoTextColor}
            onPress={onPress}>{children}</ConfirmButton>
    );
};
export const YesConfirmButton = ({children, onPress}) => {
    return(
        <ConfirmButton bgColor={accentColor} textColor={lightTextColor} 
            onPress={onPress}>{children}</ConfirmButton>
    );
};

One Level Deeper - A Handmade Lesson applied to React Native

Posted: 7/3/2020
For this project, I had to create a Drawer - a little sliding thing that sweeps in from the right of the screen. Of course there is a Drawer react native element, but it's a black box, and would of required me to change the design, or hack it to get it to work the way I wanted.
So I'm practicing something that I heard in a Handmade Network Podcast. The quote is something like:
"Go one more level deeper than I need"
The idea here is that if I go one level deeper than I actually need, I'll be able to fix bugs easier, and extend and enhance better as well.
So looking at the source code about the Drawer I found that it was super complex, and I really had to deeply consider if I wanted to go on this journey. But I time boxed it to a few hours, and was able to distill it to it's essential elements. A PanGestureHandler, and a few Animated.Views.
<PanGestureHandler
onGestureEvent={this._onPanGestureEvent}

<View style={{ flex: 1, }}>
  <Animated.View
    style={{
      transform: [
        {
          translateX: Animated.subtract(new Animated.Value(0), this._dragDistance),
        },
      ],
    }}
  >
    {this.mapRender()}
  </Animated.View>
</View>
</PanGestureHandler>
The temptation with React Native is to use components that already exist with the idea of saving time. It's true that initial development time is saved, but when you want to make a small tweak that falls outside the parameters of what the component allows, or something changes and the component starts throwing a cryptic error, you end up with a much more challenging problem - trying to hack or diagnose a black box.
And generally when you've got the urgency of a bug, or a small tweak, the idea of doing a lower level rewrite seems like a bad use of time. Provided that the lower level implementation isn't too costly, it's best to pay that small cost earlier, rather than get stuck with a hacked solution later.
So now I have code that is way more flexible and powerful, and much simpler, because it's not generic code.
This is something that is worth the cost.