You know what’s awful? If-then-elseif conditions. You have this long, long chain of them, and then what? If only there were a shorter, clearer way to write a large number of conditions.

Oh, what’s that? There is? It’s called a switch statement? But doesn’t a switch statement only work on equality comparisons? I’d really like something that works on any condition.

Fortunately for me, Sergej’s boss has found a way.

clients.findById( req.authUser._doc._id).then( function( client, error ){
        switch( true ) {
                case client == null:
                        res.send( { success: false, message: 'Your profile has not been found. Try it again or logout and then login again' });

                case client.password != req.body.profile.password:
                        res.send( { success: false, message: 'Profile has not been updated. Password is wrong.' });

                        var updateObj = {};
                        switch( true ) {
                                case client.firstname != req.body.profile.firstname:
                                        updateObj.firstname = req.body.profile.firstname;
                                case client.lastname != req.body.profile.lastname:
                                        updateObj.lastname = req.body.profile.lastname;
                                case client.username != req.body.profile.username:
                                        updateObj.username = req.body.profile.username;
                                case client.companyName != req.body.profile.companyName:
                                        updateObj.companyName = req.body.profile.companyName;
                                case client.companyAddress != req.body.profile.companyAddress:
                                        updateObj.companyAddress = req.body.profile.companyAddress;
                                case client.companyCity != req.body.profile.companyCity:
                                        updateObj.companyCity = req.body.profile.companyCity;
                                case client.companyCountry != req.body.profile.companyCountry:
                                        updateObj.companyCountry = req.body.profile.companyCountry;
                                case client.registrationNumber != req.body.profile.registrationNumber:
                                        updateObj.registrationNumber = req.body.profile.registrationNumber;
                                case client.vatNumber != req.body.profile.vatNumber:
                                        updateObj.vatNumber = req.body.profile.vatNumber;
        clients.update({_id: client._id},{$set: updateObj})
                typeof err != 'undefined'
                        ? res.send({success:false, message: 'Your profile could not be updated.'})
                        : res.send({success:true, message: 'Your profile has been updated.'});

All the functionality of an if-then-else, but it’s even more flexible, because it’s got fall-through! Why does anybody use regular if statements when they’ve got this efficient and easy-to-read construct?

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!