-Поиск по дневнику

Поиск сообщений в rss_thedaily_wtf

 -Подписка по e-mail

 

 -Постоянные читатели

 -Статистика

Статистика LiveInternet.ru: показано количество хитов и посетителей
Создан: 06.04.2008
Записей:
Комментариев:
Написано: 0


CodeSOD: Now There's a Switch…

Понедельник, 20 Июня 2016 г. 13:30 + в цитатник

You know whats 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, whats that? There is? Its called a switch statement? But doesnt a switch statement only work on equality comparisons? Id really like something that works on any condition.

Fortunately for me, Sergejs 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' });
                        break;

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

                default:
                        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})
        .exec()
        .then(function(data,err){
                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 its even more flexible, because its got fall-through! Why does anybody use regular if statements when theyve got this efficient and easy-to-read construct?

[Advertisement] Release! is a light card game about software and the people who make it. Play with 2-5 people, or up to 10 with two copies - only $9.95 shipped!

http://thedailywtf.com/articles/now-there-s-a-switch

Метки:  

 

Добавить комментарий:
Текст комментария: смайлики

Проверка орфографии: (найти ошибки)

Прикрепить картинку:

 Переводить URL в ссылку
 Подписаться на комментарии
 Подписать картинку