On Fri, 24 Feb 2017 10:55:25 +0100 Arvin Schnell <[email protected]> wrote:
> On Fri, Feb 24, 2017 at 09:28:24AM +0100, Josef Reidinger wrote: > > > What is difference between specified exception and nil class > > exception? in both case it is programmer error and need to be > > fixed. If we do not want exception, it can be possible to Null > > object pattern and define behavior for non-existing filesystem, > > but > > We also had that discussion already. Even in Wikipedia the Null > Object pattern gets strong criticism. > Link provided - https://en.wikipedia.org/wiki/Null_Object_pattern#Criticism I agree with criticism there that it should not replace check for nil just for better readability. But in this case it is valid storage setup and I think it is possible to find reasonable behavior in such case ( like user friendly to_s method or raise exception if needed ). Second half of criticism I think not affect ruby and storage-ng. Let see how it will work in reality in bootloader code with storage-ng. now: def with_btrfs?(partition) partition.filesystem.type == ::Storage::FsType_BTRFS rescue Storage::WrongNumberOfChildren, Storage::DeviceHasWrongType # No filesystem in the partition false end can be: def with_btrfs?(partition) partition.filesystem.type.btrfs? end when there is not filesystem in partition, we have NoneFilesystem with NoneType that obviously return false for btrfs? another example from current yast-storage-ng now: begin partition_table = @disk.partition_table partition_table.partitions.each do |partition| ret << partition.table_row(FIELDS) end rescue Storage::WrongNumberOfChildren, Storage::DeviceHasWrongType end Can be without this begin and rescue as for not existing partition table there is no partitions. I was curious how often this pattern can be seen in yast-storage-ng so I try grep -R "rescue.*Storage::DeviceHasWrongType" * | wc -l and result is 33 So it is repeated 33 times. That code really "smells" for me. Josef -- To unsubscribe, e-mail: [email protected] To contact the owner, e-mail: [email protected]
