Smaller is better

There is an argument that the smaller you make the parent structure, the easier it becomes to manage. This is true, but let's look at it in a different way. As it stands, we have a couple of what can be described as objects in their own right within that structure. Here, an object is something that will have its own properties. Let's look at the window.

A window has a size—width and height; it has a type—sash, for instance; it has blinds or curtains, and the blinds/curtains have a color. The window may also have a lock. It may also be a single or double window and the opening may be at the top or side.

There is also no reason why there should only be a single window. If there is more than one window, then we will need to define our window multiple times. Therefore, it makes more sense to define our window and reference that back as a vector in the main structure.

Before that, though, we said the window will have a size (width, length). Each room will have a size and, probably, so will many other things within the house; therefore, we will remove the size and have that as its own struct.

Therefore, we have this following struct for the window:

struct Area 
{ 
    width: f32, 
    length: f32, 
} 
 
struct Window 
{ 
    window_area: Area, 
    window_type: String, 
    has_blinds: bool, 
    curtain_color: String, 
    has_lock: bool, 
    top_open: bool, 
    single_window: bool, 
} 

This, back in the parent struct, will transform into the following:

struct Room 
{  
    is_upstairs: bool, 
    number_of_doors: i32, 
    window: Vec<Window> ,
    wood_or_carpet: bool, 
    carpet_color: String, 
    room_name: String, 
    has_wardrobe: bool, 
    room_area: Area, 
} 

We can carry on doing this for anything else in the room, including a struct variable for furniture, as well as possibly reducing the size for carpet—what you do with it is up to you. For now, we'll keep it at this level.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset